From d69c387f29d1096d37d627aafeea714f38de164f Mon Sep 17 00:00:00 2001 From: Dejvino Date: Mon, 16 Nov 2026 21:49:31 +0100 Subject: [PATCH] tron-room-demo: render the whole pipeline in RGB565 (real-HW color space) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sketch now draws into a 16-bit 320x240 fb565 — the ST7789 panel's native format — using the RGB565 palette constants straight from nd_layout.h (C_FLOOR, C_WALL, C_COVER, C_DOOR, C_AIRLOCK, C_LINK, C_PACKET, node colors). Shading/fog are integer fixed-point on the 5/6/5 channels so the banding matches what the MCU would produce; the desktop window sees a 565-to-8888 expansion blit that stands in for the SPI push (which ships 565 as-is on hardware). --- tron-room-demo/README.md | 7 + tron-room-demo/examples/RoomView/RoomView.cpp | 150 ++++++++++-------- 2 files changed, 92 insertions(+), 65 deletions(-) diff --git a/tron-room-demo/README.md b/tron-room-demo/README.md index 24407b2..114dd74 100644 --- a/tron-room-demo/README.md +++ b/tron-room-demo/README.md @@ -11,6 +11,13 @@ consumed through the generic `MapSource` interface). There is no local copy of the map — edit the demo map in `src/netdeck` and this view follows it. +The whole render pipeline runs in **RGB565** — the ST7789 panel's native +format — with the palette taken directly from `nd_layout.h`. Shading and +fog are integer fixed-point on the 5/6/5 channels, so the visible color +banding is exactly what real hardware would produce. Only the final +blit expands 565→8888 for the desktop window (the stand-in for the SPI +push, which on the device ships the 565 buffer as-is). + ## What you see - A true 3D orbiting camera (position + look-at basis, perspective diff --git a/tron-room-demo/examples/RoomView/RoomView.cpp b/tron-room-demo/examples/RoomView/RoomView.cpp index 3a550c4..b17cae8 100644 --- a/tron-room-demo/examples/RoomView/RoomView.cpp +++ b/tron-room-demo/examples/RoomView/RoomView.cpp @@ -1,7 +1,7 @@ /** * RoomView — top-down "3D" flythrough of NetDeck's demo map. * - * Target: T-Deck emulator (320x240, ABGR8888 framebuffer). + * Target: T-Deck emulator (320x240, RGB565 device framebuffer). * * Contract with the emulator host (src/main.cpp): * - implement setup() — called once at launch @@ -10,6 +10,15 @@ * - set emuGfx::dirty = true each frame you want presented * - read keys from emu::keyboard (ASCII queue, filled from SDL events) * + * REAL-HW COLOR PIPELINE: everything in this sketch renders into a + * 16-bit RGB565 framebuffer (fb565) — exactly what the T-Deck's ST7789 + * panel consumes over SPI — using the RGB565 palette constants straight + * out of src/netdeck/nd_layout.h. Shading and fog are integer fixed-point + * on 5/6/5 channels, so the banding you see is the banding real hardware + * would show. Only the final blit expands 565 -> 8888 for the desktop + * window; on the device that step doesn't exist, the 565 buffer is pushed + * to the panel as-is. + * * The world is NetDeck's real demo floorplan: the hardcoded 3-room * 40x24-tile Breach & Board map from src/netdeck/nd_demomap.h, consumed * through the generic MapSource interface (tiles, rooms, doors, network @@ -45,28 +54,53 @@ static constexpr int W = 320; static constexpr int H = 240; static constexpr float TILEF = 1.0f; // one map tile = one world unit -// --- Palette (ABGR8888: 0xAABBGGRR) ----------------------------------------- -static const uint32_t COL_FLOOR_A = 0xFF2E1F15; // near-black blue floor -static const uint32_t COL_FLOOR_B = 0xFF3A2A1C; // checker variant -static const uint32_t COL_WALL = 0xFFD8B090; // steel blue-gray hull -static const uint32_t COL_CRATE = 0xFF4A5A66; // industrial brown-gray -static const uint32_t COL_DOOR = 0xFF00A0FF; // amber blast door -static const uint32_t COL_TERMINAL = 0xFF40E060; // green terminal glow -static const uint32_t COL_AIRLOCK = 0xFFFFFF00; // cyan airlock ring -static const uint32_t COL_MARINE = 0xFFFFFF00; // cyan squad -static const uint32_t COL_DROID = 0xFF00A0FF; // orange droid -static const uint32_t COL_HEAVY = 0xFF0000FF; // red heavy -static const uint32_t COL_TURRET = 0xFFFF00FF; // magenta turret -static const uint32_t COL_LINK = 0xFFCF8B2B; // teal net link (dim) -static const uint32_t COL_PACKET = 0xFFBF0707; // cyan packet +// --- The "device" framebuffer: RGB565, exactly what the panel gets ---------- +static uint16_t fb565[W * H]; -// --- Drawing helpers (from the template sketch toolbox) ---------------------- -static inline void putPixel(int x, int y, uint32_t c) { - if (x < 0 || x >= W || y < 0 || y >= H) return; - emuGfx::fb[y * W + x] = c; +// --- Palette: RGB565 straight from NetDeck's firmware palette --------------- +static const uint16_t COL_BG = 0x0002; // near-black navy void +static const uint16_t COL_FLOOR_A = nd::C_FLOOR; // near-black blue corridor floor +static const uint16_t COL_FLOOR_B = 0x2146; // checker variant (32,40,48) +static const uint16_t COL_WALL = nd::C_WALL; // steel blue-gray hull +static const uint16_t COL_CRATE = nd::C_COVER; // industrial crate brown-gray +static const uint16_t COL_DOOR = nd::C_DOOR; // amber blast door +static const uint16_t COL_TERMINAL = nd::C_GOOD; // green terminal glow +static const uint16_t COL_AIRLOCK = nd::C_AIRLOCK; // cyan airlock ring +static const uint16_t COL_MARINE = nd::C_AIRLOCK; // cyan squad +static const uint16_t COL_DROID = 0xFD20; // orange droid +static const uint16_t COL_HEAVY = nd::C_HOT; // red heavy +static const uint16_t COL_TURRET = nd::C_HIVE; // magenta turret +static const uint16_t COL_LINK = nd::C_LINK; // teal network link trace +static const uint16_t COL_PACKET = nd::C_PACKET;// cyan data packet + +// --- RGB565 shading ---------------------------------------------------------- +// Fixed-point brightness scale: factor 0..32 (5 bits), applied per channel. +// Integer-only, per-pixel — the way it would be done on the MCU. +static inline uint16_t shade565(uint16_t c, int f32) { + if (f32 < 0) f32 = 0; + if (f32 > 32) f32 = 32; + int r = (((c >> 11) & 31) * f32) >> 5; if (r > 31) r = 31; + int g = (((c >> 5) & 63) * f32) >> 5; if (g > 63) g = 63; + int b = ((c & 31) * f32) >> 5; if (b > 31) b = 31; + return (uint16_t)((r << 11) | (g << 5) | b); } -static void drawLine(int x0, int y0, int x1, int y1, uint32_t color) { +// 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 + if (f > 32) f = 32; + return f; +} + +// --- Drawing helpers (uint16_t throughout) ----------------------------------- +static inline void putPixel(int x, int y, uint16_t c) { + if (x < 0 || x >= W || y < 0 || y >= H) return; + fb565[y * W + x] = c; +} + +static void drawLine(int x0, int y0, int x1, int y1, uint16_t color) { int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1; int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1; int err = dx + dy, e2; @@ -79,34 +113,24 @@ static void drawLine(int x0, int y0, int x1, int y1, uint32_t color) { } } -static void fillCircle(int cx, int cy, int r, uint32_t color) { +static void fillCircle(int cx, int cy, int r, uint16_t color) { for (int y = -r; y <= r; y++) for (int x = -r; x <= r; x++) if (x * x + y * y <= r * r) putPixel(cx + x, cy + y, color); } -// Scale an ABGR color's RGB channels by f (fog / shading). -static inline uint32_t dimColor(uint32_t c, float f) { - if (f < 0) f = 0; - if (f > 1) f = 1; - uint32_t r = (uint32_t)((c & 0xFF) * f); - uint32_t g = (uint32_t)(((c >> 8) & 0xFF) * f); - uint32_t b = (uint32_t)(((c >> 16) & 0xFF) * f); - return 0xFF000000u | (b << 16) | (g << 8) | r; -} - // Horizontal span with clipping. -static inline void hline(int y, int xa, int xb, uint32_t c) { +static inline void hline(int y, int xa, int xb, uint16_t c) { if (y < 0 || y >= H) return; if (xa > xb) { int t = xa; xa = xb; xb = t; } if (xb < 0 || xa >= W) return; if (xa < 0) xa = 0; if (xb >= W) xb = W - 1; - for (int x = xa; x <= xb; x++) emuGfx::fb[y * W + x] = c; + for (int x = xa; x <= xb; x++) fb565[y * W + x] = c; } // Scanline triangle fill (float vertices, clips to screen). -static void fillTri(float x0, float y0, float x1, float y1, float x2, float y2, uint32_t c) { +static void fillTri(float x0, float y0, float x1, float y1, float x2, float y2, uint16_t c) { if (y1 < y0) { float t; t = x0; x0 = x1; x1 = t; t = y0; y0 = y1; y1 = t; } if (y2 < y0) { float t; t = x0; x0 = x2; x2 = t; t = y0; y0 = y2; y2 = t; } if (y2 < y1) { float t; t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; } @@ -137,7 +161,7 @@ static void fillTri(float x0, float y0, float x1, float y1, float x2, float y2, } static void fillQuad(float x0, float y0, float x1, float y1, - float x2, float y2, float x3, float y3, uint32_t c) { + float x2, float y2, float x3, float y3, uint16_t c) { fillTri(x0, y0, x1, y1, x2, y2, c); fillTri(x0, y0, x2, y2, x3, y3, c); } @@ -149,17 +173,14 @@ static float camPos[3] = { 20, -6, 30 }; static float lookAt[3] = { 20, 12, 0 }; static float camF[3], camR[3], camU[3]; static float FX = 235.0f; // focal length (zoom) -static const float FAR_D = 44.0f; // fog distance (camera units) static void updateBasis() { // forward = normalize(lookAt - camPos) float f[3] = { lookAt[0] - camPos[0], lookAt[1] - camPos[1], lookAt[2] - camPos[2] }; float fl = sqrtf(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]); for (int i = 0; i < 3; i++) f[i] /= fl; - // right = normalize(cross(f, worldUp)); up = cross(right, f) - float r[3] = { f[1] * 1.0f - f[2] * 0.0f, f[2] * 0.0f - f[0] * 1.0f, f[0] * 0.0f - f[1] * 0.0f }; - // cross(f, (0,0,1)) = (f.y*1 - f.z*0, f.z*0 - f.x*1, f.x*0 - f.y*0) - r[0] = f[1]; r[1] = -f[0]; r[2] = 0; + // right = normalize(cross(f, worldUp(0,0,1))) + float r[3] = { f[1], -f[0], 0 }; float rl = sqrtf(r[0] * r[0] + r[1] * r[1]); if (rl < 1e-4f) { r[0] = 1; r[1] = 0; r[2] = 0; } else { r[0] /= rl; r[1] /= rl; } float u[3] = { @@ -184,20 +205,12 @@ static inline bool project(float X, float Y, float Z, int &sx, int &sy, float &d return true; } -// Depth fog factor for a camera-space distance. -static inline float fogFor(float d) { - float f = 1.0f - d / FAR_D; - if (f < 0.60f) f = 0.60f; - if (f > 1.0f) f = 1.0f; - return f; -} - // --- Solid shaded box (painter's algorithm, backface culled) ----------------- struct Box { float x, y; // center on the floor float hx, hy; // half extents float z0, z1; // base / top height - uint32_t col; + uint16_t col; // RGB565 base color }; // Face corner indices into an 8-corner box, and world normals. @@ -222,7 +235,7 @@ static void drawBox(const Box &b, float dist) { }; int px[8], py[8]; bool vis[8]; - float fog = fogFor(dist); + int fog = fogFor(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; @@ -253,14 +266,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; - uint32_t col = dimColor(b.col, ndl * fog); + uint16_t col = shade565(b.col, (int)(ndl * 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) { - uint32_t rim = dimColor(b.col, (0.55f + 0.45f * ndl) * fog); + uint16_t rim = shade565(b.col, (int)((0.55f + 0.45f * ndl) * 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); @@ -375,15 +388,15 @@ void loop() { } } - // --- clear framebuffer --- - for (int i = 0; i < W * H; i++) emuGfx::fb[i] = 0xFF080014; + // --- clear the 565 device framebuffer --- + for (int i = 0; i < W * H; i++) fb565[i] = COL_BG; // --- floor: flat lit quads for every non-wall tile, checker shaded --- for (int ty = 0; ty < nd::TILE_ROWS; ty++) { for (int tx = 0; tx < nd::TILE_COLS; tx++) { char ch = mapSource.tileAt(tx, ty); if (ch == '#') continue; - uint32_t base = ((tx + ty) & 1) ? COL_FLOOR_B : COL_FLOOR_A; + uint16_t base = ((tx + ty) & 1) ? COL_FLOOR_B : COL_FLOOR_A; int px[4], py[4]; float d; bool ok = true; @@ -396,7 +409,7 @@ void loop() { 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], - dimColor(base, fogFor(sqrtf((cx - camPos[0]) * (cx - camPos[0]) + + shade565(base, fogFor(sqrtf((cx - camPos[0]) * (cx - camPos[0]) + (cy - camPos[1]) * (cy - camPos[1]) + camPos[2] * camPos[2])))); } @@ -427,7 +440,7 @@ void loop() { } } - // network nodes: glowing pylons at their pinned tiles + // network nodes: glowing pylons at their pinned tiles (palette passthrough) static Box nodeBoxes[nd::MAX_NODES]; for (int i = 0; i < mapSource.nodeCount(); i++) { const nd::NetNodeDef &n = mapSource.node(i); @@ -436,11 +449,7 @@ void loop() { b.hx = b.hy = 0.28f; b.z0 = 0.0f; b.z1 = 1.9f; - // RGB565 -> ABGR8888 - uint32_t r5 = ((n.color >> 11) & 31) * 8; - uint32_t g6 = ((n.color >> 5) & 63) * 4; - uint32_t b5 = (n.color & 31) * 8; - b.col = 0xFF000000u | (b5 << 16) | (g6 << 8) | r5; + b.col = n.color; // already RGB565, straight from the map source solids[nSolids++].b = b; } @@ -481,7 +490,7 @@ void loop() { 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, dimColor(COL_LINK, fogFor((d0 + d1) * 0.5f))); + drawLine(x0, y0, x1, y1, shade565(COL_LINK, fogFor((d0 + d1) * 0.5f))); // packet: a small bright dot sweeping the edge float ph = fmodf(now / 1000.0f * 0.35f + e * 0.37f, 1.0f); int pkx, pky; @@ -490,7 +499,7 @@ void loop() { int r = (int)(0.14f * FX / pdd); if (r < 1) r = 1; if (r > 4) r = 4; - fillCircle(pkx, pky, r, dimColor(COL_PACKET, fogFor(pdd))); + fillCircle(pkx, pky, r, shade565(COL_PACKET, fogFor(pdd))); } } @@ -501,9 +510,20 @@ void loop() { const nd::NetNodeDef &root = mapSource.node(mapSource.rootNode()); if (project(root.tx + 0.5f, root.ty + 0.5f, 0.2f, ax_, ay_, dd)) { int r = (int)(0.55f * FX / dd) + (int)(2.0f * sinf(now / 1000.0f * 3.0f)); - if (r > 2) fillCircle(ax_, ay_, r, (dimColor(COL_AIRLOCK, 0.35f) & 0x00FFFFFF) | 0x50000000); + if (r > 2) fillCircle(ax_, ay_, r, shade565(COL_AIRLOCK, 11)); } } + // --- "SPI push": expand the 565 device buffer into the emulator's 8888 --- + // texture. On the T-Deck this step doesn't exist — fb565 is what the + // panel receives. This is purely a desktop-presentation shim. + for (int i = 0; i < W * H; i++) { + uint16_t c = fb565[i]; + uint32_t r5 = (c >> 11) & 31, g6 = (c >> 5) & 63, b5 = c & 31; + uint32_t r = (r5 << 3) | (r5 >> 2); // 5 -> 8 bit expansion + uint32_t g = (g6 << 2) | (g6 >> 4); // 6 -> 8 bit expansion + uint32_t b = (b5 << 3) | (b5 >> 2); + emuGfx::fb[i] = 0xFF000000u | (b << 16) | (g << 8) | r; + } emuGfx::dirty = true; }