tron-room-demo: comments
This commit is contained in:
@@ -407,24 +407,44 @@ static void rvFrame(float dt, unsigned long nowMs) {
|
|||||||
// --- floor: flat lit quads for every non-wall tile, checker shaded ---
|
// --- floor: flat lit quads for every non-wall tile, checker shaded ---
|
||||||
for (int ty = 0; ty < nd::TILE_ROWS; ty++) {
|
for (int ty = 0; ty < nd::TILE_ROWS; ty++) {
|
||||||
for (int tx = 0; tx < nd::TILE_COLS; tx++) {
|
for (int tx = 0; tx < nd::TILE_COLS; tx++) {
|
||||||
|
// Skip wall tiles
|
||||||
char ch = mapSource.tileAt(tx, ty);
|
char ch = mapSource.tileAt(tx, ty);
|
||||||
if (ch == '#') continue;
|
if (ch == '#') continue;
|
||||||
|
|
||||||
|
// Checkerboard pattern: alternate between two floor colors based on tile position
|
||||||
uint16_t base = ((tx + ty) & 1) ? COL_FLOOR_B : COL_FLOOR_A;
|
uint16_t base = ((tx + ty) & 1) ? COL_FLOOR_B : COL_FLOOR_A;
|
||||||
|
|
||||||
|
// Screen space projection coordinates for each of the 4 tile corners
|
||||||
int px[4], py[4];
|
int px[4], py[4];
|
||||||
float d;
|
float d;
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
|
|
||||||
|
// Offset arrays defining the 4 corners of a tile: bottom-left, bottom-right, top-right, top-left
|
||||||
const float OFF[4][2] = { {0,0},{1,0},{1,1},{0,1} };
|
const float OFF[4][2] = { {0,0},{1,0},{1,1},{0,1} };
|
||||||
|
|
||||||
|
// Project each corner from world space to screen space
|
||||||
for (int k = 0; k < 4; k++)
|
for (int k = 0; k < 4; k++)
|
||||||
if (!project((float)(tx + OFF[k][0]), (float)(ty + OFF[k][1]), 0.0f, px[k], py[k], d)) { ok = false; break; }
|
if (!project((float)(tx + OFF[k][0]), (float)(ty + OFF[k][1]), 0.0f, px[k], py[k], d)) { ok = false; break; }
|
||||||
if (!ok) continue;
|
if (!ok) continue; // Skip if any projection failed
|
||||||
|
|
||||||
|
// Skip this tile if all projected corners are off-screen to the left
|
||||||
if (px[0] < -8 && px[1] < -8 && px[2] < -8 && px[3] < -8) continue;
|
if (px[0] < -8 && px[1] < -8 && px[2] < -8 && px[3] < -8) continue;
|
||||||
|
// Skip this tile if all projected corners are off-screen to the right
|
||||||
if (px[0] > W + 8 && px[1] > W + 8 && px[2] > W + 8 && px[3] > W + 8) continue;
|
if (px[0] > W + 8 && px[1] > W + 8 && px[2] > W + 8 && px[3] > W + 8) continue;
|
||||||
|
|
||||||
|
// Compute the center of the current tile for fog calculation
|
||||||
float cx = tx + 0.5f, cy = ty + 0.5f;
|
float cx = tx + 0.5f, cy = ty + 0.5f;
|
||||||
|
|
||||||
|
// Calculate distance from camera to tile center for fog effect
|
||||||
|
// sqrtf of squared distances in X, Y, and Z (camera height)
|
||||||
|
float dist = sqrtf((cx - camPos[0]) * (cx - camPos[0]) +
|
||||||
|
(cy - camPos[1]) * (cy - camPos[1]) +
|
||||||
|
camPos[2] * camPos[2]);
|
||||||
|
|
||||||
|
// Apply fog coloring based on distance, mixed with the base floor color
|
||||||
fillQuad((float)px[0], (float)py[0], (float)px[1], (float)py[1],
|
fillQuad((float)px[0], (float)py[0], (float)px[1], (float)py[1],
|
||||||
(float)px[2], (float)py[2], (float)px[3], (float)py[3],
|
(float)px[2], (float)py[2], (float)px[3], (float)py[3],
|
||||||
fogMix(base, fogAmt(sqrtf((cx - camPos[0]) * (cx - camPos[0]) +
|
fogMix(base, fogAmt(dist)));
|
||||||
(cy - camPos[1]) * (cy - camPos[1]) +
|
|
||||||
camPos[2] * camPos[2]))));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user