fixed some norm errors

This commit is contained in:
whaffman 2024-12-13 17:18:20 +01:00
parent 54c6cfb9a3
commit 68e9533b91
14 changed files with 108 additions and 75 deletions

48
MLX42.supp Normal file
View File

@ -0,0 +1,48 @@
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libnvidia*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libdbus*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libglfw*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libX11*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/dri*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libLLVM*
}
{
<MLX42>
Memcheck:Leak
...
fun:_dl_open
}

View File

@ -26,8 +26,7 @@
# define SUCCESS 1
# define FAILURE 0
# 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.\n"
# define MALLOC_ERROR "Error: malloc failed\n"
# define FILE_ERROR "Error: could not open file %s\n"
# define USAGE_ERROR "Usage: %s <filename>\n"
@ -59,7 +58,7 @@ typedef struct s_point_2d
typedef struct s_map
{
t_point_3d *original;
t_point_3d *orig;
t_point_3d *rot;
t_point_2d *proj;
int width;
@ -113,5 +112,6 @@ 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);
#endif

View File

@ -1,8 +0,0 @@
while (i < alle punten)
{
x' = (x - y) / SQRT2;
y' = (x + y - 2 * z) / SQRT6;
i++;
}
}

View File

@ -16,7 +16,7 @@ bool clean_fdf(t_fdf *fdf)
{
if (fdf->map)
{
free(fdf->map->original);
free(fdf->map->orig);
free(fdf->map->rot);
free(fdf->map->proj);
free(fdf->map);

View File

@ -16,30 +16,24 @@ int get_color(char *str)
{
int color;
int i;
char *color_str;
color = 0;
i = 0;
if (str[i] && str[i] == '-')
i++;
while (str[i] && ft_isdigit(str[i]))
i++;
i++;
if (str[i] == '0' && str[i + 1] == 'x')
i += 2;
while (str[i])
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(str[i]))
color = color * 16 + str[i] - '0';
else if (str[i] >= 'a' && str[i] <= 'f')
color = color * 16 + str[i] - 'a' + 10;
else if (str[i] >= 'A' && str[i] <= 'F')
color = color * 16 + str[i] - 'A' + 10;
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++;
}
if (color == 0)
return (0xFFFFFFFF);
return ((color << 8) + 0xFF);
}

View File

@ -28,13 +28,10 @@ void fdf_draw_line_low(mlx_image_t *img, t_point_2d start, t_point_2d end)
t_point_2d delta_point;
delta_point.x = end.x - start.x;
delta_point.y = end.y - start.y;
delta_point.y = abs(end.y - start.y);
yi = 1;
if (delta_point.y < 0)
{
if (end.y - start.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)
@ -58,14 +55,11 @@ void fdf_draw_line_high(mlx_image_t *img, t_point_2d start, t_point_2d end)
t_point_2d current;
t_point_2d delta_point;
delta_point.x = end.x - start.x;
delta_point.x = abs(end.x - start.x);
delta_point.y = end.y - start.y;
xi = 1;
if (delta_point.x < 0)
{
if (end.x - start.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)

View File

@ -20,6 +20,7 @@ int fdf_hooks(t_fdf *fdf)
mlx_resize_hook(fdf->mlx, resize_hook, fdf);
return (1);
}
void resize_hook(int width, int height, void *param)
{
t_fdf *fdf;
@ -31,7 +32,7 @@ void resize_hook(int width, int height, void *param)
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))
exit(1);
fdf->offset_x += (fdf->mlx->width - fdf->last_width ) / 2 ;
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;
@ -43,7 +44,6 @@ 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)
@ -67,6 +67,7 @@ void draw_hook(void *param)
i++;
}
}
void key_hook(mlx_key_data_t keydata, void *param)
{
t_fdf *fdf;

View File

@ -27,10 +27,10 @@ void copy_map(t_fdf *fdf)
i = 0;
while (i < fdf->map->width * fdf->map->height)
{
fdf->map->rot[i].x = fdf->map->original[i].x;
fdf->map->rot[i].y = fdf->map->original[i].y;
fdf->map->rot[i].z = fdf->map->original[i].z * fdf->z_scale;
fdf->map->rot[i].color = fdf->map->original[i].color;
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++;
}
}

View File

@ -29,7 +29,7 @@ int get_map_sizes(char *filename, t_map *map)
width = ft_count_words(line, ' ');
free(line);
if (map->width != 0 && map->width != width)
return (ft_printf(WRONG_LINE_LENGTH, map->width, width, map->height), 0);
return (ft_printf(WRONG_LINE_LENGTH), 0);
else if (map->width == 0)
map->width = width;
map->height++;

View File

@ -20,8 +20,8 @@ void get_z_max(t_fdf *fdf)
fdf->map->z_max = 0;
while (i < fdf->map->width * fdf->map->height)
{
if (fdf->map->original[i].z > fdf->map->z_max)
fdf->map->z_max = fdf->map->original[i].z;
if (fdf->map->orig[i].z > fdf->map->z_max)
fdf->map->z_max = fdf->map->orig[i].z;
i++;
}
}

View File

@ -30,18 +30,17 @@ t_fdf *initialise_fdf(void)
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_height = HEIGHT;
fdf->map->original = NULL;
fdf->map->orig = NULL;
fdf->map->rot = NULL;
fdf->map->proj = NULL;
fdf->map->width = 0;
fdf->map->height = 0;
fdf->map->z_max = 0;
return (fdf);
}

View File

@ -14,15 +14,17 @@
int parse_map(char *filename, t_fdf *fdf)
{
int map_size;
if (!get_map_sizes(filename, fdf->map))
handle_error(fdf, "Error: failed to get map sizes");
fdf->map->original = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_3d));
fdf->map->rot = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_3d));
fdf->map->proj = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_2d));
if (!fdf->map->original || !fdf->map->rot || !fdf->map->proj)
map_size = fdf->map->width * fdf->map->height;
fdf->map->orig = malloc(map_size * sizeof(t_point_3d));
fdf->map->rot = malloc(map_size * sizeof(t_point_3d));
fdf->map->proj = malloc(map_size * sizeof(t_point_2d));
if (!fdf->map->orig || !fdf->map->rot || !fdf->map->proj)
handle_error(fdf, MALLOC_ERROR);
if (!read_map(filename, fdf))
handle_error(fdf, "Error: failed to read map");
return (SUCCESS);
}

View File

@ -19,12 +19,14 @@ void project_isometric(t_fdf *fdf)
double y;
i = 0;
if(fdf->zoom == 0)
fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, fdf->mlx->height / fdf->map->height) / SQRT2;
if (fdf->zoom == 0)
fdf->zoom = fmin(fdf->mlx->width / fdf->map->width,
fdf->mlx->height / fdf->map->height) / SQRT2;
while (i < fdf->map->width * fdf->map->height)
{
x = (fdf->map->rot[i].x - fdf->map->rot[i].y) / SQRT2;
y = (fdf->map->rot[i].x + fdf->map->rot[i].y - 2 * fdf->map->rot[i].z) / SQRT6;
y = (fdf->map->rot[i].x + fdf->map->rot[i].y
- 2 * fdf->map->rot[i].z) / SQRT6;
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].color = fdf->map->rot[i].color;

View File

@ -20,7 +20,6 @@ int read_map(char *filename, t_fdf *fdf)
int x;
char **split;
fd = open(filename, O_RDONLY);
if (fd < 0)
handle_error(NULL, FILE_ERROR);
@ -33,16 +32,18 @@ int read_map(char *filename, t_fdf *fdf)
break ;
split = ft_split(line, ' ');
while (split[x])
{
fdf->map->original[y * fdf->map->width + x].x = x - fdf->map->width / 2;
fdf->map->original[y * fdf->map->width + x].y = fdf->map->height / 2 - y;
fdf->map->original[y * fdf->map->width + x].z = ft_atoi(split[x]);
fdf->map->original[y * fdf->map->width + x].color = get_color(split[x]);
x++;
}
set_map_point(fdf, x++, y, split);
free_line_and_split(&line, &split);
y--;
}
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 = get_color(str[x]);
}