From 2d243b3d5715cba18fb9928524e6a92caf89679d Mon Sep 17 00:00:00 2001 From: Gregory Date: Sun, 12 Mar 2017 01:19:43 +0200 Subject: [PATCH] semiworking color gradient x2 --- inc/fdf.h | 2 ++ src/color.c | 35 +++++++++++++++++++++-------------- src/line.c | 8 ++++++-- src/map_init.c | 15 ++++++++++----- src/pnt_init.c | 6 ++++-- src/render.c | 2 +- 6 files changed, 44 insertions(+), 24 deletions(-) diff --git a/inc/fdf.h b/inc/fdf.h index b9bb942..4666f76 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -63,6 +63,7 @@ typedef struct s_map int x_max; int y_max; int z_max; + int z_min; } t_map; typedef struct s_mat4 @@ -128,6 +129,7 @@ t_color color_lerp(t_color c1, t_color c2, float 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, t_FDF *FDF); +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); diff --git a/src/color.c b/src/color.c index a6de92b..c75c3cf 100644 --- a/src/color.c +++ b/src/color.c @@ -44,20 +44,27 @@ t_color choose_color(t_line *line, t_swap *s, t_FDF *FDF) FDF = FDF; z1 = line->p1.z; z2 = line->p2.z; - if (z2 == 0 && z1 == 0) - c = int_to_color(COLOR1); - else if (z1 == z2) - c = int_to_color(COLOR2); - else if (z2 == 0) - c = int_to_color(COLOR1); - else if (z1 == 0) - c = int_to_color(COLOR2); - else - { - if (s->swap_z) - c = color_lerp(int_to_color(COLOR2), int_to_color(COLOR1), z1 / z2); + if (s->swap_z) + if (z2 == 0) + c = int_to_color(color_to_int(line->p2.color)); else - c = color_lerp(int_to_color(COLOR1), int_to_color(COLOR2), z1 / z2); - } + c = color_lerp(line->p2.color, line->p1.color, z1 / z2); + else + if (z2 == 0) + c = int_to_color(color_to_int(line->p1.color)); + else + c = color_lerp(line->p1.color, line->p2.color, z1 / z2); return (c); } + +void first_color(t_pnt *pnt, float z, t_map *map) +{ + if (z == map->z_min) + pnt->color = int_to_color(COLOR1); + if (z == map->z_max) + pnt->color = int_to_color(COLOR2); + 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 diff --git a/src/line.c b/src/line.c index c9109da..9e6c652 100644 --- a/src/line.c +++ b/src/line.c @@ -3,11 +3,15 @@ static void swap_z(t_pnt *p1, t_pnt *p2) { - float tmp; + float tmp; + t_color c; tmp = p1->z; p1->z = p2->z; p2->z = tmp; + c = p1->color; + p1->color = p2->color; + p2->color = c; } static void swap_x_y(t_pnt *p1, t_pnt *p2) @@ -37,7 +41,7 @@ static void draw_line(t_line *line, t_swap *s, t_FDF *FDF) int dx; int dy; int derr; - float dz; + double dz; dx = abs(line->p2.x - line->p1.x); dy = abs(line->p2.y - line->p1.y); diff --git a/src/map_init.c b/src/map_init.c index e890752..d013e7d 100644 --- a/src/map_init.c +++ b/src/map_init.c @@ -48,26 +48,31 @@ void map_dimensions(t_map *map) { int x; int y; - int z; + int max_z; + int min_z; t_list *l; l = map->lst_map; x = 0; y = 0; - z = 0; + max_z = 0; + min_z = 0; while (l) { if (((t_vec *)(l->content))->x > x) x = (int)((t_vec *)(l->content))->x; if (((t_vec *)(l->content))->y > y) y = (int)((t_vec *)(l->content))->y; - if (((t_vec *)(l->content))->z > z) - z = (int)((t_vec *)(l->content))->z; + if (((t_vec *)(l->content))->z > max_z) + max_z = (int)((t_vec *)(l->content))->z; + if (((t_vec *)(l->content))->z < min_z) + min_z = (int)((t_vec *)(l->content))->z; l = l->next; } map->x_max = x; map->y_max = y; - map->z_max = z; + map->z_max = max_z; + map->z_min = min_z; } t_map *map_init(char *path) diff --git a/src/pnt_init.c b/src/pnt_init.c index 2fcc35f..37648db 100644 --- a/src/pnt_init.c +++ b/src/pnt_init.c @@ -6,7 +6,7 @@ void pnt_init(t_map *map) t_list *lst_map; int i; int j; - + float z; lst_map = map->lst_map; arr_pnt = malloc(sizeof(t_pnt *) * (map->y_max + 1)); i = 0; @@ -16,7 +16,9 @@ void pnt_init(t_map *map) arr_pnt[i] = malloc(sizeof(t_pnt) * (map->x_max + 1)); while (j <= map->x_max) { - arr_pnt[i][j].z = ((t_vec *)(lst_map->content))->z; + z = ((t_vec *)(lst_map->content))->z; + arr_pnt[i][j].z = z; + first_color(&arr_pnt[i][j], z, map); lst_map = lst_map->next; j++; } diff --git a/src/render.c b/src/render.c index 4d373ff..09bd15b 100644 --- a/src/render.c +++ b/src/render.c @@ -155,7 +155,7 @@ void render(t_FDF *FDF) rot_z = mat4_init(); translate = mat4_init(); scale = mat4_init(); - final = mat4_init(); + //final = mat4_init(); vp = viewport_mat_init(FDF->w_width, FDF->w_height); orth = orth_mat_init( 0,