Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 50 ยท Lighting and Shading

Light and Reflection Models

Learn how renderers fake light: ambient, Lambert diffuse, Phong and Blinn-Phong specular, and attenuation, with a live sphere and sliders.

So far every surface in this course has had a flat color, or at best a gray level picked by a rough rule. Real objects look solid because light falls on them differently from place to place: bright where the surface faces the light, dark where it turns away, with a small glossy spot where it mirrors the lamp. A lighting model is a small formula that turns the geometry of a point (its normal, the direction to the light, the direction to the eye) into a color.

Real light transport is expensive, so classic graphics uses cheap approximations that look convincing. The most famous is the Phong reflection model. In this lesson you build it from three parts, meet its faster cousin Blinn-Phong, add distance falloff, and play with all of it on a live sphere.

The vectors involved

Every lighting formula in this chapter uses the same handful of unit vectors at the surface point being shaded:

  • N: the surface normal, pointing out of the surface.
  • L: the direction from the point toward the light.
  • V: the direction from the point toward the viewer.
  • R: the direction of the perfect mirror reflection of the light about the normal.
  • H: the "halfway" vector between L and V (used by Blinn-Phong).

All of them must be normalized (length 1), because the formulas use dot products as cosines. For unit vectors, a ยท b = cos(angle between a and b). If you need a refresher on normals, see polygon meshes.

flowchart LR
  A["Ambient<br/>constant base light"] --> S["+"]
  B["Diffuse<br/>depends on N and L"] --> S
  C["Specular<br/>depends on R and V"] --> S
  S --> O["Final color"]

The Phong model is the sum of three independent terms.

Ambient light

In reality, light bounces around a room, so even a surface that faces away from every lamp is not pitch black. Computing all those bounces is exactly what we are trying to avoid, so the Phong model uses a single constant instead:

TEXT
I_ambient = k_a * I_a

I_a is the intensity of the ambient light and k_a (between 0 and 1) says how much of it the material reflects. It adds the same amount to every point, so by itself it gives a completely flat silhouette. Its job is to keep shadowed sides from turning pure black.

