New raycaster based on Lode's tutorial works, now to add textures.
This commit is contained in:
parent
96b44a4eb2
commit
c1b7df1850
2 changed files with 89 additions and 74 deletions
159
fenscaster.cpp
159
fenscaster.cpp
|
@ -14,9 +14,8 @@ Matrix MAP{
|
|||
{1,0,1,0,0,0,0,0,1},
|
||||
{1,0,1,0,0,1,1,0,1},
|
||||
{1,0,0,0,0,0,0,0,1},
|
||||
{1,1,0,0,0,0,0,0,1},
|
||||
{1,1,0,0,0,0,0,1,1},
|
||||
{1,0,0,1,1,1,0,0,1},
|
||||
{1,0,0,0,1,0,0,0,1},
|
||||
{1,0,0,0,0,0,1,1,1},
|
||||
{1,1,1,1,1,1,1,1,1}
|
||||
};
|
||||
|
@ -37,11 +36,50 @@ const float SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS;
|
|||
|
||||
float player_x = SCREEN_WIDTH / 4;
|
||||
float player_y = SCREEN_WIDTH / 4;
|
||||
float player_angle = std::numbers::pi;
|
||||
|
||||
// x and y start position
|
||||
double posX = player_x / TILE_SIZE;
|
||||
double posY = player_y / TILE_SIZE;
|
||||
|
||||
// initial direction vector
|
||||
double dirX = -1;
|
||||
double dirY = 0;
|
||||
|
||||
// the 2d raycaster version of camera plane
|
||||
double planeX = 0;
|
||||
double planeY = 0.66;
|
||||
|
||||
#define rgba_color(r,g,b,a) (b<<(0*8))|(g<<(1*8))|(r<<(2*8))|(a<<(3*8))
|
||||
#define gray_color(c) rgba_color(c, c, c, 255)
|
||||
|
||||
std::vector<uint32_t> texture[8];
|
||||
#define texWidth 64
|
||||
#define texHeight 64
|
||||
|
||||
void load_textures() {
|
||||
for(int i = 0; i < 8; i++) {
|
||||
texture[i].resize(texWidth * texHeight);
|
||||
}
|
||||
|
||||
for(int x = 0; x < texWidth; x++) {
|
||||
for(int y = 0; y < texHeight; y++)
|
||||
{
|
||||
int xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight);
|
||||
//int xcolor = x * 256 / texWidth;
|
||||
int ycolor = y * 256 / texHeight;
|
||||
int xycolor = y * 128 / texHeight + x * 128 / texWidth;
|
||||
texture[0][texWidth * y + x] = 65536 * 254 * (x != y && x != texWidth - y); //flat red texture with black cross
|
||||
texture[1][texWidth * y + x] = xycolor + 256 * xycolor + 65536 * xycolor; //sloped greyscale
|
||||
texture[2][texWidth * y + x] = 256 * xycolor + 65536 * xycolor; //sloped yellow gradient
|
||||
texture[3][texWidth * y + x] = xorcolor + 256 * xorcolor + 65536 * xorcolor; //xor greyscale
|
||||
texture[4][texWidth * y + x] = 256 * xorcolor; //xor green
|
||||
texture[5][texWidth * y + x] = 65536 * 192 * (x % 16 && y % 16); //red bricks
|
||||
texture[6][texWidth * y + x] = 65536 * ycolor; //red gradient
|
||||
texture[7][texWidth * y + x] = 128 + 256 * 128 + 65536 * 128; //flat grey texture
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_rect(Fenster &window, Point pos, Point size, uint32_t color) {
|
||||
size_t x_start = size_t(pos.x);
|
||||
size_t y_start = size_t(pos.y);
|
||||
|
@ -114,32 +152,14 @@ void clear(Fenster &window) {
|
|||
}
|
||||
}
|
||||
|
||||
void draw_map_rays(Fenster &window, int col, int row, Point target) {
|
||||
void draw_map_blocks(Fenster &window, int col, int row) {
|
||||
draw_map_rect(window, col, row, rgba_color(100, 20, 20, 255));
|
||||
draw_line(window, {size_t(player_x), size_t(player_y)}, target, rgba_color(200, 20, 20, 255));
|
||||
}
|
||||
|
||||
void ray_casting(Fenster &window, Matrix& map) {
|
||||
int w = THREED_VIEW_WIDTH;
|
||||
int h = THREED_VIEW_HEIGHT;
|
||||
|
||||
// x and y start position
|
||||
double posX = player_x / TILE_SIZE;
|
||||
double posY = player_y / TILE_SIZE;
|
||||
|
||||
// initial direction vector
|
||||
double dirX = std::cos(player_angle);
|
||||
double dirY = 0;
|
||||
|
||||
// the 2d raycaster version of camera plane
|
||||
double planeX = 0;
|
||||
double planeY = 0.66;
|
||||
|
||||
// time of current frame
|
||||
double time = 0;
|
||||
// time of previous frame
|
||||
double oldTime = 0;
|
||||
|
||||
for(int x = 0; x < w; x++) {
|
||||
// calculate ray position and direction
|
||||
double cameraX = 2 * x / double(w) - 1; // x-coord in camera space
|
||||
|
@ -155,8 +175,8 @@ void ray_casting(Fenster &window, Matrix& map) {
|
|||
double sideDistY;
|
||||
|
||||
// length of ray from one x or y-side to next x or y-side
|
||||
double deltaDistX = (rayDirX == 0) ? 1e30 : std::abs(1.0 / rayDirX);
|
||||
double deltaDistY = (rayDirY == 0) ? 1e30 : std::abs(1.0 / rayDirY);
|
||||
double deltaDistX = std::abs(1.0 / rayDirX);
|
||||
double deltaDistY = std::abs(1.0 / rayDirY);
|
||||
double perpWallDist;
|
||||
|
||||
int stepX = 0;
|
||||
|
@ -202,6 +222,13 @@ void ray_casting(Fenster &window, Matrix& map) {
|
|||
perpWallDist = (sideDistY - deltaDistY);
|
||||
}
|
||||
|
||||
draw_map_blocks(window, mapX, mapY);
|
||||
|
||||
|
||||
// player direction ray
|
||||
draw_line(window, {size_t(posX * TILE_SIZE), size_t(posY * TILE_SIZE)},
|
||||
{(size_t)mapX * TILE_SIZE, (size_t)mapY * TILE_SIZE}, rgba_color(0, 255, 0, 255));
|
||||
|
||||
int lineHeight = int(h / perpWallDist);
|
||||
|
||||
int drawStart = -lineHeight / 2 + h / 2;
|
||||
|
@ -219,26 +246,6 @@ void ray_casting(Fenster &window, Matrix& map) {
|
|||
}
|
||||
|
||||
|
||||
void map_view_casting(Fenster &window, Matrix& map) {
|
||||
float start_angle = player_angle - HALF_FOV;
|
||||
|
||||
for(int ray = 0; ray < CASTED_RAYS; ray++, start_angle += STEP_ANGLE)
|
||||
{
|
||||
for(int depth = 1; depth < MAX_DEPTH; depth++) {
|
||||
float target_x = player_x - std::sin(start_angle) * depth;
|
||||
float target_y = player_y + std::cos(start_angle) * depth;
|
||||
|
||||
int col = int(target_x / TILE_SIZE);
|
||||
int row = int(target_y / TILE_SIZE);
|
||||
|
||||
if(map[row][col] == 1) {
|
||||
draw_map_rays(window, col, row, {size_t(target_x), size_t(target_y)});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_ceiling_floor(Fenster &window) {
|
||||
draw_rect(window,
|
||||
{size_t(window.width() / 2), 0},
|
||||
|
@ -254,51 +261,59 @@ void draw_everything(Fenster &window) {
|
|||
clear(window);
|
||||
draw_map(window, MAP);
|
||||
draw_ceiling_floor(window);
|
||||
map_view_casting(window, MAP);
|
||||
ray_casting(window, MAP);
|
||||
}
|
||||
|
||||
bool collision(float x, float y) {
|
||||
int col = int(x / TILE_SIZE);
|
||||
int row = int(y / TILE_SIZE);
|
||||
bool empty_space(int new_x, int new_y) {
|
||||
dbc::check((size_t)new_x < matrix::width(MAP),
|
||||
format("x={} too wide={}", new_x, matrix::width(MAP)));
|
||||
dbc::check((size_t)new_y < matrix::height(MAP),
|
||||
format("y={} too high={}", new_y, matrix::height(MAP)));
|
||||
|
||||
return MAP[row][col] == 1;
|
||||
return MAP[new_y][new_x] == 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Fenster window(SCREEN_WIDTH, SCREEN_HEIGHT, "Fenscaster");
|
||||
const int fps = 60;
|
||||
double moveSpeed = 0.1;
|
||||
double rotSpeed = 0.1;
|
||||
|
||||
while(window.loop(60)) {
|
||||
load_textures();
|
||||
|
||||
while(window.loop(fps)) {
|
||||
draw_everything(window);
|
||||
|
||||
float x = player_x;
|
||||
float y = player_y;
|
||||
|
||||
if(window.key('Q')) {
|
||||
player_angle -= 0.1;
|
||||
} else if(window.key('E')) {
|
||||
player_angle += 0.1;
|
||||
if(window.key('W')) {
|
||||
if(empty_space(int(posX + dirX * moveSpeed), int(posY))) posX += dirX * moveSpeed;
|
||||
if(empty_space(int(posX), int(posY + dirY * moveSpeed))) posY += dirY * moveSpeed;
|
||||
} else if(window.key('S')) {
|
||||
if(empty_space(int(posX - dirX * moveSpeed), int(posY))) posX -= dirX * moveSpeed;
|
||||
if(empty_space(int(posX), int(posY - dirY * moveSpeed))) posY -= dirY * moveSpeed;
|
||||
}
|
||||
|
||||
if(window.key('W')) {
|
||||
x += -1 * std::sin(player_angle) * 5;
|
||||
y += std::cos(player_angle) * 5;
|
||||
} else if(window.key('S')) {
|
||||
x -= -1 * std::sin(player_angle) * 5;
|
||||
y -= std::cos(player_angle) * 5;
|
||||
if(window.key('E')) {
|
||||
double oldDirX = dirX;
|
||||
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
|
||||
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
|
||||
|
||||
double oldPlaneX = planeX;
|
||||
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
|
||||
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
||||
} else if(window.key('Q')) {
|
||||
double oldDirX = dirX;
|
||||
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
|
||||
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
|
||||
|
||||
double oldPlaneX = planeX;
|
||||
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
|
||||
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
|
||||
}
|
||||
|
||||
if(window.key('D')) {
|
||||
x += -1 * std::sin(player_angle + std::numbers::pi * 0.5) * 5;
|
||||
y += std::cos(player_angle + std::numbers::pi * 0.5) * 5;
|
||||
println("NOT IMPLEMENTED USE E");
|
||||
} else if(window.key('A')) {
|
||||
x -= -1 * std::sin(player_angle + std::numbers::pi * 0.5) * 5;
|
||||
y -= std::cos(player_angle + std::numbers::pi * 0.5) * 5;
|
||||
}
|
||||
|
||||
if(!collision(x, y)) {
|
||||
player_x = x;
|
||||
player_y = y;
|
||||
println("NOT IMPLEMENTED USE Q");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,10 +84,10 @@ int main(int /*argc*/, char */*argv*/[])
|
|||
std::vector<Uint32> texture[8];
|
||||
for(int i = 0; i < 8; i++) texture[i].resize(texWidth * texHeight);
|
||||
|
||||
screen(screenWidth,screenHeight, 0, "Raycaster");
|
||||
screen(screenWidth, screenHeight, 0, "Raycaster");
|
||||
|
||||
//generate some textures
|
||||
#if 0
|
||||
#if 1
|
||||
for(int x = 0; x < texWidth; x++)
|
||||
for(int y = 0; y < texHeight; y++)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue