add depth to reflections

This commit is contained in:
ktyl 2023-02-24 01:12:51 +00:00
parent 0f21b16ed7
commit 7367a2abaf
9 changed files with 32 additions and 19 deletions

View File

@ -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;

16
shader/include/depth.glsl Normal file
View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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);
}
}