diff --git a/README.md b/README.md index 39af52c..58cecca 100644 --- a/README.md +++ b/README.md @@ -26,4 +26,8 @@ This README would normally document whatever steps are necessary to get your app ### Who do I talk to? ### * Repo owner or admin -* Other community or team contact \ No newline at end of file +* Other community or team contact + +### ressources +https://harm-smits.github.io/42docs/libs/minilibx +https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm diff --git a/builds/fdf.o b/builds/fdf.o index 3a333a5..455ccee 100644 Binary files a/builds/fdf.o and b/builds/fdf.o differ diff --git a/fdf b/fdf index fcf38b3..41239ca 100755 Binary files a/fdf and b/fdf differ diff --git a/srcs/fdf.c b/srcs/fdf.c index 78f1155..9895b57 100644 --- a/srcs/fdf.c +++ b/srcs/fdf.c @@ -4,35 +4,50 @@ t_fdf *init_fdf(void); int print_keycode(int keycode); int shut_down(t_fdf *fdf); +void draw_image(t_fdf *fdf); void draw_pixel(t_fdf *fdf, int x, int y, int color); +void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z); int keypress(int keycode, t_fdf *fdf); void rotation_state(t_fdf *fdf); int **parse_map(t_fdf *fdf); +int *new_coordinates(t_fdf *fdf, int i, int j); -void color_altitude_pixel(t_fdf *fdf, int new_x, int new_y, int z) +// return an int[3] : int[0] = x, int[1] = y, int[2] = z +int *new_coordinates(t_fdf *fdf, int i, int j) { - int color; + int x; + int y; + int z; + int *point; - color = 0xffffff; - z = z / fdf->altitude; - if (z) - { - color = color ^ (((0xff / fdf->max_z) * z) << 8 ); - color = color ^ ((0xff / fdf->max_z) * z); - ft_printf("0xff:%i z:%i (0xff / max_z):%i ((0xff / max_z) * z):%i\n", 0xff, z, (0xff / fdf->max_z), ((0xff / fdf->max_z) * z)); - } - draw_pixel(fdf, new_x, new_y, color); + point = ft_calloc(3, sizeof(int)); + x = i * fdf->offset - (fdf->map_size_x / 2); + y = j * fdf->offset - (fdf->map_size_y / 2); + z = fdf->map[j][i] * fdf->altitude; + point[0] = x * cos(fdf->rad_x) + y * sin(fdf->rad_x); + point[1] = y * cos(fdf->rad_x) - x * sin(fdf->rad_x); + point[1] = point[1] * cos(fdf->rad_y) - -z * sin(fdf->rad_y); + point[0] += fdf->margin + fdf->mov_x + (fdf->map_size_x / 2); + point[1] += fdf->margin + fdf->mov_y + (fdf->map_size_y / 2); + point[2] = z; + return (point); +} + +// point[0], point[1], point[2] -> new x, new y, new z +void draw_lines(t_fdf *fdf, int i, int j) +{ + int *point_start; + int *point_end; + + point_start = new_coordinates(fdf, i, j); + point_end = new_coordinates(fdf, i, j); + draw_color_pixel(fdf, point_start[0], point_start[1], point_start[2]); } void draw_image(t_fdf *fdf) { int i; int j; - int x; - int y; - int z; - int new_x; - int new_y; // init image with 0 i =-1; @@ -44,18 +59,7 @@ void draw_image(t_fdf *fdf) { i = -1; while (++i < fdf->map_width) - { - - x = i * fdf->offset - (fdf->map_size_x / 2); - y = j * fdf->offset - (fdf->map_size_y / 2); - z = fdf->map[j][i] * fdf->altitude; - new_x = x * cos(fdf->rad_x) + y * sin(fdf->rad_x); - new_y = y * cos(fdf->rad_x) - x * sin(fdf->rad_x); - new_y = new_y * cos(fdf->rad_y) - -z * sin(fdf->rad_y); - new_x += fdf->margin + fdf->mov_x + (fdf->map_size_x / 2); - new_y += fdf->margin + fdf->mov_y + (fdf->map_size_y / 2); - color_altitude_pixel(fdf, new_x, new_y, z); - } + draw_lines(fdf, i, j); } // put image on screen mlx_put_image_to_window(fdf->mlx_ptr, fdf->win_ptr, fdf->img_ptr, 0, 0); @@ -85,9 +89,9 @@ int keypress(int keycode, t_fdf *fdf) if (keycode == ESCAPE) shut_down(fdf); else if (keycode == LEFT) - (fdf->rot_x) -= 1; - else if (keycode == RIGHT) (fdf->rot_x) += 1; + else if (keycode == RIGHT) + (fdf->rot_x) -= 1; else if (keycode == UP) (fdf->rot_y) += 1; else if (keycode == DOWN) @@ -131,6 +135,20 @@ void draw_pixel(t_fdf *fdf, int x, int y, int color) *(unsigned int*)(fdf->img_addr + position) = color; } +void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z) +{ + int color; + + color = 0xffffff; + z = z / fdf->altitude; + if (z) + { + color = color ^ (((0xff / fdf->max_z) * z) << 8 ); + color = color ^ ((0xff / fdf->max_z) * z); + } + draw_pixel(fdf, new_x, new_y, color); +} + void rotation_state(t_fdf *fdf) { char *position;