Add switch between absolute and relative axes
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 8 Jan 2025 12:17:25 +0000 (13:17 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 8 Jan 2025 12:29:05 +0000 (13:29 +0100)
inc/miniRT.h
src/main.c
src/parsing.c

index bbe9089b387e511563b4cc561074425cc5994884..bb81993efaa28b11ad94d7d8b5963670343679cb 100644 (file)
@@ -100,6 +100,7 @@ typedef struct s_scene
        t_vec                   cameras;
        size_t                  current_camera_ind;
        t_element               *current_element;
+       int                             relative_directions;
 }                                      t_scene;
 
 typedef struct s_ray
index 14433179a34311d4257262110ce7dbc9e28df7f7..f84f59386180be4ae3fc94a7f9fefc77951c6fa3 100644 (file)
@@ -138,16 +138,35 @@ int       close_win(t_session *s)
 static const double    TRANSLATION_STEP = 0.1;
 static const double    ROTATION_STEP = 0.1;
 
+static const t_vec3    X_VEC = {.x = 1, .y = 0, .z = 0};
+static const t_vec3    Y_VEC = {.x = 0, .y = 1, .z = 0};
+static const t_vec3    Z_VEC = {.x = 0, .y = 0, .z = 1};
+
 int    handle_key_press(int keycode, t_session *s)
 {
        t_element       *element;
        t_camera        *camera;
+       t_vec3          forwards;
+       t_vec3          upwards;
+       t_vec3          sidewards;
 
        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 (s->scene.relative_directions)
+       {
+               forwards = camera->orientation;
+               upwards = camera->up_direction;
+               sidewards = vec_vec_mul(forwards, upwards);
+       }
+       else
+       {
+               forwards = X_VEC;
+               upwards = Z_VEC;
+               sidewards = Y_VEC;
+       }
        if (keycode == XK_Escape)
        {
                if (s->scene.current_element)
@@ -156,34 +175,32 @@ int       handle_key_press(int keycode, t_session *s)
                        close_win(s);
        }
        else if (keycode == XK_Up)
-               translate(element, camera->orientation, TRANSLATION_STEP);
+               translate(element, forwards, TRANSLATION_STEP);
        else if (keycode == XK_Down)
-               translate(element, camera->orientation, -TRANSLATION_STEP);
+               translate(element, forwards, -TRANSLATION_STEP);
        else if (keycode == XK_Left)
-               translate(element, vec_vec_mul(camera->orientation, camera->up_direction), TRANSLATION_STEP);
+               translate(element, sidewards, TRANSLATION_STEP);
        else if (keycode == XK_Right)
-               translate(element, vec_vec_mul(camera->orientation, camera->up_direction), -TRANSLATION_STEP);
+               translate(element, sidewards, -TRANSLATION_STEP);
        else if (keycode == XK_space)
-               translate(element, camera->up_direction, TRANSLATION_STEP);
+               translate(element, upwards, TRANSLATION_STEP);
        else if (keycode == XK_Shift_L)
-               translate(element, camera->up_direction, -TRANSLATION_STEP);
+               translate(element, upwards, -TRANSLATION_STEP);
        else if (keycode == XK_w)
-               rotate(element, vec_vec_mul(camera->orientation, camera->up_direction), -ROTATION_STEP);
+               rotate(element, sidewards, -ROTATION_STEP);
        else if (keycode == XK_s)
-               rotate(element, vec_vec_mul(camera->orientation, camera->up_direction), ROTATION_STEP);
+               rotate(element, sidewards, ROTATION_STEP);
        else if (keycode == XK_a)
-               rotate(element, camera->up_direction, -ROTATION_STEP);
+               rotate(element, upwards, -ROTATION_STEP);
        else if (keycode == XK_d)
-               rotate(element, camera->up_direction, ROTATION_STEP);
+               rotate(element, upwards, ROTATION_STEP);
        else if (keycode == XK_e)
-               rotate(element, camera->orientation, -ROTATION_STEP);
+               rotate(element, forwards, -ROTATION_STEP);
        else if (keycode == XK_q)
-               rotate(element, camera->orientation, ROTATION_STEP);
-       /*
-       else if (keycode == XK_KP_Add)
-       else if (keycode == XK_KP_Subtract)
-       */
-       if (keycode != XK_Escape)
+               rotate(element, forwards, ROTATION_STEP);
+       else if (keycode == XK_o)
+               s->scene.relative_directions = !s->scene.relative_directions;
+       if (keycode != XK_Escape && keycode != XK_A)
                draw(s);
        return (0);
 }
index 607756e728222e2573f6026cded4a213e7f79ca3..bde935a2b6c4ff336214bb3cd32ca9070931e3e9 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/11/28 12:34:20 by ljiriste          #+#    #+#             */
-/*   Updated: 2025/01/07 12:57:50 by ljiriste         ###   ########.fr       */
+/*   Updated: 2025/01/08 13:07:58 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -387,6 +387,7 @@ int parse_args(int argc, char **argv, t_session *s)
        int     got_file;
 
        s->scene.current_element = NULL;
+       s->scene.relative_directions = 1;
        got_file = 0;
        if (argc % 2 == 0)
                return (1);