Compare commits

..

2 Commits

Author SHA1 Message Date
whaffman
7e155d1166 double def of colo pixel func 2024-12-19 15:40:36 +01:00
whaffman
76d02c869f merge home en codam 2024-12-19 15:39:53 +01:00
8 changed files with 62 additions and 37 deletions

View File

@ -58,7 +58,7 @@ $(MLX42_DIR):
git submodule add https://github.com/codam-coding-college/MLX42.git $(MLX42_PATH); git submodule add https://github.com/codam-coding-college/MLX42.git $(MLX42_PATH);
$(LIBFT): $(LIBFT_PATH) $(LIBFT): $(LIBFT_PATH)
$(MAKE) -sC $(LIBFT_PATH) $(MAKE) -C $(LIBFT_PATH)
$(LIBFT_PATH): $(LIBFT_PATH):
git submodule add git@duinvoetje.nl:willem/libft.git $(LIBFT_PATH) git submodule add git@duinvoetje.nl:willem/libft.git $(LIBFT_PATH)
@ -72,6 +72,7 @@ $(OBJ_PATH)/%.o: %.c $(LIBFT) $(MLX42) | $(OBJ_PATH)
clean: clean:
$(RM) $(OBJECTS) $(OBJ_PATH) $(RM) $(OBJECTS) $(OBJ_PATH)
$(MAKE) -C $(LIBFT_PATH) clean $(MAKE) -C $(LIBFT_PATH) clean
$(MAKE) -C $(MLX42_PATH)/build clean
fclean: clean fclean: clean
$(RM) $(NAME) $(RM) $(NAME)

View File

@ -134,11 +134,12 @@ void set_map_point(t_fdf *fdf, int x, int y, char **str);
mlx_image_t *draw_menu(t_fdf *fdf); mlx_image_t *draw_menu(t_fdf *fdf);
int get_z_color(double z, int z_max); int get_z_color(double z, int z_max);
void close_hook(void *param); void close_hook(void *param);
int get_gradient_color(double z, int z_max); int get_gradient_color(double z, int z_max);
void project_parallel(t_fdf *fdf); void project_parallel(t_fdf *fdf);
void project(t_fdf *fdf); void project(t_fdf *fdf);
int get_pixel_color(t_fdf *fdf, t_point_3d point); int check_filename(char *filename);
int check_filename(char *filename); void get_pixel_color(t_fdf *fdf, t_point_2d *point);
void reset_fdf(t_fdf *fdf);

View File

@ -37,6 +37,17 @@ int parse_color_string(char *str)
return ((color << 8) + 0xFF); 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 interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current)
{ {
int dcur; int dcur;
@ -79,19 +90,10 @@ int get_z_color(double z, int z_max)
int get_gradient_color(double z, int z_max) int get_gradient_color(double z, int z_max)
{ {
const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3f, const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3ff,
0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff, 0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff,
0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff,0xffffffff}; 0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff,0xffffffff};
return (colors[(int)(z * 12 / z_max)]); return (colors[(int)(z * 12 / z_max)]);
} }
int get_pixel_color(t_fdf *fdf, t_point_3d point)
{
if (fdf->colormode == COLOR_MODE_Z)
return (get_z_color(point.z, fdf->map->z_max));
else if (fdf->colormode == COLOR_MODE_GRADIENT)
return (get_gradient_color(point.z, fdf->map->z_max));
else
return (point.color);
}

View File

