Built from scratch · C++ · Path Tracing · Summer 2026

RayTracer

A physically-based renderer built from scratch in C++, following Ray Tracing in One Weekend. No graphics libraries, just vector math and recursion. Every pixel is the average of hundreds of light rays bouncing around a scene of glass, metal, and matte spheres. Ported to JavaScript below so it renders live in this page instead of a static image.

C++17 Path Tracing Monte Carlo Vector Math CMake

The exact algorithm from main.cpp, running in this browser tab. Each pass fires one more ray per pixel through the scene, so the image starts noisy and clears up as samples accumulate, exactly like the C++ version does over many more samples on the command line. Higher presets look better but render slower, since more spheres means more ray tests per bounce.

Normal · 0 samples/px · 0.0s
Quality
10

Every sphere in the scene is one of three material types. This is called path tracing, the same technique used in production renderers like Pixar's RenderMan and Blender's Cycles.

◐
Lambertian (Matte)
Light scatters randomly across the hemisphere around the surface normal. Implemented by picking a random point on a unit sphere attached to the hit point and bouncing the ray toward it. Most spheres in the scene use this.
◑
Metal
Light reflects at the same angle it arrived, like a mirror, using v - 2·dot(v,n)·n. A fuzz parameter adds random scatter around the reflection for a brushed-metal look instead of a perfect mirror.
○
Dielectric (Glass)
Light bends as it enters the surface, following Snell's law. Schlick's approximation blends between reflection and refraction depending on viewing angle, which is why glass looks more like a mirror at shallow angles.
◎
Depth of field
The camera samples rays from random points on a lens disk instead of a single pinhole. Objects at the focus distance stay sharp; everything else blurs, exactly like a real camera aperture.

Key files from the actual C++ source.

What I learned

The rendering equation.

Each recursive bounce in ray_color() computes one term of the rendering equation: how much light arrives at a point from a given direction. Writing it recursively made the physics click in a way reading about it never did.

Math on paper first.

Every time I tried to code before understanding the derivation (Snell's law refraction, Schlick's approximation) I got it wrong. Writing it out by hand first made the code straightforward.

Why C++, and why the browser version is slower.

Ray tracing is compute-intensive. The optimised C++ binary renders the full 500-samples-per-pixel scene in a few minutes; this JavaScript port trades sample count and scene density for real-time feedback in a browser tab instead.