Inside the 3D Cube/Torus Morph & Env Mapping.
How Nation0 achieves real-time 3D geometry morphing between a subdivided cube and a torus, computes dynamic sphere environment mapping (chrome reflection) from rotated surface normals, sorts triangular facets, and rasterizes affine texture spans using fixed-point arithmetic in DOS Mode 13h.
The interactive preview uses the original executable’s 384 vertices, 768 triangles, and vertex correspondence. Coordinate interpolation uses the original separate signed shifts and 511-complement. Shading normals, RGB environment texture, rotation timing, and average-Z sorting remain illustrative; they are not the original rendering pipeline. Its depth view colors visible facets by camera distance; no Z-buffer is used.
Interactive Pipeline Inspector
Rendering Pipeline Stages
Click a stage to inspect its specific math and memory registers.
Dual-Stream Vertex & Normal Morphing
Every frame, the CPU blends vertex positions and surface normals between the cube coordinates at 0x34592 and torus coordinates at 0x32a92 using fixed-point weights t and (511 - t).
3×3 Matrix Rotation & Composition
Generates yaw, pitch, and roll rotation matrices (`fn_37a2a`) and composes them into a composite 3× 3 transformation matrix (`fn_37b68`).
fn_37a2a & fn_37b68 @ 0x37a2a · Angles DS:[0x36295..0x36297]Environment Map (U, V) Coordinate Derivation
Transforms the lookup vectors using patched fixed-point coefficients and adds the packed 0x8080 center bias before texture addressing.
fn_37cf7 @ 0x37cf7 · Output UV Table DS:[0x323a8]Backface Culling & Depth Sorting
Culls backward-facing triangles via 2× 2 determinant test, computes distance sort keys (K = ((−Σ(X² + Y² + Z²) + 65535) mod 2³²) >> 8), and emits records and distance keys for the render queue.
Affine Scanline Span Texture Rasterizer
Rasterizes each visible triangle scanline-by-scanline, linearly interpolating (U, V) and fetching texels from segment `FS:[V·256 + U]`.
fn_332b6 @ 0x332b6 · Texture Buffer FS:[0x0000]Dual-Stream 16-Bit Vertex & Normal Morphing fn_3c124 @ 0x3c2b1
Nation0 represents both the Cube and the Torus with an identical topological vertex mesh (corresponding coordinate and lookup streams). In each frame, the engine interpolates both the 3D coordinates and the surface normal vectors:
DS:[0x3bdc1]. The original complement is 0x1ff − t; each signed product is shifted separately by 9. The preview reverses the UI weight to keep 0 = torus and 511 = cube; the original first stream is the torus. It preserves the 511 complement and each separate signed shift.Sphere Environment Mapping (Chrome Reflection) fn_37cf7 @ 0x37cf7
Environment mapping creates the illusion of a polished chrome surface reflecting a surrounding room. Rather than tracing rays, Nation0 derives texture (U, V) directly from rotated vertex normal vectors:
The x86 Scanline Affine Texture Rasterizer Loop fn_332b6 @ 0x332b6
For each visible triangle, fn_332b6 divides the shape into top and bottom trapezoids, steps sub-pixel edge gradients (dX/dY, dU/dY, dV/dY), and executes an ultra-tight inner span loop reading from segment FS:[0]:
Conceptual affine span (pseudocode, not verbatim assembly):
for each covered scanline:
find left/right X, U and V from triangle edges
for each covered pixel:
write texture[(V_integer & 255) * 256 + (U_integer & 255)]
advance interpolated U and V
By mapping the 64KB (256 × 256) texture to segment FS, the high 8 bits of BX index the row V and the low 8 bits index column U with zero shift or multiplication instructions!
In fn_37dac, projected Y coordinates are multiplied by 0x1d6 (470) and shifted right by 9 (512), correcting for the non-square 1.2:1 pixel aspect ratio of VGA Mode 13h (320 × 200).
A signed determinant selects the accepted winding (the original accepts a nonnegative result): (X₁ - X₀)(Y₂ - Y₀) - (Y₁ - Y₀)(X₂ - X₀) < 0, rejecting facets with the discarded winding; the proportion varies with geometry and pose.
Complete Morph & Render Pipeline Overview
with Weight t ∈ [0, 511] resumeFn3c124FrameIteration
& Multiply Vertices/Normals fn_37a2a & fn_37b68
to (U, V) + 0x8080 fn_37cf7 @ 0x37cf7
& Distance Sort Keys fn_37dac @ 0x37dac
from Segment FS:[0] fn_332b6 @ 0x332b6