From a79dfe064c791fe681f831498b17e835fb467684 Mon Sep 17 00:00:00 2001 From: ktyl Date: Sun, 30 Jan 2022 22:01:02 +0000 Subject: [PATCH] everything is chrome in the future --- shader/include/constants.glsl | 7 ++++++- shader/include/intersect.glsl | 3 ++- shader/include/lighting.glsl | 11 ++++++++++- shader/include/ray.glsl | 1 + shader/include/scene.glsl | 4 +++- shader/include/sphere.glsl | 5 +++-- shader/root/rt.glsl | 18 ++++++++++-------- src/sphere.c | 32 ++++++++++++++++++-------------- src/sphere.h | 5 +++-- todo.md | 3 ++- 10 files changed, 58 insertions(+), 31 deletions(-) diff --git a/shader/include/constants.glsl b/shader/include/constants.glsl index bc95a6a..78e4c34 100644 --- a/shader/include/constants.glsl +++ b/shader/include/constants.glsl @@ -1,4 +1,9 @@ const float INF = 50.0; const float PI = 3.14159; const float E = 2.71828; -const int BOUNCES = 3; +const int BOUNCES = 4; + +// materials +const int MAT_SKY = -1; +const int MAT_LAMBERT = 0; +const int MAT_CHROME = 1; diff --git a/shader/include/intersect.glsl b/shader/include/intersect.glsl index b6743f0..1c268c0 100644 --- a/shader/include/intersect.glsl +++ b/shader/include/intersect.glsl @@ -17,12 +17,12 @@ void intersectSphere(Ray ray, inout RayHit bestHit, Sphere sphere) bestHit.position = ray.origin + t * ray.direction; bestHit.normal = normalize(bestHit.position-c); bestHit.albedo = sphere.albedo; + bestHit.material = sphere.material; } } void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal) { - //normal = vec3(0.0,0.0,1.0); float denom = dot(normal, ray.direction); if (abs(denom) > 0.0001) @@ -34,6 +34,7 @@ void intersectPlane(Ray ray, inout RayHit bestHit, vec3 p, vec3 normal) bestHit.position = ray.origin + t*ray.direction; bestHit.normal = normal; bestHit.albedo = vec3(1.0,1.0,1.0); + bestHit.material = MAT_LAMBERT; } } } diff --git a/shader/include/lighting.glsl b/shader/include/lighting.glsl index a422a04..5456b56 100644 --- a/shader/include/lighting.glsl +++ b/shader/include/lighting.glsl @@ -33,11 +33,20 @@ vec3 sampleHemisphere(vec3 normal) } +vec3 scatterMetal(inout Ray ray, RayHit hit) +{ + ray.origin = hit.position + hit.normal*0.001; + ray.direction = reflect(ray.direction,hit.normal); + ray.energy *= 0.5; + + return vec3(0.0); +} + vec3 scatterLambert(inout Ray ray, RayHit hit) { ray.origin = hit.position + hit.normal*0.001; ray.direction = sampleHemisphere(hit.normal); - ray.energy *= 2.0 * hit.albedo * sdot(hit.normal, ray.direction); + ray.energy *= hit.albedo * sdot(hit.normal, ray.direction); return vec3(0.0); } diff --git a/shader/include/ray.glsl b/shader/include/ray.glsl index f404efd..612e925 100644 --- a/shader/include/ray.glsl +++ b/shader/include/ray.glsl @@ -21,6 +21,7 @@ struct RayHit float distance; vec3 normal; vec3 albedo; + int material; }; RayHit createRayHit() { diff --git a/shader/include/scene.glsl b/shader/include/scene.glsl index c821a99..33be62c 100644 --- a/shader/include/scene.glsl +++ b/shader/include/scene.glsl @@ -3,8 +3,10 @@ RayHit trace(Ray ray) RayHit hit = createRayHit(); Sphere s; - s.cr = vec4(0.0,0.0,0.0,2.0); + s.cr = vec4(0.0,0.0,0.0,5.0); s.albedo = vec3(1.0,0.0,0.0); + s.material = MAT_CHROME; + intersectSphere(ray, hit, s); intersectPlane(ray, hit, vec3(0.0,-1.5,0.0),vec3(0.0,1.0,0.0)); diff --git a/shader/include/sphere.glsl b/shader/include/sphere.glsl index ec9f1e3..3bb07c9 100644 --- a/shader/include/sphere.glsl +++ b/shader/include/sphere.glsl @@ -3,9 +3,10 @@ struct Sphere // (c.x,c.y,c.z,r) vec4 cr; vec3 albedo; + int material; }; -// 253 is the maximum?? TODO: use uniform buffer objects -const int SPHERES = 250; +// TODO: use uniform buffer objects +const int SPHERES = 167; uniform int _activeSpheres; layout (location = 1) uniform Sphere _spheres[SPHERES]; diff --git a/shader/root/rt.glsl b/shader/root/rt.glsl index 191a2d8..643cb55 100644 --- a/shader/root/rt.glsl +++ b/shader/root/rt.glsl @@ -29,7 +29,16 @@ vec3 shade(inout Ray ray, RayHit hit) { if (hit.distance < INF) { - return scatterLambert(ray, hit); + switch (hit.material) + { + case MAT_SKY: break; + case MAT_LAMBERT: + scatterLambert(ray, hit); + break; + case MAT_CHROME: + return scatterMetal(ray, hit); + break; + } } // sky color @@ -78,15 +87,8 @@ void main() if (length(ray.energy) < 0.001) break; } - //pixel.xyz = mix(pixel.xyz, normal, 1.0-depth); pixel.xyz = mix(pixel.xyz, vec3(1.0), depth); - //pixel.xyz = mix(firstHit.albedo, pixel.xyz, depth); - - //pixel.a = 1.0; - - //pixel.xyz = texture(_g0, uv).xyz; - // output to a specific pixel in the image imageStore(img_output, ivec2(gl_GlobalInvocationID.xy), pixel); } diff --git a/src/sphere.c b/src/sphere.c index 4503168..3121e9d 100644 --- a/src/sphere.c +++ b/src/sphere.c @@ -38,11 +38,12 @@ void makeSpheres(struct Sphere *spheres, int count) glm_vec3_scale(col, 0.5, col); glm_vec3_adds(col, 0.5, col); - spheres[i] = makeSphere(sc,radius,col); + int material = i % 2 == 0; + spheres[i] = makeSphere(sc,radius,col,material); } } -struct Sphere makeSphere(vec3 center, float radius, vec3 albedo) +struct Sphere makeSphere(vec3 center, float radius, vec3 albedo, int material) { struct Sphere s; @@ -53,20 +54,22 @@ struct Sphere makeSphere(vec3 center, float radius, vec3 albedo) glm_vec3_copy(albedo, s.albedo); + s.material = material; + return s; } -void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere) -{ - int scrloc, saloc; - scrloc = glGetUniformLocation(shaderProgram, "_sphere.cr"); - saloc = glGetUniformLocation(shaderProgram, "_sphere.albedo"); - - glUniform4fv(scrloc, 1, sphere.cr); - glUniform3fv(saloc, 1, sphere.albedo); - - updateSphereUniforms(shaderProgram, &sphere, 0); -} +//void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere) +//{ +// int scrloc, saloc; +// scrloc = glGetUniformLocation(shaderProgram, "_sphere.cr"); +// saloc = glGetUniformLocation(shaderProgram, "_sphere.albedo"); +// +// glUniform4fv(scrloc, 1, sphere.cr); +// glUniform3fv(saloc, 1, sphere.albedo); +// +// updateSphereUniforms(shaderProgram, &sphere, 0); +//} void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count) { @@ -75,7 +78,7 @@ void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int coun glUniform1i(loc, count); // each sphere takes up two uniform locations - const int stride = 2; + const int stride = 3; // first location in the array loc = glGetUniformLocation(shaderProgram, "_spheres[0].cr"); @@ -85,5 +88,6 @@ void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int coun struct Sphere s = spheres[i]; glUniform4fv(loc+i*stride, 1, s.cr); glUniform3fv(loc+i*stride+1, 1, s.albedo); + glUniform1i( loc+i*stride+2, s.material); } } diff --git a/src/sphere.h b/src/sphere.h index 1c8f19d..67579ec 100644 --- a/src/sphere.h +++ b/src/sphere.h @@ -13,9 +13,10 @@ struct Sphere { vec4 cr; vec3 albedo; + int material; }; void makeSpheres(struct Sphere *spheres, int count); -struct Sphere makeSphere(vec3 center, float radius, vec3 albedo); -void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere); +struct Sphere makeSphere(vec3 center, float radius, vec3 albedo, int material); +//void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere); void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count); diff --git a/todo.md b/todo.md index 0ebbe06..d889022 100644 --- a/todo.md +++ b/todo.md @@ -11,7 +11,8 @@ * [x] perspective * [x] camera lookat * [x] do a bounce - * [ ] do a 'flect + * [x] do a 'flect + * [ ] properly sample depth for reflective surfaces * [ ] do a 'fract * [ ] depth of field * [ ] emission