moved colormode to projection added filename check

This commit is contained in:
whaffman 2024-12-18 15:53:24 +01:00
parent fc19ce7624
commit 785b542e52
6 changed files with 48 additions and 20 deletions

View File

@ -103,7 +103,7 @@ typedef struct s_fdf
int interpolate_color(t_point_2d start,
t_point_2d end, t_point_2d current);
int get_color(char *str);
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 load_map_from_file(char *filename, t_fdf *fdf);
@ -137,6 +137,9 @@ void close_hook(void *param);
int get_gradient_color(double z, int z_max);
void project_parallel(t_fdf *fdf);
void project(t_fdf *fdf);
int get_pixel_color(t_fdf *fdf, t_point_3d point);
int check_filename(char *filename);
#endif

View File

@ -17,8 +17,10 @@ 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]))
handle_error(fdf, "Error: wrong file extension");
if (!parse_map(argv[1], fdf))
handle_error(fdf, "Error: failed to parse map");
init_mlx(fdf);

View File

@ -12,7 +12,7 @@
#include "fdf.h"
int get_color(char *str)
int parse_color_string(char *str)
{
int color;
int i;
@ -62,12 +62,19 @@ int get_z_color(double z, int z_max)
{
int red;
int green;
int temp;
int blue;
temp = 255 * z / z_max;
red = temp;
green = 255 - temp;
return (red << 24 | green << 16 | 0x00FF);
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)
@ -78,3 +85,13 @@ int get_gradient_color(double z, int 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

@ -19,13 +19,7 @@ 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);
if (fdf->colormode == COLOR_MODE_DEFAULT)
color = point.color;
else if (fdf->colormode == COLOR_MODE_Z)
color = get_z_color(point.orig_z, fdf->map->z_max);
else if (fdf->colormode == COLOR_MODE_GRADIENT)
color = get_gradient_color(point.orig_z, fdf->map->z_max);
mlx_put_pixel(fdf->img, point.x, point.y, color);
mlx_put_pixel(fdf->img, point.x, point.y, point.color);
return (true);
}

View File

@ -29,7 +29,7 @@ void project_trimetric(t_fdf *fdf)
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->rot[i].color;
fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]);
i++;
}
}
@ -47,7 +47,7 @@ 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].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;
fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]);
i++;
}
}
@ -70,7 +70,7 @@ void project_isometric(t_fdf *fdf)
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->rot[i].color;
fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]);
i++;
}
}

View File

@ -12,6 +12,18 @@
#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;
@ -45,5 +57,5 @@ 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 = get_color(str[x]);
fdf->map->orig[y * fdf->map->width + x].color = parse_color_string(str[x]);
}