Add shadows

This commit is contained in:
Abdelrahman Said 2024-02-04 00:19:11 +00:00
parent 1f5568b63e
commit 56959990e4

View File

@ -2,6 +2,7 @@
#include "vector/vec.h" #include "vector/vec.h"
#include "window/window.h" #include "window/window.h"
#include <SDL2/SDL_events.h> #include <SDL2/SDL_events.h>
#include <float.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -282,18 +283,27 @@ f32 compute_lighting(vec3f_t position, vec3f_t surface_normal,
I += light.intensity; I += light.intensity;
} else { } else {
vec3f_t light_direction = {0}; vec3f_t light_direction = {0};
f32 t_max = FLT_EPSILON;
switch (light.type) { switch (light.type) {
case LIGHT_TYPE_POINT: case LIGHT_TYPE_POINT:
light_direction = vec_sub(vec3f_t, light.position, position); light_direction = vec_sub(vec3f_t, light.position, position);
t_max = 1;
break; break;
case LIGHT_TYPE_DIRECTIONAL: case LIGHT_TYPE_DIRECTIONAL:
light_direction = light.direction; light_direction = light.direction;
t_max = INFINITY;
break; break;
default: default:
break; 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); I += light_diffuse(light.intensity, light_direction, surface_normal);
if (specular_exponent != -1.0f) { if (specular_exponent != -1.0f) {