Implement clicking on an object to select
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 7 Jan 2025 14:36:22 +0000 (15:36 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 7 Jan 2025 14:36:22 +0000 (15:36 +0100)
The selected object can then be manipulated the same way as the
(current) camera. It also uses the axes of the current camera (relative
coordinates), which may or may not be desirable.
The object is selected by left mouse button and deselected by right
mouse button or escape.

inc/miniRT.h
src/main.c
src/parsing.c
src/scene.c

index fd75439939d26a594bbb4d2a78dc64d45c45c518..bbe9089b387e511563b4cc561074425cc5994884 100644 (file)
@@ -98,7 +98,8 @@ typedef struct s_scene
        t_vec                   objects;
        t_vec                   lights;
        t_vec                   cameras;
-       const t_camera  *current_camera;
+       size_t                  current_camera_ind;
+       t_element               *current_element;
 }                                      t_scene;
 
 typedef struct s_ray
@@ -126,8 +127,17 @@ typedef struct s_session
        t_scene scene;
 }                      t_session;
 
-t_color        trace_ray(const t_ray *ray, const t_scene *scene);
+typedef struct s_obstruction
+{
+       t_object        *object;
+       double          distance;
+}                                      t_obstruction;
+
+t_color        trace_ray(const t_ray *ray, t_scene *scene);
 t_color        color_sRGB_to_lin(t_color_sRGB sRGB);
+
+t_obstruction  find_nearest_obstruction(const t_ray *ray, t_vec *objects, const t_object *ignored);
+
 int            parse_args(int argc, char **argv, t_session *s);
 
 int            rotate(t_element *element, t_vec3 rot_axis, double angle);
index 83c4da26efd0697a8f05cebd2f6fe57d7ab54586..5e6266b54e7f0e8342de64a5cca6c9ca416c2ddd 100644 (file)
@@ -85,8 +85,9 @@ t_ray get_camera_ray(int x, int y, const t_session *s)
        t_ray                                   res;
        const float                             x_max = s->img.width;
        const float                             y_max = s->img.height;
-       const t_camera *const   camera = s->scene.current_camera;
+       const t_camera                  *camera;
 
+       camera = &(((t_element *)ft_vec_caccess(&s->scene.cameras, s->scene.current_camera_ind))->object.camera);
        res.start = camera->position;
        res.direction = camera->orientation;
        res.direction =
@@ -139,10 +140,18 @@ int       handle_key_press(int keycode, t_session *s)
        t_element       *element;
        t_camera        *camera;
 
-       element = ft_vec_access(&s->scene.cameras, 0);
-       camera = &element->object.camera;
+       if (s->scene.current_element)
+               element = s->scene.current_element;
+       else
+               element = ft_vec_access(&s->scene.cameras, s->scene.current_camera_ind);
+       camera = &(((t_element *)ft_vec_access(&s->scene.cameras, s->scene.current_camera_ind))->object.camera);
        if (keycode == XK_Escape)
-               close_win(s);
+       {
+               if (s->scene.current_element)
+                       s->scene.current_element = NULL;
+               else
+                       close_win(s);
+       }
        else if (keycode == XK_Up)
                translate(element, camera->orientation, 0.1);
        else if (keycode == XK_Down)
@@ -176,14 +185,17 @@ int       handle_key_press(int keycode, t_session *s)
        return (0);
 }
 
-int    handle_mouse_press(int button, __attribute__((unused)) int x, __attribute__((unused)) int y, t_session *s)
+int    handle_mouse_press(int button, int x, int y, t_session *s)
 {
-       /*
-       if (button == Button4)
-       else if (button == Button5)
-       */
-       if (button == Button4 || button == Button5)
-               draw(s);
+       t_ray   ray;
+
+       if (button == Button1)
+       {
+               ray = get_camera_ray(x, y, s);
+               s->scene.current_element = find_nearest_obstruction(&ray, &s->scene.objects, NULL).object;
+       }
+       else if (button == Button3)
+               s->scene.current_element = NULL;
        return (0);
 }
 
index 9890a1a0a63f6f682a10ab2ecadac9f3f1a343bb..607756e728222e2573f6026cded4a213e7f79ca3 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/11/28 12:34:20 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/12/06 12:18:02 by ljiriste         ###   ########.fr       */
+/*   Updated: 2025/01/07 12:57:50 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -352,7 +352,7 @@ int parse_rt_file(const char *filename, t_scene *scene)
                        if (parse_tree && !parse_tree_to_scene(parse_tree, scene))
                                if (scene->cameras.size > 0)
                                {
-                                       scene->current_camera = &((const t_element *)ft_vec_caccess(&scene->cameras, 0))->object.camera;
+                                       scene->current_camera_ind = 0;
                                        res = 0;
                                }
                        ft_parse_tree_free(parse_tree);
@@ -386,6 +386,7 @@ int parse_args(int argc, char **argv, t_session *s)
        int     i;
        int     got_file;
 
+       s->scene.current_element = NULL;
        got_file = 0;
        if (argc % 2 == 0)
                return (1);
index 6b35944024259cde07e4fdfb478b1abe42f81dd5..6f5c8c72ba74967daf45c4bf573dabd32e545f65 100644 (file)
@@ -271,19 +271,13 @@ double    get_self_obstruction_limit(const t_object *object)
        return (get_circumsphere(object).radius * SELF_OBSTRUCTION_MULTIPLIER);
 }
 
-typedef struct s_obstruction
-{
-       const t_object  *object;
-       double                  distance;
-}                                      t_obstruction;
-
-t_obstruction  find_nearest_obstruction(const t_ray *ray, const t_vec *objects,
+t_obstruction  find_nearest_obstruction(const t_ray *ray, t_vec *objects,
                                        const t_object *ignored)
 {
        size_t                  i;
        size_t                  j;
        t_obstruction   obstruction_found;
-       const t_object  *object;
+       t_object                *object;
        double                  distance;
        double                  ignored_dist;
        t_vec                   distances;
@@ -295,7 +289,7 @@ t_obstruction       find_nearest_obstruction(const t_ray *ray, const t_vec *objects,
        i = 0;
        while (i < objects->size)
        {
-               object = ft_vec_caccess(objects, i++);
+               object = ft_vec_access(objects, i++);
                distances = get_intersection_args(ray, object);
                j = 0;
                while (j < distances.size)
@@ -358,7 +352,7 @@ t_vec3      get_object_normal(const t_object *object, t_vec3 point)
        return ((t_vec3){.x = 0, .y = 0, .z = 0});
 }
 
-t_color        get_light_contribution(t_ray normal, const t_object *object, const t_light *light, const t_scene *scene)
+t_color        get_light_contribution(t_ray normal, const t_object *object, const t_light *light, t_scene *scene)
 {
        t_ray                   new_ray;
        t_obstruction   obstruction;
@@ -385,7 +379,7 @@ t_color     get_light_contribution(t_ray normal, const t_object *object, const t_lig
 }
 
 t_color        get_object_color(const t_ray *ray, const t_object *object,
-                       const t_scene *scene)
+                       t_scene *scene)
 {
        t_color                 result;
        t_ray                   normal_at_intersect;
@@ -411,7 +405,7 @@ t_color     get_object_color(const t_ray *ray, const t_object *object,
        return (result);
 }
 
-t_color        trace_ray(const t_ray *ray, const t_scene *scene)
+t_color        trace_ray(const t_ray *ray, t_scene *scene)
 {
        const t_object  *object_found;