/** * RoomView — top-down "3D" flythrough of NetDeck's demo map. * * Target: T-Deck emulator (320x240, RGB565 device framebuffer). * * Contract with the emulator host (src/main.cpp): * - implement setup() — called once at launch * - implement loop() — called once per frame (host paces at ~60 fps) * - write pixels into emuGfx::fb[] (320x240, ABGR8888 uint32) * - 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 * graph, entity spawn list). Nothing about the map is duplicated here — * edit the demo map in the firmware tree and this view follows it. * * Rendering (all software, no GPU): * - a true 3D orbiting camera: position + look-at basis, full * perspective divide, looking down at the deck from above * - walls / crates / doors / terminals as solid shaded boxes with * backface culling, painter's-algorithm depth sorting, lambert * lighting from above and distance fog * - floor tiles as flat lit quads with a subtle checker * - the network graph drawn as glowing pylons with link lines and * data packets traveling along the edges * - entities from the demo spawn list walking their patrol routes * * The camera drifts on its own (slow orbit + breathing radius/height); * keys adjust: a/d orbit speed, w/s zoom, r/f altitude, space pause, * q/ESC quit. */ #include "shim.h" #include "nd_layout.h" #include "nd_mapsource.h" #include "nd_demomap.h" extern uint32_t emuGfx::fb[320 * 240]; extern bool emuGfx::dirty; extern std::deque emu::keyboard; static constexpr int W = 320; static constexpr int H = 240; static constexpr float TILEF = 1.0f; // one map tile = one world unit // --- The "device" framebuffer: RGB565, exactly what the panel gets ---------- static uint16_t fb565[W * H]; // --- 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); } // 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; for (;;) { putPixel(x0, y0, color); if (x0 == x1 && y0 == y1) break; e2 = 2 * err; if (e2 >= dy) { err += dy; x0 += sx; } if (e2 <= dx) { err += dx; y0 += sy; } } } 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); } // Horizontal span with clipping. 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++) 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, 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; } if (y2 - y0 < 0.5f) return; int yA = (int)ceilf(y0), yB = (int)y2; if (yA < 0) yA = 0; if (yB > H - 1) yB = H - 1; for (int y = yA; y <= yB; y++) { float yc = (float)y + 0.5f; float t = (yc - y0) / (y2 - y0); if (t < 0) t = 0; if (t > 1) t = 1; float xa = x0 + (x2 - x0) * t; float xb; if (yc < y1) { float t2 = (y1 - y0 < 0.5f) ? 1.0f : (yc - y0) / (y1 - y0); if (t2 < 0) t2 = 0; if (t2 > 1) t2 = 1; xb = x0 + (x1 - x0) * t2; } else { float t2 = (y2 - y1 < 0.5f) ? 0.0f : (yc - y1) / (y2 - y1); if (t2 < 0) t2 = 0; if (t2 > 1) t2 = 1; xb = x1 + (x2 - x1) * t2; } hline(y, (int)floorf(xa), (int)ceilf(xb), c); } } static void fillQuad(float x0, float y0, float x1, float y1, 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); } // --- Camera ------------------------------------------------------------------ // Orbiting camera: position camPos, looking at lookAt, up = world +Z. // Basis vectors are rebuilt each frame; project() does the perspective divide. 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 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(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] = { r[1] * f[2] - r[2] * f[1], r[2] * f[0] - r[0] * f[2], r[0] * f[1] - r[1] * f[0] }; for (int i = 0; i < 3; i++) { camF[i] = f[i]; camR[i] = r[i]; camU[i] = u[i]; } } // Project a world point (X = west-east, Y = south-north, Z = up). // Returns false if behind the near plane. static inline bool project(float X, float Y, float Z, int &sx, int &sy, float &depth) { float d[3] = { X - camPos[0], Y - camPos[1], Z - camPos[2] }; float rz = d[0] * camF[0] + d[1] * camF[1] + d[2] * camF[2]; if (rz < 0.6f) return false; float rx = d[0] * camR[0] + d[1] * camR[1] + d[2] * camR[2]; float ry = d[0] * camU[0] + d[1] * camU[1] + d[2] * camU[2]; sx = (int)(160.0f + rx * FX / rz); sy = (int)(120.0f - ry * FX / rz); depth = rz; return true; } // --- 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 uint16_t col; // RGB565 base color }; // Face corner indices into an 8-corner box, and world normals. static const int FACES[6][4] = { {0,1,2,3}, // top (+Z) {4,5,6,7}, // bottom (-Z, rarely seen) {0,1,5,4}, // -Y side {2,3,7,6}, // +Y side {1,2,6,5}, // +X side {0,3,7,4}, // -X side }; static const float NORMALS[6][3] = { {0,0,1}, {0,0,-1}, {0,-1,0}, {0,1,0}, {1,0,0}, {-1,0,0}, }; static const float LIGHT[3] = { -0.35f, -0.25f, 0.90f }; // from above, slight SW static void drawBox(const Box &b, float dist) { // 8 corners: bottom 0-3 (CCW seen from above), top 4-7 static const float SIGN[8][3] = { {-1,-1,0},{1,-1,0},{1,1,0},{-1,1,0}, {-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1}, }; int px[8], py[8]; bool vis[8]; 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; float Y = b.y + SIGN[i][1] * b.hy; float Z = SIGN[i][2] ? b.z1 : b.z0; float d; vis[i] = project(X, Y, Z, px[i], py[i], d); if (vis[i]) { if (px[i] < minX) minX = px[i]; if (px[i] > maxX) maxX = px[i]; if (py[i] < minY) minY = py[i]; if (py[i] > maxY) maxY = py[i]; } } if (maxX < 0 || minX >= W || maxY < 0 || minY >= H) return; // offscreen for (int f = 0; f < 6; f++) { const int *q = FACES[f]; bool ok = true; for (int k = 0; k < 4; k++) if (!vis[q[k]]) { ok = false; break; } if (!ok) continue; // backface cull: skip faces pointing away from the camera float fcx = b.x + NORMALS[f][0] * b.hx; float fcy = b.y + NORMALS[f][1] * b.hy; float fcz = (NORMALS[f][2] > 0) ? b.z1 : (NORMALS[f][2] < 0 ? b.z0 : (b.z0 + b.z1) * 0.5f); float toCam[3] = { camPos[0] - fcx, camPos[1] - fcy, camPos[2] - fcz }; float nd = NORMALS[f][0] * toCam[0] + NORMALS[f][1] * toCam[1] + NORMALS[f][2] * toCam[2]; 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)); 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)); 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); drawLine(px[q[3]], py[q[3]], px[q[0]], py[q[0]], rim); } } } // Distance from the camera to a box center (for fog + depth sort). static inline float boxDist(const Box &b) { float dx = b.x - camPos[0], dy = b.y - camPos[1]; float dz = (b.z0 + b.z1) * 0.5f - camPos[2]; return sqrtf(dx * dx + dy * dy + dz * dz); } // --- World state -------------------------------------------------------------- static nd::DemoMapSource mapSource; struct Walker { float ax, ay, bx, by; // patrol endpoints (world units, tile centers) float t; // 0..1 along the route float dir; // +1 or -1 float speed; // tiles per second uint8_t kind; }; static const int MAX_WALKERS = nd::MAX_UNITS > 20 ? nd::MAX_UNITS : 20; static Walker walkers[MAX_WALKERS]; static int walkerCount = 0; static void buildWalkers() { walkerCount = 0; for (int i = 0; i < mapSource.spawnCount() && walkerCount < MAX_WALKERS; i++) { const nd::UnitSpawn &s = mapSource.spawn(i); Walker &w = walkers[walkerCount]; w.ax = (s.tx % nd::TILE_COLS) + 0.5f; w.ay = (s.tx / nd::TILE_COLS) + 0.5f; if (s.patA >= 0 && s.patB >= 0) { w.bx = (s.patA % nd::TILE_COLS) + 0.5f; w.by = (s.patA / nd::TILE_COLS) + 0.5f; } else { w.bx = w.ax; w.by = w.ay; // stationary } w.t = 0; w.dir = 1; w.speed = (s.kind == 2) ? 0.0f : (s.kind == 3 ? 0.0f : 1.1f + (i % 3) * 0.15f); w.kind = s.kind; walkerCount++; } } static inline float lerp(float a, float b, float t) { return a + (b - a) * t; } // --- Setup -------------------------------------------------------------------- void setup() { buildWalkers(); // initial camera pose: south of the map, looking north at the deck camPos[0] = 20.0f; camPos[1] = 12.0f - 22.0f; camPos[2] = 30.0f; } // --- Loop ---------------------------------------------------------------------- void loop() { // --- input --- static float orbitSpeed = 0.10f; // rad/s static bool paused = false; while (!emu::keyboard.empty()) { uint8_t k = emu::keyboard.front(); emu::keyboard.pop_front(); if (k == 'q' || k == 0x1b) { emu::quitRequested = true; } if (k == 'a') { orbitSpeed += 0.03f; if (orbitSpeed > 0.60f) orbitSpeed = 0.60f; } if (k == 'd') { orbitSpeed -= 0.03f; if (orbitSpeed < -0.60f) orbitSpeed = -0.60f; } if (k == 'w') { FX += 18.0f; if (FX > 320.0f) FX = 320.0f; } if (k == 's') { FX -= 18.0f; if (FX < 90.0f) FX = 90.0f; } if (k == 'r') { camPos[2] += 2.0f; if (camPos[2] > 60.0f) camPos[2] = 60.0f; } if (k == 'f') { camPos[2] -= 2.0f; if (camPos[2] < 14.0f) camPos[2] = 14.0f; } if (k == ' ') paused = !paused; } // --- delta time --- static unsigned long lastMs = 0; unsigned long now = millis(); float dt = lastMs ? (now - lastMs) / 1000.0f : 1.0f / 60.0f; lastMs = now; if (dt > 0.1f) dt = 0.1f; // --- camera drift: slow orbit + breathing radius/height --- static float orbit = 0.0f; // angle around the map center static float breathe = 0.0f; if (!paused) { orbit += orbitSpeed * dt; breathe += dt * 0.35f; } float radius = 15.0f + 3.0f * sinf(breathe * 0.7f); float height = 21.0f + 3.5f * sinf(breathe * 0.45f + 1.3f); lookAt[0] = 20.0f + 1.5f * sinf(breathe * 0.5f); lookAt[1] = 12.0f + 1.0f * cosf(breathe * 0.33f); camPos[0] = lookAt[0] + cosf(orbit) * radius; camPos[1] = lookAt[1] + sinf(orbit) * radius; camPos[2] = height; updateBasis(); // --- entities walk their patrol routes --- if (!paused) { for (int i = 0; i < walkerCount; i++) { Walker &w = walkers[i]; if (w.speed <= 0) continue; float len = sqrtf((w.bx - w.ax) * (w.bx - w.ax) + (w.by - w.ay) * (w.by - w.ay)); if (len < 0.01f) continue; w.t += w.dir * w.speed * dt / len; if (w.t > 1) { w.t = 1; w.dir = -1; } if (w.t < 0) { w.t = 0; w.dir = 1; } } } // --- 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; uint16_t base = ((tx + ty) & 1) ? COL_FLOOR_B : COL_FLOOR_A; int px[4], py[4]; float d; bool ok = true; const float OFF[4][2] = { {0,0},{1,0},{1,1},{0,1} }; 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 (!ok) continue; if (px[0] < -8 && px[1] < -8 && px[2] < -8 && px[3] < -8) continue; if (px[0] > W + 8 && px[1] > W + 8 && px[2] > W + 8 && px[3] > W + 8) continue; 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])))); } } // --- collect solid geometry: walls, crates, door, terminal, node pylons --- struct Solid { Box b; }; static Solid solids[1024]; int nSolids = 0; 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; Box b; b.x = tx + 0.5f; b.y = ty + 0.5f; b.z0 = 0.0f; b.hx = b.hy = 0.5f; switch (ch) { case '#': b.z1 = 1.6f; b.col = COL_WALL; break; case 'o': b.z1 = 0.9f; b.col = COL_CRATE; break; case '+': b.z1 = 1.3f; b.col = COL_DOOR; break; case 'T': b.z1 = 0.7f; b.col = COL_TERMINAL; break; case 'A': b.z1 = 0.15f; b.col = COL_AIRLOCK; break; default: continue; } solids[nSolids++].b = b; } } // 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); Box &b = nodeBoxes[i]; b.x = n.tx + 0.5f; b.y = n.ty + 0.5f; b.hx = b.hy = 0.28f; b.z0 = 0.0f; b.z1 = 1.9f; b.col = n.color; // already RGB565, straight from the map source solids[nSolids++].b = b; } // --- entities as small glowing boxes --- static Box entBoxes[MAX_WALKERS]; for (int i = 0; i < walkerCount; i++) { const Walker &w = walkers[i]; Box &b = entBoxes[i]; b.x = lerp(w.ax, w.bx, w.t); b.y = lerp(w.ay, w.by, w.t); b.hx = b.hy = 0.30f; b.z0 = 0.0f; b.z1 = (w.kind == 3) ? 0.45f : (w.kind == 2 ? 0.85f : 0.65f); b.col = (w.kind == 0) ? COL_MARINE : (w.kind == 1) ? COL_DROID : (w.kind == 2) ? COL_HEAVY : COL_TURRET; solids[nSolids++].b = b; } // --- depth sort far -> near (painter's algorithm) --- static float dists[1024 + nd::MAX_NODES + MAX_WALKERS]; static int order[1024 + nd::MAX_NODES + MAX_WALKERS]; for (int i = 0; i < nSolids; i++) { dists[i] = boxDist(solids[i].b); order[i] = i; } for (int a = 1; a < nSolids; a++) { int v = order[a], j = a - 1; while (j >= 0 && dists[order[j]] < dists[v]) { order[j + 1] = order[j]; j--; } order[j + 1] = v; } for (int i = 0; i < nSolids; i++) drawBox(solids[order[i]].b, dists[order[i]]); // --- network links + traveling packets (overhead lines between pylon tops) for (int e = 0; e < mapSource.edgeCount(); e++) { const nd::NetEdgeDef &ed = mapSource.edge(e); const nd::NetNodeDef &na = mapSource.node(ed.a); const nd::NetNodeDef &nb = mapSource.node(ed.b); float ax = na.tx + 0.5f, ay = na.ty + 0.5f; float bx = nb.tx + 0.5f, by = nb.ty + 0.5f; int x0, y0, x1, y1; 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))); // packet: a small bright dot sweeping the edge float ph = fmodf(now / 1000.0f * 0.35f + e * 0.37f, 1.0f); int pkx, pky; float pdd; if (project(lerp(ax, bx, ph), lerp(ay, by, ph), 1.9f + 0.15f, pkx, pky, pdd)) { 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))); } } // --- pulsing halo over the airlock tile (squad entry marker) --- { int ax_, ay_; float dd; 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, 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; }