manque partie texture

This commit is contained in:
ReverseSky 2024-10-14 17:06:45 +02:00
parent f89c74cd68
commit a8ad4d01b0
5 changed files with 243 additions and 172 deletions

View file

@ -6,13 +6,13 @@
# By: greg <greg@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/29 13:08:42 by greg #+# #+# #
# Updated: 2024/10/09 03:07:50 by greg ### ########.fr #
# Updated: 2024/10/14 15:33:53 by greg ### ########.fr #
# #
# **************************************************************************** #
NAME = cub3d
SRC = algo.c
SRC = algo.c move.c render.c
OBJS = ${SRC:.c=.o}

172
algo.c
View file

@ -6,15 +6,13 @@
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/11 05:16:06 by greg ### ########.fr */
/* Updated: 2024/10/14 16:47:50 by greg ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
#include <math.h>
#include <stdio.h>
int worldMap[mapWidth][mapHeight]=
int worldMap[mapWidth][mapHeight] =
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
@ -42,7 +40,6 @@ int worldMap[mapWidth][mapHeight]=
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
static int initalgo(t_ray *ray)
{
ray->posX = 13;
@ -53,165 +50,8 @@ static int initalgo(t_ray *ray)
ray->planeY = 0.66; // FOV de 66 degres
ray->movespeed = 0.1;
ray->rotspeed = 0.1;
return (0);
}
static int render(t_ray *ray)
{
int x;
double width = 640;
double height = 480;
int hit;
int 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;
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->mapX = (int)ray->posX;
ray->mapY = (int)ray->posY;
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 - ray->mapX) * ray->deltadistX;
}
else
{
ray->stepX = 1;
ray->sidedistX = (ray->mapX + 1.0 - ray->posX) * ray->deltadistX;
}
if (ray->raydirY < 0)
{
ray->stepY = -1;
ray->sidedistY = (ray->posY - ray->mapY) * ray->deltadistY;
}
else
{
ray->stepY = 1;
ray->sidedistY = (ray->mapY + 1.0 - ray->posY) * ray->deltadistY;
}
hit = 0;
while(hit == 0)
{
//jump to next map square, either in x-direction, or in y-direction
if(ray->sidedistX < ray->sidedistY)
{
ray->sidedistX += ray->deltadistX;
ray->mapX += ray->stepX;
side = 0;
}
else
{
ray->sidedistY += ray->deltadistY;
ray->mapY += ray->stepY;
side = 1;
}
//Check if ray has hit a wall
if(worldMap[ray->mapX][ray->mapY] == 1)
hit = 1;
}
if(side == 0)
ray->perpwalldist = (ray->sidedistX - ray->deltadistX);
else
ray->perpwalldist = (ray->sidedistY - ray->deltadistY);
//Calculate height of line to draw on screen
int lineHeight = (int)(height / ray->perpwalldist);
//calculate lowest and highest pixel to fill in current stripe
int drawStart = -lineHeight / 2 + height / 2;
if(drawStart < 0)
drawStart = 0;
int drawEnd = lineHeight / 2 + height / 2;
if(drawEnd >= height)
drawEnd = height - 1;
// draw vertical line
int y = 0;
while (y <= drawStart)
{
img_data[y * (int)width + x] = 0x29f8ff;;
y++;
}
y = drawStart;
while (y <= drawEnd)
{
img_data[y * (int)width + x] = 0xFF0000;
y++;
}
y++;
while (y <= height)
{
img_data[y * (int)width + x] = 0xFF985C;
y++;
}
x++;
}
// 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)
{
if (worldMap[(int)((ray->posX + ray->dirX * ray->movespeed))][(int)(ray->posY)] != 1)
ray->posX += ray->dirX * ray->movespeed;
if (worldMap[(int)(ray->posX)][(int)(ray->posY + ray->dirY * ray->movespeed)] != 1)
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)] != 1)
ray->posX -= ray->dirX * ray->movespeed;
if (worldMap[(int)(ray->posX)][(int)(ray->posY - ray->dirY * ray->movespeed)] != 1)
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);
}
if (keycode == 65307)
exit(0);
// render the updated frame after key press
render(ray);
ray->width = 1920;
ray->height = 1080;
return (0);
}
@ -221,8 +61,8 @@ int main(void)
// 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);
ray.win_ptr = mlx_new_window(ray.mlx_ptr, 1920, 1080, "cub3d");
ray.img_ptr = mlx_new_image(ray.mlx_ptr, 1920, 1080);
initalgo(&ray);

15
algo.h
View file

@ -6,7 +6,7 @@
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
/* Updated: 2024/10/11 04:32:39 by greg ### ########.fr */
/* Updated: 2024/10/14 16:47:36 by greg ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,8 +18,8 @@
#include "Minilibx/mlx.h"
#include "Minilibx/mlx_int.h"
# include "Libft/libft.h"
#define mapWidth 24
#define mapHeight 24
#define mapWidth 24 // TEST
#define mapHeight 24 // TEST
typedef struct s_ray
{
@ -58,6 +58,13 @@ typedef struct s_ray
int *img_data;
int mapX;
int mapY;
double width;
double height;
} t_ray;
#endif
extern int worldMap[mapWidth][mapHeight]; // TEST
int keypress(int keycode, t_ray *ray);
int render(t_ray *ray);
#endif

86
move.c Normal file
View file

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:03:10 by greg #+# #+# */
/* Updated: 2024/10/14 16:53:56 by greg ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
static int move_forward(t_ray *ray)
{
if (worldMap[(int)((ray->posX + ray->dirX * ray->movespeed))][(int)(ray->posY)] != 1)
ray->posX += ray->dirX * ray->movespeed;
if (worldMap[(int)(ray->posX)][(int)(ray->posY + ray->dirY * ray->movespeed)] != 1)
ray->posY += ray->dirY * ray->movespeed;
return(0);
}
static int move_backward(t_ray *ray)
{
if (worldMap[(int)(ray->posX - ray->dirX * ray->movespeed)][(int)(ray->posY)] != 1)
ray->posX -= ray->dirX * ray->movespeed;
if (worldMap[(int)(ray->posX)][(int)(ray->posY - ray->dirY * ray->movespeed)] != 1)
ray->posY -= ray->dirY * ray->movespeed;
return(0);
}
static int move_right(t_ray *ray)
{
//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);
}
static int move_left(t_ray *ray)
{
//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 keypress(int keycode, t_ray *ray)
{
if (keycode == 119) //move forward if no wall in front of you
{
move_forward(ray);
}
if (keycode == 115) //move backwards if no wall behind you
{
move_backward(ray);
}
if (keycode == 100) //rotate to the right
{
move_left(ray);
}
if (keycode == 97) //rotate to the left
{
move_right(ray);
}
if (keycode == 65307)
{
mlx_destroy_window(ray->mlx_ptr, ray->win_ptr);
exit(0);
}
// render the updated frame after key pressd
render(ray);
return (0);
}

138
render.c Normal file
View file

@ -0,0 +1,138 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 14:55:05 by greg #+# #+# */
/* Updated: 2024/10/14 16:53:21 by greg ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
static int init_step(t_ray *ray)
{
if (ray->raydirX < 0)
{
ray->stepX = -1;
ray->sidedistX = (ray->posX - ray->mapX) * ray->deltadistX;
}
else
{
ray->stepX = 1;
ray->sidedistX = (ray->mapX + 1.0 - ray->posX) * ray->deltadistX;
}
if (ray->raydirY < 0)
{
ray->stepY = -1;
ray->sidedistY = (ray->posY - ray->mapY) * ray->deltadistY;
}
else
{
ray->stepY = 1;
ray->sidedistY = (ray->mapY + 1.0 - ray->posY) * ray->deltadistY;
}
return (0);
}
static int ray_pos(t_ray *ray, int x)
{
// position et direction du rayon
ray->cameraX = 2 * x / ray->width - 1;
ray->raydirX = ray->dirX + ray->planeX * ray->cameraX;
ray->raydirY = ray->dirY + ray->planeY * ray->cameraX;
ray->mapX = (int)ray->posX;
ray->mapY = (int)ray->posY;
ray->deltadistX = (ray->raydirX == 0) ? 1e30 : fabs(1 / ray->raydirX);
ray->deltadistY = (ray->raydirY == 0) ? 1e30 : fabs(1 / ray->raydirY);
return (0);
}
int draw_img(t_ray *ray, int drawStart, int *img_data, int drawEnd, int x)
{
// draw vertical line
int y = 0;
while (y <= drawStart)
{
img_data[y * (int)ray->width + x] = 0x29f8ff;;
y++;
}
y = drawStart;
while (y <= drawEnd)
{
img_data[y * (int)ray->width + x] = 0xFF0000;
y++;
}
y++;
while (y <= ray->height)
{
img_data[y * (int)ray->width + x] = 0xFF985C;
y++;
}
return (0);
}
void find_wall(t_ray *ray)
{
int side;
int hit;
hit = 0;
while(hit == 0)
{
if(ray->sidedistX < ray->sidedistY) //jump to next map square, either in x-direction, or in y-direction
{
ray->sidedistX += ray->deltadistX;
ray->mapX += ray->stepX;
side = 0;
}
else
{
ray->sidedistY += ray->deltadistY;
ray->mapY += ray->stepY;
side = 1;
}
if(worldMap[ray->mapX][ray->mapY] == 1) //Check if ray has hit a wall
hit = 1;
}
if(side == 0)
ray->perpwalldist = (ray->sidedistX - ray->deltadistX);
else
ray->perpwalldist = (ray->sidedistY - ray->deltadistY);
}
int render(t_ray *ray)
{
int x;
int lineHeight;
int *img_data;
int drawStart;
int drawEnd;
img_data = (int *)mlx_get_data_addr(ray->img_ptr, &(int){32}, &(int){ray->width * 4}, &(int){0});
ft_bzero(img_data, ray->width * ray->height * sizeof(int)); // clear image data
x = 0;
while (x++ < ray->width)
{
ray_pos(ray, x);
init_step(ray); //calculate step and initial sideDist
find_wall(ray);
lineHeight = (int)(ray->height / ray->perpwalldist); //Calculate height of line to draw on screen
drawStart = -lineHeight / 2 + ray->height / 2; //calculate lowest and highest pixel to fill in current stripe
if(drawStart < 0)
drawStart = 0;
drawEnd = lineHeight / 2 + ray->height / 2;
if(drawEnd >= ray->height)
drawEnd = ray->height - 1;
draw_img(ray, drawStart, img_data, drawEnd, x);
}
mlx_put_image_to_window(ray->mlx_ptr, ray->win_ptr, ray->img_ptr, 0, 0);
return (0);
}