From becb4660efcfb74579afe77d0a22c13684230db2 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 26 Nov 2024 20:16:04 +0100 Subject: [PATCH] Solve plane shadow acne This commit makes it so an object does not use itself for shadow calculation. This only works properly for objects, that are not concave. --- src/scene.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/scene.c b/src/scene.c index 2b112d9..b7cc7aa 100644 --- a/src/scene.c +++ b/src/scene.c @@ -207,10 +207,12 @@ double get_intersection_arg(const t_ray *ray, const t_object *object) return (get_intersection_arg_bounded(ray, object)); } -const t_object *find_nearest_object(const t_ray *ray, const t_vec *objects) +const t_object *find_nearest_object(const t_ray *ray, const t_vec *objects, + const t_object *ignored) { size_t i; const t_object *object_found; + const t_object *object; double distance_found; double distance; @@ -219,13 +221,15 @@ const t_object *find_nearest_object(const t_ray *ray, const t_vec *objects) i = 0; while (i < objects->size) { - distance = get_intersection_arg(ray, ft_vec_caccess(objects, i)); + object = ft_vec_caccess(objects, i++); + if (object == ignored) + continue ; + distance = get_intersection_arg(ray, object); if (0 < distance && distance < distance_found) { distance_found = distance; - object_found = ft_vec_caccess(objects, i); + object_found = object; } - ++i; } return (object_found); } @@ -282,7 +286,7 @@ t_color get_light_contribution(t_vec3 point, const t_object *object, const t_lig new_ray.start = point; new_ray.direction = vec_diff(light->position, new_ray.start); - obstruction = find_nearest_object(&new_ray, &scene->objects); + obstruction = find_nearest_object(&new_ray, &scene->objects, object); normal = get_object_normal(object, point); angle_multiplier = vec_scalar_mul(normal, new_ray.direction) / (vec_norm(normal) * vec_norm(new_ray.direction)); @@ -323,7 +327,7 @@ t_color trace_ray(const t_ray *ray, const t_scene *scene) { const t_object *object_found; - object_found = find_nearest_object(ray, &scene->objects); + object_found = find_nearest_object(ray, &scene->objects, NULL); if (object_found) return (get_object_color(ray, object_found, scene)); else -- 2.30.2