Split files

This commit is contained in:
whaffman 2024-12-20 12:28:29 +01:00
parent 7e155d1166
commit d66beb964d
46 changed files with 795 additions and 520 deletions

View File

@ -26,7 +26,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a
OBJ_PATH = obj
VPATH = src
VPATH = src:src/parse:src/draw:src/hooks:src/util:src/transform
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

View File

@ -18,10 +18,10 @@
# include <fcntl.h>
# include <stdbool.h>
# include <math.h>
# include <stdio.h>
# include <errno.h>
# include "libft.h"
# include "MLX42.h"
# include <stdio.h>
# define SUCCESS 1
# define FAILURE 0
@ -101,64 +101,48 @@ typedef struct s_fdf
t_projection projection;
} t_fdf;
int interpolate_color(t_point_2d start,
t_point_2d end, t_point_2d current);
int parse_color_string(char *str);
int get_map_sizes(char *filename, t_map *map);
void free_line_and_split(char **line, char ***split);
int get_map_sizes(char *filename, t_map *map);
int load_map_from_file(char *filename, t_fdf *fdf);
int parse_map(char *filename, t_fdf *fdf);
void copy_map(t_fdf *fdf);
void fdf_apply_rotation(t_fdf *fdf);
void set_map_point(t_fdf *fdf, int x, int y, char **str);
int parse_color_string(char *str);
void get_z_max(t_fdf *fdf);
void handle_error(t_fdf *fdf, char *error);
int fdf_hooks(t_fdf *fdf);
void resize_hook(int width, int height, void *param);
void draw_hook(void *param);
void key_hook(mlx_key_data_t keydata, void *param);
mlx_image_t *draw_menu(t_fdf *fdf);
void apply_rotation(t_fdf *fdf);
int get_z_color(double z, int z_max);
void rotate_x(t_point_3d **points, double angle, int size);
void rotate_y(t_point_3d **points, double angle, int size);
void rotate_z(t_point_3d **points, double angle, int size);
bool fdf_put_pixel(t_fdf *fdf, t_point_2d point);
void fdf_draw_line(t_fdf *fdf, t_point_2d start,
t_point_2d end);
int fdf_hooks(t_fdf *fdf);
void resize_hook(int width, int height, void *param);
void draw_hook(void *param);
void key_hook(mlx_key_data_t keydata, void *param);
void fdf_set_background(mlx_image_t *img, int color);
void project_isometric(t_fdf *fdf);
int interpolate_color(t_point_2d start,
t_point_2d end, t_point_2d current);
void draw_line(t_fdf *fdf, t_point_2d start,
t_point_2d end);
bool put_pixel(t_fdf *fdf, t_point_2d point);
void fdf_set_background(mlx_image_t *img, int color);
double deg2rad(double deg);
t_fdf *initialise_fdf(void);
bool clean_fdf(t_fdf *fdf);
void get_z_max(t_fdf *fdf);
int init_mlx(t_fdf *fdf);
void set_map_point(t_fdf *fdf, int x, int y, char **str);
mlx_image_t *draw_menu(t_fdf *fdf);
int get_z_color(double z, int z_max);
void close_hook(void *param);
int get_gradient_color(double z, int z_max);
void project_parallel(t_fdf *fdf);
void project_trimetric(t_fdf *fdf);
void project(t_fdf *fdf);
int check_filename(char *filename);
void get_pixel_color(t_fdf *fdf, t_point_2d *point);
void reset_fdf(t_fdf *fdf);
void prepare_draw(t_fdf *fdf);
void key_hook_options(mlx_key_data_t keydata, t_fdf *fdf);
void key_hook_transform(mlx_key_data_t keydata, t_fdf *fdf);
#endif
// FdF by whaffman
// ============================
// [?] Help menu
// [a] / [d] Rotate around Z-axis
// [w] / [s] Rotate around X-axis
// [q] / [e] Rotate around Y-axis
// [z] / [x] Change Z-scale
// [=] / [-] Change Zoom
// [UP] / [DOWN] Change X-offset
// [LEFT] / [RIGHT] Change Y-offset
// [[] / []] Change animation speed
// [c] Change colormode
// [p] Change projection mode
// [ESC] Close window
// ============================

BIN
menu.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 54 KiB

33
scratch.txt Normal file
View File

@ -0,0 +1,33 @@
// FdF by whaffman
// ============================
// [?] Help menu
// [a] / [d] Rotate around Z-axis
// [w] / [s] Rotate around X-axis
// [q] / [e] Rotate around Y-axis
// [z] / [x] Change Z-scale
// [=] / [-] Change Zoom
// [UP] / [DOWN] Change X-offset
// [LEFT] / [RIGHT] Change Y-offset
// [[] / []] Change animation speed
// [c] Change colormode
// [p] Change projection mode
// [ESC] Close window
// ============================
// const int x = 20;
// const int line_height = 25;
// int y;
// y = 1;
// mlx_put_string(fdf->mlx, "[?] Help menu", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[a] / [d] Rotate around Z-axis", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[w] / [s] Rotate around X-axis", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[q] / [e] Rotate around Y-axis", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[z] / [x] Change Z-scale", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[=] / [-] Change Zoom", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[UP] / [DOWN] Change X-offset", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[LEFT] / [RIGHT] Change Y-offset", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[[] / []] Change animation speed", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[ESC] Close window", x, y++ * line_height);

