Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ set(CMAKE_C_STANDARD 99)
add_executable(miniRT main.c
include/minirt.h
src/window.c src/parser.c src/init.c src/exit.c src/scene.c
src/object.c src/vec/t_vec_new.c src/vec/t_vec_ops.c src/vec/t_vec_products.c)
src/object.c src/vec/t_vec_new.c src/vec/t_vec_ops.c src/vec/t_vec_products.c
src/vec/ray.c
src/colors.c
src/shader.c)

add_custom_target(makebuild COMMAND make -C ${mytest_SOURCE_DIR}
CLION_EXE_DIR=${PROJECT_BINARY_DIR})
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ NAME = miniRT
all: $(NAME)

$(NAME): $(OBJ)
@cd mlx && make
@make bonus -C $(LIBFTDIR)
@cp $(LIBFTDIR)/$(LIBFT) .
$(CC) $(CPPFLAGS) $(OBJ) $(LIBFT) mlx/libmlx.a -lXext -lX11 -o $(NAME) -lm
Expand All @@ -28,6 +29,7 @@ clean:

fclean: clean
$(RM) $(NAME)
@cd mlx && make clean

re: fclean $(NAME)

Expand Down
2 changes: 1 addition & 1 deletion ados.rt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
A 0.3 255,255,255
C 0,0,40 0,0,1 85
C 0,0,40 0,0,-1 85
L 0,0,20 0.8 255,255,255

pl 0,-10,0 0,1,0 0,0,150
Expand Down
8 changes: 4 additions & 4 deletions basic.rt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
A .5 125,125,125
A .8 255,255,225
C 0,0,1 0,0,1 70
L -10,0,1 .5 255,255,255
sp 0.0,0.0,20.6 12.6 10,0,255
pl 0.0,0.0,-10.0 0.0,1.0,0.0 0,0,225
L -1,0,1 .8 255,255,255
sp 0.0,0.0,20.6 12.6 10,0,255
pl 0.0,0.0,53.0 0.0,0.0,-1.0 25,125,25
6 changes: 4 additions & 2 deletions box.rt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
A 0.2 255,255,255
A 0.5 255,255,255
C 0,0,5 0,0,-1 60
L -1,2.5,2 0.7 255,255,255
L -1,2.5,5 0.7 255,255,255

pl 0,-1,0 0,1,0 100,100,100

Expand All @@ -13,3 +13,5 @@ pl -3,0,0 1,0,0 255,0,0
pl 3,0,0 -1,0,0 0,0,255

sp 0,0,0 1.0 0,255,0

