Skip to content

Commit

Permalink
texture for sphere and plane ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Agoutcho committed Jul 24, 2023
1 parent 45aa910 commit 6ea35dd
Show file tree
Hide file tree
Showing 20 changed files with 624 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ SRCS_FILES = \
\
utils/rt_init.c\
utils/rt_delete.c\
utils/rt_delete_obj_lst.c\
utils/rt_error.c\
utils/rt_exit.c\
utils/rt_error_exit.c\
Expand Down Expand Up @@ -184,6 +185,7 @@ SRCS_FILES = \
ui/rt_ui_selected.c\
\
texture/rt_get_chess_color.c\
texture/rt_get_tex_pixel.c\
\
main.c\

Expand Down
496 changes: 496 additions & 0 deletions earth.xpm

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions include/minirt.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ typedef struct s_ray {
t_hit hit;
} t_ray;

typedef struct s_img {
void *img;
char *addr;
int bpp;
int len;
int endian;
} t_img;

typedef struct s_ui {
int changed;
t_px mouse;
Expand Down Expand Up @@ -163,6 +155,7 @@ typedef struct s_data {
// utils
void rt_init(t_data *data);
void rt_delete(t_data *data);
void rt_delete_obj_lst(t_data *data, t_list **lst);
void rt_error(char *msg);
void rt_exit(t_data *data);
void rt_error_exit(t_data *data, char *msg);
Expand All @@ -181,7 +174,7 @@ int rt_parse_float_len(char *s, t_float *len);
int rt_parse_float_ratio(char *s, t_float *ratio);
int rt_parse_vec3(char *s, t_vec3 *v);
int rt_parse_vec3_dir(char *s, t_vec3 *dir);
int rt_parse_texture(char *s, t_obj *obj);
int rt_parse_texture(t_data *data, char *s, t_obj *obj);
void rt_parse_input(t_data *data, int argc, char **argv);
void rt_parse_line(t_data *data);
void rt_parse_ambient_light(t_data *data, char **strs);
Expand Down Expand Up @@ -302,5 +295,6 @@ void rt_ui_selected(t_data *data);

// texture
t_rgb rt_get_chess_color(t_vec2 p, t_chess chess);
t_rgb rt_get_tex_pixel(t_vec2 p, t_img img);

#endif
14 changes: 13 additions & 1 deletion include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,20 @@ typedef struct s_chess {

typedef enum e_tex_type {
COLOR,
CHESS
CHESS,
XPM
} t_tex_type;

typedef struct s_img {
void *img;
char *addr;
int bpp;
int len;
int endian;
int width;
int height;
} t_img;

typedef struct s_obj {
t_obj_type type;
union {
Expand All @@ -109,6 +120,7 @@ typedef struct s_obj {
union {
t_rgb color;
t_chess chess;
t_img xpm;
};
t_tf tf;
t_tf tf_down;
Expand Down
8 changes: 4 additions & 4 deletions scenes/chessboard.rt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ambient light
# ratio color
A 0.2 255,255,255
A 0.5 255,255,255

# Camera
# position direction fov
Expand All @@ -16,12 +16,12 @@ pl 0,0,0 0,1.0,0 chess:255,0,255:127,0,127:10:10

# Spheres
# center diameter color / chess
sp 0,20,-10 20 chess:0,255,0:0,127,0:3:3
sp 0,20,-10 20 xpm:earth.xpm

# Cylinders
# center axis diameter height color / chess
cy 0,20,20 0,1,0 15 30 chess:10,0,255:10,0,127:8:8
cy 0,20,20 0,1,0 15 30 xpm:earth.xpm

# Cones
# center axis diameter height color / chess
co -20,10,40 0,1,1 20 15 chess:0,255,255:0,127,127:5:5
co -20,10,40 0,1,1 20 15 xpm:earth.xpm
2 changes: 1 addition & 1 deletion src/parse/rt_parse_obj_cone.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void rt_parse_obj_cone_values(t_data *data, char **strs, t_obj *obj)
obj->cone.radius /= 2;
if (!rt_parse_float_len(strs[3], &obj->cone.height))
rt_parse_value_error_exit(data, "cone", "height", strs[3]);
if (!rt_parse_texture(strs[4], obj))
if (!rt_parse_texture(data, strs[4], obj))
rt_parse_texture_error_exit(data, "cone", strs[4]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/parse/rt_parse_obj_cylinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void rt_parse_obj_cylinder_values(t_data *data, char **strs, t_obj *obj)
obj->cylinder.radius /= 2;
if (!rt_parse_float_len(strs[3], &obj->cylinder.height))
rt_parse_value_error_exit(data, "cylinder", "height", strs[3]);
if (!rt_parse_texture(strs[4], obj))
if (!rt_parse_texture(data, strs[4], obj))
rt_parse_texture_error_exit(data, "cylinder", strs[4]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/parse/rt_parse_obj_plane.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void rt_parse_obj_plane(t_data *data, char **strs)
rt_parse_value_error_exit(data, "plane", "point", strs[0]);
if (!rt_parse_vec3_dir(strs[1], &obj.plane.normal))
rt_parse_value_error_exit(data, "plane", "normal", strs[1]);
if (!rt_parse_texture(strs[2], &obj))
if (!rt_parse_texture(data, strs[2], &obj))
rt_parse_texture_error_exit(data, "plane", strs[2]);
obj.tf = rt_get_plane_transformations(obj.plane);
content = ft_calloc(1, sizeof(t_obj));
Expand Down
2 changes: 1 addition & 1 deletion src/parse/rt_parse_obj_sphere.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void rt_parse_obj_sphere(t_data *data, char **strs)
if (!rt_parse_float_len(strs[1], &obj.sphere.radius))
rt_parse_value_error_exit(data, "sphere", "diameter", strs[1]);
obj.sphere.radius /= 2;
if (!rt_parse_texture(strs[2], &obj))
if (!rt_parse_texture(data, strs[2], &obj))
rt_parse_texture_error_exit(data, "sphere", strs[2]);
obj.tf = rt_get_sphere_transformations(obj.sphere);
content = ft_calloc(1, sizeof(t_obj));
Expand Down
21 changes: 20 additions & 1 deletion src/parse/rt_parse_texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,21 @@ static int rt_parse_chess(char *s, t_chess *chess_p)
return (ft_free_split(&args), 1);
}

int rt_parse_texture(char *s, t_obj *obj)
static int rt_parse_xpm(t_data *data, char *s, t_img *xpm)
{
char *sep;

if (!s || !xpm)
return (0);
sep = ft_strchr(s, ':');
xpm->img = mlx_xpm_file_to_image(data->mlx, sep + 1, &xpm->width, &xpm->height);
if (!xpm->img)
return (0);
xpm->addr = mlx_get_data_addr(xpm->img, &xpm->bpp, &xpm->len, &xpm->endian);
return (1);
}

int rt_parse_texture(t_data *data, char *s, t_obj *obj)
{
if (!s)
return (0);
Expand All @@ -64,6 +78,11 @@ int rt_parse_texture(char *s, t_obj *obj)
obj->tex_type = CHESS;
return (rt_parse_chess(s, &obj->chess));
}
if (ft_strncmp(s, "xpm:", 4) == 0)
{
obj->tex_type = XPM;
return (rt_parse_xpm(data, s, &obj->xpm));
}
obj->tex_type = COLOR;
return (rt_parse_rgb(s, &obj->color));
}
3 changes: 3 additions & 0 deletions src/parse/rt_parse_texture_error_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ void rt_parse_texture_error_exit(t_data *data, char *line_type, char *value)
if (value && ft_strncmp(value, "chess:", 6) == 0)
rt_parse_value_error_exit(data, line_type, \
"chessboard texture configuration", value);
else if (value && ft_strncmp(value, "xpm:", 4) == 0)
rt_parse_value_error_exit(data, line_type, \
"xpm file", value);
else
rt_parse_value_error_exit(data, line_type, "color", value);
}
5 changes: 5 additions & 0 deletions src/raytracer/rt_get_cone_hit.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ static t_rgb rt_get_cone_hit_color(t_obj *obj, t_cone co, t_vec3 hit_pos, \
if (obj->tex_type == CHESS)
color = rt_get_chess_color(\
rt_get_cone_hit_tex_coord(obj, co, hit_pos, is_body), obj->chess);
else if (obj->tex_type == XPM)
{
color = rt_get_tex_pixel(\
rt_get_cone_hit_tex_coord(obj, co, hit_pos, is_body), obj->xpm);
}
else
color = obj->color;
return (color);
Expand Down
5 changes: 5 additions & 0 deletions src/raytracer/rt_get_cylinder_hit.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ static t_rgb rt_get_cylinder_hit_color(t_obj *obj, t_cylinder cy, \
p = rt_get_cylinder_hit_tex_coord(obj, cy, hit_pos, is_body);
color = rt_get_chess_color(p, obj->chess);
}
else if (obj->tex_type == XPM)
{
p = rt_get_cylinder_hit_tex_coord(obj, cy, hit_pos, is_body);
color = rt_get_tex_pixel(p, obj->xpm);
}
else
color = obj->color;
return (color);
Expand Down
5 changes: 5 additions & 0 deletions src/raytracer/rt_get_plane_hit.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ static t_rgb rt_get_plane_hit_color(t_obj *obj, t_plane pl, t_vec3 hit_pos)
if (obj->tex_type == CHESS)
color = rt_get_chess_color(\
rt_get_plane_hit_tex_coord(obj, pl, hit_pos), obj->chess);
else if (obj->tex_type == XPM)
{
color = rt_get_tex_pixel(\
rt_get_plane_hit_tex_coord(obj, pl, hit_pos), obj->xpm);
}
else
color = obj->color;
return (color);
Expand Down
15 changes: 15 additions & 0 deletions src/raytracer/rt_get_sphere_hit.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ static t_float rt_get_sphere_hit_dist(t_ray ray, t_sphere sp, t_float t_max)
return (INFINITY);
}

static t_vec2 rt_get_sphere_hit_tex_uv(t_obj *obj, t_vec3 hit_normal)
{
t_vec2 p;
t_mat4 mr_inv;

mr_inv = mat4_from_quat(quat_invert(obj->tf.rotate));
hit_normal = mat4_multiply_axis(mr_inv, hit_normal);
p.x = 0.5 + atan2f(hit_normal.z, hit_normal.x) / ((t_float)2 * M_PI);
p.y = 0.5 - asinf(hit_normal.y) / M_PI;
return (p);
}

static t_vec2 rt_get_sphere_hit_tex_coord(t_obj *obj, t_sphere sp, \
t_vec3 hit_normal)
{
Expand All @@ -57,6 +69,9 @@ static t_rgb rt_get_sphere_hit_color(t_obj *obj, t_sphere sp, \
if (obj->tex_type == CHESS)
color = rt_get_chess_color(\
rt_get_sphere_hit_tex_coord(obj, sp, hit_normal), obj->chess);
else if (obj->tex_type == XPM)
color = rt_get_tex_pixel(\
rt_get_sphere_hit_tex_uv(obj, hit_normal), obj->xpm);
else
color = obj->color;
return (color);
Expand Down
19 changes: 19 additions & 0 deletions src/texture/rt_get_tex_pixel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "minirt.h"

t_rgb rt_get_tex_pixel(t_vec2 p, t_img xpm)
{
char *dst;
int color_int;
int x;
int y;

if (!xpm.img)
return (rgb_int(0));
x = (int)floor(p.x * (float)xpm.width) % xpm.width;
y = (int)floor(p.y * (float)xpm.height) % xpm.height;
dst = xpm.addr;
dst += y * xpm.len;
dst += x * (xpm.bpp / 8);
color_int = *(unsigned int *)dst;
return (rgb_int(color_int));
}
2 changes: 1 addition & 1 deletion src/utils/rt_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void rt_delete(t_data *data)
ft_free((void **)&(data->al));
ft_free((void **)&(data->cam));
ft_lstclear(&data->light_lst, free);
ft_lstclear(&data->obj_lst, free);
rt_delete_obj_lst(data, &data->obj_lst);
rt_viewer_destroy(data);
rt_free_buffer(data);
}
23 changes: 23 additions & 0 deletions src/utils/rt_delete_obj_lst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "minirt.h"

static void rt_delete_obj_lst_img(t_data *data, t_obj *obj)
{
if (obj->tex_type == XPM && obj->xpm.img)
mlx_destroy_image(data->mlx, obj->xpm.img);
}

void rt_delete_obj_lst(t_data *data, t_list **lst)
{
t_list *tmp;

if (!lst || !data)
return ;
while (*lst)
{
tmp = (*lst)->next;
rt_delete_obj_lst_img(data, (*lst)->content);
free((*lst)->content);
free(*lst);
*lst = tmp;
}
}
6 changes: 6 additions & 0 deletions src/utils/rt_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ void rt_init(t_data *data)
ft_bzero(data, sizeof(t_data));
data->fd = -1;
rt_alloc_buffer(data);
data->mlx = mlx_init();
if (!data->mlx)
{
rt_delete(data);
rt_error_exit(data, "Minilibx initialization failed");
}
}
6 changes: 0 additions & 6 deletions src/viewer/rt_viewer_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

void rt_viewer_start(t_data *data)
{
data->mlx = mlx_init();
if (!data->mlx)
{
rt_delete(data);
rt_error_exit(data, "Minilibx initialization failed");
}
data->win = mlx_new_window(data->mlx, WIN_WIDTH, WIN_HEIGHT, data->title);
ft_printf("[%s] File opened\n", data->title);
rt_viewer_hooks(data);
Expand Down

0 comments on commit 6ea35dd

Please sign in to comment.