Modelintro
Blinn-Phong reflection model
Also called Blinn-Phong model, modified Phong model.
An empirical reflection model that adds ambient, diffuse and a specular lobe built on the half vector between the light and view directions.
The Blinn-Phong reflection model shades a point as the sum of an ambient term, a Lambertian diffuse term and a specular highlight. For a light in the direction , a view direction and the unit normal , all pointing away from the surface, the highlight uses the half vector :
where is the light intensity and the shininess exponent. Bui Tuong Phong (1975) used the power of , the cosine between the mirror direction of and the view. James Blinn (1977) replaced it by the power of , which is cheaper when and are constant over a surface and which has a better shape at grazing angles. It was the fixed-function lighting model of OpenGL and Direct3D and is still common where cost matters. The model is a heuristic: its terms were chosen to look plausible, not derived from optics. A normalized form fits into the microfacet framework, where it is close to the Beckmann distribution.
The machine
Inputs. Unit vectors , and ; the ambient, diffuse and specular coefficients , and in linear RGB; the exponent ; the intensity of each light. For several lights the light terms are summed.
The classic form. The equation above. The specular term is set to zero when , so a light behind the surface does not cause a highlight. The ambient term is a constant that stands in for all indirect light. No term has a unit; the coefficients are tuned by eye.
The half vector. is the normal that a mirror facet must have to reflect into . The highlight is brightest when , which is exactly when is the mirror direction of . This is the link to microfacet theory: measures how many facets are oriented to reflect into .
The normalized BRDF. To use the model with physically based lights, replace the coefficients by reflectances and divide by solid angle:
The factor is a common choice that keeps the specular albedo near for all ; it is a convenient approximation, not an exact normalization (see below). The units of are 1/sr.
The distribution form. As a microfacet , the normalized lobe is , and this one is exact: for every . It goes into like GGX.
What it counts
The classic form counts nothing physical. It gives a brightness in , which the display code clamps. The normalized BRDF is a ratio of reflected radiance to incoming irradiance, and its directional albedo is the fraction of light that it reflects.
Example. The specular albedo of , computed by the code of this page:
| 1 | 4 | 16 | 64 | 256 | |
|---|---|---|---|---|---|
| View at 0° | 1.0243 | 1.0625 | 1.0669 | 1.0267 | 1.0075 |
| View at 60° | 0.8388 | 0.5938 | 0.3702 | 0.2811 | 0.2578 |
At normal view the albedo has the closed form with . It is 1 at , tends to 1 as , and peaks at near . So the factor reflects up to 7.5 percent more light than it receives at normal view. At 60° the albedo falls well below 1, because the lobe of is cut off by the horizon and there is no Fresnel or masking term. Both effects are small next to the errors of the classic model, but they show that the factor is a heuristic.
Example of the shape. For at 75° from the normal, , and a light 0.2 radians from the mirror direction: in the plane of incidence, the lobe is ; across that plane, at the same angular distance, it is . The Phong lobe gives in both directions, because it is round around . The Blinn highlight on a flat floor at a grazing view is therefore stretched toward the viewer, much like the long reflections of street lights on a wet road. The code of this page tests these values.
Relation to other models
- Phong. with . At normal view, and , and , for small , so gives about the same highlight. This rule holds only near normal view and near the peak. The Phong reflection model is a different thing from Phong shading, which interpolates normals across a triangle; both can be used together or apart, and Gouraud shading interpolates the result instead.
- Lambert. The diffuse term is Lambert's cosine law, and is the Lambertian BRDF.
- Microfacet models. The normalized is a valid microfacet distribution. Near its peak it is close to Beckmann with , a common fit. GGX has a much longer tail than both.
- Physically based shading. Physically based shading replaced the classic model in most engines after about 2010, with GGX for , a Smith and Schlick's . Some engines keep Blinn-Phong as a cheap .
- Stylized shading. Gooch shading and toon shading replace the diffuse term and often keep a Blinn-Phong highlight.
Known results in the model
- Reciprocity (proved). is symmetric in and , so the normalized BRDF is symmetric. The Phong lobe is also symmetric, because .
- Normalization of (proved). .
- Albedo of the factor (proved at normal view, measured elsewhere). The closed form above, with a largest value of ; at other angles the values in the table are quadrature results.
- No energy conservation of the classic form (proved). For the classic form can return more light than it receives; nothing in the model prevents it.
- Lobe shape (measured). The highlight is round at normal view and stretched in the plane of incidence at grazing views, while Phong's stays round.
- Fit to real materials (measurement). How well the model fits a material is a measurement for that material. With a Fresnel and a masking term in the microfacet form, it behaves like Beckmann.
Proof: Properties of the Blinn-Phong modelfull
Setting. For unit vectors , and with and , the code computes , the lobe , the normalized BRDF , the distribution and the Phong lobe with . The claims are about these formulas in exact arithmetic. That the model looks plausible is a heuristic and is not claimed.
Claim 1 (the half vector). , and exactly when .
Proof. . If , then , a positive multiple of , so . Conversely, if , then for some , and because and give ; so .
Claim 2 (reciprocity). and .
Proof. is symmetric in and , and so is . , which is symmetric.
Claim 3 (normalization of ). for every .
Proof. With and after the change of variables, the integral is .
Claim 4 (the albedo at normal view). For , the specular albedo with equals with . It is 1 at and tends to 1 as .
Proof. Change variables from to : and . With and the angle of , and , which is positive for . So , which is the closed form. At it is . As , the powers of vanish and .
The largest value, near , is found on a grid by the tests. The albedo at other view angles is computed by quadrature in and checked against the closed form at .
Claim 5 (the Phong match at normal view). For , , and as .
Proof. For , . With , and . The terms are equal.
What the tests check. Claim 1 and Claim 2 at random directions, Claim 3 by the midpoint rule for up to 1000, Claim 4 against quadrature, Claim 5 at small angles, the lobe shape at 75°, and the classic form at a mirror configuration.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/blinn-phong-reflection-model/. The authors are listed in prov/3J4E53.jsonl.
"""The Blinn-Phong reflection model, its classic shading form, its
normalized BRDF form, and the Phong lobe it replaces.
All vectors point away from the surface. n, l and v are unit vectors.
Colors are single linear channels here.
"""
import math
Vec = tuple[float, float, float]
def dot(a: Vec, b: Vec) -> float:
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
def normalize(a: Vec) -> Vec:
r = math.sqrt(dot(a, a))
return (a[0] / r, a[1] / r, a[2] / r)
def half_vector(l: Vec, v: Vec) -> Vec:
"""h = (l + v) / |l + v|; undefined when l = -v."""
return normalize((l[0] + v[0], l[1] + v[1], l[2] + v[2]))
def reflect(l: Vec, n: Vec) -> Vec:
"""The mirror direction r = 2 (n.l) n - l."""
c = 2.0 * dot(n, l)
return (c * n[0] - l[0], c * n[1] - l[1], c * n[2] - l[2])
def phong_lobe(n: Vec, l: Vec, v: Vec, p: float) -> float:
"""The Phong specular factor max(0, r.v)^p."""
return max(0.0, dot(reflect(l, n), v)) ** p
def blinn_lobe(n: Vec, l: Vec, v: Vec, s: float) -> float:
"""The Blinn specular factor max(0, n.h)^s."""
return max(0.0, dot(n, half_vector(l, v))) ** s
def shade_classic(n: Vec, l: Vec, v: Vec, ka: float, kd: float, ks: float, s: float, light: float = 1.0) -> float:
"""The classic intensity ka + light (kd max(0, n.l) + ks max(0, n.h)^s).
The specular term is set to zero when the light is behind the surface.
"""
nl = dot(n, l)
if nl <= 0.0:
return ka
return ka + light * (kd * nl + ks * blinn_lobe(n, l, v, s))
def blinn_phong_d(cos_h: float, s: float) -> float:
"""The normalized distribution (s + 2) / (2 pi) (n.h)^s, a valid microfacet D."""
if cos_h <= 0.0:
return 0.0
return (s + 2.0) / (2.0 * math.pi) * cos_h ** s
def blinn_phong_brdf(n: Vec, l: Vec, v: Vec, kd: float, ks: float, s: float) -> float:
"""The normalized BRDF kd / pi + ks (s + 8) / (8 pi) (n.h)^s, in 1/sr."""
if dot(n, l) <= 0.0 or dot(n, v) <= 0.0:
return 0.0
return kd / math.pi + ks * (s + 8.0) / (8.0 * math.pi) * blinn_lobe(n, l, v, s)
def specular_albedo(theta_v: float, s: float, nt: int = 400, npf: int = 96) -> float:
"""The directional albedo of the specular term (s + 8) / (8 pi) (n.h)^s.
It integrates f (n.l) over the hemisphere of l by a change of variables to
the half vector, d omega_l = 4 (v.h) d omega_h, with the midpoint rule in
theta_h and phi_h. The frame has n = z and v in the xz plane.
"""
n = (0.0, 0.0, 1.0)
v = (math.sin(theta_v), 0.0, math.cos(theta_v))
k = (s + 8.0) / (8.0 * math.pi)
tmax = math.pi / 2.0
total = 0.0
dt = tmax / nt
dp = 2.0 * math.pi / npf
for i in range(nt):
t = (i + 0.5) * dt
st, ct = math.sin(t), math.cos(t)
lobe = k * ct ** s
if lobe == 0.0:
continue
ring = 0.0
for j in range(npf):
p = (j + 0.5) * dp
h = (st * math.cos(p), st * math.sin(p), ct)
vh = dot(v, h)
if vh <= 0.0:
continue
l = (2.0 * vh * h[0] - v[0], 2.0 * vh * h[1] - v[1], 2.0 * vh * h[2] - v[2])
nl = dot(n, l)
if nl <= 0.0:
continue
ring += nl * 4.0 * vh
total += lobe * ring * st
return total * dt * dp
def specular_albedo_normal(s: float) -> float:
"""The exact albedo of the specular term at v = n.
There l is the mirror of v about h, so n.l = cos(2 theta_h), v.h = cos(theta_h),
and the integral over cos(theta_h) from 1/sqrt(2) to 1 has a closed form.
"""
c0 = 1.0 / math.sqrt(2.0)
return (s + 8.0) * (2.0 * (1.0 - c0 ** (s + 4.0)) / (s + 4.0) - (1.0 - c0 ** (s + 2.0)) / (s + 2.0))
def phong_to_blinn_exponent(p: float) -> float:
"""s = 4 p makes the two lobes match near the mirror direction at normal view."""
return 4.0 * p
test_blinn_phong_reflection_model.py 124 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/blinn-phong-reflection-model/. The authors are listed in prov/3J4E53.jsonl.
"""Tests of the Blinn-Phong model: normalization, albedo, reciprocity and the Phong match."""
import math
import random
from blinn_phong_reflection_model import (
blinn_lobe,
blinn_phong_brdf,
blinn_phong_d,
dot,
half_vector,
normalize,
phong_lobe,
phong_to_blinn_exponent,
reflect,
shade_classic,
specular_albedo,
specular_albedo_normal,
)
N = (0.0, 0.0, 1.0)
def random_up(rng: random.Random) -> tuple[float, float, float]:
"""A random unit vector in the upper hemisphere, by rejection."""
while True:
x, y, z = rng.uniform(-1, 1), rng.uniform(-1, 1), rng.uniform(0, 1)
r2 = x * x + y * y + z * z
if 1e-6 < r2 <= 1.0 and z > 1e-3:
return normalize((x, y, z))
def test_half_vector_and_mirror() -> None:
rng = random.Random(1)
for _ in range(500):
l, v = random_up(rng), random_up(rng)
h = half_vector(l, v)
assert abs(dot(h, l) - dot(h, v)) < 1e-12
# When v is the mirror of l, h is n and both lobes peak.
r = reflect(l, N)
assert abs(dot(half_vector(l, r), N) - 1.0) < 1e-12
assert abs(blinn_lobe(N, l, r, 50.0) - 1.0) < 1e-9
assert abs(phong_lobe(N, l, r, 50.0) - 1.0) < 1e-9
def test_d_is_normalized() -> None:
# The integral of D (n.h) over the hemisphere is 1 for every s; midpoint rule in theta.
for s in (0.0, 1.0, 10.0, 100.0, 1000.0):
k = 20000
dt = (math.pi / 2.0) / k
total = 0.0
for i in range(k):
t = (i + 0.5) * dt
total += blinn_phong_d(math.cos(t), s) * math.cos(t) * math.sin(t)
assert abs(total * 2.0 * math.pi * dt - 1.0) < 1e-4
def test_reciprocity() -> None:
rng = random.Random(2)
for _ in range(1000):
l, v = random_up(rng), random_up(rng)
s = rng.uniform(1.0, 200.0)
a = blinn_phong_brdf(N, l, v, 0.5, 0.5, s)
b = blinn_phong_brdf(N, v, l, 0.5, 0.5, s)
assert abs(a - b) <= 1e-12 * max(1.0, a)
# The Phong lobe is also symmetric: r(l).v = r(v).l.
assert abs(phong_lobe(N, l, v, 10.0) - phong_lobe(N, v, l, 10.0)) < 1e-12
def test_albedo_at_normal_incidence() -> None:
expected = {1.0: 1.0243, 4.0: 1.0625, 16.0: 1.0669, 64.0: 1.0267, 256.0: 1.0075}
for s, val in expected.items():
assert abs(specular_albedo_normal(s) - val) < 5e-5
assert abs(specular_albedo(0.0, s) - specular_albedo_normal(s)) < 5e-4
# The largest value over s is 1.0752, near s = 8.76; it tends to 1 at both ends.
best = max(specular_albedo_normal(i / 100.0) for i in range(1, 5001))
assert abs(best - 1.0752) < 5e-5
assert abs(specular_albedo_normal(1e-6) - 1.0) < 1e-4
assert abs(specular_albedo_normal(1e6) - 1.0) < 1e-4
def test_albedo_at_sixty_degrees() -> None:
expected = {1.0: 0.8388, 4.0: 0.5938, 16.0: 0.3702, 64.0: 0.2811, 256.0: 0.2578}
for s, val in expected.items():
assert abs(specular_albedo(math.radians(60.0), s) - val) < 5e-4
def test_phong_to_blinn_match() -> None:
# At v = n, n.h = cos(t / 2) and r.v = cos t for the angle t between l and n.
for p in (10.0, 50.0, 200.0):
s = phong_to_blinn_exponent(p)
for t in (0.02, 0.05, 0.1):
l = (math.sin(t), 0.0, math.cos(t))
ph = phong_lobe(N, l, N, p)
bl = blinn_lobe(N, l, N, s)
assert abs(math.log(bl) - math.log(ph)) < 0.02 * p * t * t + 1e-12
# Away from normal view the Blinn lobe gets narrow across the plane of incidence.
def sph(t: float, p: float) -> tuple[float, float, float]:
return (math.sin(t) * math.cos(p), math.sin(t) * math.sin(p), math.cos(t))
tv = math.radians(75.0)
v = sph(tv, 0.0)
d = 0.2
in_plane = sph(tv - d, math.pi)
dphi = 2.0 * math.asin(math.sin(d / 2.0) / math.sin(tv))
across = sph(tv, math.pi + dphi)
assert abs(math.acos(dot(across, reflect(v, N))) - d) < 1e-12
assert abs(phong_lobe(N, in_plane, v, 10.0) - 0.818) < 5e-4
assert abs(blinn_lobe(N, in_plane, v, 40.0) - 0.818) < 5e-4
assert abs(phong_lobe(N, across, v, 10.0) - 0.818) < 5e-4
assert abs(blinn_lobe(N, across, v, 40.0) - 0.062) < 5e-4
def test_classic_shading() -> None:
l = normalize((1.0, 0.0, 1.0))
v = normalize((-1.0, 0.0, 1.0))
# Mirror configuration: n.l = 0.7071 and n.h = 1.
val = shade_classic(N, l, v, 0.1, 0.6, 0.3, 32.0)
assert abs(val - (0.1 + 0.6 * math.sqrt(0.5) + 0.3)) < 1e-12
# Light behind the surface: only ambient.
assert shade_classic(N, (0.0, 0.0, -1.0), v, 0.1, 0.6, 0.3, 32.0) == 0.1
References
- James F. Blinn, Models of light reflection for computer synthesized pictures, Computer Graphics (SIGGRAPH 77) 11(2), 1977, 192 to 198, the specular term with the half vector H and the exponent of N.H, and its comparison with the Phong term and the Torrance-Sparrow model (general locator)
- Bui Tuong Phong, Illumination for computer generated pictures, Communications of the ACM 18(6), 1975, 311 to 317, the reflection model with the power of the cosine between the reflected direction and the view direction (general locator)
- Tomas Akenine-Möller, Eric Haines, Naty Hoffman, Angelo Pesce, Michał Iwanicki and Sébastien Hillaire, Real-Time Rendering, fourth edition, CRC Press, 2018, Chapter 5, Shading Basics, and Chapter 9, Physically Based Shading (the Blinn-Phong distribution as a microfacet D) (general locator)
- Matt Pharr, Wenzel Jakob and Greg Humphreys, Physically Based Rendering, From Theory to Implementation, third edition, Morgan Kaufmann, 2016, Section 8.4, Microfacet Models (the microfacet framework into which a normalized Blinn-Phong D fits)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 3J4E53 Edit this page History Report an error Markdown