cy 0,0,0 0,1,0 .5 2 140,118,206
8 changes: 5 additions & 3 deletions cylindre.rt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
A .5 125,125,125
L 40,-75,100 .8 255,255,255
C 1,-10,8 0,0.3,-1 60
cy 0,-3,-1 0,1,0 4 8 140,118,206
L 0,0,305 .5 255,255,255
C 0,0,12 0,0,-1 60
#cy 0,0,0 0,1,0 2 8 140,118,206
# cy 0,0,0 0,1,0 2 8 140,118,206
cy 0,0,0 0,1,0 2 8 140,118,206
106 changes: 74 additions & 32 deletions include/minirt.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: aevstign <aevstign@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/09 14:39:27 by voparkan #+# #+# */
/* Updated: 2025/05/21 16:07:04 by aevstign ### ########.fr */
/* Updated: 2025/06/07 22:02:51 by voparkan ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -154,44 +154,77 @@ typedef struct s_spbag
t_vec oc;
} t_spbag;

typedef struct s_cybag
{
int i;
float t1;
float t2;
float radius;
float a_f;
float b_f;
float c_f;
float discriminant;
float sqrt_disc;
float t_candidates[2];
float height;
float ti;
float denom;
float t_cap;
float crb;
t_vec sub;
t_vec oc;
t_vec nor_cyl;
t_vec a;
t_vec b;
t_vec p;
t_vec p_b;
t_vec cap_center;
t_vec mul;
t_vec lp;
} t_cybag;

typedef struct s_hit_record
{
float t;
t_vec point;
t_vec normal;
t_shapes *object;
bool hit;
bool is_cap;
} t_hit_record;

typedef struct s_cybag
{
int i;
float t1;
float t2;
float radius;
float a_f;
float b_f;
float c_f;
float discriminant;
float sqrt_disc;
float t_candidates[2];
float height;
float ti;
float denom;
float t_cap;
float t_body;
float crb;
float sign;
float half_h;
t_vec sub;
t_vec oc;
t_vec nor_cyl;
t_vec a;
t_vec b;
t_vec p;
t_vec p_b;
t_vec cap_center;
t_vec mul;
t_vec lp;
t_hit_record rec_body;
t_hit_record rec_cap;
t_vec to_point;
t_vec projection;
} t_cybag;

typedef struct s_cap_bag
{
t_vec cap_center;
t_vec dist;
t_vec lp;
t_vec cap_normal;
t_vec sub;
float t_cap;
float half_h;
float denom;
int i;
float r2;
float bias;
float sign;
} t_cap_bag;

typedef struct s_cap_hit_ctx
{
t_cap_bag *cb;
t_cybag *b;
t_shapes *shp;
t_ray ray;
t_hit_record *hit;
} t_cap_hit_ctx;

void do_j_bzero(t_obag *ob);
void init_tobag(t_obag *obag);

Expand Down Expand Up @@ -252,19 +285,20 @@ void iter_pl(t_data *data, char *tmp, t_shapes *pl_shape);
void iter_sp(t_data *data, char *tmp, t_shapes *sp_shape);
void iter_cy(t_data *data, char *tmp, t_shapes *cy_shape);
void move_cp_buf(char *tmp, t_obag *ob);
float process_cy_cap(t_cybag b, t_shapes *shp, t_ray ray);
float process_cy_cap(t_cybag b, t_shapes *shp, t_ray ray, t_hit_record *rec);
float process_cy_body(t_cybag b, t_shapes *shp, t_ray ray);

/*rays*/
t_ray shoot_ray(int x, int y, t_data *data);
bool ray_inter_sp(t_ray ray, t_shapes *shp, float *t);
bool ray_inter_pl(t_ray ray, t_shapes *shp, float *t);
bool ray_inter_cy(t_ray ray, t_shapes *shp, float *t);
bool ray_inter_cy(t_ray ray, t_shapes *shp, float *t, t_hit_record *rec);
void cast_rays(t_data *data);

/*hit*/
bool hit_objects(t_data *data, t_ray ray, t_hit_record *rec);
bool is_in_shadow(t_data *data, t_vec point, t_vec light_pos, t_vec normal);
void resolve_hit(t_hit_record *rec, float t, t_ray ray, t_shapes *shp);

/*colors*/
int make_color(t_rgb rgb);
Expand All @@ -273,6 +307,8 @@ t_rgb shader(t_rgb color, t_data *data, t_hit_record *rec);
t_rgb calculate_diffuse(t_data *data, t_vec dir, t_rgb color,
t_hit_record *rec);
t_rgb calculate_ambient(t_data *data, t_shapes *shp);
t_sbag colors_in_shadow(t_rgb *color, t_data *data, t_hit_record *rec,
t_sbag *sb);

/*validator*/
bool check_line(char *line, t_obag *bag);
Expand All @@ -289,4 +325,10 @@ bool validate_cylindr(char **tokens, const int count);
bool validate_rgb(const char *token);
bool validate_orientation(const char *tokens);

void remove_comment(char *line);

/*cy_utils*/
void calculate_cy_bag(t_cybag *b, t_shapes *shp, t_ray ray);
void handle_cy_body(t_cybag *b, t_shapes *shp, t_ray ray);

#endif
14 changes: 10 additions & 4 deletions include/mlx_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
# define KEY_ESC 65307
# define A_KEY_UP 237
# define A_KEY_DWN 233
# define L_KEY_A_L 97
# define L_KEY_D_R 100
# define L_KEY_S_B 115
# define L_KEY_W_U 119

# define C_ORI_A_L 97
# define C_ORI_D_R 100
# define C_ORI_S_B 115
# define C_ORI_W_U 119

# define L_KEY_A_L 106
# define L_KEY_D_R 108
# define L_KEY_S_B 107
# define L_KEY_W_U 105
# define L_KEY_X_F 120
# define L_KEY_Y_B 121
# define C_KEY_UP 65362
Expand Down
12 changes: 6 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ void do_hit(t_ray ray, t_hit_record *rec, float *closest_t, t_shapes *shp)
*closest_t = t;
resolve_hit(rec, t, ray, shp);
rec->normal = normalize(shp->axis);
if (vec_dot(&rec->normal, &ray.dir) > 0.0f)
rec->normal = scale(rec->normal, -1.0f);
}
if ((ft_strncmp(shp->identifier, "cy", 2) == 0)
&& (ray_inter_cy(ray, shp, &t) && t < *closest_t))
{
&& (ray_inter_cy(ray, shp, &t, rec) && t < *closest_t))
*closest_t = t;
resolve_hit(rec, t, ray, shp);
rec->normal = normalize(vec_sub(rec->point, shp->cords));
}
}

