Compare commits

..

No commits in common. "ddad475c7ddbe89fe1506ba05adbb285b357b837" and "be46b3ccf9f5b9ad3b8f98cc3220686af7ed3e41" have entirely different histories.

5 changed files with 48 additions and 199 deletions

View File

@ -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 fdf_draw.c SOURCES = fdf.c fdf_rotations.c
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

View File

@ -22,64 +22,37 @@
# include "libft.h" # include "libft.h"
# include "MLX42.h" # include "MLX42.h"
# define WRONG_LINE_LENGTH "Error: wrong line length. \ #define WRONG_LINE_LENGTH "Error: wrong line length. Expected %d, got %d in line nr %d\n"
Expected %d, got %d in line nr %d\n" #define MALLOC_ERROR "Error: malloc failed\n"
# define MALLOC_ERROR "Error: malloc failed\n" #define FILE_ERROR "Error: could not open file %s\n"
# define FILE_ERROR "Error: could not open file %s\n" #define USAGE_ERROR "Usage: %s <filename>\n"
# define USAGE_ERROR "Usage: %s <filename>\n"
# define WIDTH 1500 #define WIDTH 1024
# define HEIGHT 1500 #define HEIGHT 1024
typedef struct s_point_3d typedef struct s_point
{ {
double x; double x;
double y; double y;
double z; double z;
int color; } t_point;
} 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_3d *points; t_point *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_3d **points, double angle, int size); void rotate_x(t_point **points, double angle, int size);
void rotate_y(t_point_3d **points, double angle, int size); void rotate_y(t_point **points, double angle, int size);
void rotate_z(t_point_3d **points, double angle, int size); void rotate_z(t_point **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

View File

@ -78,8 +78,7 @@ 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.14; map->points[y * map->width + x].z = ft_atoi(split[x]) * 0.05;
map->points[y * map->width + x].color = 0xFFFFFFFF;
x++; x++;
} }
free_line_and_split(&line, &split); free_line_and_split(&line, &split);
@ -120,57 +119,21 @@ 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_3d)); map->points = malloc(map->width * map->height * sizeof(t_point));
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)
@ -179,15 +142,27 @@ 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);
fdf_set_background(img, 0x000000FF); x = 0;
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)
{ {
fdf_put_pixel(img, map->projected[i]); x = WIDTH / 2 + map->points[i].x * WIDTH / 2.0 / map->height;
if (i % map->width != map->width - 1) y = HEIGHT / 2 + map->points[i].z * HEIGHT / 2.0 / map->height;
fdf_draw_line(img, map->projected[i], map->projected[i + 1]); //printf("x: %.2f, y: %.2f\n", x, y);
if (i / map->width != map->height - 1) if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
fdf_draw_line(img, map->projected[i], map->projected[i + map->width]); mlx_put_pixel(img,x + WIDTH , HEIGHT- y, 0xFFFFFFFF);
i++; i++;
} }
mlx_loop(mlx); mlx_loop(mlx);
@ -212,8 +187,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);
} }

View File

@ -1,99 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}
}

View File

@ -1,6 +1,6 @@
#include "fdf.h" #include "fdf.h"
void rotate_x(t_point_3d **points, double angle, int size) void rotate_x(t_point **points, double angle, int size)
{ {
int i; int i;
double previous_y; double previous_y;
@ -14,7 +14,7 @@ void rotate_x(t_point_3d **points, double angle, int size)
i++; i++;
} }
} }
void rotate_y(t_point_3d **points, double angle, int size) void rotate_y(t_point **points, double angle, int size)
{ {
int i; int i;
double previous_x; double previous_x;
@ -28,7 +28,7 @@ void rotate_y(t_point_3d **points, double angle, int size)
i++; i++;
} }
} }
void rotate_z(t_point_3d **points, double angle, int size) void rotate_z(t_point **points, double angle, int size)
{ {
int i; int i;
double previous_x; double previous_x;