From: Lukáš Jiřiště Date: Wed, 27 Nov 2024 09:08:03 +0000 (+0100) Subject: Fix shadow casting X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=a2872aa31d766d497827b580db546535bc87ccc9;p=42%2FminiRT.git Fix shadow casting The shadows did not work because the obstruction detection used a function that expects ray with normalized direction vector. But the obstruction detection did not normalize its ray because the check for whether the obstruction is between the object and the light was easier. The fix is normalizing the ray vector and using distance. --- diff --git a/src/scene.c b/src/scene.c index b7cc7aa..005b996 100644 --- a/src/scene.c +++ b/src/scene.c @@ -283,9 +283,12 @@ t_color get_light_contribution(t_vec3 point, const t_object *object, const t_lig const t_object *obstruction; t_vec3 normal; double angle_multiplier; + double distance; new_ray.start = point; new_ray.direction = vec_diff(light->position, new_ray.start); + distance = vec_norm(new_ray.direction); + new_ray.direction = vec_real_mul(new_ray.direction, 1 / distance); obstruction = find_nearest_object(&new_ray, &scene->objects, object); normal = get_object_normal(object, point); angle_multiplier = vec_scalar_mul(normal, new_ray.direction) @@ -293,7 +296,7 @@ t_color get_light_contribution(t_vec3 point, const t_object *object, const t_lig if (object->type == PLANE) angle_multiplier = fabs(angle_multiplier); if (angle_multiplier > 0 && (!obstruction - || 1 < get_intersection_arg(&new_ray, obstruction) + || distance < get_intersection_arg(&new_ray, obstruction) || get_intersection_arg(&new_ray, obstruction) < 0)) return (vec_real_mul( vec_elwise_mul(light->color, object->object.plane.color),