From 59c581a9ac8f5c4e5944fe6d1dac4e4eb302e1b4 Mon Sep 17 00:00:00 2001 From: Gregory Date: Thu, 16 Mar 2017 00:12:00 +0200 Subject: [PATCH] parse input --- Makefile | 7 ++-- inc/fdf.h | 39 +++++++++++---------- src/FDF_init.c | 4 ++- src/color.c | 34 +++++++++++------- src/main.c | 28 +++------------ src/matrix_routine.c | 4 +-- src/parse_input.c | 83 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 140 insertions(+), 59 deletions(-) create mode 100644 src/parse_input.c diff --git a/Makefile b/Makefile index 0db9693..b134854 100644 --- a/Makefile +++ b/Makefile @@ -43,11 +43,14 @@ OBJ_FILES = $(SRC_FILES:.c=.o) SRC = $(addprefix $(SRC_DIR), $(SRC_FILES)) OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES)) +FDF_HEADER = fdf.h + # header files +FDF_INC = ./inc/ INC = -I $(LIBFT_DIR)includes/ \ -I $(MLX_DIR) \ - -I ./inc + -I $(FDF_INC) # compiler flags @@ -73,7 +76,7 @@ CC = gcc all: $(NAME) -$(NAME): $(OBJ) $(LIBFT_DIR)$(LIBFT) $(MLX_DIR)$(MLX) +$(NAME): $(OBJ) $(LIBFT_DIR)$(LIBFT) $(MLX_DIR)$(MLX) $(FDF_INC)$(FDF_HEADER) @echo "$(CYAN)Linking fdf binary...$(NORMAL)" @$(CC) $(OBJ) $(FLAGS) -o $(NAME) @echo "$(GREEN)Done!$(NORMAL)" diff --git a/inc/fdf.h b/inc/fdf.h index 202bc21..9a5ad71 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -3,7 +3,7 @@ // FILE: inc/fdf.h // ROLE: to rule them all // CREATED: 2017-02-13 20:07:04 -// MODIFIED: 2017-02-18 01:11:28 +// MODIFIED: 2017-03-16 00:08:26 #ifndef FDF_H @@ -15,11 +15,11 @@ #include #include -# define HEIGHT 1300 -# define WIDTH 2500 +# define HEIGHT 800 +# define WIDTH 700 # define ENDIAN 0 -# define COLOR1 16515072 -# define COLOR2 252 +# define COLOR1 0 +# define COLOR2 16777215 # define INIT_X 30 # define INIT_Y -20 # define INIT_Z -45 @@ -92,6 +92,8 @@ typedef struct s_map t_trnsf *trnsf; t_vec **arr_vec; t_pnt **arr_pnt; + t_color *low; + t_color *high; int x_max; int y_max; int z_max; @@ -115,53 +117,54 @@ typedef struct s_FDF int endian; } t_FDF; -void line(t_pnt p1, t_pnt p2, t_FDF *FDF); +void parse_input(int ac, char **av, t_FDF **FDF); t_map *map_init(char *path); + void pnt_init(t_map *map); - t_mat4 *mat4_init(void); -t_trnsf *trnsf_mat_init(); +t_trnsf *trnsf_mat_init(); t_mat4 *viewport_mat_init(int x, int y); + t_mat4 *orth_mat_init(float r, float t, float f); -t_trnsf *initial_trnsf_mats(t_FDF *FDF); +void initial_trnsf_mats(t_FDF *FDF); void mat4_mult_chain(t_FDF *FDF); void viewport_projection_translate(t_trnsf *tf); -void recalculate_trnsf_matrs(t_map *map); - void mat4_translate(t_mat4 *m, double x, double y, double z); void mat4_scale(t_mat4 *m, double x, double y, double z); + void mat4_z_rot(t_mat4 *m, double angle); void mat4_x_rot(t_mat4 *m, double angle); void mat4_y_rot(t_mat4 *m, double angle); - void vec_mat_mult(t_mat4 *m, t_vec *v, t_pnt *result); + t_mat4 *mat4_mult(t_mat4 *m1, t_mat4 *m2); void transform(t_mat4 *mat, t_map *map); +void recalculate_trnsf_matrs(t_map *map); void center(t_map *map); -void render(t_FDF *FDF); - int tab_length(char **tab); + void free_tab(char **tab); void quit(t_FDF *FDF); void swap_init(t_swap *s); void print_help(void); - t_color color_init(int red, int green, int blue); -t_color color_lerp(t_color c1, t_color c2, float step); +t_color color_lerp(t_color c1, t_color c2, double step); + t_color int_to_color(int c); int color_to_int(t_color c); t_color choose_color(t_line *line, t_swap *s); void first_color(t_pnt *pnt, float z, t_map *map); - t_mw *mlx_and_win_ptr_init(int x, int y); -t_FDF *FDF_init(char *path); +t_FDF *FDF_init(char *path, t_color *low, t_color *high); int key_hook(int keycode, void *m); +void line(t_pnt p1, t_pnt p2, t_FDF *FDF); +void render(t_FDF *FDF); void put_pixel_to_image(t_line *line, t_swap *s, t_FDF *FDF); void left_rot(t_FDF *FDF); diff --git a/src/FDF_init.c b/src/FDF_init.c index cdc6cd8..f4a67e1 100644 --- a/src/FDF_init.c +++ b/src/FDF_init.c @@ -12,13 +12,15 @@ t_mw *mlx_and_win_ptr_init(int x, int y) return (mw); } -t_FDF *FDF_init(char *path) +t_FDF *FDF_init(char *path, t_color *low, t_color *high) { t_FDF *FDF; if ((FDF = malloc(sizeof(t_FDF)))) { FDF->map = map_init(path); + FDF->map->low = low; + FDF->map->high = high; pnt_init(FDF->map); FDF->w_height = HEIGHT; FDF->w_width = WIDTH; diff --git a/src/color.c b/src/color.c index 90c6be9..0f36779 100644 --- a/src/color.c +++ b/src/color.c @@ -10,12 +10,19 @@ t_color color_init(int red, int green, int blue) return (c); } -t_color color_lerp(t_color c1, t_color c2, float step) +t_color color_lerp(t_color c1, t_color c2, double step) { t_color new; - new.red = (float)(c2.red - c1.red) * step + c1.red; - new.green = (float)(c2.green - c1.green) * step + c1.green; - new.blue = (float)(c2.blue - c1.blue) * step + c1.blue; + double red; + double green; + double blue; + + red = (double)(c2.red - c1.red) * step + c1.red; + green = (double)(c2.green - c1.green) * step + c1.green; + blue = (double)(c2.blue - c1.blue) * step + c1.blue; + new.red = (int)red; + new.green = (int)green; + new.blue = (int)blue; return (new); } @@ -31,17 +38,15 @@ t_color int_to_color(int c) new.blue = c & 0x0000FF; new.green = (c >> 8) & 0x0000FF; new.red = (c >> 16 ) & 0x0000FF; - c = 0; return (new); } -t_color choose_color(t_line *line, t_swap *s, t_FDF *FDF) +t_color choose_color(t_line *line, t_swap *s) { t_color c; float z1; float z2; - FDF = FDF; z1 = line->p1.z; z2 = line->p2.z; if (s->swap_z) @@ -59,12 +64,17 @@ t_color choose_color(t_line *line, t_swap *s, t_FDF *FDF) void first_color(t_pnt *pnt, float z, t_map *map) { + int low_color; + int high_color; + + low_color = (map->low == NULL) ? COLOR1 : color_to_int(*map->low); + high_color = (map->high == NULL) ? COLOR2 : color_to_int(*map->high); if (z == 0) - pnt->color = int_to_color(COLOR1); + pnt->color = int_to_color(low_color); if (z == map->z_max - map->z_min) - pnt->color = int_to_color(COLOR2); + pnt->color = int_to_color(high_color); else pnt->color = color_lerp( - int_to_color(COLOR1), - int_to_color(COLOR2), fabs(z / (map->z_max - map->z_min))); -} \ No newline at end of file + int_to_color(low_color), + int_to_color(high_color), fabs(z / (map->z_max - map->z_min))); +} diff --git a/src/main.c b/src/main.c index 7275b2e..8873d8f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,33 +1,13 @@ #include "fdf.h" -void parse_input(int ac, char **av) -{ - if (ac == 2 && ft_strstr(av[1], "--help")) - { - print_help(); - } - if (ac == 4 && ft_strstr(av[1], "--heatmap")) - { - - } -} - int main(int argc, char **argv) { t_FDF *FDF; - if (argc < 2 && argc > 4) - { - print_help(); - return (0); - } - else if (argc < 4) - { - parse_input(argc, argv); - } - FDF = FDF_init(argv[1]); + FDF = NULL; + parse_input(argc, argv, &FDF); center(FDF->map); - FDF->map->trnsf = initial_trnsf_mats(FDF); + initial_trnsf_mats(FDF); viewport_projection_translate(FDF->map->trnsf); mat4_mult_chain(FDF); transform(FDF->map->trnsf->fin, FDF->map); @@ -35,4 +15,4 @@ int main(int argc, char **argv) mlx_hook(FDF->mw->win, 2, 5, key_hook, FDF); mlx_loop(FDF->mw->mlx); return (0); -} +} \ No newline at end of file diff --git a/src/matrix_routine.c b/src/matrix_routine.c index f2d3e40..42b60ee 100644 --- a/src/matrix_routine.c +++ b/src/matrix_routine.c @@ -15,7 +15,7 @@ void recalculate_trnsf_matrs(t_map *map) INIT_Z_SCALE * map->scale[2]); } -t_trnsf *initial_trnsf_mats(t_FDF *FDF) +void initial_trnsf_mats(t_FDF *FDF) { t_trnsf *tf; @@ -39,7 +39,7 @@ t_trnsf *initial_trnsf_mats(t_FDF *FDF) mat4_z_rot(tf->zr, INIT_Z); mat4_y_rot(tf->yr, INIT_Y); mat4_scale(tf->sc, INIT_X_SCALE, INIT_Y_SCALE, INIT_Z_SCALE); - return (tf); + FDF->map->trnsf = tf; } void viewport_projection_translate(t_trnsf *tf) diff --git a/src/parse_input.c b/src/parse_input.c new file mode 100644 index 0000000..75bca8b --- /dev/null +++ b/src/parse_input.c @@ -0,0 +1,83 @@ +#include "fdf.h" + +static int tab_to_color(t_color **color, char **tab) +{ + int red; + int green; + int blue; + + red = ft_atoi(tab[0]); + green = ft_atoi(tab[1]); + blue = ft_atoi(tab[2]); + if (red > 255 || red < 0 || + green > 255 || green < 0 || + blue > 255 || blue < 0) + return (0); + (*color)->red = red; + (*color)->green = green; + (*color)->blue = blue; + return (1); +} + +static void wrong_heatmap(t_color **low, t_color **high) +{ + ft_putstr("wrong heatmap! Type \"fdf --help\" for heatmap example\n"); + free(*low); + free(*high); + *low = NULL; + *high = NULL; +} + +static void parse_heatmap(char *hm, t_color **low, t_color **high) +{ + char **hm_tab; + char **low_tab; + char **high_tab; + + *low = malloc(sizeof(t_color)); + *high = malloc(sizeof(t_color)); + hm_tab = ft_strsplit(hm, '-'); + if ((tab_length(hm_tab) != 1)) + { + wrong_heatmap(low, high); + return ; + } + low_tab = ft_strsplit(hm_tab[0], ','); + high_tab = ft_strsplit(hm_tab[1], ','); + if (tab_length(low_tab) != 2 || tab_length(high_tab) != 2) + { + wrong_heatmap(low, high); + return ; + } + if (!tab_to_color(low, low_tab) || !tab_to_color(high, high_tab)) + { + wrong_heatmap(low, high); + return ; + } +} + +void parse_input(int ac, char **av, t_FDF **FDF) +{ + t_color *low; + t_color *high; + + low = NULL; + high = NULL; + if ((ac < 2 || ac > 4) || (ac == 2 && ft_strstr(av[1], "--help"))) + { + print_help(); + quit(*FDF); + } + else if (ac == 2) + *FDF = FDF_init(av[1], low, high); + else if (ac == 4 && ft_strstr(av[1], "--heatmap")) + { + parse_heatmap(av[2], &low, &high); + *FDF = FDF_init(av[3], low, high); + } + else + { + print_help(); + quit(*FDF); + } +} \ No newline at end of file