feat: testing how to play with textures
This commit is contained in:
parent
badd6534f4
commit
e581c72b02
14 changed files with 598 additions and 313 deletions
129
test/test2.c
129
test/test2.c
|
|
@ -6,10 +6,10 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/27 12:09:00 by rparodi #+# #+# */
|
||||
/* Updated: 2024/12/01 17:25:59 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/12/05 19:07:51 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minilibx-linux/mlx.h"
|
||||
#include "test.h"
|
||||
|
||||
int worldMap[MAP_WIDTH][MAP_HEIGHT] = {\
|
||||
|
|
@ -146,26 +146,49 @@ int key_hook(int keycode, t_data *data)
|
|||
return (0);
|
||||
}
|
||||
|
||||
int render_frame(t_data *data)
|
||||
void clear_window(t_data *data)
|
||||
{
|
||||
int y;
|
||||
int x;
|
||||
int x;
|
||||
|
||||
y = 0;
|
||||
while (y < data->screen_y)
|
||||
while (y < WINDOW_HEIGHT / 2)
|
||||
{
|
||||
x = 0;
|
||||
while (x < data->screen_x)
|
||||
while (x < WINDOW_WIDTH)
|
||||
{
|
||||
my_mlx_pixel_put(data, x, y, 0x00000000);
|
||||
my_mlx_pixel_put(data, x, y, 0x000000);
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
for(int x = 0; x < data->screen_x; x++)
|
||||
void draw_floor(t_data *data)
|
||||
{
|
||||
int y;
|
||||
int x;
|
||||
|
||||
y = WINDOW_HEIGHT / 2;
|
||||
while (y < WINDOW_HEIGHT)
|
||||
{
|
||||
double cameraX = 2 * x / (double)data->screen_x - 1;
|
||||
x = 0;
|
||||
while (x < WINDOW_WIDTH)
|
||||
{
|
||||
my_mlx_pixel_put(data, x, y, 0xFFFFFF);
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
int render_frame(t_data *data)
|
||||
{
|
||||
clear_window(data);
|
||||
draw_floor(data);
|
||||
for(int x = 0; x < WINDOW_WIDTH; x++)
|
||||
{
|
||||
double cameraX = 2 * x / (double)WINDOW_WIDTH - 1;
|
||||
double rayDirX = data->dir_x + data->plane_x * cameraX;
|
||||
double rayDirY = data->dir_y + data->plane_y * cameraX;
|
||||
|
||||
|
|
@ -215,24 +238,48 @@ int render_frame(t_data *data)
|
|||
else
|
||||
perpWallDist = (mapY - data->pos_y + (1 - stepY) / 2) / rayDirY;
|
||||
|
||||
int lineHeight = (int)(data->screen_y / perpWallDist);
|
||||
int drawStart = -lineHeight / 2 + data->screen_y / 2;
|
||||
int lineHeight = (int)(WINDOW_HEIGHT / perpWallDist);
|
||||
int drawStart = -lineHeight / 2 + WINDOW_HEIGHT / 2;
|
||||
if(drawStart < 0) drawStart = 0;
|
||||
int drawEnd = lineHeight / 2 + data->screen_y / 2;
|
||||
if(drawEnd >= data->screen_y) drawEnd = data->screen_y - 1;
|
||||
int drawEnd = lineHeight / 2 + WINDOW_HEIGHT / 2;
|
||||
if(drawEnd >= WINDOW_HEIGHT) drawEnd = WINDOW_HEIGHT - 1;
|
||||
|
||||
int color;
|
||||
if (side == 0 && stepX > 0)
|
||||
color = 0x00FF0000;
|
||||
else if (side == 0 && stepX < 0)
|
||||
color = 0x0000FF00;
|
||||
else if (side == 1 && stepY > 0)
|
||||
color = 0x000000FF;
|
||||
// Get correct wall texture based on direction
|
||||
t_texture *wall_tex;
|
||||
if (side == 0)
|
||||
{
|
||||
if (stepX > 0)
|
||||
wall_tex = &data->wall_so;
|
||||
else
|
||||
wall_tex = &data->wall_no;
|
||||
}
|
||||
else
|
||||
color = 0x00FFFF00;
|
||||
{
|
||||
if (stepY > 0)
|
||||
wall_tex = &data->wall_ea;
|
||||
else
|
||||
wall_tex = &data->wall_we;
|
||||
}
|
||||
|
||||
// Wall texture calculations
|
||||
double wallX;
|
||||
if (side == 0) wallX = data->pos_y + perpWallDist * rayDirY;
|
||||
else wallX = data->pos_x + perpWallDist * rayDirX;
|
||||
wallX -= floor(wallX);
|
||||
|
||||
int texX = (int)(wallX * (double)wall_tex->width);
|
||||
|
||||
double step = 1.0 * wall_tex->height / lineHeight;
|
||||
double texPos = (drawStart - WINDOW_HEIGHT / 2 + lineHeight / 2) * step;
|
||||
|
||||
for(int y = drawStart; y < drawEnd; y++)
|
||||
my_mlx_pixel_put(data, x, y, color);
|
||||
{
|
||||
int texY = (int)texPos & (wall_tex->height - 1);
|
||||
texPos += step;
|
||||
|
||||
char *tex_pixel = wall_tex->addr + (texY * wall_tex->line_length + texX * (wall_tex->bits_per_pixel / 8));
|
||||
my_mlx_pixel_put(data, x, y, *(unsigned int*)tex_pixel);
|
||||
}
|
||||
}
|
||||
mlx_put_image_to_window(data->mlx, data->win, data->img, 0, 0);
|
||||
return (0);
|
||||
|
|
@ -241,13 +288,45 @@ int render_frame(t_data *data)
|
|||
int main(void)
|
||||
{
|
||||
t_data data;
|
||||
int width, height;
|
||||
|
||||
data.mlx = mlx_init();
|
||||
mlx_get_screen_size(data.mlx, &data.screen_x, &data.screen_y);
|
||||
data.win = mlx_new_window(data.mlx, data.screen_x, data.screen_y, "Raycaster");
|
||||
data.img = mlx_new_image(data.mlx, data.screen_x, data.screen_y);
|
||||
data.win = mlx_new_window(data.mlx, WINDOW_WIDTH, WINDOW_HEIGHT, "Raycaster");
|
||||
data.img = mlx_new_image(data.mlx, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
data.addr = mlx_get_data_addr(data.img, &data.bits_per_pixel, &data.line_length, &data.endian);
|
||||
|
||||
// Load wall textures
|
||||
data.wall_no.img = mlx_xpm_file_to_image(data.mlx, "./textures/b.xpm", &width, &height);
|
||||
data.wall_no.addr = mlx_get_data_addr(data.wall_no.img, &data.wall_no.bits_per_pixel,
|
||||
&data.wall_no.line_length, &data.wall_no.endian);
|
||||
data.wall_no.width = width;
|
||||
data.wall_no.height = height;
|
||||
|
||||
data.wall_so.img = mlx_xpm_file_to_image(data.mlx, "./textures/cobblestone_32.xpm", &width, &height);
|
||||
data.wall_so.addr = mlx_get_data_addr(data.wall_so.img, &data.wall_so.bits_per_pixel,
|
||||
&data.wall_so.line_length, &data.wall_so.endian);
|
||||
data.wall_so.width = width;
|
||||
data.wall_so.height = height;
|
||||
|
||||
data.wall_we.img = mlx_xpm_file_to_image(data.mlx, "./textures/sandy_32.xpm", &width, &height);
|
||||
data.wall_we.addr = mlx_get_data_addr(data.wall_we.img, &data.wall_we.bits_per_pixel,
|
||||
&data.wall_we.line_length, &data.wall_we.endian);
|
||||
data.wall_we.width = width;
|
||||
data.wall_we.height = height;
|
||||
|
||||
data.wall_ea.img = mlx_xpm_file_to_image(data.mlx, "./textures/wasteland_32.xpm", &width, &height);
|
||||
data.wall_ea.addr = mlx_get_data_addr(data.wall_ea.img, &data.wall_ea.bits_per_pixel,
|
||||
&data.wall_ea.line_length, &data.wall_ea.endian);
|
||||
data.wall_ea.width = width;
|
||||
data.wall_ea.height = height;
|
||||
|
||||
// Load floor texture
|
||||
data.floor.img = mlx_xpm_file_to_image(data.mlx, "./textures/b.xpm", &width, &height);
|
||||
data.floor.addr = mlx_get_data_addr(data.floor.img, &data.floor.bits_per_pixel,
|
||||
&data.floor.line_length, &data.floor.endian);
|
||||
data.floor.width = width;
|
||||
data.floor.height = height;
|
||||
|
||||
data.pos_x = 22;
|
||||
data.pos_y = 12;
|
||||
data.dir_x = -1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue