Compare commits
4 Commits
be46b3ccf9
...
ddad475c7d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ddad475c7d | ||
|
|
7b195e4536 | ||
|
|
0cb11a2bbe | ||
|
|
33f398bd60 |
2
Makefile
2
Makefile
@ -27,7 +27,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a
|
|||||||
OBJ_PATH = obj
|
OBJ_PATH = obj
|
||||||
|
|
||||||
VPATH = src
|
VPATH = src
|
||||||
SOURCES = fdf.c fdf_rotations.c
|
SOURCES = fdf.c fdf_rotations.c fdf_draw.c
|
||||||
|
|
||||||
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
|
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
|
||||||
|
|
||||||
|
|||||||
59
inc/fdf.h
59
inc/fdf.h
@ -22,37 +22,64 @@
|
|||||||
# include "libft.h"
|
# include "libft.h"
|
||||||
# include "MLX42.h"
|
# include "MLX42.h"
|
||||||
|
|
||||||
#define WRONG_LINE_LENGTH "Error: wrong line length. Expected %d, got %d in line nr %d\n"
|
# define WRONG_LINE_LENGTH "Error: wrong line length. \
|
||||||
#define MALLOC_ERROR "Error: malloc failed\n"
|
Expected %d, got %d in line nr %d\n"
|
||||||
#define FILE_ERROR "Error: could not open file %s\n"
|
# define MALLOC_ERROR "Error: malloc failed\n"
|
||||||
#define USAGE_ERROR "Usage: %s <filename>\n"
|
# define FILE_ERROR "Error: could not open file %s\n"
|
||||||
|
# define USAGE_ERROR "Usage: %s <filename>\n"
|
||||||
|
|
||||||
#define WIDTH 1024
|
# define WIDTH 1500
|
||||||
#define HEIGHT 1024
|
# define HEIGHT 1500
|
||||||
|
|
||||||
typedef struct s_point
|
typedef struct s_point_3d
|
||||||
{
|
{
|
||||||
double x;
|
double x;
|
||||||
double y;
|
double y;
|
||||||
double z;
|
double z;
|
||||||
} t_point;
|
int color;
|
||||||
|
} t_point_3d;
|
||||||
|
|
||||||
|
typedef struct s_point_2d
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int color;
|
||||||
|
} t_point_2d;
|
||||||
|
|
||||||
|
|
||||||
typedef struct s_map
|
typedef struct s_map
|
||||||
{
|
{
|
||||||
t_point *points;
|
t_point_3d *points;
|
||||||
|
t_point_2d *projected;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
int z_max;
|
int z_max;
|
||||||
} t_map;
|
} t_map;
|
||||||
|
|
||||||
|
typedef struct s_fdf
|
||||||
|
{
|
||||||
|
t_map *map;
|
||||||
|
mlx_t *mlx;
|
||||||
|
mlx_image_t *img;
|
||||||
|
double angle_x;
|
||||||
|
double angle_y;
|
||||||
|
double angle_z;
|
||||||
|
double zoom;
|
||||||
|
int offset_x;
|
||||||
|
int offset_y;
|
||||||
|
double z_scale;
|
||||||
|
} t_fdf;
|
||||||
|
|
||||||
int get_map_sizes(char *filename, t_map *map);
|
int get_map_sizes(char *filename, t_map *map);
|
||||||
void free_line_and_split(char **line, char ***split);
|
void free_line_and_split(char **line, char ***split);
|
||||||
int read_map(char *filename, t_map *map);
|
int read_map(char *filename, t_map *map);
|
||||||
t_map *parse_map(char *filename);
|
t_map *parse_map(char *filename);
|
||||||
void print_map(t_map *map);
|
void print_map(t_map *map);
|
||||||
void rotate_x(t_point **points, double angle, int size);
|
void rotate_x(t_point_3d **points, double angle, int size);
|
||||||
void rotate_y(t_point **points, double angle, int size);
|
void rotate_y(t_point_3d **points, double angle, int size);
|
||||||
void rotate_z(t_point **points, double angle, int size);
|
void rotate_z(t_point_3d **points, double angle, int size);
|
||||||
|
bool fdf_put_pixel(mlx_image_t *img, t_point_2d point);
|
||||||
|
void fdf_draw_line(mlx_image_t *img, t_point_2d start,
|
||||||
|
t_point_2d end);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
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].x = x - map->width / 2;
|
||||||
map->points[y * map->width + x].y = map->height / 2 - y;
|
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++;
|
x++;
|
||||||
}
|
}
|
||||||
free_line_and_split(&line, &split);
|
free_line_and_split(&line, &split);
|
||||||
@ -119,21 +120,57 @@ t_map *parse_map(char *filename)
|
|||||||
return (ft_printf(MALLOC_ERROR), NULL);
|
return (ft_printf(MALLOC_ERROR), NULL);
|
||||||
if (!get_map_sizes(filename, map))
|
if (!get_map_sizes(filename, map))
|
||||||
return (free(map), NULL);
|
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)
|
if (!map->points)
|
||||||
return (free(map), ft_printf(MALLOC_ERROR), NULL);
|
return (free(map), ft_printf(MALLOC_ERROR), NULL);
|
||||||
if (!read_map(filename, map))
|
if (!read_map(filename, map))
|
||||||
return (free(map->points), free(map), NULL);
|
return (free(map->points), free(map), NULL);
|
||||||
return (map);
|
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)
|
int draw_map(t_map *map)
|
||||||
{
|
{
|
||||||
mlx_t* mlx;
|
mlx_t* mlx;
|
||||||
mlx_image_t* img;
|
mlx_image_t* img;
|
||||||
int i;
|
int i;
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
|
|
||||||
mlx = mlx_init(WIDTH, HEIGHT, "FdF", false);
|
mlx = mlx_init(WIDTH, HEIGHT, "FdF", false);
|
||||||
if (!mlx)
|
if (!mlx)
|
||||||
@ -142,27 +179,15 @@ int draw_map(t_map *map)
|
|||||||
i = 0;
|
i = 0;
|
||||||
if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0))
|
if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0))
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
x = 0;
|
fdf_set_background(img, 0x000000FF);
|
||||||
|
fdf_project_isometric(map);
|
||||||
|
|
||||||
while (x < WIDTH)
|
|
||||||
{
|
|
||||||
y = 0;
|
|
||||||
while (y < HEIGHT)
|
|
||||||
{
|
|
||||||
mlx_put_pixel(img, x, y, 0x000000FF);
|
|
||||||
y++;
|
|
||||||
}
|
|
||||||
x++;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (i < map->width * map->height)
|
while (i < map->width * map->height)
|
||||||
{
|
{
|
||||||
x = WIDTH / 2 + map->points[i].x * WIDTH / 2.0 / map->height;
|
fdf_put_pixel(img, map->projected[i]);
|
||||||
y = HEIGHT / 2 + map->points[i].z * HEIGHT / 2.0 / map->height;
|
if (i % map->width != map->width - 1)
|
||||||
//printf("x: %.2f, y: %.2f\n", x, y);
|
fdf_draw_line(img, map->projected[i], map->projected[i + 1]);
|
||||||
if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
|
if (i / map->width != map->height - 1)
|
||||||
mlx_put_pixel(img,x + WIDTH , HEIGHT- y, 0xFFFFFFFF);
|
fdf_draw_line(img, map->projected[i], map->projected[i + map->width]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
mlx_loop(mlx);
|
mlx_loop(mlx);
|
||||||
@ -187,8 +212,8 @@ int main(int argc, char *argv[])
|
|||||||
map = parse_map(argv[1]);
|
map = parse_map(argv[1]);
|
||||||
if (!map)
|
if (!map)
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
rotate_z(&map->points, deg2rad(30) , map->width * map->height);
|
// rotate_z(&map->points, deg2rad(30) , map->width * map->height);
|
||||||
rotate_x(&map->points, deg2rad(70), map->width * map->height);
|
//rotate_x(&map->points, deg2rad(70), map->width * map->height);
|
||||||
draw_map(map);
|
draw_map(map);
|
||||||
return (EXIT_SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|||||||
99
src/fdf_draw.c
Normal file
99
src/fdf_draw.c
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: o_ :::::: ::: */
|
||||||
|
/* fdf_draw.c :+: / :+::+: :+: */
|
||||||
|
/* +:+ > +:++:+ +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||||
|
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||||
|
/* Created: 2024/12/08 11:45:12 by whaffman #+#+# #+#+# #+# #+# | */
|
||||||
|
/* Updated: 2024/12/08 11:45:12 by whaffman ### ### ### ### / \ */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
#include "fdf.h"
|
||||||
|
|
||||||
|
bool fdf_put_pixel(mlx_image_t *img, t_point_2d point)
|
||||||
|
{
|
||||||
|
if (point.x < 0 || point.x >= (int) img->width
|
||||||
|
|| point.y < 0 || point.y >= (int) img->height)
|
||||||
|
return (false);
|
||||||
|
mlx_put_pixel(img, point.x, point.y, point.color);
|
||||||
|
return (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fdf_draw_line_low(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||||
|
{
|
||||||
|
int delta;
|
||||||
|
int yi;
|
||||||
|
t_point_2d current;
|
||||||
|
t_point_2d delta_point;
|
||||||
|
|
||||||
|
delta_point.x = end.x - start.x;
|
||||||
|
delta_point.y = end.y - start.y;
|
||||||
|
yi = 1;
|
||||||
|
if (delta_point.y < 0)
|
||||||
|
{
|
||||||
|
yi = -1;
|
||||||
|
delta_point.y = -delta_point.y;
|
||||||
|
}
|
||||||
|
delta = 2 * delta_point.y - delta_point.x;
|
||||||
|
current = start;
|
||||||
|
while (current.x <= end.x)
|
||||||
|
{
|
||||||
|
fdf_put_pixel(img, current);
|
||||||
|
if (delta > 0)
|
||||||
|
{
|
||||||
|
current.y += yi;
|
||||||
|
delta -= 2 * delta_point.x;
|
||||||
|
}
|
||||||
|
delta += 2 * delta_point.y;
|
||||||
|
current.x++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fdf_draw_line_high(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||||
|
{
|
||||||
|
int delta;
|
||||||
|
int xi;
|
||||||
|
t_point_2d current;
|
||||||
|
t_point_2d delta_point;
|
||||||
|
|
||||||
|
delta_point.x = end.x - start.x;
|
||||||
|
delta_point.y = end.y - start.y;
|
||||||
|
xi = 1;
|
||||||
|
if (delta_point.x < 0)
|
||||||
|
{
|
||||||
|
xi = -1;
|
||||||
|
delta_point.x = -delta_point.x;
|
||||||
|
}
|
||||||
|
delta = 2 * delta_point.x - delta_point.y;
|
||||||
|
current = start;
|
||||||
|
while (current.y <= end.y)
|
||||||
|
{
|
||||||
|
fdf_put_pixel(img, current);
|
||||||
|
if (delta > 0)
|
||||||
|
{
|
||||||
|
current.x += xi;
|
||||||
|
delta -= 2 * delta_point.y;
|
||||||
|
}
|
||||||
|
delta += 2 * delta_point.x;
|
||||||
|
current.y++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fdf_draw_line(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||||
|
{
|
||||||
|
if (abs(end.y - start.y) < abs(end.x - start.x))
|
||||||
|
{
|
||||||
|
if (start.x > end.x)
|
||||||
|
fdf_draw_line_low(img, end, start);
|
||||||
|
else
|
||||||
|
fdf_draw_line_low(img, start, end);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (start.y > end.y)
|
||||||
|
fdf_draw_line_high(img, end, start);
|
||||||
|
else
|
||||||
|
fdf_draw_line_high(img, start, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
#include "fdf.h"
|
#include "fdf.h"
|
||||||
|
|
||||||
void rotate_x(t_point **points, double angle, int size)
|
void rotate_x(t_point_3d **points, double angle, int size)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
double previous_y;
|
double previous_y;
|
||||||
@ -14,7 +14,7 @@ void rotate_x(t_point **points, double angle, int size)
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void rotate_y(t_point **points, double angle, int size)
|
void rotate_y(t_point_3d **points, double angle, int size)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
double previous_x;
|
double previous_x;
|
||||||
@ -28,7 +28,7 @@ void rotate_y(t_point **points, double angle, int size)
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void rotate_z(t_point **points, double angle, int size)
|
void rotate_z(t_point_3d **points, double angle, int size)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
double previous_x;
|
double previous_x;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user