From 56959990e4649f11e91ef386c485a3ad37fd0ddd Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 4 Feb 2024 00:19:11 +0000 Subject: [PATCH] Add shadows --- src/raytracer/main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/raytracer/main.c b/src/raytracer/main.c index 0e305ad..2f2a4ad 100644 --- a/src/raytracer/main.c +++ b/src/raytracer/main.c @@ -2,6 +2,7 @@ #include "vector/vec.h" #include "window/window.h" #include +#include #include #include #include @@ -282,18 +283,27 @@ f32 compute_lighting(vec3f_t position, vec3f_t surface_normal, I += light.intensity; } else { vec3f_t light_direction = {0}; + f32 t_max = FLT_EPSILON; switch (light.type) { case LIGHT_TYPE_POINT: light_direction = vec_sub(vec3f_t, light.position, position); + t_max = 1; break; case LIGHT_TYPE_DIRECTIONAL: light_direction = light.direction; + t_max = INFINITY; break; default: break; } + intersection_t shadow = find_closest_intersection( + position, light_direction, 0.001f, t_max, scene); + if (shadow.closest_sphere != NULL) { + continue; + } + I += light_diffuse(light.intensity, light_direction, surface_normal); if (specular_exponent != -1.0f) {