tron-room-demo: true depth fog (aerial perspective)

The old 'fog' was multiplicative darkening (color scaled toward black),
which reads as shading rather than atmosphere. Geometry now blends
toward a steel haze color (COL_FOG) with camera distance: fogAmt()
maps distance to a 0..32 fixed-point amount between FOG_NEAR/FOG_FAR,
and fogMix() lerps the 5/6/5 channels toward the haze with rounding.
Applied to box faces + top rims, floor quads, network links and
packets. Near geometry stays crisp and saturated; the far corners of
the deck dissolve into the haze, giving a real depth cue.
This commit is contained in:
Dejvino
2026-11-16 22:53:20 +01:00
parent c58f31d02c
commit 89250d7f19
2 changed files with 36 additions and 14 deletions
+4 -1
View File
@@ -25,7 +25,10 @@ push, which on the device ships the 565 buffer as-is).
radius/height.
- `#` walls, `o` cover crates, the `+` blast door and the `T` terminal
as solid shaded boxes: backface culling, painter's-algorithm depth
sort, lambert lighting from above, distance fog, bright top rims.
sort, lambert lighting from above, bright top rims, and depth fog —
geometry fades toward a steel haze color with camera distance
(aerial perspective), so the far deck dissolves atmospherically
instead of just darkening.
- Floor tiles as lit quads with a subtle checker.
- The demo map's **network graph** as glowing pylons with teal link
lines and cyan packets traveling along the edges.
+32 -13
View File
@@ -70,15 +70,34 @@ static inline uint16_t shade565(uint16_t c, int f32) {
return (uint16_t)((r << 11) | (g << 5) | b);
}
// Fog factor as a 0..32 fixed-point factor for a camera-space distance.
static const int FAR_D = 44; // fog distance (camera units)
static inline int fogFor(float d) {
int f = 32 - (int)(d * 32.0f / FAR_D);
if (f < 19) f = 19; // floor at ~0.60 — distant walls never vanish
// --- Depth fog (aerial perspective) -------------------------------------------
// Geometry fades toward a haze color with camera distance, instead of
// merely darkening: near surfaces stay crisp and saturated, the far deck
// dissolves into the haze. Integer fixed-point on the 5/6/5 channels.
static const uint16_t COL_FOG = 0x4AAD; // cool steel haze (76,88,104)
static const float FOG_NEAR = 16.0f; // distance where fog starts
static const float FOG_FAR = 50.0f; // distance of full fog
// Fog amount: 0 (clear) .. 32 (fully fogged) for a camera-space distance.
static inline int fogAmt(float d) {
int f = (int)((d - FOG_NEAR) * 32.0f / (FOG_FAR - FOG_NEAR));
if (f < 0) f = 0;
if (f > 32) f = 32;
return f;
}
// Blend a native 565 color toward the fog color by a 0..32 fixed-point amount.
static inline uint16_t fogMix(uint16_t c, int f32) {
if (f32 <= 0) return c;
if (f32 > 32) f32 = 32;
int r = (c >> 11) & 31, g = (c >> 5) & 63, b = c & 31;
int fr = (COL_FOG >> 11) & 31, fg = (COL_FOG >> 5) & 63, fb = COL_FOG & 31;
r += ((fr - r) * f32 + 16) >> 5;
g += ((fg - g) * f32 + 32) >> 6;
b += ((fb - b) * f32 + 16) >> 5;
return (uint16_t)((r << 11) | (g << 5) | b);
}
// --- Drawing helpers (uint16_t throughout) -----------------------------------
// The buffer holds pixels in PANEL WIRE ORDER: the ST7789 receives bytes
// big-endian (high byte first), and TFT_eSprite's pushSprite sends the
@@ -228,7 +247,7 @@ static void drawBox(const Box &b, float dist) {
};
int px[8], py[8];
bool vis[8];
int fog = fogFor(dist);
int fog = fogAmt(dist);
int minX = W, maxX = -1, minY = H, maxY = -1;
for (int i = 0; i < 8; i++) {
float X = b.x + SIGN[i][0] * b.hx;
@@ -259,14 +278,14 @@ static void drawBox(const Box &b, float dist) {
if (nd <= 0.001f) continue;
float ndl = NORMALS[f][0] * LIGHT[0] + NORMALS[f][1] * LIGHT[1] + NORMALS[f][2] * LIGHT[2];
if (ndl < 0.55f) ndl = 0.55f;
uint16_t col = shade565(b.col, (int)(ndl * fog));
uint16_t col = fogMix(shade565(b.col, (int)(ndl * 32.0f)), fog);
fillQuad((float)px[q[0]], (float)py[q[0]],
(float)px[q[1]], (float)py[q[1]],
(float)px[q[2]], (float)py[q[2]],
(float)px[q[3]], (float)py[q[3]], col);
// bright rim on the top edges (f == 0 is the top face)
if (f == 0) {
uint16_t rim = shade565(b.col, (int)((0.55f + 0.45f * ndl) * fog));
uint16_t rim = fogMix(shade565(b.col, (int)((0.55f + 0.45f * ndl) * 32.0f)), fog);
drawLine(px[q[0]], py[q[0]], px[q[1]], py[q[1]], rim);
drawLine(px[q[1]], py[q[1]], px[q[2]], py[q[2]], rim);
drawLine(px[q[2]], py[q[2]], px[q[3]], py[q[3]], rim);
@@ -401,9 +420,9 @@ static void rvFrame(float dt, unsigned long nowMs) {
float cx = tx + 0.5f, cy = ty + 0.5f;
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],
shade565(base, fogFor(sqrtf((cx - camPos[0]) * (cx - camPos[0]) +
(cy - camPos[1]) * (cy - camPos[1]) +
camPos[2] * camPos[2]))));
fogMix(base, fogAmt(sqrtf((cx - camPos[0]) * (cx - camPos[0]) +
(cy - camPos[1]) * (cy - camPos[1]) +
camPos[2] * camPos[2]))));
}
}
@@ -482,7 +501,7 @@ static void rvFrame(float dt, unsigned long nowMs) {
float d0, d1;
if (!project(ax, ay, 1.9f, x0, y0, d0)) continue;
if (!project(bx, by, 1.9f, x1, y1, d1)) continue;
drawLine(x0, y0, x1, y1, shade565(COL_LINK, fogFor((d0 + d1) * 0.5f)));
drawLine(x0, y0, x1, y1, fogMix(COL_LINK, fogAmt((d0 + d1) * 0.5f)));
// packet: a small bright dot sweeping the edge
float ph = fmodf(nowMs / 1000.0f * 0.35f + e * 0.37f, 1.0f);
int pkx, pky;
@@ -491,7 +510,7 @@ static void rvFrame(float dt, unsigned long nowMs) {
int r = (int)(0.14f * FX / pdd);
if (r < 1) r = 1;
if (r > 4) r = 4;
fillCircle(pkx, pky, r, shade565(COL_PACKET, fogFor(pdd)));
fillCircle(pkx, pky, r, fogMix(COL_PACKET, fogAmt(pdd)));
}
}