From a9b3993461166af988d9de46be09597edbb1923a Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 7 Jan 2025 15:36:22 +0100 Subject: [PATCH] Implement clicking on an object to select 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 | 14 ++++++++++++-- src/main.c | 34 +++++++++++++++++++++++----------- src/parsing.c | 5 +++-- src/scene.c | 18 ++++++------------ 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/inc/miniRT.h b/inc/miniRT.h index fd75439..bbe9089 100644 --- a/inc/miniRT.h +++ b/inc/miniRT.h @@ -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); diff --git a/src/main.c b/src/main.c index 83c4da2..5e6266b 100644 --- a/src/main.c +++ b/src/main.c @@ -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); } diff --git a/src/parsing.c b/src/parsing.c index 9890a1a..607756e 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: 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); diff --git a/src/scene.c b/src/scene.c index 6b35944..6f5c8c7 100644 --- a/src/scene.c +++ b/src/scene.c @@ -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; -- 2.30.2