Analytic Volumetric Fog on a Pentium II
// status: budding
Stand in the Hong Kong canal tunnels, the Battery Park subway, or the flooded hold of the freighter in Deus Ex and glowing halos of coloured mist pool around streetlights and bulkhead lamps. Today you would reach for a froxel grid or a 64-step ray marcher in a compute shader. In 1998–2000, Tim Sweeney's Render.dll had neither — it had a Pentium II and an exact closed-form cubic integral evaluated once per lightmap texel.
Integrating a sphere of mist in closed form
Every volumetric light in Unreal Engine 1 is a sphere centred at camera-space position L with radius R = 25 \cdot (\text{VolumeRadius} + 1). Inside that sphere, fog density is not uniform — it follows a smooth parabolic radial falloff that peaks at the centre and drops smoothly to zero at distance R:
\rho(r) \propto 1 - \frac{r^2}{R^2}
Now trace a ray from the camera (at the origin) to a surface texel P at distance \ell = \|P\|, with unit direction \hat{d} = P / \ell. Let t = L \cdot \hat{d} be the distance along the ray to the point of closest approach to the sphere centre, and let h^2 = \|L\|^2 - t^2 be the squared perpendicular miss distance.
If h^2 \ge R^2, the ray misses the sphere completely. Otherwise, the ray pierces the sphere along a chord of half-length
w = \sqrt{R^2 - h^2}
spanning the signed interval [-w, +w] around the closest point. Crucially, the ray only travels from the camera (at signed coordinate t relative to the closest point, wait — from 0 to \ell, which relative to t runs from -t to \ell - t, or symmetrically from s = t - \ell to t along the reversed axis!). Clamping the endpoints to the sphere boundary [-w, +w] gives the visible chord segment [s, \text{far}]:
- If the camera is outside the sphere and the wall is behind it, the ray crosses the entire diameter [-w, +w].
- If the wall cuts inside the sphere, the segment stops at the wall — so objects stepping into a fog globe are naturally less obscured than the back wall!
- If the camera itself is inside the sphere, the near clamp sits at the eye instead of the front shell.
Normalize by the sphere radius: h_n = h^2 / R^2, a = \text{far} / R, b = s / R, and c = 3(1 - h_n). Integrating the parabolic density 1 - (h^2 + u^2)/R^2 from b to a collapses to a single degree-3 polynomial with zero loops and zero samples:
V = \Bigl(\,a(c - a^2) - b(c - b^2)\,\Bigr) \cdot B
When the ray crosses the whole chord (b = -a), the two halves are symmetric and Render.dll skips the second cubic altogether, simply doubling a(c - a^2) \cdot B.
Table square roots and the early-out flag
Even a single FSQRT was expensive on a Pentium II, so SetupForSurf (0x10b073bb..0x10b07c44) avoids FSQRT completely: both \ell = \sqrt{P \cdot P} and w = \sqrt{R^2 - h^2} use FLightManager's 2,048-entry mantissa lookup table (tableSqrt), indexing the top 11 bits of the IEEE-754 float mantissa plus half the exponent!
It also carries a quirky global early-out flag (0x10b4ea00) across consecutive texels — and even across consecutive surfaces: after any texel misses a sphere (R^2 < h^2), subsequent texels run a division-free cross-multiplied reject test
(P \cdot P)\|L\|^2 - (P \cdot L)^2 > (P \cdot P) R^2
before ever issuing an FDIV for h^2. When we translated fog.rs for Deus Ex in the Browser, forgetting that that global flag persists between surfaces caused 2 surfaces out of 45 million texel-lights to diverge by 1 LSB.
The chunky 7-bit BGRA FogMap
Per-pixel evaluation wasn't remotely possible in 1999, so Unreal evaluates the analytic chord integral at lightmap texel resolution — typically one texel every 16 or 32 world units — and hands the GPU a tiny texture called the FogMap.
Each texel in the FogMap stores four 7-bit bytes (B, G, R, A) in [0, 127] (half of 255, matching Direct3D's 2× overbright lightmap convention). When multiple volumetric lights overlap on the same wall, SetupForSurf sorts them back-to-front in camera Z and blends each nearer light over the accumulated FogMap using a precomputed 128×128 integer attenuation table:
\text{FOG\_ATTENUATION}[a][v] = \left\lfloor \frac{(127 - a) \cdot v}{127} \right\rfloor
Finally, the hardware draws the FogMap over the lit surface in a dedicated pass with SRC = ONE, DEST = INVSRCALPHA: the existing wall dims by (1 - \alpha) and the glowing coloured haze adds straight on top. Because bilinear filtering interpolates that coarse texel lattice across the wall, you get silky volumetric pools at a fraction of a millisecond — and if you drag the texel grid slider down in the demo above, you can watch the exact blocky 2000s lightmap lattice snap into view.
Part of the Deus Ex in the Browser notes · see BSP Occlusion & Span Buffers · 80-bit Ghosts · Ray marching and SDFs · back to Notes.