diff options
Diffstat (limited to 'resources/shaders/algorithms')
| -rw-r--r-- | resources/shaders/algorithms/normal.frag | 24 | ||||
| -rw-r--r-- | resources/shaders/algorithms/ray_marching.glsl | 5 | ||||
| -rw-r--r-- | resources/shaders/algorithms/raymarching.frag | 26 |
3 files changed, 50 insertions, 5 deletions
diff --git a/resources/shaders/algorithms/normal.frag b/resources/shaders/algorithms/normal.frag new file mode 100644 index 0000000..0f61544 --- /dev/null +++ b/resources/shaders/algorithms/normal.frag @@ -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); +}
\ No newline at end of file diff --git a/resources/shaders/algorithms/ray_marching.glsl b/resources/shaders/algorithms/ray_marching.glsl deleted file mode 100644 index d984c0e..0000000 --- a/resources/shaders/algorithms/ray_marching.glsl +++ /dev/null @@ -1,5 +0,0 @@ - - -RayMarching(vec3 ro, vec3 rd){ - -}
\ No newline at end of file diff --git a/resources/shaders/algorithms/raymarching.frag b/resources/shaders/algorithms/raymarching.frag new file mode 100644 index 0000000..b8de515 --- /dev/null +++ b/resources/shaders/algorithms/raymarching.frag @@ -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; +}
\ No newline at end of file |
