render termine manque textures
This commit is contained in:
parent
613f066547
commit
c1e17997ff
3 changed files with 125 additions and 124 deletions
242
algo.c
242
algo.c
|
@ -6,7 +6,7 @@
|
|||
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
|
||||
/* Updated: 2024/10/09 03:30:08 by greg ### ########.fr */
|
||||
/* Updated: 2024/10/10 18:22:08 by greg ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -45,8 +45,8 @@ int worldMap[mapWidth][mapHeight]=
|
|||
|
||||
static int initalgo(t_ray *ray)
|
||||
{
|
||||
ray->posX = 2;
|
||||
ray->posY = 2;
|
||||
ray->posX = 13;
|
||||
ray->posY = 10;
|
||||
ray->dirX = -1;
|
||||
ray->dirY = 0;
|
||||
ray->planeX = 0;
|
||||
|
@ -56,123 +56,67 @@ static int initalgo(t_ray *ray)
|
|||
return (0);
|
||||
}
|
||||
|
||||
static int keypress(int keycode, t_ray *ray, int **worldMap)
|
||||
static int render(t_ray *ray)
|
||||
{
|
||||
//move forward if no wall in front of you
|
||||
if (keycode == 119)
|
||||
{
|
||||
if(worldMap[(int)(ray->posX + ray->dirX * ray->movespeed)][(int)(ray->posY)] == 0)
|
||||
ray->posX += ray->dirX * ray->movespeed;
|
||||
if(worldMap[(int)(ray->posX)][((int)ray->posY + (int)ray->dirY * (int)ray->movespeed)] == 0)
|
||||
ray->posY += ray->dirY * ray->movespeed;
|
||||
}
|
||||
//move backwards if no wall behind you
|
||||
if (keycode == 115)
|
||||
{
|
||||
if(worldMap[(int)(ray->posX - ray->dirX * ray->movespeed)][(int)(ray->posY)] == 0)
|
||||
ray->posX -= ray->dirX * ray->movespeed;
|
||||
if(worldMap[(int)(ray->posX)][((int)ray->posY - (int)ray->dirY * (int)ray->movespeed)] == 0)
|
||||
ray->posY -= ray->dirY * ray->movespeed;
|
||||
}
|
||||
//rotate to the right
|
||||
if (keycode == 100)
|
||||
{
|
||||
//both camera direction and camera plane must be rotated
|
||||
ray->oldDirX = ray->dirX;
|
||||
ray->dirX = ray->dirX * cos(-ray->rotspeed) - ray->dirY * sin(-ray->rotspeed);
|
||||
ray->dirY = ray->oldDirX * sin(-ray->rotspeed) + ray->dirY * cos(-ray->rotspeed);
|
||||
ray->oldPlaneX = ray->planeX;
|
||||
ray->planeX = ray->planeX * cos(-ray->rotspeed) - ray->planeY * sin(-ray->rotspeed);
|
||||
ray->planeY = ray->oldPlaneX * sin(-ray->rotspeed) + ray->planeY * cos(-ray->rotspeed);
|
||||
}
|
||||
//rotate to the left
|
||||
if (keycode == 97)
|
||||
{
|
||||
//both camera direction and camera plane must be rotated
|
||||
ray->oldDirX = ray->dirX;
|
||||
ray->dirX = ray->dirX * cos(ray->rotspeed) - ray->dirY * sin(ray->rotspeed);
|
||||
ray->dirY = ray->oldDirX * sin(ray->rotspeed) + ray->dirY * cos(ray->rotspeed);
|
||||
ray->oldPlaneX = ray->planeX;
|
||||
ray->planeX = ray->planeX * cos(ray->rotspeed) - ray->planeY * sin(ray->rotspeed);
|
||||
ray->planeY = ray->oldPlaneX * sin(ray->rotspeed) + ray->planeY * cos(ray->rotspeed);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int /*argc*/, char */*argv*/[])
|
||||
{
|
||||
t_ray ray;
|
||||
int x;
|
||||
double width;
|
||||
double height;
|
||||
// quelle box sur la map on est
|
||||
int mapX;
|
||||
int mapY;
|
||||
// wall hit and if yes in wich direction
|
||||
int hit;
|
||||
int side;
|
||||
|
||||
initalgo(&ray);
|
||||
|
||||
int x;
|
||||
double width = 640;
|
||||
double height = 480;
|
||||
int mapX, mapY, hit, side;
|
||||
int *img_data = (int *)mlx_get_data_addr(ray->img_ptr, &(int){32}, &(int){width * 4}, &(int){0});
|
||||
|
||||
// clear image data
|
||||
ft_bzero(img_data, width * height * sizeof(int));
|
||||
|
||||
x = 0;
|
||||
width = 640;
|
||||
height = 480;
|
||||
|
||||
void *mlx_ptr;
|
||||
void *win_ptr;
|
||||
|
||||
mlx_ptr = mlx_init(); // Initialise la connexion à la MLX
|
||||
win_ptr = mlx_new_window(mlx_ptr, width, height, "cub3d");
|
||||
|
||||
while (x < width)
|
||||
{
|
||||
// position et direction du rayon
|
||||
ray.cameraX = 2 * x / width - 1;
|
||||
ray.raydirX = ray.dirX + ray.planeX * ray.cameraX;
|
||||
ray.raydirY = ray.dirY + ray.planeY * ray.cameraX;
|
||||
ray->cameraX = 2 * x / width - 1;
|
||||
ray->raydirX = ray->dirX + ray->planeX * ray->cameraX;
|
||||
ray->raydirY = ray->dirY + ray->planeY * ray->cameraX;
|
||||
|
||||
mapX = (int)ray.posX;
|
||||
mapY = (int)ray.posY;
|
||||
mapX = (int)ray->posX;
|
||||
mapY = (int)ray->posY;
|
||||
|
||||
ray.deltadistX = (ray.raydirX == 0) ? 1e30 : fabs(1 / ray.raydirX);
|
||||
ray.deltadistY = (ray.raydirY == 0) ? 1e30 : fabs(1 / ray.raydirY);
|
||||
ray->deltadistX = (ray->raydirX == 0) ? 1e30 : fabs(1 / ray->raydirX);
|
||||
ray->deltadistY = (ray->raydirY == 0) ? 1e30 : fabs(1 / ray->raydirY);
|
||||
|
||||
//calculate step and initial sideDist
|
||||
if (ray->raydirX < 0)
|
||||
{
|
||||
ray->stepX = -1;
|
||||
ray->sidedistX = (ray->posX - mapX) * ray->deltadistX;
|
||||
}
|
||||
else
|
||||
{
|
||||
ray->stepX = 1;
|
||||
ray->sidedistX = (mapX + 1.0 - ray->posX) * ray->deltadistX;
|
||||
}
|
||||
if (ray->raydirY < 0)
|
||||
{
|
||||
ray->stepY = -1;
|
||||
ray->sidedistY = (ray->posY - mapY) * ray->deltadistY;
|
||||
}
|
||||
else
|
||||
{
|
||||
ray->stepY = 1;
|
||||
ray->sidedistY = (mapY + 1.0 - ray->posY) * ray->deltadistY;
|
||||
}
|
||||
|
||||
hit = 0;
|
||||
|
||||
//calculate step and initial sideDist
|
||||
if (ray.raydirX < 0)
|
||||
{
|
||||
ray.stepX = -1;
|
||||
ray.sidedistX = (ray.posX - mapX) * ray.deltadistX;
|
||||
}
|
||||
else
|
||||
{
|
||||
ray.stepX = 1;
|
||||
ray.sidedistX = (mapX + 1.0 - ray.posX) * ray.deltadistX;
|
||||
}
|
||||
if (ray.raydirY < 0)
|
||||
{
|
||||
ray.stepY = -1;
|
||||
ray.sidedistY = (ray.posY - mapY) * ray.deltadistY;
|
||||
}
|
||||
else
|
||||
{
|
||||
ray.stepY = 1;
|
||||
ray.sidedistY = (mapY + 1.0 - ray.posY) * ray.deltadistY;
|
||||
}
|
||||
while(hit == 0)
|
||||
{
|
||||
//jump to next map square, either in x-direction, or in y-direction
|
||||
if(ray.sidedistX < ray.sidedistY)
|
||||
if(ray->sidedistX < ray->sidedistY)
|
||||
{
|
||||
ray.sidedistX += ray.deltadistX;
|
||||
mapX += ray.stepX;
|
||||
ray->sidedistX += ray->deltadistX;
|
||||
mapX += ray->stepX;
|
||||
side = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ray.sidedistY += ray.deltadistY;
|
||||
mapY += ray.stepY;
|
||||
ray->sidedistY += ray->deltadistY;
|
||||
mapY += ray->stepY;
|
||||
side = 1;
|
||||
}
|
||||
//Check if ray has hit a wall
|
||||
|
@ -180,45 +124,97 @@ int main(int /*argc*/, char */*argv*/[])
|
|||
hit = 1;
|
||||
}
|
||||
if(side == 0)
|
||||
ray.perpwalldist = (ray.sidedistX - ray.deltadistX);
|
||||
ray->perpwalldist = (ray->sidedistX - ray->deltadistX);
|
||||
else
|
||||
ray.perpwalldist = (ray.sidedistY - ray.deltadistY);
|
||||
ray->perpwalldist = (ray->sidedistY - ray->deltadistY);
|
||||
//Calculate height of line to draw on screen
|
||||
int lineHeight = (int)(height / ray.perpwalldist);
|
||||
lineHeight = (int)(height / ray.perpwalldist);
|
||||
int lineHeight = (int)(height / ray->perpwalldist);
|
||||
|
||||
//calculate lowest and highest pixel to fill in current stripe
|
||||
int drawStart;
|
||||
drawStart = -lineHeight / 2 + height / 2;
|
||||
int drawStart = -lineHeight / 2 + height / 2;
|
||||
if(drawStart < 0)
|
||||
drawStart = 0;
|
||||
int drawEnd;
|
||||
drawEnd = lineHeight / 2 + height / 2;
|
||||
int drawEnd = lineHeight / 2 + height / 2;
|
||||
if(drawEnd >= height)
|
||||
drawEnd = height - 1;
|
||||
|
||||
// draw vertical line
|
||||
int y = drawStart;
|
||||
while (y <= drawEnd)
|
||||
{
|
||||
mlx_pixel_put(mlx_ptr, win_ptr, x, y, 0xFF0000);
|
||||
img_data[y * (int)width + x] = 0xFF0000;
|
||||
y++;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
|
||||
//draw the pixels of the stripe as a vertical line
|
||||
//verLine(x, drawStart, drawEnd, color); */
|
||||
|
||||
|
||||
// put image to window
|
||||
// destroy old image
|
||||
|
||||
|
||||
mlx_hook(win_ptr, 2, 1L<<0, keypress, &ray);
|
||||
|
||||
// put image to window
|
||||
mlx_put_image_to_window(ray->mlx_ptr, ray->win_ptr, ray->img_ptr, 0, 0);
|
||||
return (0);
|
||||
|
||||
}
|
||||
|
||||
static int keypress(int keycode, t_ray *ray)
|
||||
{
|
||||
//move forward if no wall in front of you
|
||||
if (keycode == 119)
|
||||
{
|
||||
ray->posX += ray->dirX * ray->movespeed;
|
||||
ray->posY += ray->dirY * ray->movespeed;
|
||||
}
|
||||
//move backwards if no wall behind you
|
||||
if (keycode == 115)
|
||||
{
|
||||
ray->posX -= ray->dirX * ray->movespeed;
|
||||
ray->posY -= ray->dirY * ray->movespeed;
|
||||
}
|
||||
//rotate to the right
|
||||
if (keycode == 100)
|
||||
{
|
||||
//both camera direction and camera plane must be rotated
|
||||
ray->oldDirX = ray->dirX;
|
||||
ray->dirX = ray->dirX * cos(-ray->rotspeed) - ray->dirY * sin(-ray->rotspeed);
|
||||
ray->dirY = ray->oldDirX * sin(-ray->rotspeed) + ray->dirY * cos(-ray->rotspeed);
|
||||
ray->oldPlaneX = ray->planeX;
|
||||
ray->planeX = ray->planeX * cos(-ray->rotspeed) - ray->planeY * sin(-ray->rotspeed);
|
||||
ray->planeY = ray->oldPlaneX * sin(-ray->rotspeed) + ray->planeY * cos(-ray->rotspeed);
|
||||
}
|
||||
//rotate to the left
|
||||
if (keycode == 97)
|
||||
{
|
||||
//both camera direction and camera plane must be rotated
|
||||
ray->oldDirX = ray->dirX;
|
||||
ray->dirX = ray->dirX * cos(ray->rotspeed) - ray->dirY * sin(ray->rotspeed);
|
||||
ray->dirY = ray->oldDirX * sin(ray->rotspeed) + ray->dirY * cos(ray->rotspeed);
|
||||
ray->oldPlaneX = ray->planeX;
|
||||
ray->planeX = ray->planeX * cos(ray->rotspeed) - ray->planeY * sin(ray->rotspeed);
|
||||
ray->planeY = ray->oldPlaneX * sin(ray->rotspeed) + ray->planeY * cos(ray->rotspeed);
|
||||
}
|
||||
// render the updated frame after key press
|
||||
render(ray);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
t_ray ray;
|
||||
|
||||
// Initialisation
|
||||
ray.mlx_ptr = mlx_init();
|
||||
ray.win_ptr = mlx_new_window(ray.mlx_ptr, 640, 480, "cub3d");
|
||||
ray.img_ptr = mlx_new_image(ray.mlx_ptr, 640, 480);
|
||||
|
||||
initalgo(&ray);
|
||||
|
||||
// hook keypress
|
||||
mlx_hook(ray.win_ptr, 2, 1L<<0, keypress, &ray);
|
||||
// render the initial frame
|
||||
render(&ray);
|
||||
|
||||
// start the mlx loop
|
||||
mlx_loop(ray.mlx_ptr);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
7
algo.h
7
algo.h
|
@ -6,7 +6,7 @@
|
|||
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
|
||||
/* Updated: 2024/10/09 02:51:53 by greg ### ########.fr */
|
||||
/* Updated: 2024/10/10 17:53:01 by greg ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
|||
#include <math.h>
|
||||
#include "Minilibx/mlx.h"
|
||||
#include "Minilibx/mlx_int.h"
|
||||
# include "Libft/libft.h"
|
||||
#define mapWidth 24
|
||||
#define mapHeight 24
|
||||
|
||||
|
@ -51,6 +52,10 @@ typedef struct s_ray
|
|||
double rotspeed;
|
||||
double oldDirX;
|
||||
double oldPlaneX;
|
||||
void *mlx_ptr;
|
||||
void *win_ptr;
|
||||
void *img_ptr;
|
||||
int *img_data;
|
||||
} t_ray;
|
||||
|
||||
#endif
|
||||
|
|
BIN
cub3d
Executable file
BIN
cub3d
Executable file
Binary file not shown.
Loading…
Add table
Reference in a new issue