From b312d390da577923c25156572017dfa7e310a8c1 Mon Sep 17 00:00:00 2001 From: ktyl Date: Fri, 13 Aug 2021 13:43:54 +0100 Subject: [PATCH] logarithmic depth --- shader/include/constants.glsl | 3 ++- shader/root/rt.glsl | 6 ++++-- shader/root/rtpre.glsl | 18 ++++++++++++------ src/cam.c | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/shader/include/constants.glsl b/shader/include/constants.glsl index 7892915..bcba6b7 100644 --- a/shader/include/constants.glsl +++ b/shader/include/constants.glsl @@ -1,4 +1,5 @@ -const float INF = 17.0; +const float INF = 100.0; const float PI = 3.14159; +const float E = 2.71828; const int SAMPLES = 1; const int BOUNCES = 4; diff --git a/shader/root/rt.glsl b/shader/root/rt.glsl index 77362e7..e778f86 100644 --- a/shader/root/rt.glsl +++ b/shader/root/rt.glsl @@ -61,9 +61,11 @@ void main() pixel.xyz /= SAMPLES; vec4 d = imageLoad(_g0, ivec2(gl_GlobalInvocationID.xy)); - float depth = d.x; + float depth = d.w; + vec3 normal = d.xyz*2.0-1.0; // unpack normal packaged into texture - pixel.xyz = mix(pixel.xyz, vec3(0), depth); + //pixel.xyz = mix(pixel.xyz, normal, 1.0-depth); + pixel.xyz = mix(pixel.xyz, vec3(1.0), depth); //pixel.a = 1.0; diff --git a/shader/root/rtpre.glsl b/shader/root/rtpre.glsl index dfb9019..793fc89 100644 --- a/shader/root/rtpre.glsl +++ b/shader/root/rtpre.glsl @@ -33,16 +33,22 @@ void main() Ray ray = createCameraRay(uv); RayHit hit = trace(ray); - //pixel.xyz = hit.normal; - - // TODO: non-linear depth - float n = 1; + // n roughly correlates to steepness of log curve + // TODO: what does this mean in mathematical terms?? + float n = 3; float f = INF; float z = hit.distance; + + float depth; + // linear depth + //depth = z/f; - float depth = (1.0/z-1.0/n)/(1.0/n-1.0/f); + // logarithmic depth + depth = log(z*pow(E,n)/f)/n; - pixel.x = z/f; + // pack normal into texture + pixel.xyz = hit.normal*0.5+0.5; + pixel.w = depth; imageStore(g0_output, pixelCoords, pixel); } diff --git a/src/cam.c b/src/cam.c index 675d9ad..a2864e5 100644 --- a/src/cam.c +++ b/src/cam.c @@ -8,7 +8,7 @@ void updateCameraUniforms(GLuint shaderProgram, float aspect) // wobble up dir vec3 cdir, cright, cup; - vec3 up = {0.1*sin(t),1.0,0.2*cos(t)}; + vec3 up = {0.1*sin(t),1.0,0.05*cos(0.5*t)}; glm_vec3_normalize(up); // camera and target pos