@ -13,9 +13,6 @@
bool fdf_put_pixel(t_fdf *fdf, t_point_2d point) bool fdf_put_pixel(t_fdf *fdf, t_point_2d point)
{ {
int color;
color = 0xFFFFFFFF;
if (point.x < 0 || point.x >= (int) fdf->img->width if (point.x < 0 || point.x >= (int) fdf->img->width
|| point.y < 0 || point.y >= (int) fdf->img->height) || point.y < 0 || point.y >= (int) fdf->img->height)
return (false); return (false);

View File

@ -101,9 +101,14 @@ mlx_image_t *draw_menu(t_fdf *fdf)
void close_hook(void *param) void close_hook(void *param)
{ {
mlx_close_window(((t_fdf *)param)->mlx); t_fdf *fdf;
mlx_terminate(((t_fdf *)param)->mlx);
clean_fdf((t_fdf *)param); 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); exit(0);
} }
@ -152,5 +157,7 @@ void key_hook(mlx_key_data_t keydata, void *param)
fdf->colormode = (fdf->colormode + 1) % 3; fdf->colormode = (fdf->colormode + 1) % 3;
if (keydata.key == MLX_KEY_P && keydata.action == MLX_PRESS) if (keydata.key == MLX_KEY_P && keydata.action == MLX_PRESS)
fdf->projection = (fdf->projection + 1) % 3; fdf->projection = (fdf->projection + 1) % 3;
if (keydata.key == MLX_KEY_SPACE && keydata.action == MLX_PRESS)
reset_fdf(fdf);
} }

View File

@ -15,7 +15,7 @@
int init_mlx(t_fdf *fdf) int init_mlx(t_fdf *fdf)
{ {
mlx_t *mlx; mlx_t *mlx;
mlx = mlx_init(WIDTH, HEIGHT, "FdF", true); mlx = mlx_init(WIDTH, HEIGHT, "FdF", true);
if (!mlx) if (!mlx)
handle_error(fdf, "Error: failed to initialise MLX"); handle_error(fdf, "Error: failed to initialise MLX");

View File

@ -26,23 +26,37 @@ t_fdf *initialise_fdf(void)
fdf->map = map; fdf->map = map;
fdf->mlx = NULL; fdf->mlx = NULL;
fdf->img = NULL; fdf->img = NULL;
fdf->angle_x = 0;
fdf->angle_y = 0;
fdf->angle_z = 0;
fdf->zoom = 0;
fdf->animate_z = 0;
fdf->offset_x = WIDTH / 2;
fdf->offset_y = HEIGHT / 2;
fdf->z_scale = 0.1;
fdf->last_width = WIDTH; fdf->last_width = WIDTH;
fdf->last_height = HEIGHT; fdf->last_height = HEIGHT;
fdf->colormode = COLOR_MODE_DEFAULT;
fdf->projection = PROJECTION_ISOMETRIC;
fdf->map->orig = NULL; fdf->map->orig = NULL;
fdf->map->rot = NULL; fdf->map->rot = NULL;
fdf->map->proj = NULL; fdf->map->proj = NULL;
fdf->map->width = 0; fdf->map->width = 0;
fdf->map->height = 0; fdf->map->height = 0;
fdf->map->z_max = 0; fdf->map->z_max = 0;
reset_fdf(fdf);
return (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;
}

View File

@ -29,7 +29,8 @@ void project_trimetric(t_fdf *fdf)
fdf->map->proj[i].x = x * fdf->zoom + fdf->offset_x; 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].y = y * fdf->zoom + fdf->offset_y;
fdf->map->proj[i].orig_z = fdf->map->orig[i].z; fdf->map->proj[i].orig_z = fdf->map->orig[i].z;
fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]); fdf->map->proj[i].color = fdf->map->orig[i].color;
get_pixel_color(fdf, &fdf->map->proj[i]);
i++; i++;
} }
} }
@ -47,7 +48,8 @@ void project_parallel(t_fdf *fdf)
fdf->map->proj[i].x = fdf->map->rot[i].x * fdf->zoom + fdf->offset_x; 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].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].orig_z = fdf->map->orig[i].z;
fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]); fdf->map->proj[i].color = fdf->map->orig[i].color;
get_pixel_color(fdf, &fdf->map->proj[i]);
i++; i++;
} }
} }
@ -70,7 +72,8 @@ void project_isometric(t_fdf *fdf)
fdf->map->proj[i].x = x * fdf->zoom + fdf->offset_x; 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].y = y * fdf->zoom + fdf->offset_y;
fdf->map->proj[i].orig_z = fdf->map->orig[i].z; fdf->map->proj[i].orig_z = fdf->map->orig[i].z;
fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]); fdf->map->proj[i].color = fdf->map->orig[i].color;
get_pixel_color(fdf, &fdf->map->proj[i]);
i++; i++;
} }
} }