A photorealistic image looks like magic, but it is really just geometry solved backwards. Instead of following light from the lamp — most of which never reaches you — a raytracer shoots one ray out of your eye through each pixel and asks a single question: "what is the first thing I hit, and where did its colour come from?" When the surface is a mirror or glass, the answer is another ray — so every pixel unfolds into a little tree of reflections and refractions. Below, the whole scene is raytraced live on your GPU, one ray per pixel per bounce; the second panel slows it right down and draws the individual rays so you can watch the tree grow.
Backwards is the whole trick. Real photons stream out of a lamp in every direction, and only a vanishingly small fraction ever land on your retina. Simulating them forwards would waste almost all its effort on light you never see. Because the physics of light is reversible, we can run it the other way: shoot rays from the eye, through the pixels of the image plane, and out into the scene. Each ray asks only about light that actually matters — the light that reaches this pixel.
A pixel is an intersection test. For every pixel, we solve where its ray first meets a surface. A ray is a point plus a direction, p(t) = o + t·d; a sphere is all points a fixed distance from its centre. Substituting one into the other gives a quadratic in t — its smallest positive root is the nearest hit. A plane is even simpler, one linear equation. The winner is the closest positive t across all objects.
Colour is local light plus borrowed light. At the hit point we compute direct shading: how much the lamp illuminates it, dimmed by the angle between the surface and the light (Lambert's cosine law) and switched off entirely if a shadow ray to the lamp is blocked. Then, if the surface is a mirror or glass, we borrow: we spawn a new ray and repeat the entire procedure to find out what that reflected or refracted direction sees.
Reflection and refraction are two rules. A mirror sends the ray off symmetrically about the surface normal — the reflection law r = d − 2(d·n)n. Glass bends the ray as it crosses the boundary according to Snell's law, n₁ sin θ₁ = n₂ sin θ₂, refracting once on the way in and again on the way out. Turn the index up and the bending gets stronger; past a critical angle the ray can't leave at all and reflects internally instead.
Recursion builds a tree. Because each shiny hit spawns another ray, a single camera ray becomes a branching tree: primary → reflection and refraction → their reflections and refractions, and so on. Left unchecked a glass-and-mirror scene would trace forever, so we impose a maximum depth. That single number trades realism for speed: shallow trees render fast but miss reflections-of-reflections; deep trees look richer but cost more. The Ray debug panel is literally drawing this tree, one branch per bounce.
From two spheres to Hollywood. The same recursive question — scaled to millions of triangles, textured materials, and area lights, and sampled with many random rays per pixel (path tracing) — is what renders modern films and, increasingly, real-time games on ray-tracing GPUs. The engine here is a toy, but the idea is exactly the production one.
Outgoing light at a point is emitted light plus all incoming light reflected by the surface's BRDF fr — an integral the raytracer samples one ray at a time. Recursion is how the Li term (incoming light) is evaluated: it's just Lo at wherever that ray lands.
Go deeper: Turner Whitted, "An Improved Illumination Model for Shaded Display" (1980) — the paper that introduced recursive ray tracing. James Kajiya, "The Rendering Equation" (1986). Peter Shirley, Ray Tracing in One Weekend (free online) — build the whole thing from scratch. Éric Veach's thesis on path tracing for the statistics of sampling light.