added line drawing and projection to fdf.c
This commit is contained in:
parent
7b195e4536
commit
ddad475c7d
81
src/fdf.c
81
src/fdf.c
@ -78,7 +78,8 @@ int read_map(char *filename, t_map *map)
|
||||
{
|
||||
map->points[y * map->width + x].x = x - map->width / 2;
|
||||
map->points[y * map->width + x].y = map->height / 2 - y;
|
||||
map->points[y * map->width + x].z = ft_atoi(split[x]) * 0.05;
|
||||
map->points[y * map->width + x].z = ft_atoi(split[x]) * 0.14;
|
||||
map->points[y * map->width + x].color = 0xFFFFFFFF;
|
||||
x++;
|
||||
}
|
||||
free_line_and_split(&line, &split);
|
||||
@ -119,21 +120,57 @@ t_map *parse_map(char *filename)
|
||||
return (ft_printf(MALLOC_ERROR), NULL);
|
||||
if (!get_map_sizes(filename, map))
|
||||
return (free(map), NULL);
|
||||
map->points = malloc(map->width * map->height * sizeof(t_point));
|
||||
map->points = malloc(map->width * map->height * sizeof(t_point_3d));
|
||||
if (!map->points)
|
||||
return (free(map), ft_printf(MALLOC_ERROR), NULL);
|
||||
if (!read_map(filename, map))
|
||||
return (free(map->points), free(map), NULL);
|
||||
return (map);
|
||||
}
|
||||
void fdf_set_background(mlx_image_t *img, int color)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
y = 0;
|
||||
while (y < (int) img->height)
|
||||
{
|
||||
x = 0;
|
||||
while (x < (int) img->width)
|
||||
{
|
||||
mlx_put_pixel(img, x, y, color);
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
void fdf_project_isometric(t_map *map)
|
||||
{
|
||||
int i;
|
||||
double x;
|
||||
double y;
|
||||
|
||||
i = 0;
|
||||
map->projected = malloc(map->width * map->height * sizeof(t_point_2d));
|
||||
if (!map->projected)
|
||||
return ;
|
||||
while (i < map->width * map->height)
|
||||
{
|
||||
x = (map->points[i].x - map->points[i].y) * cos(0.523599);
|
||||
y = -map->points[i].z + (map->points[i].x + map->points[i].y) * sin(0.523599);
|
||||
map->projected[i].x = x * 0.5 * WIDTH * 0.1 + WIDTH / 2;
|
||||
map->projected[i].y = y * 0.5 * HEIGHT * 0.1 + HEIGHT / 2;
|
||||
map->projected[i].color = map->points[i].color;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int draw_map(t_map *map)
|
||||
{
|
||||
mlx_t* mlx;
|
||||
mlx_image_t* img;
|
||||
int i;
|
||||
int x;
|
||||
int y;
|
||||
mlx_t* mlx;
|
||||
mlx_image_t* img;
|
||||
int i;
|
||||
|
||||
mlx = mlx_init(WIDTH, HEIGHT, "FdF", false);
|
||||
if (!mlx)
|
||||
@ -142,27 +179,15 @@ int draw_map(t_map *map)
|
||||
i = 0;
|
||||
if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0))
|
||||
return (EXIT_FAILURE);
|
||||
x = 0;
|
||||
|
||||
|
||||
while (x < WIDTH)
|
||||
{
|
||||
y = 0;
|
||||
while (y < HEIGHT)
|
||||
{
|
||||
mlx_put_pixel(img, x, y, 0x000000FF);
|
||||
y++;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
|
||||
fdf_set_background(img, 0x000000FF);
|
||||
fdf_project_isometric(map);
|
||||
while (i < map->width * map->height)
|
||||
{
|
||||
x = WIDTH / 2 + map->points[i].x * WIDTH / 2.0 / map->height;
|
||||
y = HEIGHT / 2 + map->points[i].z * HEIGHT / 2.0 / map->height;
|
||||
//printf("x: %.2f, y: %.2f\n", x, y);
|
||||
if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
|
||||
mlx_put_pixel(img,x + WIDTH , HEIGHT- y, 0xFFFFFFFF);
|
||||
fdf_put_pixel(img, map->projected[i]);
|
||||
if (i % map->width != map->width - 1)
|
||||
fdf_draw_line(img, map->projected[i], map->projected[i + 1]);
|
||||
if (i / map->width != map->height - 1)
|
||||
fdf_draw_line(img, map->projected[i], map->projected[i + map->width]);
|
||||
i++;
|
||||
}
|
||||
mlx_loop(mlx);
|
||||
@ -187,8 +212,8 @@ int main(int argc, char *argv[])
|
||||
map = parse_map(argv[1]);
|
||||
if (!map)
|
||||
return (EXIT_FAILURE);
|
||||
rotate_z(&map->points, deg2rad(30) , map->width * map->height);
|
||||
rotate_x(&map->points, deg2rad(70), map->width * map->height);
|
||||
// rotate_z(&map->points, deg2rad(30) , map->width * map->height);
|
||||
//rotate_x(&map->points, deg2rad(70), map->width * map->height);
|
||||
draw_map(map);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user