Annotate the ceiling/floor rendering.

master
Zed A. Shaw 3 months ago
parent 942bd052d4
commit eee66720b7
  1. 30
      sfmlcaster.cpp

@ -37,6 +37,9 @@ 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=0;
// I chose fixed textures for this instead
const int floorTexture = 3;
const int ceilingTexture = 6;
float player_x = SCREEN_WIDTH / 4;
float player_y = SCREEN_WIDTH / 4;
@ -288,39 +291,60 @@ void draw_ceiling_floor(sf::RenderWindow &window) {
int screenWidth = THREED_VIEW_HEIGHT;
for(int y = screenHeight / 2 + 1; y < screenHeight; ++y) {
// rayDir for leftmost ray (x=0) and rightmost (x = w)
float rayDirX0 = dirX - planeX;
float rayDirY0 = dirY - planeY;
float rayDirX1 = dirX + planeX;
float rayDirY1 = dirY + planeY;
// current y position compared to the horizon
int p = y - screenHeight / 2;
// vertical position of the camera
// 0.5 will the camera at the center horizon. For a
// different value you need a separate loop for ceiling
// and floor since they're no longer symmetrical.
float posZ = 0.5 * screenHeight;
// horizontal distance from the camera to the floor for the current row
// 0.5 is the z position exactly in the middle between floor and ceiling
// See NOTE in Lode's code for more.
float rowDistance = posZ / p;
// calculate the real world step vector we have to add for each x (parallel to camera plane)
// adding step by step avoids multiplications with a wight in the inner loop
float floorStepX = rowDistance * (rayDirX1 - rayDirX0) / screenWidth;
float floorStepY = rowDistance * (rayDirY1 - rayDirY0) / screenWidth;
// real world coordinates of the leftmost column.
// This will be updated as we step to the right
float floorX = posX + rowDistance * rayDirX0;
float floorY = posY + rowDistance * rayDirY0;
for(int x = 0; x < screenWidth; ++x) {
// the cell coord is simply taken from the int parts of
// floorX and floorY.
int cellX = int(floorX);
int cellY = int(floorY);
// get the texture coordinat from the fractional part
int tx = int(texWidth * (floorX - cellX)) & (texWidth - 1);
int ty = int(texWidth * (floorY - cellY)) & (texHeight - 1);
floorX += floorStepX;
floorY += floorStepY;
int floorTexture = 3;
int ceilingTexture = 6;
// now get the pixel from the texture
uint32_t color;
// this uses the previous ty/tx fractional parts of
// floorX cellX to find the texture x/y. How?
// FLOOR
color = texture[floorTexture][texWidth * ty + tx];
pixels[pixcoord(x, y)] = color;
// CEILING
color = texture[ceilingTexture][texWidth * ty + tx];
pixels[pixcoord(x, screenHeight - y - 1)] = color;
}

Loading…
Cancel
Save