aboutsummaryrefslogtreecommitdiff
path: root/resources/shaders/algorithms/raymarching.frag
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2020-07-09 13:02:31 +0200
committerLoic Guegan <manzerbredes@mailbox.org>2020-07-09 13:02:31 +0200
commit12829892b262a0c7fcccba9198e5b6b31b2a8015 (patch)
tree908152ccc4fd1f4ee2c16b4bb8e0bee215ebdae7 /resources/shaders/algorithms/raymarching.frag
parentaac94e911b0bab8db5cdb5efb8d4d8f2d4072610 (diff)
Cleaning codeHEADmaster
Diffstat (limited to 'resources/shaders/algorithms/raymarching.frag')
-rw-r--r--resources/shaders/algorithms/raymarching.frag26
1 files changed, 26 insertions, 0 deletions
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