From 8048d0b799150d976ccb54089981734eaa742cb3 Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Wed, 15 Jan 2025 12:18:21 +0100 Subject: [PATCH] + | New window2 scene + better light direct sampling, to tweak and see --- scenes/window.rt | 2 +- scenes/window2.rt | 30 ++++++++++++++ shaders/compute.glsl | 2 +- shaders/light.glsl | 83 ++++++++++++++++++++++++++++++++++++-- shaders/volumetric.glsl | 2 +- srcs/class/SceneParser.cpp | 5 +-- 6 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 scenes/window2.rt diff --git a/scenes/window.rt b/scenes/window.rt index 4b6f998..7a850b9 100644 --- a/scenes/window.rt +++ b/scenes/window.rt @@ -7,7 +7,7 @@ MAT 255 255 255 10.0 0.0 0.0 //light pl 0 0 0 0 1 0 1 -sp -10 10 -9 0.01 2 +sp -10 10 -9 10 2 qu 0 0 0 0 6 0 18 0 0 0 qu 0 0 -18 0 6 0 18 0 0 0 diff --git a/scenes/window2.rt b/scenes/window2.rt new file mode 100644 index 0000000..7fcc991 --- /dev/null +++ b/scenes/window2.rt @@ -0,0 +1,30 @@ +CAM 0 2 -2 0 90 + +MAT 255 255 255 0.0 1.0 0.0 //white +MAT 150 150 150 0.0 1.0 0.0 //grey + +MAT 255 255 255 10.0 0.0 0.0 //light + +sp -5 5 0 1 2 + +# qu -5 0 -2.5 0 5 0 0 0 5 2 + + +pl 0 0 0 0 1 0 1 + +qu 2.5 0 -2.5 0 5 0 0 0 5 0 +qu -2.5 0 -2.5 0 5 0 5 0 0 0 +qu -2.5 0 2.5 0 5 0 5 0 0 0 +qu -2.5 5 -2.5 0 0 5 5 0 0 0 + +qu -2.5 0 -2.5 0 2.5 0 0 0 5 0 +qu -2.5 3.5 -2.5 0 1.5 0 0 0 5 0 + +qu -2.5 2.5 0.5 0 1 0 0 0 2 0 +qu -2.5 2.5 -2.5 0 1 0 0 0 2 0 + + + + + + diff --git a/shaders/compute.glsl b/shaders/compute.glsl index 6d9ef86..2b0a0a5 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -192,7 +192,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state) color /= p; // - calculateLightColor(color, light, mat, hit); + calculateLightColor(mat, hit, color, light, rng_state); if (mat.emission > 0.0) break; diff --git a/shaders/light.glsl b/shaders/light.glsl index 41f886f..35da9dc 100644 --- a/shaders/light.glsl +++ b/shaders/light.glsl @@ -1,6 +1,9 @@ +hitInfo traceRay(Ray ray); vec3 GetEnvironmentLight(Ray ray) { + return vec3(0.); + vec3 sun_pos = vec3(-0.5, 0.5, 0.5); float SunFocus = 1.5; float SunIntensity = 1.; @@ -18,9 +21,55 @@ vec3 GetEnvironmentLight(Ray ray) return composite; } -hitInfo traceRay(Ray ray); +vec3 sampleSphereLight(vec3 position, GPUObject obj, GPUMaterial mat, inout uint rng_state) +{ + float theta = 2.0 * M_PI * randomValue(rng_state); + float phi = acos(2.0 * randomValue(rng_state) - 1.0); + + vec3 sample_point = obj.position + obj.radius * vec3( + sin(phi) * cos(theta), + sin(phi) * sin(theta), + cos(phi) + ); + + vec3 light_dir = normalize(sample_point - position); + float light_dist = length(sample_point - position); + + Ray shadow_ray = Ray(position + light_dir * 0.001, light_dir); + hitInfo shadow_hit = traceRay(shadow_ray); + + if (shadow_hit.obj_index != -1 && shadow_hit.t < light_dist) + return vec3(0.0); -vec3 sampleLights(vec3 position) + float cos_theta = max(0.0, -dot(light_dir, normalize(sample_point - obj.position))); + return mat.emission * mat.color / (light_dist); +} + +vec3 sampleQuadLight(vec3 position, GPUObject obj, GPUMaterial mat, inout uint rng_state) +{ + float u = randomValue(rng_state); + float v = randomValue(rng_state); + + vec3 sample_point = obj.position + u * obj.vertex1 + v * obj.vertex2; + vec3 light_dir = normalize(sample_point - position); + float light_dist = length(sample_point - position); + + Ray shadow_ray = Ray(position + light_dir * 0.001, light_dir); + hitInfo shadow_hit = traceRay(shadow_ray); + + if (shadow_hit.obj_index != -1 && shadow_hit.t < light_dist) + return vec3(0.0); + + vec3 crossQuad = cross(obj.vertex1, obj.vertex2); + float area = length(crossQuad); + float pdf = 1.0 / area; + + vec3 normal = normalize(crossQuad); + float cos_theta = max(0.0, dot(normal, -light_dir)); + return mat.emission * mat.color * cos_theta / (pdf * light_dist * light_dist); +} + +vec3 sampleLights(vec3 position, inout uint rng_state) { vec3 light = vec3(0.0); @@ -45,9 +94,35 @@ vec3 sampleLights(vec3 position) return (light); } -void calculateLightColor(inout vec3 color, inout vec3 light, GPUMaterial mat, hitInfo hit) +// vec3 sampleLights(vec3 position, inout uint rng_state) +// { +// vec3 light = vec3(0.0); + +// int emissive_count = 0; + +// for (int i = 0; i < u_objectsNum; i++) +// if (materials[objects[i].mat_index].emission > 0.0) +// emissive_count++; + +// if (emissive_count == 0) +// return (vec3(0.)); + +// int target_light = int(floor(randomValue(rng_state) * float(emissive_count))); + +// GPUObject obj = objects[target_light]; +// GPUMaterial mat = materials[obj.mat_index]; + +// if (obj.type == 0) +// light = sampleSphereLight(position, obj, mat, rng_state); +// else if (obj.type == 2) +// light = sampleQuadLight(position, obj, mat, rng_state); + +// return (light); +// } + +void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state) { color *= mat.color; light += mat.emission * mat.color; - // light += sampleLights(hit.position); + // light += sampleLights(hit.position, rng_state); } \ No newline at end of file diff --git a/shaders/volumetric.glsl b/shaders/volumetric.glsl index ad8c814..95ca3c8 100644 --- a/shaders/volumetric.glsl +++ b/shaders/volumetric.glsl @@ -44,7 +44,7 @@ void calculateVolumetricLight(float t_scatter, inout Ray ray, inout vec3 color, transmittance *= exp(-volume.sigma_t * t_scatter); color *= volume.sigma_s / volume.sigma_t; - light += transmittance * color * sampleLights(scatter_pos); + light += transmittance * color * sampleLights(scatter_pos, rng_state); float cos_theta = sampleHG(volume.g, rng_state); vec3 new_dir = sampleDirection(ray.direction, cos_theta, rng_state); diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index 786343d..364d188 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -124,10 +124,7 @@ Triangle *SceneParser::getFace(std::stringstream &line, std::vector & triangle[0] = vertices[getVertexIndex(line, vertices.size())]; triangle[1] = vertices[getVertexIndex(line, vertices.size())]; triangle[2] = vertices[getVertexIndex(line, vertices.size())]; - std::cout << triangle[0].x << " " << triangle[0].y << " " << triangle[0].z << std::endl; - std::cout << triangle[1].x << " " << triangle[1].y << " " << triangle[1].z << std::endl; - std::cout << triangle[2].x << " " << triangle[2].y << " " << triangle[2].z << std::endl; - return(new Triangle(triangle[0], triangle[1], triangle[2], 0)); + return (new Triangle(triangle[0], triangle[1], triangle[2], 0)); } void SceneParser::parseObj(std::stringstream &objInfo)