Cleaning code
This commit is contained in:
parent
aac94e911b
commit
12829892b2
9 changed files with 166 additions and 169 deletions
24
resources/shaders/algorithms/normal.frag
Normal file
24
resources/shaders/algorithms/normal.frag
Normal file
|
@ -0,0 +1,24 @@
|
|||
// Requirements:
|
||||
// #define NORMAL_ACCURACY 0.01
|
||||
// GetDist: (vec3 position) => float
|
||||
// GetDist should return the closest object point distance from position (in the whole scene)
|
||||
|
||||
/**
|
||||
* Compute the normal of an object at a given surface point
|
||||
* this can be usefull for lighting etc..
|
||||
* This function exploit the fact that the gradient on a given surface point in 3D space
|
||||
* given the normal direction (source: https://fr.wikipedia.org/wiki/Gradient#Dimension_3_:_gradient_normal_%C3%A0_une_surface_en_un_point,_plan_tangent)
|
||||
*
|
||||
*
|
||||
*/
|
||||
vec3 GetNormal(vec3 point_position) {
|
||||
float point_distance = GetDist(point_position);
|
||||
vec2 epsilon = vec2(NORMAL_ACCURACY, 0); // Just a convenient way to use NORMAL_ACCURACY and 0
|
||||
// Compute the gradient
|
||||
vec3 gradient = point_distance - vec3(
|
||||
GetDist(point_position-epsilon.xyy),
|
||||
GetDist(point_position-epsilon.yxy),
|
||||
GetDist(point_position-epsilon.yyx));
|
||||
|
||||
return normalize(gradient);
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
|
||||
|
||||
RayMarching(vec3 ro, vec3 rd){
|
||||
|
||||
}
|
26
resources/shaders/algorithms/raymarching.frag
Normal file
26
resources/shaders/algorithms/raymarching.frag
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Requirements:
|
||||
// #define MAX_STEPS 100
|
||||
// #define MAX_DIST 100.
|
||||
// GetDist: (vec3 position) => float
|
||||
// GetDist should return the closest object point distance from position (in the whole scene)
|
||||
|
||||
/**
|
||||
* Ray Marching algorithm
|
||||
* Please take a look at: https://www.youtube.com/watch?v=PGtv-dBi2wE&t=1367s
|
||||
* This function return the distance reached by the ray:
|
||||
* - If dist<MAX_DIST we hit something
|
||||
*/
|
||||
float RayMarch(vec3 ray_origin, vec3 ray_direction) {
|
||||
float dist_origin=0.; // Ray start at the origin
|
||||
for(int i=0; i<MAX_STEPS; i++) { // If we take to much time to converge (i>=MAX_STEPS)
|
||||
// We move the ray:
|
||||
vec3 ray_position = ray_origin + ray_direction*dist_origin;
|
||||
// We find the next closest point:
|
||||
float closest_point = GetDist(ray_position);
|
||||
// Increase the ray distance
|
||||
dist_origin += closest_point;
|
||||
// Check if we went to far or we are too close from a point (we hit a surface)
|
||||
if(dist_origin>MAX_DIST || closest_point<SURF_DIST) break;
|
||||
}
|
||||
return dist_origin;
|
||||
}
|
48
resources/shaders/julia.frag
Normal file
48
resources/shaders/julia.frag
Normal file
|
@ -0,0 +1,48 @@
|
|||
// ----- Vertex Shader -----
|
||||
#version 410 core
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
uniform mat4 projection;
|
||||
uniform mat4 model;
|
||||
|
||||
void main(){
|
||||
gl_Position = projection * model * vec4(position,1);
|
||||
}
|
||||
|
||||
// ----- Fragment Shader -----
|
||||
|
||||
#version 410 core
|
||||
|
||||
uniform vec2 resolution;
|
||||
uniform float time;
|
||||
|
||||
out vec3 color;
|
||||
|
||||
#define ACCURACY 1000
|
||||
#define LIMIT 1000
|
||||
|
||||
vec2 cmult(vec2 z1,vec2 z2){
|
||||
return(vec2(z1.x*z2.x-z1.y*z2.y,z1.x*z2.y+z1.y*z2.x));
|
||||
}
|
||||
|
||||
vec2 IsDiverging(vec2 coord){
|
||||
vec2 z=vec2(coord.x,coord.y);
|
||||
int i;
|
||||
for(i=0;i<ACCURACY;i++){
|
||||
z=cmult(z,z)+vec2(-0.3,0.5);
|
||||
if(length(z) > LIMIT)
|
||||
break;
|
||||
|
||||
}
|
||||
return(z);
|
||||
}
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 coord=gl_FragCoord.xy/resolution.xy;
|
||||
coord-=0.5;
|
||||
coord/=(time/10);
|
||||
float d=length(IsDiverging(coord));
|
||||
color=vec3(LIMIT/(d*2),0,0);
|
||||
}
|
|
@ -1,16 +1,3 @@
|
|||
// ----- Vertex Shader -----
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
uniform mat4 projection;
|
||||
uniform mat4 model;
|
||||
|
||||
void main(){
|
||||
gl_Position = projection * model * vec4(position,1);
|
||||
}
|
||||
|
||||
// ----- Fragment Shader -----
|
||||
|
||||
#version 330 core
|
||||
|
||||
uniform vec2 resolution;
|
||||
|
@ -18,45 +5,24 @@ uniform float time;
|
|||
|
||||
out vec3 color;
|
||||
|
||||
|
||||
#define MAX_STEPS 100
|
||||
#define MAX_DIST 100.
|
||||
#define SURF_DIST .01
|
||||
#define NORMAL_ACCURACY 0.01
|
||||
|
||||
#include "objects/sphere.frag"
|
||||
float GetDist(vec3 p) {
|
||||
vec4 s = vec4(0, 1, 6, 1);
|
||||
|
||||
float sphereDist = length(p-s.xyz)-s.w;
|
||||
float sphereDist = SphereSDF(p,vec3(0, 1, 6),1);
|
||||
float planeDist = p.y;
|
||||
|
||||
float d = min(sphereDist, planeDist);
|
||||
return d;
|
||||
}
|
||||
|
||||
float RayMarch(vec3 ro, vec3 rd) {
|
||||
float dO=0.;
|
||||
|
||||
for(int i=0; i<MAX_STEPS; i++) {
|
||||
vec3 p = ro + rd*dO;
|
||||
float dS = GetDist(p);
|
||||
dO += dS;
|
||||
if(dO>MAX_DIST || dS<SURF_DIST) break;
|
||||
}
|
||||
|
||||
return dO;
|
||||
}
|
||||
#include "algorithms/raymarching.frag"
|
||||
#include "algorithms/normal.frag"
|
||||
|
||||
|
||||
vec3 GetNormal(vec3 p) {
|
||||
float d = GetDist(p);
|
||||
vec2 e = vec2(.01, 0);
|
||||
|
||||
vec3 n = d - vec3(
|
||||
GetDist(p-e.xyy),
|
||||
GetDist(p-e.yxy),
|
||||
GetDist(p-e.yyx));
|
||||
|
||||
return normalize(n);
|
||||
}
|
||||
|
||||
float GetLight(vec3 p) {
|
||||
vec3 lightPos = vec3(0, 5, 6);
|
7
resources/shaders/objects/sphere.frag
Normal file
7
resources/shaders/objects/sphere.frag
Normal file
|
@ -0,0 +1,7 @@
|
|||
/**
|
||||
* Compute the Signed Distance Function (SDF)
|
||||
* of a sphere.
|
||||
*/
|
||||
float SphereSDF(vec3 ray_position, vec3 sphere_position, float radius){
|
||||
return(length(ray_position - sphere_position)-radius);
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
|
||||
float Sphere(vec3 ray_position, vec3 sphere_position){
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue