norm WIP
This commit is contained in:
parent
e03ff1e9ab
commit
c36db1ef4b
@ -6,7 +6,7 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/09 15:05:36 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/12 11:31:51 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -32,6 +32,7 @@
|
||||
|
||||
# define NUM_KEYS 256
|
||||
# define TILE_SIZE 8
|
||||
#define MINIMAP_SIZE 300
|
||||
|
||||
# include "MLX42.h"
|
||||
# include "allowed.h"
|
||||
|
||||
@ -6,13 +6,13 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/25 10:11:44 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/12 11:04:28 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/12 12:11:59 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef VEC_MATH_H
|
||||
#define VEC_MATH_H
|
||||
#include "types.h"
|
||||
# define VEC_MATH_H
|
||||
# include "types.h"
|
||||
|
||||
/**
|
||||
* @brief Calculates the distance between two 2D vectors.
|
||||
@ -118,4 +118,18 @@ t_vec2_int vec2_to_int(t_vec2 vec);
|
||||
*/
|
||||
t_vec2 vec2_from_int(t_vec2_int vec);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Calculates the fractional part of each component of a 2D vector.
|
||||
*
|
||||
* This function takes a 2D vector as input and returns a new vector where
|
||||
* each component is the fractional part of the corresponding component
|
||||
* in the input vector. The fractional part of a number is the part
|
||||
* after the decimal point.
|
||||
*
|
||||
* @param vec The input 2D vector.
|
||||
* @return A 2D vector containing the fractional parts.
|
||||
*/
|
||||
t_vec2 get_fraction(t_vec2 vec);
|
||||
|
||||
#endif // VEC_MATH_H
|
||||
22
src/math/get_fraction.c
Normal file
22
src/math/get_fraction.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* get_fraction.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/12 12:09:27 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/12 12:10:01 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
|
||||
t_vec2 get_fraction(t_vec2 vec)
|
||||
{
|
||||
t_vec2 result;
|
||||
|
||||
result.x = vec.x - (int)vec.x;
|
||||
result.y = vec.y - (int)vec.y;
|
||||
return (result);
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/09 15:20:04 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/12 12:55:30 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -193,7 +193,6 @@ void draw_floor(t_game *game)
|
||||
double row_dist;
|
||||
t_vec2 floor_step;
|
||||
t_vec2 floor_pos;
|
||||
t_vec2_int floor_cell;
|
||||
t_vec2_int tex_coords;
|
||||
const t_vec2 left_ray = sub(game->player->dir, game->player->camera);
|
||||
const t_vec2 right_ray = add(game->player->dir, game->player->camera);
|
||||
@ -207,14 +206,12 @@ void draw_floor(t_game *game)
|
||||
x = 0;
|
||||
while (x < game->screen->width)
|
||||
{
|
||||
floor_cell = (t_vec2_int){(int)floor_pos.x, (int)floor_pos.y};
|
||||
tex_coords = (t_vec2_int){(int)(64 * (floor_pos.x - floor_cell.x)) & 63,
|
||||
(int)(64 * (floor_pos.y - floor_cell.y)) & 63};
|
||||
floor_pos = add(floor_pos, floor_step);
|
||||
tex_coords = vec2_to_int(mul(get_fraction(floor_pos), 64)); // & 63 is gone heap buffer overflow with asan i dont get it
|
||||
color = get_texture_color(game->map->texture_floor, (t_render){row_dist, 0, 0}, tex_coords.x, tex_coords.y);
|
||||
mlx_put_pixel(game->screen->img, x, y, color);
|
||||
color = get_texture_color(game->map->texture_ceiling, (t_render){row_dist, 0, 0}, tex_coords.x, tex_coords.y);
|
||||
mlx_put_pixel(game->screen->img, x, game->screen->height - y - 1, color);
|
||||
floor_pos = add(floor_pos, floor_step);
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
|
||||
@ -1,51 +1,66 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* render_minimap.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/12 11:31:34 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/12 11:31:43 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
|
||||
|
||||
#define MINIMAP_SIZE 300
|
||||
void render_map(t_game *game)
|
||||
static int inside_circle(t_vec2_int point)
|
||||
{
|
||||
t_vec2_int disp;
|
||||
t_vec2 map;
|
||||
const int radius_squared = 0.25 * MINIMAP_SIZE * MINIMAP_SIZE;
|
||||
int dx;
|
||||
int dy;
|
||||
|
||||
disp.x = 0;
|
||||
while (disp.x < MINIMAP_SIZE)
|
||||
{
|
||||
disp.y = 0;
|
||||
while (disp.y < MINIMAP_SIZE)
|
||||
{
|
||||
if (disp.x == 0.5 * MINIMAP_SIZE || disp.y == 0.5 * MINIMAP_SIZE)
|
||||
{
|
||||
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff00dd);
|
||||
disp.y++;
|
||||
continue ;
|
||||
}
|
||||
if ((disp.x - 0.5 * MINIMAP_SIZE) *(disp.x - 0.5 * MINIMAP_SIZE) + (disp.y - 0.5 * MINIMAP_SIZE) * (disp.y - 0.5 * MINIMAP_SIZE) > 0.25* MINIMAP_SIZE * MINIMAP_SIZE)
|
||||
{
|
||||
disp.y++;
|
||||
continue ;
|
||||
}
|
||||
map.x = ((double)disp.x/TILE_SIZE) + game->player->pos.x - (0.5 * MINIMAP_SIZE/TILE_SIZE);
|
||||
map.y = ((double)disp.y/TILE_SIZE) + game->player->pos.y - (0.5 * MINIMAP_SIZE/TILE_SIZE);
|
||||
map = rot_by_dir(map, game->player->dir, game->player->pos);
|
||||
dx = point.x - 0.5 * MINIMAP_SIZE;
|
||||
dy = point.y - 0.5 * MINIMAP_SIZE;
|
||||
return (dx * dx + dy * dy <= radius_squared);
|
||||
}
|
||||
|
||||
static void draw_minimap_pixel(t_game *game, t_vec2_int disp, t_vec2 map)
|
||||
{
|
||||
mlx_image_t *img;
|
||||
|
||||
img = game->screen->minimap;
|
||||
if (map.x < 0 || map.x >= game->map->width
|
||||
|| map.y < 0 || map.y >= game->map->height)
|
||||
{
|
||||
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff0044);
|
||||
|
||||
disp.y++;
|
||||
continue ;
|
||||
}
|
||||
if (game->map->grid[(int)map.y][(int)map.x] == TILE_WALL)
|
||||
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff00ff);
|
||||
mlx_put_pixel(img, disp.x, disp.y, 0x00ff0044);
|
||||
else if (game->map->grid[(int)map.y][(int)map.x] == TILE_WALL)
|
||||
mlx_put_pixel(img, disp.x, disp.y, 0x00ff00ff);
|
||||
else if (game->map->grid[(int)map.y][(int)map.x] == TILE_EMPTY
|
||||
|| game->map->grid[(int)map.y][(int)map.x] == TILE_PLAYER)
|
||||
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff0077);
|
||||
mlx_put_pixel(img, disp.x, disp.y, 0x00ff0077);
|
||||
else
|
||||
mlx_put_pixel(game->screen->minimap, disp.x, disp.y, 0x00ff0044);
|
||||
disp.y++;
|
||||
}
|
||||
disp.x++;
|
||||
mlx_put_pixel(img, disp.x, disp.y, 0x00ff0044);
|
||||
}
|
||||
|
||||
void render_map(t_game *game)
|
||||
{
|
||||
int i;
|
||||
t_vec2_int disp;
|
||||
t_vec2 map;
|
||||
double map_center;
|
||||
|
||||
i = 0;
|
||||
map_center = 0.5 * MINIMAP_SIZE / TILE_SIZE;
|
||||
while (i < MINIMAP_SIZE * MINIMAP_SIZE)
|
||||
{
|
||||
disp.x = i % MINIMAP_SIZE;
|
||||
disp.y = i / MINIMAP_SIZE;
|
||||
if (inside_circle(disp))
|
||||
{
|
||||
map = mul(vec2_from_int(disp), 1.0 / TILE_SIZE);
|
||||
map = add(map, game->player->pos);
|
||||
map = sub(map, (t_vec2){map_center, map_center});
|
||||
map = rot_by_dir(map, game->player->dir, game->player->pos);
|
||||
draw_minimap_pixel(game, disp, map);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/08 12:23:17 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/11 13:26:18 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/12 12:46:26 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -33,37 +33,10 @@ static void sort_sprites(t_game *game)
|
||||
}
|
||||
}
|
||||
|
||||
// static void sort_sprites(t_game *game)
|
||||
// {
|
||||
// t_sprite sprite_temp;
|
||||
// int n;
|
||||
// int i;
|
||||
// int new_n;
|
||||
|
||||
// n = game->map->n_sprites;
|
||||
// while (n > 1)
|
||||
// {
|
||||
// i = 1;
|
||||
// new_n = 0;
|
||||
// while (i <= n - 1)
|
||||
// {
|
||||
// if (game->map->sprites[i - 1].dist < game->map->sprites[i].dist)
|
||||
// {
|
||||
// sprite_temp = game->map->sprites[i - 1];
|
||||
// game->map->sprites[i - 1] = game->map->sprites[i];
|
||||
// game->map->sprites[i] = sprite_temp;
|
||||
// new_n = i;
|
||||
// }
|
||||
// i++;
|
||||
// }
|
||||
// n = new_n;
|
||||
// }
|
||||
// }
|
||||
|
||||
static void calculate_sprite_dist(t_game *game)
|
||||
{
|
||||
int i;
|
||||
t_vec2 player_pos;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
player_pos = game->player->pos;
|
||||
@ -78,39 +51,38 @@ static void calculate_sprite_dist(t_game *game)
|
||||
static void cam_fraction(t_game *game, t_sprite *sprite)
|
||||
{
|
||||
t_vec2 ps;
|
||||
t_vec2 cam;
|
||||
double frac_cam;
|
||||
double frac_sprite;
|
||||
double denominator;
|
||||
|
||||
cam = game->player->camera;
|
||||
ps = sub(sprite->pos, game->player->pos);
|
||||
frac_cam = (ps.y * game->player->dir.x - ps.x * game->player->dir.y);
|
||||
denominator = (ps.x * game->player->camera.y - ps.y * game->player->camera.x);
|
||||
frac_sprite = (game->player->camera.y * game->player->dir.x - game->player->camera.x * game->player->dir.y);
|
||||
if (denominator == 0 || (frac_sprite / denominator) < 0)
|
||||
{
|
||||
denominator = (ps.x * cam.y - ps.y * cam.x);
|
||||
frac_sprite = (cam.y * game->player->dir.x - cam.x * game->player->dir.y);
|
||||
sprite->cam_frac = NAN;
|
||||
return;
|
||||
}
|
||||
if (denominator == 0 || (frac_sprite / denominator) < 0)
|
||||
return ;
|
||||
sprite->cam_frac = frac_cam / denominator;
|
||||
if (sprite->cam_frac < -1 || sprite->cam_frac > 1)
|
||||
{
|
||||
sprite->cam_frac = NAN;
|
||||
return;
|
||||
}
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
unsigned int calculate_alpha(mlx_texture_t *texture, int x, int y, double dist)
|
||||
|
||||
unsigned int calculate_alpha(int x, int y, double dist)
|
||||
{
|
||||
// int index;
|
||||
int alpha;
|
||||
|
||||
// index = (x + y * texture->width) * texture->bytes_per_pixel;
|
||||
dist = (dist > 1) * dist + (dist <= 1) * 1;
|
||||
alpha = (255.0 / dist);
|
||||
return (alpha);
|
||||
}
|
||||
|
||||
unsigned int sample_texture_color(mlx_texture_t *texture, int x, int y, int alpha)
|
||||
unsigned int sample_texture_color(
|
||||
mlx_texture_t *texture,
|
||||
int x, int y, int alpha
|
||||
)
|
||||
{
|
||||
int index;
|
||||
|
||||
@ -121,52 +93,53 @@ unsigned int sample_texture_color(mlx_texture_t *texture, int x, int y, int alph
|
||||
| (texture->pixels[index + 3] != 0) * alpha);
|
||||
}
|
||||
|
||||
void draw_sprite(t_game *game, t_sprite *sprite, t_render *render)
|
||||
static void get_start_end(t_game *game,
|
||||
t_sprite *sprite, t_vec2_int *start, t_vec2_int *end)
|
||||
{
|
||||
double sprite_scale;
|
||||
double x_invrange;
|
||||
double y_invrange;
|
||||
int x_start;
|
||||
int y_start;
|
||||
int x_end;
|
||||
int y_end;
|
||||
|
||||
int x;
|
||||
int y;
|
||||
sprite_scale = 16.0 / sprite->dist;
|
||||
start->x = 0.5 * (game->screen->width * (1.0 + sprite->cam_frac)
|
||||
- sprite->texture->width * sprite_scale);
|
||||
start->y = 0.5 * (game->screen->height
|
||||
- sprite->texture->height * sprite_scale);
|
||||
end->x = start->x + (sprite->texture->width * sprite_scale);
|
||||
end->y = start->y + (sprite->texture->height * sprite_scale);
|
||||
}
|
||||
|
||||
void draw_sprite(t_game *game, t_sprite *sprite, t_render *render)
|
||||
{
|
||||
t_vec2 invrange;
|
||||
t_vec2_int start;
|
||||
t_vec2_int end;
|
||||
t_vec2_int tex;
|
||||
t_vec2_int disp;
|
||||
int alpha;
|
||||
double tex_x;
|
||||
double tex_y;
|
||||
unsigned int color;
|
||||
|
||||
if (isnan(sprite->cam_frac) || sprite->dist <= 0)
|
||||
return;
|
||||
sprite_scale = 16.0 / sprite->dist;
|
||||
x_start = game->screen->width * 0.5 * (1.0 + sprite->cam_frac) - (sprite->texture->width * sprite_scale * 0.5);
|
||||
y_start = (game->screen->height - sprite->texture->height * sprite_scale )* 0.5;
|
||||
x_end = x_start + (sprite->texture->width * sprite_scale);
|
||||
y_end = y_start + (sprite->texture->height * sprite_scale);
|
||||
y_invrange = 1.0 / (y_end - y_start);
|
||||
x_invrange = 1.0 / (x_end - x_start);
|
||||
x = x_start;
|
||||
alpha = calculate_alpha(sprite->texture, x_start, y_start, sprite->dist);
|
||||
while (x < x_end)
|
||||
return ;
|
||||
get_start_end(game, sprite, &start, &end);
|
||||
invrange.y = 1.0 / (end.y - start.y);
|
||||
invrange.x = 1.0 / (end.x - start.x);
|
||||
alpha = calculate_alpha(start.x, start.y, sprite->dist);
|
||||
disp.x = start.x;
|
||||
while (disp.x < end.x)
|
||||
{
|
||||
if (x > 0 && x < game->screen->width && sprite->dist <= render[x].perp_dist)
|
||||
if (disp.x > 0 && disp.x < game->screen->width && sprite->dist <= render[disp.x].perp_dist)
|
||||
{
|
||||
y = y_start;
|
||||
tex_x = (x - x_start) * x_invrange * sprite->texture->width;
|
||||
while (y < y_end)
|
||||
tex.x = (disp.x - start.x) * invrange.x * sprite->texture->width;
|
||||
disp.y = start.y;
|
||||
while (disp.y < end.y)
|
||||
{
|
||||
tex_y = (y- y_start) * y_invrange * sprite->texture->height;
|
||||
color = sample_texture_color(sprite->texture, (int)(tex_x), (int)(tex_y), alpha);
|
||||
if (y > 0 && y < game->screen->height && (color & 0xFF) != 0)
|
||||
{
|
||||
mlx_put_pixel(game->screen->img, x, y, color);
|
||||
}
|
||||
y++;
|
||||
tex.y = (disp.y - start.y) * invrange.y * sprite->texture->height;
|
||||
color = sample_texture_color(sprite->texture, tex.x, tex.y, alpha);
|
||||
if (disp.y > 0 && disp.y < game->screen->height && (color & 0xFF) != 0)
|
||||
mlx_put_pixel(game->screen->img, disp.x, disp.y, color);
|
||||
disp.y++;
|
||||
}
|
||||
}
|
||||
x++;
|
||||
disp.x++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/11 14:03:16 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/12 12:51:27 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
|
||||
static int init_temp(t_game **game)
|
||||
{
|
||||
(*game)->map->sprites = malloc(sizeof(t_sprite) * 5);
|
||||
(*game)->map->sprites = malloc(sizeof(t_sprite) * 10);
|
||||
if (!(*game)->map->sprites)
|
||||
return (FAILURE);
|
||||
(*game)->map->sprites[0].n_frames = 1;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user