+ | focal distance

This commit is contained in:
RedShip
2025-01-08 19:14:14 +01:00
parent e31d17489c
commit ed7bd0f38b
4 changed files with 49 additions and 36 deletions

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/27 14:52:10 by TheRed #+# #+# */ /* Created: 2024/09/27 14:52:10 by TheRed #+# #+# */
/* Updated: 2024/12/23 19:04:53 by ycontre ### ########.fr */ /* Updated: 2025/01/08 17:44:56 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,6 +29,7 @@
# include <iostream> # include <iostream>
# include <fstream> # include <fstream>
# include <sstream> # include <sstream>
# include <vector>
# include <string> # include <string>
# include <memory> # include <memory>
# include <map> # include <map>

View File

@ -71,7 +71,7 @@ MAT 255 255 255 0.0 1.0 1.0
sp 0 5 -4 3 30 sp 0 5 -4 3 30
sp -4 4 0 3 31 sp -4 4 0 3 31
cu 4 6 2 5 8 5 32 cu 4 7 2 5 8 5 32
cu -20 4 0 5 8 5 32 cu -20 4 0 5 8 5 32
MAT 30 30 30 0.0 0.0 0.0 MAT 30 30 30 0.0 0.0 0.0

View File

@ -135,7 +135,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
hitInfo hit = traceRay(ray); hitInfo hit = traceRay(ray);
if (hit.obj_index == -1) if (hit.obj_index == -1)
{ {
light += GetEnvironmentLight(ray); // light += GetEnvironmentLight(ray);
// light += vec3(135 / 255.0f, 206 / 255.0f, 235 / 255.0f); //ambient color // light += vec3(135 / 255.0f, 206 / 255.0f, 235 / 255.0f); //ambient color
break; break;
} }
@ -145,7 +145,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
// RR // RR
float p = max(color.r, max(color.g, color.b)); float p = max(color.r, max(color.g, color.b));
if (randomValue(rng_state) > p) if (randomValue(rng_state) > p && i > 1)
break; break;
color /= p; color /= p;
// //
@ -161,6 +161,33 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
return (color * light); return (color * light);
} }
Ray initRay(vec2 uv, inout uint rng_state)
{
float fov = 90.0;
float focal_length = 1.0 / tan(radians(fov) / 2.0);
vec3 origin = u_cameraPosition;
vec3 view_space_ray = normalize(vec3(uv.x, uv.y, -focal_length));
vec3 ray_direction = normalize((inverse(u_viewMatrix) * vec4(view_space_ray, 0.0)).xyz);
float focus_distance = 4.5;
float aperture = 0.25;
vec3 right = vec3(u_viewMatrix[0][0], u_viewMatrix[1][0], u_viewMatrix[2][0]);
vec3 up = vec3(u_viewMatrix[0][1], u_viewMatrix[1][1], u_viewMatrix[2][1]);
vec3 focal_point = u_cameraPosition + ray_direction * focus_distance;
float r = sqrt(randomValue(rng_state));
float theta = 2.0 * M_PI * randomValue(rng_state);
vec2 lens_point = aperture * r * vec2(cos(theta), sin(theta));
origin += right * lens_point.x + up * lens_point.y;
ray_direction = normalize(focal_point - origin);
return (Ray(origin, ray_direction));
}
void main() void main()
{ {
ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy); ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
@ -175,22 +202,7 @@ void main()
vec2 uv = ((vec2(pixel_coords) + jitter) / u_resolution) * 2.0 - 1.0;; vec2 uv = ((vec2(pixel_coords) + jitter) / u_resolution) * 2.0 - 1.0;;
uv.x *= u_resolution.x / u_resolution.y; uv.x *= u_resolution.x / u_resolution.y;
float fov = 90.0; Ray ray = initRay(uv, rng_state);
float focal_length = 1.0 / tan(radians(fov) / 2.0);
vec3 view_space_ray = normalize(vec3(uv.x, uv.y, -focal_length));
vec3 ray_direction = normalize((inverse(u_viewMatrix) * vec4(view_space_ray, 0.0)).xyz);
float focal_distance = 0.0;
float aperture_size = 0.0;
float theta = randomValue(rng_state) * 2.0 * M_PI;
float radius = sqrt(randomValue(rng_state)) * aperture_size;
vec2 aperture_point = vec2(cos(theta) * radius, sin(theta) * radius);
vec3 ray_origin = u_cameraPosition + vec3(aperture_point, 0.0);
Ray ray = Ray(ray_origin, ray_direction);
vec3 color = pathtrace(ray, rng_state); vec3 color = pathtrace(ray, rng_state);
float blend = 1.0 / float(u_frameCount + 1); float blend = 1.0 / float(u_frameCount + 1);

View File

@ -36,21 +36,21 @@ vec3 randomHemisphereDirection(vec3 normal, inout uint rng_state)
return (direction * sign(dot(normal, direction))); return (direction * sign(dot(normal, direction)));
} }
vec3 GetEnvironmentLight(Ray ray) // vec3 GetEnvironmentLight(Ray ray)
{ // {
vec3 sun_pos = vec3(-0.5, 0.5, 0.5); // vec3 sun_pos = vec3(-0.5, 0.5, 0.5);
float SunFocus = 1.5; // float SunFocus = 1.5;
float SunIntensity = 1; // float SunIntensity = 1;
vec3 GroundColour = vec3(0.5, 0.5, 0.5); // vec3 GroundColour = vec3(0.5, 0.5, 0.5);
vec3 SkyColourHorizon = vec3(135 / 255.0f, 206 / 255.0f, 235 / 255.0f); // vec3 SkyColourHorizon = vec3(135 / 255.0f, 206 / 255.0f, 235 / 255.0f);
vec3 SkyColourZenith = SkyColourHorizon / 2.0; // vec3 SkyColourZenith = SkyColourHorizon / 2.0;
float skyGradientT = pow(smoothstep(0, 0.4, ray.direction.y), 0.35); // float skyGradientT = pow(smoothstep(0, 0.4, ray.direction.y), 0.35);
float groundToSkyT = smoothstep(-0.01, 0, ray.direction.y); // float groundToSkyT = smoothstep(-0.01, 0, ray.direction.y);
vec3 skyGradient = mix(SkyColourHorizon, SkyColourZenith, skyGradientT); // vec3 skyGradient = mix(SkyColourHorizon, SkyColourZenith, skyGradientT);
float sun = pow(max(0, dot(ray.direction, sun_pos.xyz)), SunFocus) * SunIntensity; // float sun = pow(max(0, dot(ray.direction, sun_pos.xyz)), SunFocus) * SunIntensity;
// Combine ground, sky, and sun // // Combine ground, sky, and sun
vec3 composite = mix(GroundColour, skyGradient, groundToSkyT) + sun * int(groundToSkyT >= 1); // vec3 composite = mix(GroundColour, skyGradient, groundToSkyT) + sun * int(groundToSkyT >= 1);
return composite; // return composite;
} // }