Work

Ray Tracer

A multithreaded CPU ray tracer in modern C++. A thread pool keeps every core busy, a BVH with Möller–Trumbore triangle tests keeps traversal fast, and Monte-Carlo GI with photon and caustics maps handles the light transport.

Glass, liquids and candlelight, with caustics cast onto the table

Overview

I built a CPU ray tracer in modern C++ from scratch. It grew from basic shading into a physically inspired renderer with Monte-Carlo global illumination and photon mapping. Along the way, performance work became as important as the lighting itself. A thread pool spreads the render across every core, a BVH with Möller–Trumbore triangle intersection keeps traversal cheap, and I profiled the hot paths and tuned them iteratively to cut render times.

Light transportMonte-Carlo GI for indirect light and color bleeding, and photon mapping for caustics.
PerformanceA BVH with Möller–Trumbore triangle tests, a thread pool across all cores, and profiled, tuned hot paths.
ShadingBlinn-Phong, reflection, refraction, textures, soft shadows, anti-aliasing, and depth of field.
The mathTransforms, ray-object intersection, sampling, and light transport, implemented from first principles.

Architecture

The renderer runs in two passes. First, photons are shot from the lights, bounced through the scene, and stored in a photon map. Then camera rays are traced for each pixel. At each hit point, direct light (with soft-shadow sampling), indirect light (Monte-Carlo), and caustics (estimated from nearby photons) are combined, and many samples per pixel are averaged into the final color.

Two-pass photon mapping renderer PASS 1 · PHOTON TRACING Lights emitters Emit photons random directions Bounce through scene BVH traversal Photon map stored hits PASS 2 · RENDERING · MULTITHREADED Camera per pixel Primary rays N samples, AA + DoF Hit point via BVH Caustics photon density estimate Direct light soft-shadow samples Indirect light Monte-Carlo GI Pixel average samples
The two-pass pipeline: photon tracing, then multithreaded rendering.

Deep Dive

Acceleration: the BVH

Testing every ray against every object doesn’t scale. The scene’s geometry is organized into a bounding volume hierarchy, a tree of boxes, so each ray only tests the few primitives whose boxes it actually passes through. Both passes rely on it, so it pays off twice. At the leaves, triangles are tested with the Möller–Trumbore algorithm, which computes the hit and its barycentric coordinates in one pass without precomputing a plane equation.

I profiled traversal and intersection as the hottest paths in the renderer and tuned them iteratively, measuring each change against render time.

Global illumination & photon mapping

Indirect lighting is estimated with Monte-Carlo sampling: secondary rays bounce in random directions and their contributions are averaged. That’s what gives the Cornell box its red and green color bleeding. Caustics, the light focused through glass, are hard to find with random sampling, so photon and caustics maps handle them. Photons follow the light forward, and the renderer estimates their density at each hit point.

Color bleeding from Monte-Carlo global illumination in a Cornell box.
Color bleeding from Monte-Carlo global illumination in a Cornell box.

Soft shadows, glossiness & anti-aliasing

Area lights are sampled many times per shading point to produce soft shadow edges. Glossy surfaces scatter their reflection rays, and multiple jittered samples per pixel smooth out aliasing. Depth of field uses the same sampling idea, applied to the camera’s lens.

Glossy reflections and soft shadows from area-light sampling.
Glossy reflections and soft shadows from area-light sampling.

Multithreading

Rendering is embarrassingly parallel: each pixel is independent. Render work is fed to a thread pool sized to the machine, so every hardware thread stays busy and render time scales with core count.

Results & Learnings

  • Built end to end from first principles, from ray-object intersection math up to Monte-Carlo GI and photon-mapped caustics.
  • Profiling made performance a measured loop: every change to traversal and intersection was checked against render time.