Implemented the next part of the tutorial with floors and ceilings.

This commit is contained in:
Zed A. Shaw 2025-01-13 14:24:19 -05:00
parent e8803f0ad7
commit 1d6458ba19
2 changed files with 463 additions and 13 deletions

View file

@ -12,15 +12,15 @@ using matrix::Matrix;
using namespace fmt;
Matrix MAP{
{2,2,2,2,2,2,2,2,2},
{2,0,8,0,0,0,0,0,2},
{2,0,7,0,0,5,6,0,2},
{2,0,0,0,0,0,0,0,2},
{2,2,0,0,0,0,0,2,2},
{2,0,0,1,3,4,0,0,2},
{2,0,0,0,0,0,2,2,2},
{2,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2}
{8,8,8,8,8,8,8,8,8},
{8,0,2,0,0,0,0,0,8},
{8,0,7,0,0,5,6,0,8},
{8,0,0,0,0,0,0,0,8},
{8,8,0,0,0,0,0,8,8},
{8,0,0,1,3,4,0,0,8},
{8,0,0,0,0,0,8,8,8},
{8,0,0,0,0,0,0,0,8},
{8,8,8,8,8,8,8,8,8}
};
const int SCREEN_HEIGHT=480;
@ -36,7 +36,7 @@ const int CASTED_RAYS=120;
const float STEP_ANGLE = FOV / CASTED_RAYS;
const int MAX_DEPTH = MAP_SIZE * TILE_SIZE;
const float SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS;
int PITCH=25;
int PITCH=0;
float player_x = SCREEN_WIDTH / 4;
float player_y = SCREEN_WIDTH / 4;
@ -285,14 +285,56 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
}
void draw_ceiling_floor(sf::RenderWindow &window) {
int screenHeight = THREED_VIEW_HEIGHT;
int screenWidth = THREED_VIEW_HEIGHT;
for(int y = screenHeight / 2 + 1; y < screenHeight; ++y) {
float rayDirX0 = dirX - planeX;
float rayDirY0 = dirY - planeY;
float rayDirX1 = dirX + planeX;
float rayDirY1 = dirY + planeY;
int p = y - screenHeight / 2;
float posZ = 0.5 * screenHeight;
float rowDistance = posZ / p;
float floorStepX = rowDistance * (rayDirX1 - rayDirX0) / screenWidth;
float floorStepY = rowDistance * (rayDirY1 - rayDirY0) / screenWidth;
float floorX = posX + rowDistance * rayDirX0;
float floorY = posY + rowDistance * rayDirY0;
for(int x = 0; x < screenWidth; ++x) {
int cellX = int(floorX);
int cellY = int(floorY);
int tx = int(texWidth * (floorX - cellX)) & (texWidth - 1);
int ty = int(texWidth * (floorY - cellY)) & (texHeight - 1);
floorX += floorStepX;
floorY += floorStepY;
int checkerBoardPattern = int(cellX + cellY) & 1;
int floorTexture = checkerBoardPattern == 0 ? 3 : 4;
int ceilingTexture = 6;
uint32_t color;
color = texture[floorTexture][texWidth * ty + tx];
pixels[pixcoord(x, y)] = color;
color = texture[ceilingTexture][texWidth * ty + tx];
pixels[pixcoord(x, screenHeight - y - 1)] = color;
}
}
}
void draw_everything(sf::RenderWindow &window) {
clear(window);
draw_map(window, MAP);
draw_ceiling_floor(window);
//ray_casting(window, MAP);
ray_casting(window, MAP);
draw_pixel_buffer(window);
window.display();
}
@ -350,9 +392,11 @@ int main() {
}
if(KB::isKeyPressed(KB::E)) {
PITCH = std::clamp(PITCH + 10, -60, 240);
println("PITCH DISABLED");
// PITCH = std::clamp(PITCH + 10, -60, 240);
} else if(KB::isKeyPressed(KB::Q)) {
PITCH = std::clamp(PITCH - 10, -60, 240);
println("PITCH DISABLED");
// PITCH = std::clamp(PITCH - 10, -60, 240);
}
sf::Event event;