parse input
This commit is contained in:
parent
66634b039e
commit
59c581a9ac
7 changed files with 140 additions and 59 deletions
7
Makefile
7
Makefile
|
@ -43,11 +43,14 @@ OBJ_FILES = $(SRC_FILES:.c=.o)
|
||||||
SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
|
SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
|
||||||
OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
|
OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
|
||||||
|
|
||||||
|
FDF_HEADER = fdf.h
|
||||||
|
|
||||||
# header files
|
# header files
|
||||||
|
|
||||||
|
FDF_INC = ./inc/
|
||||||
INC = -I $(LIBFT_DIR)includes/ \
|
INC = -I $(LIBFT_DIR)includes/ \
|
||||||
-I $(MLX_DIR) \
|
-I $(MLX_DIR) \
|
||||||
-I ./inc
|
-I $(FDF_INC)
|
||||||
|
|
||||||
# compiler flags
|
# compiler flags
|
||||||
|
|
||||||
|
@ -73,7 +76,7 @@ CC = gcc
|
||||||
|
|
||||||
all: $(NAME)
|
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)"
|
@echo "$(CYAN)Linking fdf binary...$(NORMAL)"
|
||||||
@$(CC) $(OBJ) $(FLAGS) -o $(NAME)
|
@$(CC) $(OBJ) $(FLAGS) -o $(NAME)
|
||||||
@echo "$(GREEN)Done!$(NORMAL)"
|
@echo "$(GREEN)Done!$(NORMAL)"
|
||||||
|
|
39
inc/fdf.h
39
inc/fdf.h
|
@ -3,7 +3,7 @@
|
||||||
// FILE: inc/fdf.h
|
// FILE: inc/fdf.h
|
||||||
// ROLE: to rule them all
|
// ROLE: to rule them all
|
||||||
// CREATED: 2017-02-13 20:07:04
|
// CREATED: 2017-02-13 20:07:04
|
||||||
// MODIFIED: 2017-02-18 01:11:28
|
// MODIFIED: 2017-03-16 00:08:26
|
||||||
|
|
||||||
|
|
||||||
#ifndef FDF_H
|
#ifndef FDF_H
|
||||||
|
@ -15,11 +15,11 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
# define HEIGHT 1300
|
# define HEIGHT 800
|
||||||
# define WIDTH 2500
|
# define WIDTH 700
|
||||||
# define ENDIAN 0
|
# define ENDIAN 0
|
||||||
# define COLOR1 16515072
|
# define COLOR1 0
|
||||||
# define COLOR2 252
|
# define COLOR2 16777215
|
||||||
# define INIT_X 30
|
# define INIT_X 30
|
||||||
# define INIT_Y -20
|
# define INIT_Y -20
|
||||||
# define INIT_Z -45
|
# define INIT_Z -45
|
||||||
|
@ -92,6 +92,8 @@ typedef struct s_map
|
||||||
t_trnsf *trnsf;
|
t_trnsf *trnsf;
|
||||||
t_vec **arr_vec;
|
t_vec **arr_vec;
|
||||||
t_pnt **arr_pnt;
|
t_pnt **arr_pnt;
|
||||||
|
t_color *low;
|
||||||
|
t_color *high;
|
||||||
int x_max;
|
int x_max;
|
||||||
int y_max;
|
int y_max;
|
||||||
int z_max;
|
int z_max;
|
||||||
|
@ -115,53 +117,54 @@ typedef struct s_FDF
|
||||||
int endian;
|
int endian;
|
||||||
} t_FDF;
|
} 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);
|
t_map *map_init(char *path);
|
||||||
|
|
||||||
void pnt_init(t_map *map);
|
void pnt_init(t_map *map);
|
||||||
|
|
||||||
t_mat4 *mat4_init(void);
|
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 *viewport_mat_init(int x, int y);
|
||||||
|
|
||||||
t_mat4 *orth_mat_init(float r, float t, float f);
|
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 mat4_mult_chain(t_FDF *FDF);
|
||||||
void viewport_projection_translate(t_trnsf *tf);
|
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_translate(t_mat4 *m, double x, double y, double z);
|
||||||
void mat4_scale(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_z_rot(t_mat4 *m, double angle);
|
||||||
void mat4_x_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 mat4_y_rot(t_mat4 *m, double angle);
|
||||||
|
|
||||||
void vec_mat_mult(t_mat4 *m, t_vec *v, t_pnt *result);
|
void vec_mat_mult(t_mat4 *m, t_vec *v, t_pnt *result);
|
||||||
|
|
||||||
t_mat4 *mat4_mult(t_mat4 *m1, t_mat4 *m2);
|
t_mat4 *mat4_mult(t_mat4 *m1, t_mat4 *m2);
|
||||||
|
|
||||||
void transform(t_mat4 *mat, t_map *map);
|
void transform(t_mat4 *mat, t_map *map);
|
||||||
|
void recalculate_trnsf_matrs(t_map *map);
|
||||||
void center(t_map *map);
|
void center(t_map *map);
|
||||||
|
|
||||||
void render(t_FDF *FDF);
|
|
||||||
|
|
||||||
int tab_length(char **tab);
|
int tab_length(char **tab);
|
||||||
|
|
||||||
void free_tab(char **tab);
|
void free_tab(char **tab);
|
||||||
void quit(t_FDF *FDF);
|
void quit(t_FDF *FDF);
|
||||||
void swap_init(t_swap *s);
|
void swap_init(t_swap *s);
|
||||||
void print_help(void);
|
void print_help(void);
|
||||||
|
|
||||||
t_color color_init(int red, int green, int blue);
|
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);
|
t_color int_to_color(int c);
|
||||||
int color_to_int(t_color c);
|
int color_to_int(t_color c);
|
||||||
t_color choose_color(t_line *line, t_swap *s);
|
t_color choose_color(t_line *line, t_swap *s);
|
||||||
void first_color(t_pnt *pnt, float z, t_map *map);
|
void first_color(t_pnt *pnt, float z, t_map *map);
|
||||||
|
|
||||||
t_mw *mlx_and_win_ptr_init(int x, int y);
|
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);
|
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 put_pixel_to_image(t_line *line, t_swap *s, t_FDF *FDF);
|
||||||
|
|
||||||
void left_rot(t_FDF *FDF);
|
void left_rot(t_FDF *FDF);
|
||||||
|
|
|
@ -12,13 +12,15 @@ t_mw *mlx_and_win_ptr_init(int x, int y)
|
||||||
return (mw);
|
return (mw);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_FDF *FDF_init(char *path)
|
t_FDF *FDF_init(char *path, t_color *low, t_color *high)
|
||||||
{
|
{
|
||||||
t_FDF *FDF;
|
t_FDF *FDF;
|
||||||
|
|
||||||
if ((FDF = malloc(sizeof(t_FDF))))
|
if ((FDF = malloc(sizeof(t_FDF))))
|
||||||
{
|
{
|
||||||
FDF->map = map_init(path);
|
FDF->map = map_init(path);
|
||||||
|
FDF->map->low = low;
|
||||||
|
FDF->map->high = high;
|
||||||
pnt_init(FDF->map);
|
pnt_init(FDF->map);
|
||||||
FDF->w_height = HEIGHT;
|
FDF->w_height = HEIGHT;
|
||||||
FDF->w_width = WIDTH;
|
FDF->w_width = WIDTH;
|
||||||
|
|
34
src/color.c
34
src/color.c
|
@ -10,12 +10,19 @@ t_color color_init(int red, int green, int blue)
|
||||||
return (c);
|
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;
|
t_color new;
|
||||||
new.red = (float)(c2.red - c1.red) * step + c1.red;
|
double red;
|
||||||
new.green = (float)(c2.green - c1.green) * step + c1.green;
|
double green;
|
||||||
new.blue = (float)(c2.blue - c1.blue) * step + c1.blue;
|
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);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,17 +38,15 @@ t_color int_to_color(int c)
|
||||||
new.blue = c & 0x0000FF;
|
new.blue = c & 0x0000FF;
|
||||||
new.green = (c >> 8) & 0x0000FF;
|
new.green = (c >> 8) & 0x0000FF;
|
||||||
new.red = (c >> 16 ) & 0x0000FF;
|
new.red = (c >> 16 ) & 0x0000FF;
|
||||||
c = 0;
|
|
||||||
return (new);
|
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;
|
t_color c;
|
||||||
float z1;
|
float z1;
|
||||||
float z2;
|
float z2;
|
||||||
|
|
||||||
FDF = FDF;
|
|
||||||
z1 = line->p1.z;
|
z1 = line->p1.z;
|
||||||
z2 = line->p2.z;
|
z2 = line->p2.z;
|
||||||
if (s->swap_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)
|
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)
|
if (z == 0)
|
||||||
pnt->color = int_to_color(COLOR1);
|
pnt->color = int_to_color(low_color);
|
||||||
if (z == map->z_max - map->z_min)
|
if (z == map->z_max - map->z_min)
|
||||||
pnt->color = int_to_color(COLOR2);
|
pnt->color = int_to_color(high_color);
|
||||||
else
|
else
|
||||||
pnt->color = color_lerp(
|
pnt->color = color_lerp(
|
||||||
int_to_color(COLOR1),
|
int_to_color(low_color),
|
||||||
int_to_color(COLOR2), fabs(z / (map->z_max - map->z_min)));
|
int_to_color(high_color), fabs(z / (map->z_max - map->z_min)));
|
||||||
}
|
}
|
||||||
|
|
28
src/main.c
28
src/main.c
|
@ -1,33 +1,13 @@
|
||||||
#include "fdf.h"
|
#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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
t_FDF *FDF;
|
t_FDF *FDF;
|
||||||
|
|
||||||
if (argc < 2 && argc > 4)
|
FDF = NULL;
|
||||||
{
|
parse_input(argc, argv, &FDF);
|
||||||
print_help();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
else if (argc < 4)
|
|
||||||
{
|
|
||||||
parse_input(argc, argv);
|
|
||||||
}
|
|
||||||
FDF = FDF_init(argv[1]);
|
|
||||||
center(FDF->map);
|
center(FDF->map);
|
||||||
FDF->map->trnsf = initial_trnsf_mats(FDF);
|
initial_trnsf_mats(FDF);
|
||||||
viewport_projection_translate(FDF->map->trnsf);
|
viewport_projection_translate(FDF->map->trnsf);
|
||||||
mat4_mult_chain(FDF);
|
mat4_mult_chain(FDF);
|
||||||
transform(FDF->map->trnsf->fin, FDF->map);
|
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_hook(FDF->mw->win, 2, 5, key_hook, FDF);
|
||||||
mlx_loop(FDF->mw->mlx);
|
mlx_loop(FDF->mw->mlx);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
|
@ -15,7 +15,7 @@ void recalculate_trnsf_matrs(t_map *map)
|
||||||
INIT_Z_SCALE * map->scale[2]);
|
INIT_Z_SCALE * map->scale[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_trnsf *initial_trnsf_mats(t_FDF *FDF)
|
void initial_trnsf_mats(t_FDF *FDF)
|
||||||
{
|
{
|
||||||
t_trnsf *tf;
|
t_trnsf *tf;
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ t_trnsf *initial_trnsf_mats(t_FDF *FDF)
|
||||||
mat4_z_rot(tf->zr, INIT_Z);
|
mat4_z_rot(tf->zr, INIT_Z);
|
||||||
mat4_y_rot(tf->yr, INIT_Y);
|
mat4_y_rot(tf->yr, INIT_Y);
|
||||||
mat4_scale(tf->sc, INIT_X_SCALE, INIT_Y_SCALE, INIT_Z_SCALE);
|
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)
|
void viewport_projection_translate(t_trnsf *tf)
|
||||||
|
|
83
src/parse_input.c
Normal file
83
src/parse_input.c
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue