From ee68d877e694161c22f53161faabe35ae39b4443 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Wed, 8 Jan 2025 13:17:25 +0100 Subject: [PATCH] Add switch between absolute and relative axes --- inc/miniRT.h | 1 + src/main.c | 51 ++++++++++++++++++++++++++++++++++----------------- src/parsing.c | 3 ++- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/inc/miniRT.h b/inc/miniRT.h index bbe9089..bb81993 100644 --- a/inc/miniRT.h +++ b/inc/miniRT.h @@ -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 diff --git a/src/main.c b/src/main.c index 1443317..f84f593 100644 --- a/src/main.c +++ b/src/main.c @@ -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); } diff --git a/src/parsing.c b/src/parsing.c index 607756e..bde935a 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); -- 2.30.2