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.
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.
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.
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.Key files from the actual C++ source.
What I learned
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.
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.
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.