Specular: Phong and Blinn-Phong
Lambert makes everything look like chalk. Real materials also have specular reflection: a bright highlight where the surface mirrors the light source toward your eye. Plastic, wet stones, car paint and polished wood all have it. Unlike diffuse light, a highlight moves when the camera moves, because it depends on where you are looking from.
Two classic models compute it. Both are pre-PBR approximations, but they are still used on low-end hardware, in stylized games and inside toon shaders, and they explain the ideas that PBR refines.
Phong: reflect the light
Reflect the light direction about the normal and compare it with the direction to the viewer V. The closer they are, the brighter the highlight; a shininess exponent controls how tight it is:
R = reflect(-L, N) // GLSL's reflect expects the incoming direction
specular = pow(max(dot(R, V), 0.0), shininess)Blinn-Phong: the half vector
Jim Blinn's variant uses the half vector H, halfway between the light and view directions, and compares it with the normal:
H = normalize(L + V)
specular = pow(max(dot(N, H), 0.0), shininess)Blinn-Phong is the one engines used for decades (it was the fixed-function OpenGL and Direct3D 9 model). It is cheaper when the light and viewer are far away, and it behaves better at grazing angles: Phong's highlight gets clipped into a hard-edged blob when R·V falls off quickly, while N·H stays smooth. The same visual tightness needs roughly 4× the Phong exponent.
Here are both numbers for a viewer looking straight down at the surface and a light moving from overhead toward the horizon:
const norm = (v) => { const l = Math.hypot(...v); return v.map((c) => c / l); };
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
const reflect = (i, n) => { const d = 2 * dot(n, i); return i.map((c, k) => c - d * n[k]); };
const N = [0, 1, 0];
const V = norm([0.5, 1, 0]); // viewer up and to the right
for (const deg of [-30, 0, 20, 26.57, 40, 70]) {
const a = (deg * Math.PI) / 180;
const L = [-Math.sin(a), Math.cos(a), 0]; // light up and to the left
const R = reflect(L.map((c) => -c), N);
const H = norm(L.map((c, k) => c + V[k]));
const phong = Math.pow(Math.max(dot(R, V), 0), 32);
const blinn = Math.pow(Math.max(dot(N, H), 0), 128);
console.log(`light ${String(deg).padStart(6)}° Phong(32) ${phong.toFixed(4)} Blinn(128) ${blinn.toFixed(4)}`);
}light -30° Phong(32) 0.0000 Blinn(128) 0.0000
light 0° Phong(32) 0.0281 Blinn(128) 0.0311
light 20° Phong(32) 0.8102 Blinn(128) 0.8104
light 26.57° Phong(32) 1.0000 Blinn(128) 1.0000
light 40° Phong(32) 0.4115 Blinn(128) 0.4141
light 70° Phong(32) 0.0000 Blinn(128) 0.0001
Both peak at 26.57°, where the light is the mirror image of the viewer, and with the exponent four times larger Blinn-Phong tracks Phong almost exactly. (The 4× rule is an approximation that works best near the peak and when the light, viewer and normal lie in one plane, as here; elsewhere the two highlights have slightly different shapes: Phong's stays round, Blinn-Phong's stretches at grazing angles, which looks more like real surfaces.)
Lit scene with specular
This scene adds Blinn-Phong to the Lambert sphere. The left sphere is matte (exponent 8, weak), the middle one plastic (exponent 64), the right one glossy (exponent 512, strong). Drag across the frame to move the light.
float map(vec3 p) {
vec3 q = p;
q.x = clamp(q.x, -2.4, 2.4); // three spheres at x = -2.4, 0, 2.4
q.x = p.x - 2.4 * floor(q.x / 2.4 + 0.5);
float spheres = length(q - vec3(0.0, 1.0, 0.0)) - 1.0;
return min(spheres, p.y);
}
vec3 calcNormal(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float e = 0.001;
return normalize(k.xyy * map(p + k.xyy * e) + k.yyx * map(p + k.yyx * e) +
k.yxy * map(p + k.yxy * e) + k.xxx * map(p + k.xxx * e));
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
vec3 ro = vec3(0.0, 2.2, -6.5);
vec3 rd = normalize(vec3(uv, 1.8) + vec3(0.0, -0.2, 0.0));
float lightX = iMouse.z > 0.0 ? (iMouse.x / iResolution.x - 0.5) * 8.0 : 3.0 * sin(iTime * 0.6);
vec3 lightPos = vec3(lightX, 4.0, -3.0);
vec3 sky = vec3(0.3, 0.45, 0.7);
vec3 color = sky;
float t = 0.0;
for (int i = 0; i < 120; i++) {
vec3 p = ro + rd * t;
float d = map(p);
if (d < 0.001) {
vec3 n = calcNormal(p);
vec3 l = normalize(lightPos - p);
vec3 v = -rd; // direction to the viewer
vec3 h = normalize(l + v);
float shininess = 64.0, strength = 0.6;
vec3 albedo = vec3(0.5);
if (p.y > 0.01) {
if (p.x < -1.2) { shininess = 8.0; strength = 0.15; albedo = vec3(0.2, 0.5, 0.2); }
else if (p.x < 1.2) { shininess = 64.0; strength = 0.5; albedo = vec3(0.7, 0.1, 0.1); }
else { shininess = 512.0; strength = 1.0; albedo = vec3(0.1, 0.15, 0.6); }
} else {
strength = 0.05;
}
float diff = max(dot(n, l), 0.0);
float spec = pow(max(dot(n, h), 0.0), shininess) * strength;
spec *= diff > 0.0 ? 1.0 : 0.0; // no highlight on the dark side
vec3 hemi = mix(vec3(0.08), sky * 0.4, 0.5 + 0.5 * n.y);
color = albedo * (diff + hemi) + vec3(spec);
break;
}
t += d;
if (t > 40.0) break;
}
fragColor = vec4(pow(color, vec3(1.0 / 2.2)), 1.0);
}Two details worth copying:
- The highlight is not multiplied by albedo. For non-metals the reflected light keeps the light's color, which is why a red plastic ball has a white highlight. (Metals tint their reflections; PBR handles that.)
spec *= diff > 0.0. Without it,N·Hcan be positive on the unlit side when the light is behind the surface, giving a highlight that floats in the shadow.
Energy and normalization
Raise the exponent and the highlight gets smaller but stays just as bright, so the surface reflects less total light: a polished ball looks darker than a matte one. A normalized Blinn-Phong multiplies the specular by (shininess + 8) / (8π) so the total reflected energy stays roughly constant. Highlights on smooth surfaces then become small and intense, as in reality. This normalization is the first step from ad-hoc shading toward physically based shading.
Where specular goes wrong
| Problem | Cause |
|---|---|
| Highlight in the wrong place, or none | L or V pointing the wrong way (from the surface vs to it) |
| Highlight swims across a surface when the camera rotates but should be fixed | using view-space normals with world-space light (mixed spaces) |
| Sparkly, flickering highlights on distant objects | very high exponents on normal-mapped or small geometry: the highlight is smaller than a pixel. Lower the gloss with distance, or use a PBR roughness with specular anti-aliasing |
Black spots with pow |
pow(x, y) is undefined for x < 0; always max(dot(...), 0.0) first |
Test yourself
What is the half vector H in Blinn-Phong?
- normalize(N + L)
- normalize(L + V)
- reflect(-L, N)
- cross(N, V)
Why is the specular highlight of red plastic usually white?
- Specular ignores the light color
- Dielectrics reflect light without tinting it by their albedo
- Gamma correction whitens it
- The shininess exponent is high
Which quantity makes the specular term view-dependent?
- The albedo
- The view direction V (through R·V or N·H)
- The ambient term
- The light color