From 7367a2abaf03778a0695a0910f56dcbbcca0076a Mon Sep 17 00:00:00 2001 From: ktyl Date: Fri, 24 Feb 2023 01:12:51 +0000 Subject: [PATCH] add depth to reflections --- shader/include/constants.glsl | 4 ++-- shader/include/depth.glsl | 16 ++++++++++++++++ shader/include/intersect.glsl | 2 +- shader/include/ray.glsl | 2 ++ shader/include/scene.glsl | 4 +++- shader/root/rt.glsl | 5 ++++- shader/root/rtpre.glsl | 14 ++------------ src/main.c | 2 +- src/sphere.c | 2 +- 9 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 shader/include/depth.glsl diff --git a/shader/include/constants.glsl b/shader/include/constants.glsl index 78e4c34..104b27f 100644 --- a/shader/include/constants.glsl +++ b/shader/include/constants.glsl @@ -1,7 +1,7 @@ -const float INF = 50.0; +const float INF = 30.0; const float PI = 3.14159; const float E = 2.71828; -const int BOUNCES = 4; +const int BOUNCES = 5; // materials const int MAT_SKY = -1; diff --git a/shader/include/depth.glsl b/shader/include/depth.glsl new file mode 100644 index 0000000..34fc7f2 --- /dev/null +++ b/shader/include/depth.glsl @@ -0,0 +1,16 @@ +float getLinearDepth(float distance) +{ + return distance/INF; +} + +float getLogarithmicDepth(float distance) +{ + // n roughly correlates to steepness of log curve + // TODO: what does this mean in mathematical terms?? + float n = 4; + float f = INF; + float z = distance; + + // logarithmic depth + return max(0,log(z*pow(E,n)/f)/n); +} diff --git a/shader/include/intersect.glsl b/shader/include/intersect.glsl index 1c268c0..403e743 100644 --- a/shader/include/intersect.glsl +++ b/shader/include/intersect.glsl @@ -33,7 +33,7 @@ void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal) bestHit.distance = t; bestHit.position = ray.origin + t*ray.direction; bestHit.normal = normal; - bestHit.albedo = vec3(1.0,1.0,1.0); + bestHit.albedo = vec3(1.0,.4,.4); bestHit.material = MAT_LAMBERT; } } diff --git a/shader/include/ray.glsl b/shader/include/ray.glsl index 612e925..9191a9d 100644 --- a/shader/include/ray.glsl +++ b/shader/include/ray.glsl @@ -3,6 +3,7 @@ struct Ray vec3 origin; vec3 direction; vec3 energy; + float distance; }; Ray createRay(vec3 origin, vec3 direction) { @@ -11,6 +12,7 @@ Ray createRay(vec3 origin, vec3 direction) ray.origin = origin; ray.direction = direction; ray.energy = vec3(1.0,1.0,1.0); + ray.distance = 0; return ray; } diff --git a/shader/include/scene.glsl b/shader/include/scene.glsl index 57324d0..c9a20ec 100644 --- a/shader/include/scene.glsl +++ b/shader/include/scene.glsl @@ -1,4 +1,4 @@ -RayHit trace(Ray ray) +RayHit trace(inout Ray ray) { RayHit hit = createRayHit(); @@ -11,5 +11,7 @@ RayHit trace(Ray ray) intersectSphere(ray, hit, _spheres[i]); } + ray.distance += hit.distance * float(hit.distance < INF); + return hit; } diff --git a/shader/root/rt.glsl b/shader/root/rt.glsl index 643cb55..9d9d575 100644 --- a/shader/root/rt.glsl +++ b/shader/root/rt.glsl @@ -24,6 +24,7 @@ uniform vec3 _skyColor = vec3(0.68,0.85,0.9); // scene.glsl includes scene trace function #include scene.glsl #include lighting.glsl +#include depth.glsl vec3 shade(inout Ray ray, RayHit hit) { @@ -77,11 +78,13 @@ void main() int bounces = (1-sky) * BOUNCES; pixel.xyz = mix(pixel.xyz, _skyColor, sky); - // trace the rays path around the scene + // trace the ray's path around the scene for (int j = 0; j < bounces; j++) { RayHit hit = trace(ray); + depth = getLogarithmicDepth(ray.distance); + pixel.xyz += ray.energy * shade(ray, hit); if (length(ray.energy) < 0.001) break; diff --git a/shader/root/rtpre.glsl b/shader/root/rtpre.glsl index 53fb86d..047b644 100644 --- a/shader/root/rtpre.glsl +++ b/shader/root/rtpre.glsl @@ -18,6 +18,7 @@ layout (rgba32f, binding = 3) writeonly uniform image2D g1_output; #include camera.glsl #include image.glsl #include scene.glsl +#include depth.glsl void main() { @@ -34,18 +35,7 @@ void main() Ray ray = createCameraRay(uv); RayHit hit = trace(ray); - // n roughly correlates to steepness of log curve - // TODO: what does this mean in mathematical terms?? - float n = 4; - float f = INF; - float z = hit.distance; - - float depth; - // linear depth - //depth = z/f; - - // logarithmic depth - depth = max(0,log(z*pow(E,n)/f)/n); + float depth = getLogarithmicDepth(hit.distance); // pack normal into texture pixel.xyz = hit.normal*0.5+0.5; diff --git a/src/main.c b/src/main.c index 5b1237b..4a8ba8c 100644 --- a/src/main.c +++ b/src/main.c @@ -135,7 +135,7 @@ void updateUniforms(GLuint shaderProgram, float t) updateCameraUniforms(shaderProgram, aspect, t); // make and update spheres - const int sphereCount = 42; + const int sphereCount = 41; struct Sphere spheres[sphereCount]; makeSpheres(spheres, sphereCount, t); updateSphereUniforms(shaderProgram, spheres, sphereCount); diff --git a/src/sphere.c b/src/sphere.c index 08c625f..e3daa97 100644 --- a/src/sphere.c +++ b/src/sphere.c @@ -48,7 +48,7 @@ void makeSpheres(struct Sphere *spheres, int count, float t) glm_vec3_scale(col, 0.5, col); glm_vec3_adds(col, 0.5, col); - int material = 0; + int material = i % 2; spheres[sphereIdx++] = makeSphere(sc,radius,col,material); } }