Skip to content

Commit dcefc9a

Browse files
🧹 cleanup: Merge functions
2 parents 71e439f + 5bda0fd commit dcefc9a

File tree

11 files changed

+72
-64
lines changed

11 files changed

+72
-64
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ SRCS = $(addprefix $(SRCS_PATH), \
113113
09-Patterns/Ring.c \
114114
09-Patterns/Stripes.c \
115115
11-Parser/Parse_ambient.c \
116+
11-Parser/Parse_camera.c \
116117
11-Parser/Parse_color.c \
117118
11-Parser/Parse_light.c \
118119
11-Parser/Parse_position.c \
@@ -122,7 +123,6 @@ SRCS = $(addprefix $(SRCS_PATH), \
122123
11-Parser/Validations.c \
123124
main.c \
124125
)
125-
# 11-Parser/Parse_camera.c # ! TEM Q VOLTAR LÁ PRA CIMA
126126
OBJS = $(SRCS:%$(FILE_EXTENSION)=$(BUILD_DIR)%.o)
127127
DEPS = $(OBJS:.o=.d)
128128
LIBFT = $(addprefix $(LIBFT_DIR), libft.a)

includes/Canvas.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: cnatanae <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/11 14:43:44 by cnatanae #+# #+# */
9-
/* Updated: 2025/02/05 09:52:05 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/05 09:59:29 by cnatanae ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

includes/Codam.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: cnatanae <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/07 14:28:57 by cnatanae #+# #+# */
9-
/* Updated: 2025/02/04 12:46:31 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/05 09:59:35 by cnatanae ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

includes/Parser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* Parser.h :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: cnatanae <cnatanae@student.42sp.org.br> +#+ +:+ +#+ */
6+
/* By: tmalheir <tmalheir@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/22 14:57:54 by tmalheir #+# #+# */
9-
/* Updated: 2025/02/03 12:07:18 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/04 15:09:18 by tmalheir ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -18,7 +18,7 @@
1818
# include "Count_el.h"
1919
# include <stdbool.h>
2020

21-
21+
#define PI 3.14159265358979323846
2222

2323
t_world *parser(int argc, char **argv);
2424

@@ -37,7 +37,7 @@ void ft_free_array(char **str);
3737
void get_line(int fd, t_world *world);
3838
bool parse_line(char *line, t_world *world);
3939
bool parse_ambient(char *line, t_world *world);
40-
// bool parse_camera(char *line, t_world *world); // !Comentado por enquanto
40+
bool parse_camera(char *line, t_world *world);
4141
bool parse_light(char *line, t_world *world);
4242
bool parse_sphere(char *line, t_world *world);
4343
bool parse_plane(char *line, t_world *world);

includes/Scenes.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* Scenes.h :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: cnatanae <cnatanae@student.42sp.org.br> +#+ +:+ +#+ */
6+
/* By: tmalheir <tmalheir@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/02 08:15:09 by cnatanae #+# #+# */
9-
/* Updated: 2025/02/03 11:17:03 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/04 15:13:00 by tmalheir ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -47,22 +47,15 @@ typedef struct s_scene
4747
{
4848
t_colors ambient;
4949
int has_ambient;
50-
double *camera_pos;
51-
double *camera_dir;
50+
double *camera_from;
51+
double *camera_to;
5252
int has_camera;
5353
t_colors light;
5454
double *light_pos;
5555
int has_light;
5656
bool save_img;
5757
} t_scene;
5858

59-
typedef struct s_world
60-
{
61-
t_obj *obj_lst;
62-
t_lights *lights_lst;
63-
t_scene scene;
64-
} t_world;
65-
6659
typedef struct s_camera
6760
{
6861
double hsize;
@@ -71,9 +64,18 @@ typedef struct s_camera
7164
double pixel_sz;
7265
double half_width;
7366
double half_heigth;
67+
double *up;
7468
t_matrix transform;
7569
} t_camera;
7670

71+
typedef struct s_world
72+
{
73+
t_obj *obj_lst;
74+
t_lights *lights_lst;
75+
t_scene scene;
76+
t_camera camera;
77+
} t_world;
78+
7779
t_world *world(void);
7880

7981
void create_obj_lst(t_world *world, t_shape_id id);

src/07-Scenes/Camera.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* Camera.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: cnatanae <cnatanae@student.42sp.org.br> +#+ +:+ +#+ */
6+
/* By: tmalheir <tmalheir@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/07 08:51:05 by cnatanae #+# #+# */
9-
/* Updated: 2025/01/07 10:45:24 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/04 14:47:36 by tmalheir ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -57,6 +57,7 @@ t_camera camera(int hsize, int vsize, double field_view)
5757
cam.hsize = hsize;
5858
cam.vsize = vsize;
5959
cam.field_view = field_view;
60+
cam.up = vector(0, 1, 0);
6061
cam.transform = id_mtx();
6162
calculate_pixel(&cam);
6263
return (cam);

src/08-Canvas/Codam.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: cnatanae <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/07 14:30:39 by cnatanae #+# #+# */
9-
/* Updated: 2025/02/04 12:46:40 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/05 09:59:54 by cnatanae ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -37,6 +37,13 @@
3737
// canvas->image = mlx_new_image(canvas->mlx, WIDTH, HEIGHT);
3838
// mlx_image_to_window(canvas->mlx, canvas->image, 0, 0);
3939
// }
40+
// void init_window(t_canvas *canvas)
41+
// {
42+
// mlx_set_setting(MLX_STRETCH_IMAGE, true);
43+
// canvas->mlx = mlx_init(WIDTH, HEIGHT, "MiniRT", true);
44+
// canvas->image = mlx_new_image(canvas->mlx, WIDTH, HEIGHT);
45+
// mlx_image_to_window(canvas->mlx, canvas->image, 0, 0);
46+
// }
4047

4148
/**
4249
* @brief Converts canvas data to a usable MLX image

src/11-Parser/Parse_ambient.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* Parse_ambient.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: cnatanae <cnatanae@student.42sp.org.br> +#+ +:+ +#+ */
6+
/* By: tmalheir <tmalheir@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/29 10:36:02 by tmalheir #+# #+# */
9-
/* Updated: 2025/02/03 09:56:39 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/04 15:18:29 by tmalheir ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -17,6 +17,9 @@ bool parse_ambient(char *line, t_world *world)
1717
double *norm_col;
1818
char **info;
1919

20+
world->scene.has_ambient += 1;
21+
if (world->scene.has_ambient > 1)
22+
error("Error\n", "Only one ambient light allowed", "", 1);
2023
info = ft_split(line, ' ');
2124
if (!check_count(info, 3) || !is_double(info[1])
2225
|| !in_range_double(info[1]) || !parse_color(info[2]))
@@ -25,11 +28,5 @@ bool parse_ambient(char *line, t_world *world)
2528
norm_col = normalize_rgb_to_double(info[2]);
2629
world->scene.ambient = multiply_col(create_color
2730
(norm_col[0], norm_col[1], norm_col[2]), ft_atod(info[1]));
28-
world->scene.has_ambient += 1;
29-
if (world->scene.has_ambient > 1)
30-
{
31-
warning("Error\n", "Only one ambient light allowed", "");
32-
return (true_or_false(info, false));
33-
}
3431
return (true_or_false(info, true));
3532
}

src/11-Parser/Parse_camera.c

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
/* ::: :::::::: */
44
/* Parse_camera.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: cnatanae <cnatanae@student.42sp.org.br> +#+ +:+ +#+ */
6+
/* By: tmalheir <tmalheir@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/31 10:06:00 by tmalheir #+# #+# */
9-
/* Updated: 2025/02/03 12:00:19 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/04 15:19:24 by tmalheir ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "Parser.h"
14+
#include "Canvas.h"
15+
#include "Scenes.h"
16+
#include "math.h"
1417

15-
// static bool dir_is_norm(double *dir)
16-
// {
17-
// if (equal(mag(dir)))
18-
19-
// }
18+
static bool check_fov(char *str)
19+
{
20+
if (!is_double(str) || (ft_atod(str) < 0 || ft_atod(str) > 180))
21+
return (false);
22+
return (true);
23+
}
2024

2125
static bool dir_in_range(char *str)
2226
{
@@ -48,24 +52,24 @@ static bool parse_dir(char *str)
4852

4953
bool parse_camera(char *line, t_world *world)
5054
{
51-
double *view_pt;
52-
double *dir;
53-
char **info;
55+
double *from;
56+
double *to;
57+
char **info;
5458

55-
info = ft_split(line, ' ');
56-
if (!check_count(info, 4) || !parse_pos(info[1]) || !parse_dir(info[2]));
57-
return (true_or_false(info, false));
58-
view_pt = allocate(sizeof(double) * 3);
59-
view_pt = pos_to_double(info[1]);
60-
world->scene.camera_pos = point(view_pt[X], view_pt[Y], view_pt[Z]);
61-
world->scene.has_camera += world->scene.has_camera + 1;
59+
world->scene.has_camera += 1;
6260
if (world->scene.has_camera > 1)
63-
{
64-
warning("Error\n", "Only one camera allowed", "");
61+
error("Error\n", "Only one camera allowed", "", 1);
62+
info = ft_split(line, ' ');
63+
if (!check_count(info, 4) || !parse_pos(info[1]) || !parse_dir(info[2])
64+
|| !check_fov(info[3]))
6565
return (true_or_false(info, false));
66-
}
67-
dir = allocate(sizeof(double) * 3);
68-
dir = pos_to_double(info[2]);
69-
world->scene.camera_dir = vector(dir[X], dir[Y], dir[Z]);
66+
from = allocate(sizeof(double) * 3);
67+
from = pos_to_double(info[1]);
68+
world->scene.camera_from = point(from[X], from[Y], from[Z]);
69+
to = allocate(sizeof(double) * 3);
70+
to = pos_to_double(info[2]);
71+
world->scene.camera_to = vector(to[X], to[Y], to[Z]);
72+
world->camera = camera(WIDHT, HEIGHT, (ft_atod(info[3])) * PI / 180);
73+
world->camera.transform = view_transform(from, to, world->camera.up);
7074
return (true_or_false(info, true));
7175
}

src/11-Parser/Parse_light.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* Parse_light.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: cnatanae <cnatanae@student.42sp.org.br> +#+ +:+ +#+ */
6+
/* By: tmalheir <tmalheir@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/29 10:36:02 by tmalheir #+# #+# */
9-
/* Updated: 2025/02/03 09:56:20 by cnatanae ### ########.fr */
9+
/* Updated: 2025/02/04 15:18:04 by tmalheir ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -18,6 +18,9 @@ bool parse_light(char *line, t_world *world)
1818
double *pos;
1919
char **info;
2020

21+
world->scene.has_light += 1;
22+
if (world->scene.has_light > 1)
23+
error("Error\n", "Only one light allowed", "", 1);
2124
info = ft_split(line, ' ');
2225
if (!check_count(info, 4) || !parse_pos(info[1]) || !is_double(info[2])
2326
|| !in_range_double(info[2]) || !parse_color(info[3]))
@@ -31,11 +34,5 @@ bool parse_light(char *line, t_world *world)
3134
world->scene.light_pos[X] = pos[X];
3235
world->scene.light_pos[Y] = pos[Y];
3336
world->scene.light_pos[Z] = pos[Z];
34-
world->scene.has_light += 1;
35-
if (world->scene.has_light > 1)
36-
{
37-
warning("Error\n", "Only one light allowed", "");
38-
return (true_or_false(info, false));
39-
}
4037
return (true_or_false(info, true));
4138
}

0 commit comments

Comments
 (0)