The Lighting Equation

Knowing where a ray goes isn't enough; a raytracer also has to decide how bright and what colour each surface point looks. The classic answer — the local lighting model that ran in nearly every game for two decades — is a sum of three simple terms, each capturing one way light reaches your eye. Add them up and a flat patch of geometry turns into something that looks lit, rounded, and solid:

\text{colour} = \text{ambient} + \text{diffuse} + \text{specular}.

Every term is built from dot products of unit vectors — the surface normal \vec{N}, the direction to the light \vec{L}, the direction to the viewer \vec{V} — exactly the cosine-as-brightness idea, used three ways.

The three terms, line by line

Step 1 — Ambient: a constant fill so shadows aren't pitch black. In the real world light bounces around a room and seeps into every crevice. Faking all those bounces is expensive, so the cheapest stand-in is a flat constant added everywhere:

\text{ambient} = k_a\,\cdot\,\text{(light colour)}.

Without it, anything not directly facing the light would be a featureless black hole. Ambient lifts the floor so unlit surfaces still read as their own colour.

Step 2 — Diffuse: matte brightness is the cosine to the light (Lambert). A chalky, matte surface scatters light equally in all directions, so its brightness depends only on how squarely it faces the light — the cosine of the angle between the normal \vec{N} and the light direction \vec{L}. With both unit vectors, that cosine is the dot product, clamped so the back side stays dark:

\text{diffuse} = k_d\,\max(0,\ \vec{N}\cdot\vec{L})\,\cdot\,\text{(light colour)}\,\cdot\,\text{(surface colour)}.

Face the light squarely (\vec{N}\cdot\vec{L} = 1): full brightness. Tilt to edge-on (0): it fades to nothing. Turn away (negative): the \max(0,\cdot) clamps it to dark — a surface can't be lit by a light behind it. This single line is Lambert's cosine law, and it gives a sphere its smooth round shading.

Step 3 — Specular: the shiny highlight (Blinn–Phong). Glossy surfaces add a bright pinpoint where they mirror the light toward your eye. Blinn's trick is the half-vector — the unit vector halfway between the light and the view directions:

\vec{H} = \frac{\vec{L} + \vec{V}}{\lVert\vec{L} + \vec{V}\rVert}.

The highlight is brightest when \vec{H} lines up with the normal — i.e. when the surface is tilted to mirror the light straight at the viewer. Measure that alignment with a clamped dot product and raise it to a shininess power s:

\text{specular} = k_s\,\big(\max(0,\ \vec{N}\cdot\vec{H})\big)^{s}\,\cdot\,\text{(light colour)}.

The exponent s controls the tightness of the glint. A small s spreads a broad, soft sheen (think satin); a large s crushes it to a tiny mirror-bright dot (think wet glass or chrome), because raising a number below 1 to a high power drives it sharply toward zero everywhere except right at the peak.

Step 4 — add them. The final shaded colour is just the three terms summed, per light:

\text{colour} = k_a + k_d\,\max(0,\ \vec{N}\cdot\vec{L}) + k_s\,\big(\max(0,\ \vec{N}\cdot\vec{H})\big)^{s},

folding in the light and surface colours where they belong. Ambient keeps shadows readable, diffuse gives shape, specular gives the wet glint — three dot products, one lit surface.

For a surface point with unit normal \vec{N}, unit light direction \vec{L}, unit view direction \vec{V}, and half-vector \vec{H} = (\vec{L}+\vec{V})/\lVert\vec{L}+\vec{V}\rVert:

The two reflection terms model two extremes. Pure Lambert (diffuse only) is the matte world — chalk, paper, unpolished stone — where brightness is just the cosine to the light and there's no glint at all. Blinn–Phong adds the specular highlight on top, and by dialling k_s and s you slide from satin to plastic to glass. For thirty years that was how games looked lit.

Its successor is physically based rendering (PBR). Instead of ad-hoc constants, PBR parameterizes a surface by roughness and metalness and insists the maths conserve energy (a surface can't reflect more light than it receives) and obey real microfacet statistics. The spirit is the same — sum the ways light leaves a surface — but the terms are grounded in physics rather than chosen to look right, which is why modern materials hold up under any lighting. Blinn–Phong is the honest ancestor every PBR shader still rhymes with.