Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/13 move objects #44

Merged
merged 16 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[#13][feat] coordinate system of selected object
  • Loading branch information
davfront committed Jul 10, 2023
commit f70b776aefb87c809826f1b336d3767dc25fc767
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: dapereir <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/12/14 16:34:41 by dapereir #+# #+# #
# Updated: 2023/07/05 13:34:01 by dapereir ### ########.fr #
# Updated: 2023/07/10 06:45:07 by dapereir ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -81,6 +81,7 @@ SRCS_FILES = \
px_draw/rt_draw_line.c\
px_draw/rt_draw_disc.c\
px_draw/rt_draw_rect.c\
px_draw/rt_draw_cs.c\
\
utils/rt_init.c\
utils/rt_delete.c\
Expand Down Expand Up @@ -144,10 +145,13 @@ SRCS_FILES = \
camera/rt_cam_update_fov.c\
camera/rt_cam_update_c2w.c\
camera/rt_cam_camera_space_to_ndc.c\
camera/rt_cam_world_to_screen.c\
camera/rt_cam_c2s.c\
camera/rt_cam_w2s.c\
\
ui/rt_ui_help_bg.c\
ui/rt_ui_frame_progress.c\
ui/rt_ui_bounding_box.c\
ui/rt_ui_selected.c\
\
main.c\

Expand Down
36 changes: 34 additions & 2 deletions include/minirt.h
davfront marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/14 16:35:14 by dapereir #+# #+# */
/* Updated: 2023/07/05 13:36:42 by dapereir ### ########.fr */
/* Updated: 2023/07/10 06:52:10 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -49,10 +49,38 @@

# define BLACK (0x00000000)
# define WHITE (0x00FFFFFF)
# define GRAY (0x00999999)
# define RED (0x00FF0000)
# define GREEN (0x0000FF00)
# define BLUE (0x000000FF)

# define CS_POINT_RAD (8)
# define CS_MOV_LEN (100)
# define CS_ROT_LEN (80)
# define CS_RED (0x00eb6663)
# define CS_GREEN (0x0071deb6)
# define CS_BLUE (0x004b98f7)

enum e_cs_helper_type {
CS_ORIG,
CS_MOV_X,
CS_MOV_Y,
CS_MOV_Z,
CS_ROT_X,
CS_ROT_Y,
CS_ROT_Z,
CS_SIZE
};

typedef struct s_cs {
t_vec3 center;
t_vec3 dx;
t_vec3 dy;
t_vec3 dz;
t_px helper_px[CS_SIZE];
int ordered_helper_types[CS_SIZE];
} t_cs;

typedef struct s_hit {
t_obj *obj;
t_rgb color;
Expand Down Expand Up @@ -211,17 +239,21 @@ t_vec3 rt_cam_ndc_to_camera_space(t_cam cam, t_vec3 ndc);
t_vec3 rt_cam_camera_space_to_ndc(t_cam cam, t_vec3 v);
void rt_cam_update_fov(t_data *data, int delta_fov);
void rt_cam_update_c2w(t_data *data, t_mat4 c2w);
t_px rt_cam_world_to_screen(t_data *data, t_vec3 v);
t_px rt_cam_c2s(t_data *data, t_vec3 v);
t_px rt_cam_w2s(t_data *data, t_vec3 v);

// px_draw
void rt_draw_px(t_data *data, t_px p, t_rgb color, float alpha);
void rt_draw_line(t_data *data, t_line, t_rgb color);
void rt_draw_line_px(t_data *data, t_line line, t_rgb color, float alpha);
void rt_draw_disc(t_data *data, t_disc, t_rgb color, float alpha);
void rt_draw_rect(t_data *data, t_rect, t_rgb color, float alpha);
void rt_draw_cs(t_data *data, t_cs cs);

// ui
void rt_ui_help_bg(t_data *data);
void rt_ui_frame_progress(t_data *data);
void rt_ui_bounding_box(t_data *data, t_obj *obj);
void rt_ui_selected(t_data *data);

#endif
3 changes: 2 additions & 1 deletion include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/09 16:24:08 by atchougo #+# #+# */
/* Updated: 2023/07/04 10:00:08 by dapereir ### ########.fr */
/* Updated: 2023/07/05 14:54:57 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -66,6 +66,7 @@ typedef enum e_obj_type {

typedef struct s_obj {
t_obj_type type;
t_mat4 mt;
union {
t_sphere sphere;
t_plane plane;
Expand Down
8 changes: 4 additions & 4 deletions libft/include/libft_printf.h
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je pense pas que quelqu'un le verra mais si je ne me trompe pas le header guard doit avoir le nom du fichier

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je crée un ticket : #50

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/* ::: :::::::: */
/* libft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dapereir <dapereir@student.42.fr> +#+ +:+ +#+ */
/* By: dapereir <dapereir@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/22 09:13:46 by dapereir #+# #+# */
/* Updated: 2022/12/22 11:10:59 by dapereir ### ########.fr */
/* Updated: 2023/07/07 15:07:56 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef LIBFT_PRINTF_H
# define LIBFT_PRINTF_H
#ifndef LIBFT_PRINCS_H
# define LIBFT_PRINCS_H

# include <stdarg.h>
# include <stdlib.h>
Expand Down
28 changes: 28 additions & 0 deletions src/camera/rt_cam_c2s.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rt_cam_c2s.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/23 15:32:06 by dapereir #+# #+# */
/* Updated: 2023/07/10 06:43:33 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

#include "minirt.h"

t_px rt_cam_c2s(t_data *data, t_vec3 v)
{
t_cam cam;
t_px p;
t_vec3 ndc;

if (!data || !data->cam)
return (px(-1, -1));
cam = *data->cam;
ndc = rt_cam_camera_space_to_ndc(cam, v);
p.x = (ndc.x + 1.0) / 2 * WIN_WIDTH - 0.5;
p.y = (1.0 - ndc.y) / 2 * WIN_HEIGHT - 0.5;
return (p);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rt_cam_world_to_screen.c :+: :+: :+: */
/* rt_cam_w2s.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/23 15:32:06 by dapereir #+# #+# */
/* Updated: 2023/07/04 16:05:38 by dapereir ### ########.fr */
/* Updated: 2023/07/10 06:43:23 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

#include "minirt.h"

t_px rt_cam_world_to_screen(t_data *data, t_vec3 v)
t_px rt_cam_w2s(t_data *data, t_vec3 v)
{
t_cam cam;
t_px p;
Expand Down
31 changes: 30 additions & 1 deletion src/parse/rt_parse_obj_plane.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,40 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 11:30:58 by dapereir #+# #+# */
/* Updated: 2023/06/15 00:03:31 by dapereir ### ########.fr */
/* Updated: 2023/07/10 10:29:27 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

#include "minirt.h"

static t_mat4 rt_get_plane_transformation_matrix(t_plane pl)
{
t_vec3 up;
t_vec3 forward;
t_vec3 right;
t_vec3 top;
t_mat4 mt;

up = vec3(0, 1, 0);
top = pl.normal;
right = vec3(1, 0, 0);
if (vec3_length_squared(vec3_cross(up, top)) != 0)
right = vec3_normalize(vec3_cross(up, top));
forward = vec3_normalize(vec3_cross(right, top));
mt = mat4_identity();
mt.m[0][0] = right.x;
mt.m[1][0] = right.y;
mt.m[2][0] = right.z;
mt.m[0][1] = top.x;
mt.m[1][1] = top.y;
mt.m[2][1] = top.z;
mt.m[0][2] = forward.x;
mt.m[1][2] = forward.y;
mt.m[2][2] = forward.z;
mt = mat4_translate(mt, pl.point);
return (mt);
}

void rt_parse_obj_plane(t_data *data, char **strs)
{
t_obj obj;
Expand All @@ -29,6 +57,7 @@ void rt_parse_obj_plane(t_data *data, char **strs)
rt_parse_value_error_exit(data, "plane", "normal", strs[1]);
if (!rt_parse_rgb(strs[2], &obj.plane.color))
rt_parse_value_error_exit(data, "plane", "color", strs[2]);
obj.mt = rt_get_plane_transformation_matrix(obj.plane);
content = ft_calloc(1, sizeof(t_obj));
if (!content)
rt_parse_line_error_exit(data, "plane: t_obj alloc failed");
Expand Down
13 changes: 12 additions & 1 deletion src/parse/rt_parse_obj_sphere.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 11:30:58 by dapereir #+# #+# */
/* Updated: 2023/06/15 00:03:33 by dapereir ### ########.fr */
/* Updated: 2023/07/07 22:59:54 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

#include "minirt.h"

static t_mat4 rt_get_sphere_transformation_matrix(t_sphere sp)
{
t_mat4 mt;

mt = mat4_identity();
mt = mat4_scale(mt, sp.radius);
mt = mat4_translate(mt, sp.center);
return (mt);
}

void rt_parse_obj_sphere(t_data *data, char **strs)
{
t_obj obj;
Expand All @@ -30,6 +40,7 @@ void rt_parse_obj_sphere(t_data *data, char **strs)
obj.sphere.radius /= 2;
if (!rt_parse_rgb(strs[2], &obj.sphere.color))
rt_parse_value_error_exit(data, "sphere", "color", strs[2]);
obj.mt = rt_get_sphere_transformation_matrix(obj.sphere);
content = ft_calloc(1, sizeof(t_obj));
if (!content)
rt_parse_line_error_exit(data, "sphere: t_obj alloc failed");
Expand Down
117 changes: 117 additions & 0 deletions src/px_draw/rt_draw_cs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rt_draw_cs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/06 12:59:49 by dapereir #+# #+# */
/* Updated: 2023/07/10 06:53:47 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

#include "minirt.h"

static t_rgb rt_cs_helper_color(int cs)
{
if (cs == CS_MOV_X || cs == CS_ROT_X)
return (rgb_int(CS_RED));
if (cs == CS_MOV_Y || cs == CS_ROT_Y)
return (rgb_int(CS_BLUE));
if (cs == CS_MOV_Z || cs == CS_ROT_Z)
return (rgb_int(CS_GREEN));
return (rgb_int(GRAY));
}

static void rt_draw_cs_mov(t_data *data, t_cs cs, int helper_id, t_rgb color)
{
t_px p1;
t_px p2;
t_vec3 dir;

if (helper_id == CS_MOV_X)
dir = cs.dx;
else if (helper_id == CS_MOV_Y)
dir = cs.dy;
else
dir = cs.dz;
p1 = rt_cam_c2s(data, vec3_add(cs.center, vec3_scale(dir, CS_POINT_RAD)));
p2 = cs.helper_px[helper_id];
rt_draw_line(data, line(p1, p2, 2), color);
rt_draw_disc(data, disc(p2, CS_POINT_RAD), color, 1);
}

static void rt_draw_cs_rot_arc(t_data *data, t_cs cs, t_vec3 arc_dir[2], \
t_rgb color)
{
t_line line;
int i;
t_float angle;
t_vec3 dir;

line.thickness = 2;
i = 0;
while (i < 8)
{
angle = M_PI / 2 / 8;
dir = vec3_add(vec3_scale(arc_dir[0], cos(angle * i)), \
vec3_scale(arc_dir[1], sin(angle * i)));
line.p1 = rt_cam_c2s(data, \
vec3_add(cs.center, vec3_scale(dir, CS_ROT_LEN)));
dir = vec3_add(vec3_scale(arc_dir[0], cos(angle * (i + 1))), \
vec3_scale(arc_dir[1], sin(angle * (i + 1))));
line.p2 = rt_cam_c2s(data, \
vec3_add(cs.center, vec3_scale(dir, CS_ROT_LEN)));
rt_draw_line(data, line, color);
i++;
}
}

static void rt_draw_cs_rot(t_data *data, t_cs cs, int helper_id, t_rgb color)
{
t_vec3 arc_dir[2];

rt_draw_disc(data, disc(cs.helper_px[helper_id], CS_POINT_RAD), color, 1);
if (helper_id == CS_ROT_X)
{
arc_dir[0] = cs.dz;
arc_dir[1] = cs.dy;
}
else if (helper_id == CS_ROT_Y)
{
arc_dir[0] = cs.dx;
arc_dir[1] = cs.dz;
}
else
{
arc_dir[0] = cs.dy;
arc_dir[1] = cs.dx;
}
rt_draw_cs_rot_arc(data, cs, arc_dir, color);
}

void rt_draw_cs(t_data *data, t_cs cs)
{
int i;
int helper_id;
t_rgb color;

i = 0;
while (i < CS_SIZE)
{
helper_id = cs.ordered_helper_types[i];
color = rt_cs_helper_color(helper_id);
if (helper_id == CS_ORIG)
rt_draw_disc(data, \
disc(cs.helper_px[CS_ORIG], CS_POINT_RAD), color, 1);
if (helper_id == CS_MOV_X \
|| helper_id == CS_MOV_Y \
|| helper_id == CS_MOV_Z)
rt_draw_cs_mov(data, cs, helper_id, color);
if (helper_id == CS_ROT_X \
|| helper_id == CS_ROT_Y \
|| helper_id == CS_ROT_Z)
rt_draw_cs_rot(data, cs, helper_id, color);
i++;
}
}
Loading