Phong Specular
Adding a specular highlight to a diffuse sphere comes down to these two lines:
Let's break them down.
What is a specular highlight
Diffuse lighting simulates rough surfaces that scatter light evenly. Specular lighting simulates smooth surfaces that reflect light like a mirror — the point where the reflected ray points directly at your eye becomes very bright, just like glints on metal, plastic, or water.
What reflect and dot(r, v) do
Step one: compute the direction light bounces off the surface:
reflect(-l, n) takes the reversed light direction and mirrors it across the normal, producing the reflected ray direction r.
Step two: measure how much the reflected ray points toward you (the view direction v = (0,0,1) faces straight at the screen):
The more aligned they are → closer to 1.0 → brighter the highlight.
What pow does
pow(x, 32.0) raises x to the 32nd power — values near 1.0 survive, everything else drops quickly toward 0. A larger exponent means a smaller, sharper highlight; a smaller exponent spreads the highlight across a wider area.
32 = medium smooth (like painted plastic); 128+ = metal or mirror.
Try changing it
| Change | Effect |
|---|---|
32.0 to 8.0 | Wider, softer highlight — matte-like surface |
32.0 to 128.0 | Tiny, sharp pinpoint — metallic or mirror surface |
Highlight color from vec3(1.0) to vec3(1.0, 0.8, 0.4) | Warm golden specular |
Exercise
The exercise sphere has no highlight (spec = 0.0). Fill in the TODO to compute the reflection direction r and the Phong specular value.
Answer Breakdown
Starting state: spec = 0.0 — the sphere has only diffuse shading with no bright spot.
The fix: use reflect(-l, n) to find where light bounces, then dot(r, v) to check how much it points at the camera, then pow(..., 32.0) to concentrate the result into a tight bright spot.
Try changing the exponent from 32.0 to 4.0 and see what happens to the size and shape of the highlight.