Solve plane shadow acne
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 26 Nov 2024 19:16:04 +0000 (20:16 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 26 Nov 2024 19:16:04 +0000 (20:16 +0100)
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

index 2b112d9ef7adcb8d03585f325758a8c2d107752d..b7cc7aa08c8fdec4c19393df5102aac12861b9c2 100644 (file)
@@ -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