Skip to content

Commit

Permalink
[#47][feat] color disruption: chessboard
Browse files Browse the repository at this point in the history
  • Loading branch information
davfront committed Jul 20, 2023
1 parent d7fa57d commit 44d90ed
Show file tree
Hide file tree
Showing 19 changed files with 368 additions and 48 deletions.
7 changes: 6 additions & 1 deletion 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/20 11:56:20 by dapereir ### ########.fr #
# Updated: 2023/07/21 00:49:11 by dapereir ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -57,6 +57,7 @@ SRCS_FILES = \
vec3/quat_from_vec3.c\
vec3/quat_multiply.c\
vec3/quat_from_axis_angle.c\
vec3/quat_invert.c\
vec3/mat4_zero.c\
vec3/mat4_identity.c\
vec3/mat4_translate.c\
Expand Down Expand Up @@ -100,6 +101,7 @@ SRCS_FILES = \
parse/rt_parse_rgb.c\
parse/rt_parse_float.c\
parse/rt_parse_vec3.c\
parse/rt_parse_texture.c\
parse/rt_parse_input.c\
parse/rt_parse_line.c\
parse/rt_parse_ambient_light.c\
Expand All @@ -111,6 +113,7 @@ SRCS_FILES = \
parse/rt_parse_obj_cone.c\
parse/rt_parse_line_error_exit.c\
parse/rt_parse_value_error_exit.c\
parse/rt_parse_texture_error_exit.c\
parse/rt_parse.c\
\
viewer/rt_viewer_start.c\
Expand Down Expand Up @@ -180,6 +183,8 @@ SRCS_FILES = \
ui/rt_ui_bounding_box.c\
ui/rt_ui_selected.c\
\
texture/rt_get_chess_color.c\
\
main.c\

SRCS_DIR = ./src
Expand Down
8 changes: 7 additions & 1 deletion include/minirt.h
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/20 11:55:09 by dapereir ### ########.fr */
/* Updated: 2023/07/21 01:11:26 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -181,6 +181,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);
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 All @@ -193,6 +194,8 @@ void rt_parse_obj_cone(t_data *data, char **strs);
void rt_parse_line_error_exit(t_data *data, char *msg);
void rt_parse_value_error_exit(t_data *data, char *line_type, \
char *label, char *value);
void rt_parse_texture_error_exit(t_data *data, char *line_type, \
char *value);
void rt_parse(t_data *data, int argc, char **argv);

// viewer
Expand Down Expand Up @@ -297,4 +300,7 @@ 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);

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

#endif
31 changes: 24 additions & 7 deletions 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/19 11:02:50 by dapereir ### ########.fr */
/* Updated: 2023/07/21 00:23:38 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -41,29 +41,25 @@ typedef struct s_light {
typedef struct s_sphere {
t_vec3 center;
t_float radius;
t_rgb color;
} t_sphere;

typedef struct s_plane {
t_vec3 point;
t_vec3 normal;
t_rgb color;
} t_plane;

typedef struct s_cylinder {
t_vec3 center;
t_vec3 axis;
t_float radius;
t_float height;
t_rgb color;
} t_cylinder;

typedef struct s_cone {
t_vec3 center;
t_vec3 axis;
t_float radius;
t_float height;
t_rgb color;
} t_cone;

typedef struct s_disc3 {
Expand All @@ -85,16 +81,37 @@ typedef struct s_tf {
t_vec3 scale;
} t_tf;

typedef struct s_vec2 {
t_float x;
t_float y;
} t_vec2;

typedef struct s_chess {
t_rgb color1;
t_rgb color2;
t_vec2 size;
} t_chess;

typedef enum e_tex_type {
COLOR,
CHESS
} t_tex_type;

typedef struct s_obj {
t_obj_type type;
t_tf tf;
t_tf tf_down;
union {
t_sphere sphere;
t_plane plane;
t_cylinder cylinder;
t_cone cone;
};
t_tex_type tex_type;
union {
t_rgb color;
t_chess chess;
};
t_tf tf;
t_tf tf_down;
} t_obj;

#endif
3 changes: 2 additions & 1 deletion include/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/06 17:10:57 by dapereir #+# #+# */
/* Updated: 2023/07/15 14:45:23 by dapereir ### ########.fr */
/* Updated: 2023/07/20 14:52:41 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -57,6 +57,7 @@ t_quat quat_identity(void);
t_quat quat_from_vec3(t_vec3 v0, t_vec3 v1);
t_quat quat_multiply(t_quat a, t_quat b);
t_quat quat_from_axis_angle(t_vec3 axis, t_float angle);
t_quat quat_invert(t_quat q);

t_mat4 mat4_zero(void);
t_mat4 mat4_identity(void);
Expand Down
27 changes: 27 additions & 0 deletions scenes/chessboard.rt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Ambient light
# ratio color
A 0.2 255,255,255

# Camera
# position direction fov
C -100,10,20 1,-0.1,0 35

# Lights
# position ratio color
L -40,30,-10 0.7 255,255,255

# Planes
# point normal color / chess
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

# 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

# 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
6 changes: 3 additions & 3 deletions src/parse/rt_parse_obj_cone.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 11:30:58 by dapereir #+# #+# */
/* Updated: 2023/07/19 11:01:00 by dapereir ### ########.fr */
/* Updated: 2023/07/21 00:51:27 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,8 +26,8 @@ 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_rgb(strs[4], &obj->cone.color))
rt_parse_value_error_exit(data, "cone", "color", strs[4]);
if (!rt_parse_texture(strs[4], obj))
rt_parse_texture_error_exit(data, "cone", strs[4]);
}

void rt_parse_obj_cone(t_data *data, char **strs)
Expand Down
6 changes: 3 additions & 3 deletions src/parse/rt_parse_obj_cylinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 11:30:58 by dapereir #+# #+# */
/* Updated: 2023/07/18 21:57:07 by dapereir ### ########.fr */
/* Updated: 2023/07/21 00:51:32 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,8 +26,8 @@ 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_rgb(strs[4], &obj->cylinder.color))
rt_parse_value_error_exit(data, "cylinder", "color", strs[4]);
if (!rt_parse_texture(strs[4], obj))
rt_parse_texture_error_exit(data, "cylinder", strs[4]);
}

void rt_parse_obj_cylinder(t_data *data, char **strs)
Expand Down
6 changes: 3 additions & 3 deletions src/parse/rt_parse_obj_plane.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 11:30:58 by dapereir #+# #+# */
/* Updated: 2023/07/15 14:19:36 by dapereir ### ########.fr */
/* Updated: 2023/07/21 00:47:44 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,8 +27,8 @@ 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_rgb(strs[2], &obj.plane.color))
rt_parse_value_error_exit(data, "plane", "color", strs[2]);
if (!rt_parse_texture(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));
if (!content)
Expand Down
6 changes: 3 additions & 3 deletions src/parse/rt_parse_obj_sphere.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 11:30:58 by dapereir #+# #+# */
/* Updated: 2023/07/15 14:48:16 by dapereir ### ########.fr */
/* Updated: 2023/07/21 00:51:50 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,8 +28,8 @@ 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_rgb(strs[2], &obj.sphere.color))
rt_parse_value_error_exit(data, "sphere", "color", strs[2]);
if (!rt_parse_texture(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));
if (!content)
Expand Down
2 changes: 1 addition & 1 deletion src/parse/rt_parse_rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 21:15:04 by dapereir #+# #+# */
/* Updated: 2023/07/20 11:24:11 by dapereir ### ########.fr */
/* Updated: 2023/07/20 23:31:01 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
69 changes: 69 additions & 0 deletions src/parse/rt_parse_texture.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rt_parse_texture.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 11:30:58 by dapereir #+# #+# */
/* Updated: 2023/07/21 00:47:28 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

#include "minirt.h"

static int rt_parse_chess_is_str_valid(char *s)
{
int n;
char *sep;

n = 0;
if (ft_strncmp(s, "chess:", 6) != 0)
return (0);
sep = ft_strchr(s, ':');
while (sep)
{
n++;
if (n > 4 || !sep[1] || sep[1] == ':')
return (0);
sep = ft_strchr(sep + 1, ':');
}
return (n == 4);
}

static int rt_parse_chess(char *s, t_chess *chess_p)
{
char **args;
t_chess chess;

if (!s || !chess_p)
return (0);
if (!rt_parse_chess_is_str_valid(s))
return (0);
args = ft_split(s + 6, ":");
if (!args)
return (rt_error("rt_parse_chess: split failed\n"), 0);
if (!rt_parse_rgb(args[0], &chess.color1))
return (ft_free_split(&args), 0);
if (!rt_parse_rgb(args[1], &chess.color2))
return (ft_free_split(&args), 0);
if (!rt_parse_float_len(args[2], &chess.size.x))
return (ft_free_split(&args), 0);
if (!rt_parse_float_len(args[3], &chess.size.y))
return (ft_free_split(&args), 0);
*chess_p = chess;
return (ft_free_split(&args), 1);
}

int rt_parse_texture(char *s, t_obj *obj)
{
if (!s)
return (0);
if (ft_strncmp(s, "chess:", 6) == 0)
{
obj->tex_type = CHESS;
return (rt_parse_chess(s, &obj->chess));
}
obj->tex_type = COLOR;
return (rt_parse_rgb(s, &obj->color));
}
22 changes: 22 additions & 0 deletions src/parse/rt_parse_texture_error_exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rt_parse_texture_error_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dapereir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 11:30:58 by dapereir #+# #+# */
/* Updated: 2023/07/21 01:11:56 by dapereir ### ########.fr */
/* */
/* ************************************************************************** */

#include "minirt.h"

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
rt_parse_value_error_exit(data, line_type, "color", value);
}
Loading

0 comments on commit 44d90ed

Please sign in to comment.