Diffuse reflection (Lambert's law)

A rough, matte surface such as chalk, paper or unpolished wood scatters light in all directions equally. Its brightness does not depend on where you look from, only on how directly the light hits it. Lambert's cosine law says the brightness is proportional to the cosine of the angle between the normal and the light direction:

TEXT
I_diffuse = k_d * I_l * max(0, N ยท L)

When the light shines straight down onto the surface (N = L) the cosine is 1, the maximum. As the light tilts, the same beam is spread over a larger area and the surface gets dimmer. At 90 degrees the cosine is 0, and for angles beyond that the surface faces away from the light, where the max(0, ...) clamps the result to zero instead of going negative.

Notice that V does not appear. A diffuse surface looks the same from every viewpoint, which is why it is called view-independent.

Specular reflection (the highlight)

Polished surfaces such as plastic, metal or a wet apple also mirror part of the light. You see a bright spot only when your eye is close to the mirror direction. The Phong model uses the reflection vector R:

TEXT
R = 2 (N ยท L) N - L
I_specular = k_s * I_l * max(0, R ยท V) ^ n

The exponent n is the shininess. The dot product R ยท V is 1 when the eye is exactly on the mirror direction and falls off as you move away. Raising it to the power n makes the falloff steeper: with n = 5 you get a broad, soft sheen, with n = 200 a tiny, sharp, mirror-like dot. The specular color is usually white (for plastic) because the highlight shows the light's color, not the object's.

Blinn-Phong

Computing R for every point costs a few extra operations, and the Phong highlight misbehaves when R ยท V goes negative at grazing angles. James Blinn proposed using the halfway vector instead:

TEXT
H = normalize(L + V)
I_specular = k_s * I_l * max(0, N ยท H) ^ n

When N points exactly halfway between the light and the eye, the mirror condition holds, so N ยท H is 1 there too. The highlight shape is slightly different (it is a little wider, and rounder on slanted floors), so Blinn-Phong needs a bigger exponent, roughly four times, to match Phong's highlight. It is the default specular model of the old fixed-function OpenGL pipeline and is still widely used.

The complete model

Adding the three terms gives the color at a point:

TEXT
color = base_color * (k_a + k_d * max(0, N ยท L)) + white * k_s * specular_term

With several lights, you sum the diffuse and specular parts of each one; the ambient term is added once.

One more practical point: these formulas work in linear light. Monitors apply a gamma curve, so lighting results should be converted before display (the gamma discussion explains why). Skipping the conversion makes shading look too dark in the mid-tones, and the demo below has a checkbox so you can see the difference.

A live sphere

For a sphere, the normal at any visible pixel is easy: if the sphere has radius 1 and sits at the origin, the normal at position (x, y, z) on its surface is the position itself. The demo below loops over the pixels of a 200 by 200 canvas, computes z = sqrt(1 - xยฒ - yยฒ) for the pixels inside the circle, and evaluates the three terms with the viewer looking down the z-axis (V = (0, 0, 1)). Move the sliders for the light direction, switch terms on and off, and compare Phong with Blinn-Phong.

Phong and Blinn-Phong on a sphere

Try this: turn on only the diffuse term and sweep the azimuth slider. The terminator, the line where the sphere goes from lit to unlit, is a clean half-circle. Add specular and watch the highlight slide across the surface, always exactly midway between the light and your eye direction. Under Blinn-Phong, the highlight looks larger for the same exponent; raise the shininess to compensate.

Attenuation: light fades with distance

The model so far treats the light as arriving with the same strength everywhere, like the sun. For a lamp close to the scene, the intensity at a point drops with the distance d to the light. In physics, a point source follows an inverse-square law, 1 / dยฒ. Graphics often uses a more flexible polynomial with three tunable coefficients:

TEXT
attenuation = 1 / (k_c + k_l * d + k_q * dยฒ)

k_c is a constant term (it keeps the result finite when d is tiny), k_l is linear and k_q is quadratic. Each light's diffuse and specular contribution is multiplied by this factor. Ambient light is usually not attenuated. Directional lights, such as the sun, have no position and therefore no attenuation.

The second demo looks from above at a floor lit by a lamp hovering at height h. At each floor pixel N = (0, 0, 1), and the diffuse term N ยท L is h / d, so the floor is bright below the lamp and fades both because the light arrives at a slant and because of attenuation. The plot underneath shows the brightness along the middle row.

Distance attenuation on a floor

Set k_c = 1 and k_l = k_q = 0 to turn attenuation off: the floor then fades only because of the cosine term. Then increase k_q. The pool of light shrinks quickly, which is why artists prefer the linear or mixed terms, as they give a gentler fall-off than strict physics.

Limits of the model

The Phong model is a hack. It ignores shadows (nothing blocks the light), reflections of other objects, light bouncing between surfaces (so ambient is a constant guess), and it does not conserve energy. Modern engines use physically based rendering (PBR) with microfacet specular models, but they keep the same skeleton: a diffuse term driven by N ยท L, a specular lobe around the mirror direction, and falloff with distance. Learn this one first, and the others become variations on it. Shadows and true reflections arrive later with ray tracing.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which term of the Phong model does NOT depend on the viewer's position?

    Lambert's law uses only N and L, so a matte surface looks the same from every viewpoint. The specular highlight depends on V.
  2. 2. A surface point has N ยท L = 0.5 for a unit normal and unit light vector. What is the angle between them?

    For unit vectors the dot product is the cosine of the angle, and cos(60 degrees) = 0.5.
  3. 3. What does a larger shininess exponent n do to the specular highlight?

    Raising a number between 0 and 1 to a high power makes it fall to zero quickly, so only directions very near the mirror direction stay bright.
  4. 4. How does Blinn-Phong compute its specular term?

    Blinn-Phong avoids computing R. When N points halfway between L and V, N ยท H = 1, which is the same mirror condition.
  5. 5. Why is max(0, N ยท L) used for the diffuse term?

    When the normal points away from the light the dot product is negative; clamping to zero means such a surface just gets no direct light.

Summary

  • A lighting model turns N (normal), L (to the light), V (to the eye) and material numbers into a color. Keep all vectors normalized.
  • Phong = ambient (constant) + diffuse (k_d * max(0, N ยท L)) + specular (k_s * max(0, R ยท V)^n).
  • Blinn-Phong replaces R by the halfway vector H, using max(0, N ยท H)^n, which is cheaper and better behaved.
  • Shininess n controls highlight size; the specular color is usually white.
  • Attenuation 1 / (k_c + k_l d + k_q dยฒ) dims point lights with distance; compute lighting in linear space and gamma-correct for display.

Next, learn where to evaluate this formula: once per face, per vertex or per pixel, in Flat, Gouraud and Phong Shading.