bool hit_objects(t_data *data, t_ray ray, t_hit_record *rec)
Expand Down Expand Up @@ -104,11 +102,13 @@ int main(int argc, char *argv[])
if (init_mlx_window(data) == -1)
exit(-1);
cast_rays(data);
mlx_key_hook(data->win_ptr, &key_mapping, (void *)data);
mlx_hook(data->win_ptr, 2, 1L << 0, &key_mapping, data);
mlx_hook(data->win_ptr, 33, 1L << 17, &mlx_loop_end, data->mlx_ptr);
mlx_loop(data->mlx_ptr);
mlx_destroy_image(data->mlx_ptr, data->scene_img[data->frame % 2]->ptr);
mlx_destroy_window(data->mlx_ptr, data->win_ptr);
mlx_destroy_display(data->mlx_ptr);
free(data->mlx_ptr);
free_data(data);
return (0);
}
9 changes: 4 additions & 5 deletions multiobject.rt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
A 0.5 255,255,255
C 0.0,-2.0,8.0 0,0,1 70
L 40,-75,100 0.8 255,255,255
C 0.0,-2.0,8.0 0,0,-1 70
L 40,-75,10 0.8 255,255,255
sp 2,-1,0 1.8 216,56,136
sp 2,-0.2,0 1.8 140,118,206
sp 0,2,0 1.4 216,56,136
sq -1,0,-0.5 1,0,0 4 0,176,240
sq 2,0,-0.5 1,0,0 4 0,176,240
cy 0,-2,0 0,1,0 0.8 4 255,204,0
cy -3,0.6,0 1,0,0 0.8 7 255,204,0
cy -3,0.6,0 1,0,0 0.8 7 255,204,0
#
7 changes: 2 additions & 5 deletions multiobject2.rt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ sp 0,2,0 1.4 216,56,136

pl 0,-2,0 0,1,0 140,118,206

sq -1,0,-0.5 1,0,0 4 0,176,240
sq 2,0,-0.5 1,0,0 4 0,176,240

cy 0,-1,0 0,2,0 0.8 4 255,204,0
cy 0,0.6,0 1,2,0 0.8 7 255,204,0
cy 0,-1,0 0,1,0 0.8 4 255,204,0
cy 0,0.6,0 1,0,0 0.8 7 255,204,0
14 changes: 14 additions & 0 deletions src/colors.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* colors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: voparkan <voparkan@student.42prague.cz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/07 12:28:10 by voparkan #+# #+# */
/* Updated: 2025/06/07 14:33:25 by voparkan ### ########.fr */
/* */
/* ************************************************************************** */

/* ************************************************************************** */
/* */
/* ::: :::::::: */
Expand Down Expand Up @@ -54,6 +66,8 @@ bool is_in_shadow(t_data *data, t_vec point, t_vec light_pos, t_vec normal)
t_hit_record tmp;
double hit_dist;

if (data->scene->ambi.ratio == 1)
return (false);
to_light = vec_sub(light_pos, point);
light_dist = vec_length(to_light);
shadow_ray.origin = add(point, scale(normal, 1e-3));
Expand Down
31 changes: 21 additions & 10 deletions src/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@

#include "../include/minirt.h"

void free_imgs(void *shp)
{
t_shapes *shape;

shape = (t_shapes *)shp;
if (shape->img.mlx_ptr != NULL)
free(shape->img.ptr);
}

void free_lines(char **lines)
{
int i;
Expand All @@ -47,6 +38,26 @@ void free_data(t_data *data)
ft_lstclear(&data->shapes, &free);
if (data->scene)
free(data->scene);
close(data->scenefd);
if (data->scenefd != -1)
{
close(data->scenefd);
data->scenefd = -1;
}
free(data);
}

int min(int a, int b)
{
if (a < b)
return (a);
else
return (b);
}

int max(int a, int b)
{
if (a > b)
return (a);
else
return (b);
}
Loading