View File

@ -1,26 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* fdf_draw.c :+: / :+::+: :+: */
/* draw_line.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/08 11:45:12 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:12 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:15 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
bool fdf_put_pixel(t_fdf *fdf, t_point_2d point)
{
if (point.x < 0 || point.x >= (int) fdf->img->width
|| point.y < 0 || point.y >= (int) fdf->img->height)
return (false);
mlx_put_pixel(fdf->img, point.x, point.y, point.color);
return (true);
}
void fdf_draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end)
void draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end)
{
int delta;
int yi;
@ -37,7 +28,7 @@ void fdf_draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end)
while (current.x <= end.x)
{
current.color = interpolate_color(start, end, current);
fdf_put_pixel(fdf, current);
put_pixel(fdf, current);
if (delta > 0)
{
current.y += yi;
@ -48,7 +39,7 @@ void fdf_draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end)
}
}
void fdf_draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end)
void draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end)
{
int delta;
int xi;
@ -65,7 +56,7 @@ void fdf_draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end)
while (current.y <= end.y)
{
current.color = interpolate_color(start, end, current);
fdf_put_pixel(fdf, current);
put_pixel(fdf, current);
if (delta > 0)
{
current.x += xi;
@ -76,7 +67,7 @@ void fdf_draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end)
}
}
void fdf_draw_line(t_fdf *fdf, t_point_2d start, t_point_2d end)
void draw_line(t_fdf *fdf, t_point_2d start, t_point_2d end)
{
if ((start.x < 0 || start.x >= (int) fdf->img->width
|| start.y < 0 || start.y >= (int) fdf->img->height)
@ -86,15 +77,15 @@ void fdf_draw_line(t_fdf *fdf, 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(fdf, end, start);
draw_line_low(fdf, end, start);
else
fdf_draw_line_low(fdf, start, end);
draw_line_low(fdf, start, end);
}
else
{
if (start.y > end.y)
fdf_draw_line_high(fdf, end, start);
draw_line_high(fdf, end, start);
else
fdf_draw_line_high(fdf, start, end);
draw_line_high(fdf, start, end);
}
}

25
src/draw/draw_menu.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* draw_menu.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:20 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:20 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
mlx_image_t *draw_menu(t_fdf *fdf)
{
mlx_texture_t *texture;
mlx_image_t *img;
texture = mlx_load_png("menu.png");
img = mlx_texture_to_image(fdf->mlx, texture);
mlx_delete_texture(texture);
mlx_image_to_window(fdf->mlx, img, 10, 10);
return (img);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* get_gradient_color.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:22 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:22 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
int get_gradient_color(double z, int z_max)
{
const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3ff,
0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff,
0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff, 0xffffffff};
return (colors[(int)(z * 12 / z_max)]);
}

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* get_pixel_color.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:23 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:23 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void get_pixel_color(t_fdf *fdf, t_point_2d *point)
{
if (fdf->colormode == COLOR_MODE_DEFAULT)
point->color = point->color;
else if (fdf->colormode == COLOR_MODE_Z)
point->color = get_z_color(point->orig_z, fdf->map->z_max);
else if (fdf->colormode == COLOR_MODE_GRADIENT)
point->color = get_gradient_color(point->orig_z, fdf->map->z_max);
}

30
src/draw/get_z_color.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* get_z_color.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:24 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:24 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
int get_z_color(double z, int z_max)
{
int red;
int green;
int blue;
red = 0;
green = 0;
blue = 0;
if (z < 0)
blue = 127;
else
{
red = 255 * z / z_max;
green = 255 - red;
}
return (red << 24 | green << 16 | blue << 8 | 0x00FF);
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* interpolate_color.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:25 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:25 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current)
{
int dcur;
int dtotal;
int red;
int green;
int blue;
dcur = fmax(abs(current.x - start.x), abs(current.y - start.y));
dtotal = fmax(abs(end.x - start.x), abs(end.y - start.y));
if (dtotal == 0)
return (start.color);
red = (int)((1 - (double) dcur / dtotal) * (start.color >> 24 & 0xFF)
+ (double) dcur / dtotal * (end.color >> 24 & 0xFF));
green = (int)((1 - (double) dcur / dtotal) * (start.color >> 16 & 0xFF)
+ (double) dcur / dtotal * (end.color >> 16 & 0xFF));
blue = (int)((1 - (double) dcur / dtotal) * (start.color >> 8 & 0xFF)
+ (double) dcur / dtotal * (end.color >> 8 & 0xFF));
return (red << 24 | green << 16 | blue << 8 | 0xFF);
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* parse_color_string.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:11 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:25 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
int parse_color_string(char *str)
{
int color;
int i;
char *color_str;
color = 0;
i = 1;
color_str = ft_strchr(str, ',');
if (!color_str || *++color_str != '0' || *++color_str != 'x')
return (0xFFFFFFFF);
while (color_str[i])
{
if (ft_isdigit(color_str[i]))
color = color * 16 + color_str[i] - '0';
else if (ft_tolower(color_str[i]) >= 'a'
&& ft_tolower(color_str[i]) <= 'f')
color = color * 16 + ft_tolower(color_str[i]) - 'a' + 10;
else
break ;
i++;
}
return ((color << 8) + 0xFF);
}

10
src/draw/prepare_draw.c Normal file
View File

@ -0,0 +1,10 @@
#include "fdf.h"
void prepare_draw(t_fdf *fdf)
{
fdf_set_background(fdf->img, 0x000000FF);
if (fdf->animate_z)
fdf->angle_z += deg2rad(fdf->animate_z);
apply_rotation(fdf);
project(fdf);
}

22
src/draw/put_pixel.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* put_pixel.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:26 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:26 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
bool put_pixel(t_fdf *fdf, t_point_2d point)
{
if (point.x < 0 || point.x >= (int) fdf->img->width
|| point.y < 0 || point.y >= (int) fdf->img->height)
return (false);
mlx_put_pixel(fdf->img, point.x, point.y, point.color);
return (true);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:20 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:20 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:27 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/06 11:07:30 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:10 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:28 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
@ -17,9 +17,9 @@ int main(int argc, char *argv[])
t_fdf *fdf;
fdf = initialise_fdf();
if (argc != 2 )
if (argc != 2)
handle_error(fdf, "Usage: ./fdf <filename>");
if(!check_filename(argv[1]))
if (!check_filename(argv[1]))
handle_error(fdf, "Error: wrong file extension");
if (!parse_map(argv[1], fdf))
handle_error(fdf, "Error: failed to parse map");

View File

@ -1,99 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* fdf_color.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:11 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:11 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
int parse_color_string(char *str)
{
int color;
int i;
char *color_str;
color = 0;
i = 1;
color_str = ft_strchr(str, ',');
if (!color_str || *++color_str != '0' || *++color_str != 'x')
return (0xFFFFFFFF);
while (color_str[i])
{
if (ft_isdigit(color_str[i]))
color = color * 16 + color_str[i] - '0';
else if (ft_tolower(color_str[i]) >= 'a'
&& ft_tolower(color_str[i]) <= 'f')
color = color * 16 + ft_tolower(color_str[i]) - 'a' + 10;
else
break ;
i++;
}
return ((color << 8) + 0xFF);
}
void get_pixel_color(t_fdf *fdf, t_point_2d *point)
{
if (fdf->colormode == COLOR_MODE_DEFAULT)
point->color = point->color;
else if (fdf->colormode == COLOR_MODE_Z)
point->color = get_z_color(point->orig_z, fdf->map->z_max);
else if (fdf->colormode == COLOR_MODE_GRADIENT)
point->color = get_gradient_color(point->orig_z, fdf->map->z_max);
}
int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current)
{
int dcur;
int dtotal;
int red;
int green;
int blue;
dcur = fmax(abs(current.x - start.x), abs(current.y - start.y));
dtotal = fmax(abs(end.x - start.x), abs(end.y - start.y));
if (dtotal == 0)
return (start.color);
red = (int)((1 - (double) dcur / dtotal) * (start.color >> 24 & 0xFF)
+ (double) dcur / dtotal * (end.color >> 24 & 0xFF));
green = (int)((1 - (double) dcur / dtotal) * (start.color >> 16 & 0xFF)
+ (double) dcur / dtotal * (end.color >> 16 & 0xFF));
blue = (int)((1 - (double) dcur / dtotal) * (start.color >> 8 & 0xFF)
+ (double) dcur / dtotal * (end.color >> 8 & 0xFF));
return (red << 24 | green << 16 | blue << 8 | 0xFF);
}
int get_z_color(double z, int z_max)
{
int red;
int green;
int blue;
red = 0;
green = 0;
blue = 0;
if(z < 0)
blue = 127;
else
{
red = 255 * z / z_max;
green = 255 - red;
}
return (red << 24 | green << 16 | blue << 8 | 0x00FF);
}
int get_gradient_color(double z, int z_max)
{
const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3ff,
0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff,
0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff,0xffffffff};
return (colors[(int)(z * 12 / z_max)]);
}

View File

@ -1,163 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* fdf_hooks.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:12 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:12 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
#include "MLX42.h"
int fdf_hooks(t_fdf *fdf)
{
mlx_loop_hook(fdf->mlx, draw_hook, fdf);
mlx_key_hook(fdf->mlx, key_hook, fdf);
mlx_close_hook(fdf->mlx, close_hook, fdf);
mlx_resize_hook(fdf->mlx, resize_hook, fdf);
return (1);
}
void resize_hook(int width, int height, void *param)
{
t_fdf *fdf;
(void)width;
(void)height;
fdf = (t_fdf *)param;
mlx_delete_image(fdf->mlx, fdf->img);
fdf->img = mlx_new_image(fdf->mlx, fdf->mlx->width, fdf->mlx->height);
if (!fdf->img
|| (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0)
|| (mlx_image_to_window(fdf->mlx, fdf->menu, 10, 10) < 0))
exit(1);
fdf->offset_x += (fdf->mlx->width - fdf->last_width) / 2 ;
fdf->offset_y += (fdf->mlx->height - fdf->last_height) / 2;
fdf->zoom = 0;
fdf->last_height = fdf->mlx->height;
fdf->last_width = fdf->mlx->width;
}
void draw_hook(void *param)
{
t_fdf *fdf;
int i;
fdf = (t_fdf *)param;
if (fdf->last_width != fdf->mlx->width
|| fdf->last_height != fdf->mlx->height)
{
resize_hook(fdf->mlx->width, fdf->mlx->height, fdf);
return ;
}
fdf_set_background(fdf->img, 0x000000FF);
if (fdf->animate_z)
fdf->angle_z += deg2rad(fdf->animate_z);
fdf_apply_rotation(fdf);
project(fdf);
i = 0;
while (i < fdf->map->width * fdf->map->height)
{
fdf_put_pixel(fdf, fdf->map->proj[i]);
if (i % fdf->map->width != fdf->map->width - 1)
fdf_draw_line(fdf, fdf->map->proj[i], fdf->map->proj[i + 1]);
if (i / fdf->map->width != fdf->map->height - 1)
fdf_draw_line(fdf, fdf->map->proj[i], fdf->map->proj[i + fdf->map->width]);
i++;
}
// draw_menu(fdf);
}
mlx_image_t *draw_menu(t_fdf *fdf)
{
// const int x = 20;
// const int line_height = 25;
// int y;
mlx_texture_t *texture;
mlx_image_t *img;
// y = 1;
texture = mlx_load_png("menu.png");
img = mlx_texture_to_image(fdf->mlx, texture);
mlx_delete_texture(texture);
mlx_image_to_window(fdf->mlx, img, 10, 10);
// mlx_put_string(fdf->mlx, "[?] Help menu", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[a] / [d] Rotate around Z-axis", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[w] / [s] Rotate around X-axis", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[q] / [e] Rotate around Y-axis", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[z] / [x] Change Z-scale", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[=] / [-] Change Zoom", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[UP] / [DOWN] Change X-offset", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[LEFT] / [RIGHT] Change Y-offset", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[[] / []] Change animation speed", x, y++ * line_height);
// mlx_put_string(fdf->mlx, "[ESC] Close window", x, y++ * line_height);
return (img);
}
void close_hook(void *param)
{
t_fdf *fdf;
fdf = (t_fdf *)param;
mlx_close_window(fdf->mlx);
mlx_delete_image(fdf->mlx, fdf->img);
mlx_delete_image(fdf->mlx, fdf->menu);
mlx_terminate(fdf->mlx);
clean_fdf(fdf);
exit(0);
}
void key_hook(mlx_key_data_t keydata, void *param)
{
t_fdf *fdf;
fdf = (t_fdf *)param;
if (keydata.key == MLX_KEY_ESCAPE)
close_hook(fdf);
if (keydata.key == MLX_KEY_A && keydata.action != MLX_RELEASE)
fdf->angle_z -= deg2rad(5);
if (keydata.key == MLX_KEY_D && keydata.action != MLX_RELEASE)
fdf->angle_z += deg2rad(5);
if (keydata.key == MLX_KEY_W && keydata.action != MLX_RELEASE)
fdf->angle_x -= deg2rad(5);
if (keydata.key == MLX_KEY_S && keydata.action != MLX_RELEASE)
fdf->angle_x += deg2rad(5);
if (keydata.key == MLX_KEY_Q && keydata.action != MLX_RELEASE)
fdf->angle_y -= deg2rad(5);
if (keydata.key == MLX_KEY_E && keydata.action != MLX_RELEASE)
fdf->angle_y += deg2rad(5);
if (keydata.key == MLX_KEY_X && keydata.action != MLX_RELEASE)
fdf->z_scale *= 1.05;
if (keydata.key == MLX_KEY_Z && keydata.action != MLX_RELEASE)
fdf->z_scale /= 1.05;
if (keydata.key == MLX_KEY_UP && keydata.action != MLX_RELEASE)
fdf->offset_y -= 10;
if (keydata.key == MLX_KEY_DOWN && keydata.action != MLX_RELEASE)
fdf->offset_y += 10;
if (keydata.key == MLX_KEY_LEFT && keydata.action != MLX_RELEASE)
fdf->offset_x -= 10;
if (keydata.key == MLX_KEY_RIGHT && keydata.action != MLX_RELEASE)
fdf->offset_x += 10;
if (keydata.key == MLX_KEY_EQUAL && keydata.action != MLX_RELEASE)
fdf->zoom *= 1.1;
if (keydata.key == MLX_KEY_MINUS && keydata.action != MLX_RELEASE)
fdf->zoom /= 1.1;
if (keydata.key == MLX_KEY_LEFT_BRACKET && keydata.action == MLX_PRESS)
fdf->animate_z -= 0.5;
if (keydata.key == MLX_KEY_RIGHT_BRACKET && keydata.action == MLX_PRESS)
fdf->animate_z += 0.5;
if (keydata.key == MLX_KEY_SLASH && keydata.action == MLX_PRESS)
fdf->menu->enabled = !fdf->menu->enabled;
if (keydata.key == MLX_KEY_C && keydata.action == MLX_PRESS)
fdf->colormode = (fdf->colormode + 1) % 3;
if (keydata.key == MLX_KEY_P && keydata.action == MLX_PRESS)
fdf->projection = (fdf->projection + 1) % 3;
if (keydata.key == MLX_KEY_SPACE && keydata.action == MLX_PRESS)
reset_fdf(fdf);
}

View File

@ -1,83 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* fdf_rotations.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:13 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:13 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void fdf_apply_rotation(t_fdf *fdf)
{
copy_map(fdf);
rotate_x(&fdf->map->rot, fdf->angle_x, fdf->map->width * fdf->map->height);
rotate_y(&fdf->map->rot, fdf->angle_y, fdf->map->width * fdf->map->height);
rotate_z(&fdf->map->rot, fdf->angle_z, fdf->map->width * fdf->map->height);
}
void copy_map(t_fdf *fdf)
{
int i;
i = 0;
while (i < fdf->map->width * fdf->map->height)
{
fdf->map->rot[i].x = fdf->map->orig[i].x;
fdf->map->rot[i].y = fdf->map->orig[i].y;
fdf->map->rot[i].z = fdf->map->orig[i].z * fdf->z_scale;
fdf->map->rot[i].color = fdf->map->orig[i].color;
i++;
}
}
void rotate_x(t_point_3d **points, double angle, int size)
{
int i;
double previous_y;
i = 0;
while (i < size)
{
previous_y = (*points)[i].y;
(*points)[i].y = previous_y * cos(angle) + (*points)[i].z * sin(angle);
(*points)[i].z = -previous_y * sin(angle) + (*points)[i].z * cos(angle);
i++;
}
}
void rotate_y(t_point_3d **points, double angle, int size)
{
int i;
double previous_x;
i = 0;
while (i < size)
{
previous_x = (*points)[i].x;
(*points)[i].x = previous_x * cos(angle) + (*points)[i].z * sin(angle);
(*points)[i].z = -previous_x * sin(angle) + (*points)[i].z * cos(angle);
i++;
}
}
void rotate_z(t_point_3d **points, double angle, int size)
{
int i;
double previous_x;
double previous_y;
i = 0;
while (i < size)
{
previous_x = (*points)[i].x;
previous_y = (*points)[i].y;
(*points)[i].x = previous_x * cos(angle) - previous_y * sin(angle);
(*points)[i].y = previous_x * sin(angle) + previous_y * cos(angle);
i++;
}
}

26
src/hooks/close_hook.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* close_hook.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:28 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:28 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void close_hook(void *param)
{
t_fdf *fdf;
fdf = (t_fdf *)param;
mlx_close_window(fdf->mlx);
mlx_delete_image(fdf->mlx, fdf->img);
mlx_delete_image(fdf->mlx, fdf->menu);
mlx_terminate(fdf->mlx);
clean_fdf(fdf);
exit(0);
}

39
src/hooks/draw_hook.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* draw_hook.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:29 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:29 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void draw_hook(void *param)
{
t_fdf *fdf;
int i;
fdf = (t_fdf *)param;
if (fdf->last_width != fdf->mlx->width
|| fdf->last_height != fdf->mlx->height)
{
resize_hook(fdf->mlx->width, fdf->mlx->height, fdf);
return ;
}
prepare_draw(fdf);
i = 0;
while (i < fdf->map->width * fdf->map->height)
{
put_pixel(fdf, fdf->map->proj[i]);
if (i % fdf->map->width != fdf->map->width - 1)
draw_line(fdf, fdf->map->proj[i], fdf->map->proj[i + 1]);
if (i / fdf->map->width != fdf->map->height - 1)
draw_line(fdf, fdf->map->proj[i],
fdf->map->proj[i + fdf->map->width]);
i++;
}
}

23
src/hooks/fdf_hooks.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* fdf_hooks.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:12 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:30 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
#include "MLX42.h"
int fdf_hooks(t_fdf *fdf)
{
mlx_loop_hook(fdf->mlx, draw_hook, fdf);
mlx_key_hook(fdf->mlx, key_hook, fdf);
mlx_close_hook(fdf->mlx, close_hook, fdf);
mlx_resize_hook(fdf->mlx, resize_hook, fdf);
return (1);
}

22
src/hooks/key_hook.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* key_hook.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:30 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:30 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void key_hook(mlx_key_data_t keydata, void *param)
{
t_fdf *fdf;
fdf = (t_fdf *)param;
key_hook_transform(keydata, fdf);
key_hook_options(keydata, fdf);
}

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* key_hook_options.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:40:59 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:40:59 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void key_hook_options(mlx_key_data_t keydata, t_fdf *fdf)
{
if (keydata.key == MLX_KEY_X && keydata.action != MLX_RELEASE)
fdf->z_scale *= 1.05;
if (keydata.key == MLX_KEY_Z && keydata.action != MLX_RELEASE)
fdf->z_scale /= 1.05;
if (keydata.key == MLX_KEY_EQUAL && keydata.action != MLX_RELEASE)
fdf->zoom *= 1.1;
if (keydata.key == MLX_KEY_MINUS && keydata.action != MLX_RELEASE)
fdf->zoom /= 1.1;
if (keydata.key == MLX_KEY_SLASH && keydata.action == MLX_PRESS)
fdf->menu->enabled = !fdf->menu->enabled;
if (keydata.key == MLX_KEY_C && keydata.action == MLX_PRESS)
fdf->colormode = (fdf->colormode + 1) % 3;
if (keydata.key == MLX_KEY_P && keydata.action == MLX_PRESS)
fdf->projection = (fdf->projection + 1) % 3;
if (keydata.key == MLX_KEY_SPACE && keydata.action == MLX_PRESS)
reset_fdf(fdf);
if (keydata.key == MLX_KEY_LEFT_BRACKET && keydata.action == MLX_PRESS)
fdf->animate_z -= 0.5;
if (keydata.key == MLX_KEY_RIGHT_BRACKET && keydata.action == MLX_PRESS)
fdf->animate_z += 0.5;
if (keydata.key == MLX_KEY_ESCAPE)
close_hook(fdf);
}

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* key_hook_transform.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:41:05 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:41:05 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void key_hook_transform(mlx_key_data_t keydata, t_fdf *fdf)
{
if (keydata.key == MLX_KEY_A && keydata.action != MLX_RELEASE)
fdf->angle_z -= deg2rad(5);
if (keydata.key == MLX_KEY_D && keydata.action != MLX_RELEASE)
fdf->angle_z += deg2rad(5);
if (keydata.key == MLX_KEY_W && keydata.action != MLX_RELEASE)
fdf->angle_x -= deg2rad(5);
if (keydata.key == MLX_KEY_S && keydata.action != MLX_RELEASE)
fdf->angle_x += deg2rad(5);
if (keydata.key == MLX_KEY_Q && keydata.action != MLX_RELEASE)
fdf->angle_y -= deg2rad(5);
if (keydata.key == MLX_KEY_E && keydata.action != MLX_RELEASE)
fdf->angle_y += deg2rad(5);
if (keydata.key == MLX_KEY_UP && keydata.action != MLX_RELEASE)
fdf->offset_y -= 10;
if (keydata.key == MLX_KEY_DOWN && keydata.action != MLX_RELEASE)
fdf->offset_y += 10;
if (keydata.key == MLX_KEY_LEFT && keydata.action != MLX_RELEASE)
fdf->offset_x -= 10;
if (keydata.key == MLX_KEY_RIGHT && keydata.action != MLX_RELEASE)
fdf->offset_x += 10;
}

33
src/hooks/resize_hook.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* resize_hook.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:31 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:31 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void resize_hook(int width, int height, void *param)
{
t_fdf *fdf;
(void)width;
(void)height;
fdf = (t_fdf *)param;
mlx_delete_image(fdf->mlx, fdf->img);
fdf->img = mlx_new_image(fdf->mlx, fdf->mlx->width, fdf->mlx->height);
if (!fdf->img
|| (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0)
|| (mlx_image_to_window(fdf->mlx, fdf->menu, 10, 10) < 0))
exit(1);
fdf->offset_x += (fdf->mlx->width - fdf->last_width) / 2 ;
fdf->offset_y += (fdf->mlx->height - fdf->last_height) / 2;
fdf->zoom = 0;
fdf->last_height = fdf->mlx->height;
fdf->last_width = fdf->mlx->width;
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:14 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:14 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:32 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:15 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:15 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:33 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -1,29 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* load_map_from_file.c :+: / :+::+: :+: */
/* load_map_from_file.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:19 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:19 by whaffman ### ### ### ### / \ */
/* Created: 2024/12/20 11:21:34 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:34 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
int check_filename(char *filename)
{
int i;
i = 0;
while (filename[i])
i++;
if (i < 5 || ft_strncmp(&filename[i - 4], ".fdf", 4))
return (0);
return (1);
}
int load_map_from_file(char *filename, t_fdf *fdf)
{
int fd;
@ -51,11 +39,3 @@ int load_map_from_file(char *filename, t_fdf *fdf)
close(fd);
return (1);
}
void set_map_point(t_fdf *fdf, int x, int y, char **str)
{
fdf->map->orig[y * fdf->map->width + x].x = x - fdf->map->width / 2;
fdf->map->orig[y * fdf->map->width + x].y = fdf->map->height / 2 - y;
fdf->map->orig[y * fdf->map->width + x].z = ft_atoi(str[x]);
fdf->map->orig[y * fdf->map->width + x].color = parse_color_string(str[x]);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:18 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:18 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:34 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

21
src/parse/set_map_point.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* set_map_point.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:45 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:45 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void set_map_point(t_fdf *fdf, int x, int y, char **str)
{
fdf->map->orig[y * fdf->map->width + x].x = x - fdf->map->width / 2;
fdf->map->orig[y * fdf->map->width + x].y = fdf->map->height / 2 - y;
fdf->map->orig[y * fdf->map->width + x].z = ft_atoi(str[x]);
fdf->map->orig[y * fdf->map->width + x].color = parse_color_string(str[x]);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* apply_rotation.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:46 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:46 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void apply_rotation(t_fdf *fdf)
{
int i;
i = 0;
while (i < fdf->map->width * fdf->map->height)
{
fdf->map->rot[i].x = fdf->map->orig[i].x;
fdf->map->rot[i].y = fdf->map->orig[i].y;
fdf->map->rot[i].z = fdf->map->orig[i].z * fdf->z_scale;
fdf->map->rot[i].color = fdf->map->orig[i].color;
i++;
}
rotate_x(&fdf->map->rot, fdf->angle_x, fdf->map->width * fdf->map->height);
rotate_y(&fdf->map->rot, fdf->angle_y, fdf->map->width * fdf->map->height);
rotate_z(&fdf->map->rot, fdf->angle_z, fdf->map->width * fdf->map->height);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:10 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:10 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:47 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

23
src/transform/project.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* project.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:47 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:47 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void project(t_fdf *fdf)
{
if (fdf->projection == PROJECTION_ISOMETRIC)
project_isometric(fdf);
else if (fdf->projection == PROJECTION_PARALLEL)
project_parallel(fdf);
else if (fdf->projection == PROJECTION_TRIMETRIC)
project_trimetric(fdf);
}

View File

@ -6,54 +6,12 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:19 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:19 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:48 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void project_trimetric(t_fdf *fdf)
{
int i;
double x;
double y;
i = 0;
if (fdf->zoom == 0)
fdf->zoom = fmin(fdf->mlx->width / fdf->map->width,
fdf->mlx->height / fdf->map->height) / 2;
while (i < fdf->map->width * fdf->map->height)
{
x = fdf->map->rot[i].x - fdf->map->rot[i].y;
y = fdf->map->rot[i].x + fdf->map->rot[i].y - fdf->map->rot[i].z;
fdf->map->proj[i].x = x * fdf->zoom + fdf->offset_x;
fdf->map->proj[i].y = y * fdf->zoom + fdf->offset_y;
fdf->map->proj[i].orig_z = fdf->map->orig[i].z;
fdf->map->proj[i].color = fdf->map->orig[i].color;
get_pixel_color(fdf, &fdf->map->proj[i]);
i++;
}
}
void project_parallel(t_fdf *fdf)
{
int i;
i = 0;
if (fdf->zoom == 0)
fdf->zoom = fmin(fdf->mlx->width / fdf->map->width,
fdf->mlx->height / fdf->map->height) / 2;
while (i < fdf->map->width * fdf->map->height)
{
fdf->map->proj[i].x = fdf->map->rot[i].x * fdf->zoom + fdf->offset_x;
fdf->map->proj[i].y = fdf->map->rot[i].y * fdf->zoom + fdf->offset_y;
fdf->map->proj[i].orig_z = fdf->map->orig[i].z;
fdf->map->proj[i].color = fdf->map->orig[i].color;
get_pixel_color(fdf, &fdf->map->proj[i]);
i++;
}
}
void project_isometric(t_fdf *fdf)
{
int i;
@ -77,13 +35,3 @@ void project_isometric(t_fdf *fdf)
i++;
}
}
void project(t_fdf *fdf)
{
if (fdf->projection == PROJECTION_ISOMETRIC)
project_isometric(fdf);
else if (fdf->projection == PROJECTION_PARALLEL)
project_parallel(fdf);
else if (fdf->projection == PROJECTION_TRIMETRIC)
project_trimetric(fdf);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* project_parallel.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:50 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:50 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void project_parallel(t_fdf *fdf)
{
int i;
i = 0;
if (fdf->zoom == 0)
fdf->zoom = fmin(fdf->mlx->width / fdf->map->width,
fdf->mlx->height / fdf->map->height) / 2;
while (i < fdf->map->width * fdf->map->height)
{
fdf->map->proj[i].x = fdf->map->rot[i].x * fdf->zoom + fdf->offset_x;
fdf->map->proj[i].y = fdf->map->rot[i].y * fdf->zoom + fdf->offset_y;
fdf->map->proj[i].orig_z = fdf->map->orig[i].z;
fdf->map->proj[i].color = fdf->map->orig[i].color;
get_pixel_color(fdf, &fdf->map->proj[i]);
i++;
}
}

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* project_trimetric.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:51 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:51 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void project_trimetric(t_fdf *fdf)
{
int i;
double x;
double y;
i = 0;
if (fdf->zoom == 0)
fdf->zoom = fmin(fdf->mlx->width / fdf->map->width,
fdf->mlx->height / fdf->map->height) / 2;
while (i < fdf->map->width * fdf->map->height)
{
x = fdf->map->rot[i].x - fdf->map->rot[i].y;
y = fdf->map->rot[i].x + fdf->map->rot[i].y - fdf->map->rot[i].z;
fdf->map->proj[i].x = x * fdf->zoom + fdf->offset_x;
fdf->map->proj[i].y = y * fdf->zoom + fdf->offset_y;
fdf->map->proj[i].orig_z = fdf->map->orig[i].z;
fdf->map->proj[i].color = fdf->map->orig[i].color;
get_pixel_color(fdf, &fdf->map->proj[i]);
i++;
}
}

28
src/transform/rotate_x.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* rotate_x.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:52 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:52 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void rotate_x(t_point_3d **points, double angle, int size)
{
int i;
double previous_y;
i = 0;
while (i < size)
{
previous_y = (*points)[i].y;
(*points)[i].y = previous_y * cos(angle) + (*points)[i].z * sin(angle);
(*points)[i].z = -previous_y * sin(angle) + (*points)[i].z * cos(angle);
i++;
}
}

28
src/transform/rotate_y.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* rotate_y.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:53 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:53 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void rotate_y(t_point_3d **points, double angle, int size)
{
int i;
double previous_x;
i = 0;
while (i < size)
{
previous_x = (*points)[i].x;
(*points)[i].x = previous_x * cos(angle) + (*points)[i].z * sin(angle);
(*points)[i].z = -previous_x * sin(angle) + (*points)[i].z * cos(angle);
i++;
}
}

30
src/transform/rotate_z.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* rotate_z.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:55 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:55 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void rotate_z(t_point_3d **points, double angle, int size)
{
int i;
double previous_x;
double previous_y;
i = 0;
while (i < size)
{
previous_x = (*points)[i].x;
previous_y = (*points)[i].y;
(*points)[i].x = previous_x * cos(angle) - previous_y * sin(angle);
(*points)[i].y = previous_x * sin(angle) + previous_y * cos(angle);
i++;
}
}

25
src/util/check_filename.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* check_filename.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:21:56 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:21:56 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
int check_filename(char *filename)
{
int i;
i = 0;
while (filename[i])
i++;
if (i < 5 || ft_strncmp(&filename[i - 4], ".fdf", 4))
return (0);
return (1);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:15:51 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:09 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:57 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:14 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:14 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:58 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:16 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:16 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:21:59 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:17 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:17 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:22:00 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
@ -15,7 +15,7 @@
int init_mlx(t_fdf *fdf)
{
mlx_t *mlx;
mlx = mlx_init(WIDTH, HEIGHT, "FdF", true);
if (!mlx)
handle_error(fdf, "Error: failed to initialise MLX");

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/13 15:23:17 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:17 by whaffman ### ### ### ### / \ */
/* Updated: 2024/12/20 11:22:00 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
@ -26,7 +26,6 @@ t_fdf *initialise_fdf(void)
fdf->map = map;
fdf->mlx = NULL;
fdf->img = NULL;
fdf->last_width = WIDTH;
fdf->last_height = HEIGHT;
fdf->map->orig = NULL;
@ -34,29 +33,7 @@ t_fdf *initialise_fdf(void)
fdf->map->proj = NULL;
fdf->map->width = 0;
fdf->map->height = 0;
fdf->map->z_max = 0;
fdf->map->z_max = 0;
reset_fdf(fdf);
return (fdf);
}
void reset_fdf(t_fdf *fdf)
{
fdf->offset_x = WIDTH / 2;
fdf->offset_y = HEIGHT / 2;
if (fdf->mlx)
{
fdf->offset_x = fdf->mlx->width / 2;
fdf->offset_y = fdf->mlx->height / 2;
}
fdf->angle_x = 0;
fdf->angle_y = 0;
fdf->angle_z = 0;
fdf->zoom = 0;
fdf->z_scale = 0.1;
fdf->animate_z = 0;
fdf->colormode = COLOR_MODE_DEFAULT;
fdf->projection = PROJECTION_ISOMETRIC;
}

32
src/util/reset_fdf.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* reset_fdf.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/20 11:22:01 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/20 11:22:01 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
void reset_fdf(t_fdf *fdf)
{
fdf->offset_x = WIDTH / 2;
fdf->offset_y = HEIGHT / 2;
if (fdf->mlx)
{
fdf->offset_x = fdf->mlx->width / 2;
fdf->offset_y = fdf->mlx->height / 2;
}
fdf->angle_x = 0;
fdf->angle_y = 0;
fdf->angle_z = 0;
fdf->zoom = 0;
fdf->z_scale = 0.1;
fdf->animate_z = 0;
fdf->colormode = COLOR_MODE_DEFAULT;
fdf->projection = PROJECTION_ISOMETRIC;
}