From ecc1f683297a571efcf45fd5b979043126b2bedd Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Tue, 4 Mar 2025 21:15:04 +0100 Subject: [PATCH] + | Better bouncing --- scenes/window2.rt | 2 +- shaders/light.glsl | 31 +++++++++++++++++++++---------- srcs/class/Window.cpp | 4 +--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/scenes/window2.rt b/scenes/window2.rt index 0327005..4e52591 100644 --- a/scenes/window2.rt +++ b/scenes/window2.rt @@ -5,7 +5,7 @@ MAT 150 150 150 0.0 1.0 0.0 //grey MAT 255 255 255 1.0 0.0 0.0 //light -sp -10 5 0 0.01 2 +sp -10 7 0 0.1 2 # qu -5 0 -2.5 0 5 0 0 0 5 0 2 diff --git a/shaders/light.glsl b/shaders/light.glsl index ddc4c38..d094dff 100644 --- a/shaders/light.glsl +++ b/shaders/light.glsl @@ -2,7 +2,7 @@ hitInfo traceRay(inout Ray ray); vec3 GetEnvironmentLight(Ray ray) { - return vec3(0.); + // return vec3(0.); vec3 sun_pos = vec3(-0.5, 0.5, 0.5); float SunFocus = 1.5; float SunIntensity = 1.; @@ -37,17 +37,26 @@ vec3 sampleSphereLight(vec3 position, GPUObject obj, int light_index, GPUMateria Ray shadow_ray = Ray(position + light_dir * 0.001, light_dir, (1.0 / light_dir)); hitInfo shadow_hit = traceRay(shadow_ray); - vec3 dir = normalize(vec3(-0.5, 0.2, 0.)); + vec3 dir = normalize(vec3(-0.5, 0.15, 0.)); if (dot(shadow_ray.direction, dir) < 0.995 || shadow_hit.obj_index != light_index) { - theta = 2.0 * M_PI * randomValue(rng_state); - phi = acos(2.0 * randomValue(rng_state) - 1.0); - - sample_point = obj.position + tan(acos(0.995)) * light_dist * vec3( - sin(phi) * cos(theta), - sin(phi) * sin(theta), - cos(phi) - ); + float circleRadius = light_dist * tan(acos(0.995)); + + // Uniformly sample a point in a disk. + float r = circleRadius * sqrt(randomValue(rng_state)); + float theta = 2.0 * M_PI * randomValue(rng_state); + vec2 diskSample = vec2(r * cos(theta), r * sin(theta)); + + // Build an orthonormal basis for the plane perpendicular to 'dir'. + vec3 up = abs(dir.y) < 0.99 ? vec3(0, 1, 0) : vec3(1, 0, 0); + vec3 tangent = normalize(cross(up, dir)); + vec3 bitangent = cross(dir, tangent); + + // Determine the center of the projected circle on the wall. + vec3 circleCenter = obj.position + light_dist * dir; + + // Compute the final sample point on the projected circle. + vec3 sample_point = circleCenter + diskSample.x * tangent + diskSample.y * bitangent; Ray light_ray = Ray(sample_point, -dir, (1.0 / -dir)); hitInfo light_ray_hit = traceRay(light_ray); @@ -66,6 +75,8 @@ vec3 sampleSphereLight(vec3 position, GPUObject obj, int light_index, GPUMateria if (dot(reflect_ray.direction, reflect_to_particle) < 0.995) return vec3(0.0); + mat.color *= light_ray_mat.color; + return mat.emission * mat.color * vec3(10.0); } float cos_theta = max(0.0, -dot(light_dir, normalize(sample_point - obj.position))); diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 4e076d3..c0a2a9a 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */ -/* Updated: 2025/03/18 16:23:10 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 16:24:39 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -326,7 +326,6 @@ void Window::imGuiRender(ShaderProgram &raytracing_program) has_changed |= ImGui::SliderInt("Triangle treshold", &_scene->getDebug().triangle_treshold, 1, 2000); } - _renderer->renderImgui(*_clusterizer); _clusterizer->imguiRender(); @@ -335,7 +334,6 @@ void Window::imGuiRender(ShaderProgram &raytracing_program) ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - if (has_changed) _frameCount = (accumulate == 0) - 1; }