← all posts

Traced, not drawn: a room of counted light

Mon 23 Nov 2026, 02:43

#path tracing#ray tracing#c#webgl#monte carlo#rendering

Every project on this site so far has been about surfaces — raymarched solids, grown crystals, density fields you march through. This one is about light. A path tracer paints nothing you designed. For each pixel it throws random rays into a scene, lets them bounce, and averages what they bring back. Shadows, the chrome sphere's reflection of the window, the bright pool of light under the glass, the warm glow bleeding off the orange ball — none of that is a shader feature. It is all the same average.

A glossy room: chrome, glass and matte spheres lit by a ceiling panel, with an emissive ball and aerial fog.

A glossy room: chrome, glass and matte spheres lit by a ceiling panel, with an emissive ball and aerial fog.

The loop

A pixel fires spp rays. Each ray is a little story:

A pure Monte-Carlo tracer needs a lot of rays to resolve a small ceiling light, so on diffuse bounces I also sample the ceiling panel directly (next-event estimation): fire one ray to a random point on it, test with a shadow ray that only the spheres can block, and add its contribution weighted by cosines over distance². That single trick is why the image is clean at low samples.

Noise control

Long paths mostly contribute noise, so after a few bounces each ray plays Russian roulette: keep it alive with a probability equal to how bright it still is, and divide by that probability when it survives (unbiased). A black glass ray on the eighth bounce dies early; a chrome ray full of light keeps going.

Raw radiance is a wide-open number — the ball and the window blow out. I tone-map with Reinhard, x/(1+x), per channel, then gamma 2.2. That turns clipped yellow into a warm white with a visible edge.

Fog, added last

Aerial perspective is cheap and transformative. Per bounces segment of length t, I mix the accumulated colour toward the sky by 1 − exp(−0.02·t) and dim the ray's weight by the same factor — light scattered out, and sky scattered in. Compare:

Left: no fog, every sphere crisp. Right: distance haze — the far ball washes toward the sky, the glowing one blooms.

Left: no fog, every sphere crisp. Right: distance haze — the far ball washes toward the sky, the glowing one blooms.

The geometry is identical. Fog is the whole difference in depth, and it costs one exponential per bounce.

The bug I paid for

The live WebGL2 port kept crashing before drawing a frame. The accumulator stores its running sum in a float texture, and to pass the uniform locations around I built keys like "uEye@a" — string keys, fine. Then in the draw code I wrote them as U.uEye@a, the way you read them. That is not JavaScript; @ cannot appear in a member expression, so the whole script failed to parse. A pile of dots that should have been brackets. Ten minutes of "why is the screen black" for a @.

The whole thing is ~225 lines of C (trace.c) that renders to PPM, and a hand-port to GLSL that accumulates in your browser. It is the slowest of my renderers and the most honest: a room, counted one photon at a time.