Algorithmadvanced
Spherical harmonic irradiance environment map
Also called Ramamoorthi-Hanrahan irradiance map.
Nine spherical harmonic coefficients give the diffuse irradiance of a distant environment as E(n) = n^T M n. The error is at most 3/32 (about 9%) of the total light, and under 3% on average.
- Time
- Θ(s) buildΘ(1) evaluate
- Space
- Θ(1) extra
- Where
- s = the number of texels of the environment map
- Model
- Real RAM
- Properties
- deterministicnot in placeonlineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Ravi Ramamoorthi, Pat Hanrahan, 2001
A spherical harmonic irradiance environment map gives the light that a diffuse surface receives from a distant environment, for any normal, with nine numbers per color channel. The input is an environment map of radiance. The algorithm projects it onto the nine real spherical harmonics of order , multiplies them by the coefficients of the clamped cosine, and puts the result into a symmetric matrix . The irradiance at a unit normal is then with , a few multiply-adds in a shader. Ramamoorthi and Hanrahan published it in 2001. The error is at most of the total light of the environment for any lighting, which is a proved bound, and the average error over all normals is under 3% for any physical lighting, which the paper takes from Basri and Jacobs. On natural maps the paper measured smaller errors. The page handles the diffuse part of image-based lighting; the glossy part is the split sum approximation, and the general use of spherical harmonics for light is spherical harmonic lighting.
In plain words
A matte surface gets light from every direction above it, and it weights each direction by how much it faces that direction. This sum changes slowly when the surface turns, because it averages over half of the sky. A slow function can be described by a few numbers, like a smooth curve that needs only a few terms of a series. The method finds nine such numbers for the whole environment, once. To shade a pixel, it combines the nine numbers with the normal of the pixel, which gives the light for that normal at once, without a look at the environment again. The answer is smooth and fast, but it loses sharp detail such as the exact edge of a shadow from the sun.
The problem
The input is a distant lighting environment, given as texels : a unit direction, a radiance and a solid angle, for one color channel. The solid angles add up to . The output is a function that returns, for a unit normal , an approximation of the irradiance
A Lambertian surface with albedo then has the outgoing radiance , see Lambertian reflectance. The standing assumptions are: the sources are far away, so does not depend on the position (a physical approximation); the surface is diffuse; and nothing blocks the light, so there is no shadow and no interreflection. The environment can be a cube map or a latitude and longitude map; only the texel directions and solid angles matter.
The spherical harmonics are the ones of the paper, with as the polar axis: ; ; ; ; and . They are orthonormal on the sphere (a proved property).
The algorithm
IRRADIANCE-MAP(texels)- 1
L_lm ← 0 for the nine pairs (l, m) with l ≤ 2 - 2
for each texel (ω, L, Δω) - 3
for each (l, m): L_lm ← L_lm + L · Y_lm(ω) · Δω - 4
M ← [[c1 L22, c1 L2-2, c1 L21, c2 L11], [c1 L2-2, -c1 L22, c1 L2-1, c2 L1-1],[c1 L21, c1 L2-1, c3 L20, c2 L10],[c2 L11, c2 L1-1, c2 L10, c4 L00 - c5 L20]]- 5
return M IRRADIANCE(M, n)- 6
ñ ← (n_x, n_y, n_z, 1) - 7
return ñᵀ M ñ
Projection (lines L1 to L3). This is Equation 10 of the paper, a quadrature of . Each texel adds its radiance times the nine basis values times its solid angle. The texels can come in any order and need no storage, so the loop is one pass over the map.
The kernel. The irradiance is the convolution of with the clamped cosine . By the Funk-Hecke theorem, this convolution multiplies each coefficient of band by one number, . The values are , and . The paper gives the general formula: is 0 for odd , and for even it falls off like , so and are already small. This decay is why nine terms are enough.
The matrix (line L4). Line L4 folds and the basis constants into five numbers: , , , and , for example and . The quadratic form then equals term by term. One matrix is built per color channel, so an RGB map gives three matrices.
Evaluation (lines L6 and L7). Line L7 is Equation 11 of the paper. In a shader it is three 4-vector products per channel. The expanded form of Equation 13, , gives the same number.
Example
The environment has two texels that act as point lights: power from and power from . The table shows the nonzero coefficients after each texel.
| step | |||||
|---|---|---|---|---|---|
| after L1 | 0 | 0 | 0 | 0 | 0 |
| texel , weight 2 | 0.564190 | 0.977206 | 0 | 1.261566 | 0 |
| texel , weight 1 | 0.846284 | 0.977206 | 0.488603 | 0.946174 | 0.546274 |
At , ; at , and . Line L4 gives, in exact fractions,
| normal | exact | error | |
|---|---|---|---|
| 142/64 = 2.21875 | 2 | 0.21875 | |
| 80/64 = 1.25 | 1 | 0.25 | |
| 18/64 = 0.28125 | 0 | 0.28125 | |
| 14/64 = 0.21875 | 0 | 0.21875 |
The total power is 3, so the bound is . The normal is perpendicular to both lights and reaches the bound. Point lights are the worst case for the method. A smooth sky is the best case: for the radiance on the upper hemisphere and 0 below, the projection gives , and 0 for the other seven coefficients, and , which is the exact irradiance.
Correctness
Three facts make the algorithm correct; the proof gives all steps. First, by the Funk-Hecke theorem, the convolution with the clamped cosine maps each harmonic to . Second, the quadratic form of line L7 equals , term by term. Together they give for lighting with no component above band 2 (a proved property). Third, for any lighting , with . Since , with equality at , the error is at most of , the total light, at every normal (a proved bound). The same bound holds exactly for the texel sums, which is what the tests check.
The paper states the error in two ways (Section 3.1). The maximum error for any pixel, as a fraction of the total intensity of the lighting, is 9%, the value of . The average error over all surface orientations is under 3% for any physical lighting, a result of Basri and Jacobs. For its three natural environment maps, the paper measured an average error under 1% and a maximum pixel error under 5% (a measurement). The stub summary of this page said only "an error below about three percent"; that number is an average, not a worst case.
Proof: Correctness and error bound of the nine term irradiance mapfull
Setting. The lighting is a radiance on the unit sphere , for distant sources, so it depends on the direction only. The surface is Lambertian and the visibility is ignored. The irradiance at a unit normal is
are the real spherical harmonics of the page, orthonormal on . Lines L1 to L3 compute for , line L4 builds the matrix and line L7 returns with . The claims are about this computation in exact arithmetic.
Claim 1 (the kernel in each band). For every and every unit ,
where is the Legendre polynomial. In particular , and .
Proof. The function is in , so it has the Legendre expansion with . The addition theorem gives . Put this into the integral. By orthonormality only the term , is left, and its value is . With , and , the integrals are , and , so , and .
Claim 2 (the matrix is the truncated sum). For all coefficients and all unit , .
Proof. Write the constants as , , , and . Expand for the symmetric of line L4:
Compare with the sum term by term. Band 0: . Band 1: , and are times . Band 2: the factor of is , so their terms are times , , ; the term of is ; the term of is . Every term matches.
Claim 3 (exact for band limited lighting). If the rule of lines L1 to L3 gives the exact coefficients , and has no component in the bands , then for every .
Proof. Write . By linearity and Claim 1, , which is by Claim 2.
The test file uses a rule that is Gauss-Legendre in on each hemisphere and uniform in . It integrates every polynomial of degree at most 4 in exactly, so the coefficients of a band limited lighting, and of the sky of the page, are exact.
Claim 4 (the kernel of nine terms). Define . Then , and , reached only at .
Proof. The three terms are , and . Their sum is the stated polynomial. On , . This parabola has its values at and at , and its smallest value at , so its absolute value is at most , with equality only at . On , , the mirror image of the same parabola, with the same bound.
Claim 5 (error bound for any lighting). If and the coefficients are exact, then for every unit ,
The same bound holds for the texel sums: if is the brute force sum and is computed from the coefficients of lines L1 to L3 on the same texels, then . The bound is reached by a point light at a normal perpendicular to it.
Proof. By the addition theorem, . Multiply by and integrate, then use Claim 2: . So . With and Claim 4, its absolute value is at most . For the texels, the same steps hold with the integral replaced by the sum over , because the coefficients of line L3 are exactly . For a point light of power in the direction and , and .
Ramamoorthi and Hanrahan state this as a maximum pixel error of 9% of the total intensity; . Their second number, an average error under 3% over all normals for any physical lighting, they take from Basri and Jacobs. This proof does not cover it.
Cost. Lines L1 to L3 do 9 multiply-adds per texel, for texels, and keep 9 numbers. Line L4 costs . Lines L6 and L7 cost 16 multiplications and additions, per normal. An RGB map repeats the work three times.
Complexity
The model is the real RAM, with one unit per arithmetic operation and per evaluation of and , and texels.
- Build: . Lines L2 and L3 do 9 multiply-adds for each texel. Line L4 is constant work. The paper compares this with the direct convolution of the map, which costs for output texels, and reports under a second against about two hours for a map on the hardware of 2001 (a measurement).
- Evaluate: . Lines L6 and L7 cost 16 multiplications and additions per channel. There is no texture fetch.
- Space: . The algorithm keeps 9 coefficients, or 10 distinct entries of the symmetric matrix, per channel. The output map is not stored.
Properties
- Exact for band limited lighting. Lighting that is a polynomial of degree at most 2 in the direction gives the exact irradiance. The hemisphere sky above is one such case for the irradiance.
- Error bound. At most of the total light at any normal, for lighting that is not negative (proved). The bound is tight for a point light.
- Linear in the lighting. The coefficients and the matrix depend linearly on , so the map of a sum of environments is the sum of their maps, and an environment can be blended over time by blending the matrices.
- Smooth output. is a quadratic polynomial in , so it has no seams and no aliasing, and it needs no filtering.
- Rotation. A rotation of the environment mixes the coefficients only inside each band: goes to for the rotation that acts on and keeps the last coordinate.
Variants and improvements
- Tabulated irradiance map. The older way to get the same function is to convolve the environment with the clamped cosine into a small cube map, the irradiance map of Miller and Hoffman and Greene. It costs one texture fetch per pixel, and its memory and build cost are higher.
- Coefficients in place of the matrix. Many engines store the nine values per channel and evaluate the sum of Equation 7 in the shader. It is the same function.
- Fewer bands. Bands 0 and 1 (four coefficients) give the constant and linear part only, with a larger error. More bands add little for the diffuse term, because is small.
- Windowing. Bright small sources make go below zero on the far side, because the truncated series rings. A window on the coefficients, for example the Hanning window that Sloan describes, reduces the ringing and blurs the light. The window is tuning.
- Probes in space. One map per point of a grid gives an irradiance volume, with interpolation between the probes.
- Shadows and interreflection. The method ignores visibility. Precomputed radiance transfer adds them with a transfer vector per vertex.
When to use it
Use it for the diffuse part of image-based lighting on dynamic objects, when the environment is far away and smooth enough, and when memory and bandwidth matter: nine numbers per channel replace a texture. It is a good fit for sky and probe lighting, and for lighting that changes every frame, because the projection is one pass. For sharp lighting such as a small sun, add the sun as a separate light and leave it out of the map. For glossy reflection, use a prefiltered map and the split sum approximation. For the reference answer, integrate the environment by Monte Carlo integration.
Pitfalls
- Solid angles left out. Line L3 must multiply by . On a cube map or a latitude and longitude map, the texels near the corners or the poles have a smaller solid angle. Equal weights at line L3 give a biased .
- Wrong axis convention. The paper uses as the polar axis. In a up engine, either rotate the directions at line L3 and the normal at line L6 in the same way, or permute the entries of . Mixing the two conventions gives light from the wrong side.
- Forgetting , or applying it twice. Line L4 already holds in to . If the code stores convolved coefficients and then uses the matrix of line L4, the bands are scaled twice.
- The of the BRDF. Line L7 returns irradiance. The diffuse radiance is . Using makes the surface times too bright.
- Negative values. For lighting with a bright small source, can be negative at normals that face away from it. Clamp the result of line L7 at 0, or window the coefficients.
- Gamma encoded input. The projection needs linear radiance. Decode an sRGB map first, see gamma correction.
- Rounded constants. The printed and come from the rounded basis constants; the exact values are and . Compute them from when a check needs more than six digits.
Implementations
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/irradiance-environment-map/. The authors are listed in prov/H7PF86.jsonl.
"""The irradiance environment map of Ramamoorthi and Hanrahan (2001).
The environment is a list of texels (direction, radiance, solid angle) of one
color channel, for distant lighting. The algorithm projects the radiance onto
the nine real spherical harmonics of order l <= 2, puts the coefficients into
a symmetric 4 by 4 matrix M (Equation 12 of the paper), and evaluates the
irradiance at a unit normal n as E(n) = (n, 1)^T M (n, 1) (Equation 11).
For an RGB map, run it once for each channel.
Precondition: the directions are unit vectors and the solid angles add up to
4 pi (any quadrature rule on the sphere). Cost: Theta(9 S) for S texels to
build M, and Theta(1) for each evaluation.
The basis order in a coefficient list is (0,0), (1,-1), (1,0), (1,1), (2,-2),
(2,-1), (2,0), (2,1), (2,2). The convention is the one of the paper:
z is the polar axis, x = sin theta cos phi and y = sin theta sin phi.
"""
import math
# Normalization constants of the real spherical harmonics (Equation 3).
Y0 = 0.5 / math.sqrt(math.pi) # 0.282095
Y1 = math.sqrt(3.0 / (4.0 * math.pi)) # 0.488603
Y2 = 0.5 * math.sqrt(15.0 / math.pi) # 1.092548
Y20 = 0.25 * math.sqrt(5.0 / math.pi) # 0.315392
Y22 = 0.25 * math.sqrt(15.0 / math.pi) # 0.546274
# The clamped cosine kernel in each band (Equations 8 and 9).
A_HAT = (math.pi, 2.0 * math.pi / 3.0, math.pi / 4.0)
BAND = (0, 1, 1, 1, 2, 2, 2, 2, 2)
# The constants of Equation 12, from the two lists above.
C1 = A_HAT[2] * Y22 # 0.429043
C2 = A_HAT[1] * Y1 / 2.0 # 0.511664
C3 = A_HAT[2] * Y20 * 3.0 # 0.743125
C4 = A_HAT[0] * Y0 # 0.886227
C5 = A_HAT[2] * Y20 # 0.247708
Vec = tuple[float, float, float]
Texel = tuple[Vec, float, float]
def sh9(d: Vec) -> list[float]:
"""The nine real spherical harmonics at the unit vector d."""
x, y, z = d
return [
Y0,
Y1 * y,
Y1 * z,
Y1 * x,
Y2 * x * y,
Y2 * y * z,
Y20 * (3.0 * z * z - 1.0),
Y2 * x * z,
Y22 * (x * x - y * y),
]
def project(texels: list[Texel]) -> list[float]:
"""Equation 10: L_lm = sum of L(w) Y_lm(w) dw over the texels."""
coeffs = [0.0] * 9 # @line L1
for d, radiance, domega in texels: # @line L2
y = sh9(d)
for k in range(9): # @line L3
coeffs[k] += radiance * y[k] * domega
return coeffs
def irradiance_matrix(coeffs: list[float]) -> list[list[float]]:
"""Equation 12: the symmetric 4 by 4 matrix M from the nine L_lm."""
l00, l1m1, l10, l11, l2m2, l2m1, l20, l21, l22 = coeffs
return [ # @line L4
[C1 * l22, C1 * l2m2, C1 * l21, C2 * l11],
[C1 * l2m2, -C1 * l22, C1 * l2m1, C2 * l1m1],
[C1 * l21, C1 * l2m1, C3 * l20, C2 * l10],
[C2 * l11, C2 * l1m1, C2 * l10, C4 * l00 - C5 * l20],
]
def build(texels: list[Texel]) -> list[list[float]]:
"""IRRADIANCE-MAP: lines L1 to L5."""
m = irradiance_matrix(project(texels))
return m # @line L5
def irradiance(m: list[list[float]], n: Vec) -> float:
"""Equation 11: E(n) = (n, 1)^T M (n, 1) for a unit normal n."""
h = (n[0], n[1], n[2], 1.0) # @line L6
return sum(h[i] * m[i][j] * h[j] for i in range(4) for j in range(4)) # @line L7
def irradiance_sh(coeffs: list[float], n: Vec) -> float:
"""Equation 7: E(n) = sum of A_hat_l L_lm Y_lm(n), the same value as line L7."""
y = sh9(n)
return sum(A_HAT[BAND[k]] * coeffs[k] * y[k] for k in range(9))
def irradiance_brute_force(texels: list[Texel], n: Vec) -> float:
"""The reference: E(n) = sum of L(w) max(0, n . w) dw over all texels."""
total = 0.0
for d, radiance, domega in texels:
c = n[0] * d[0] + n[1] * d[1] + n[2] * d[2]
if c > 0.0:
total += radiance * c * domega
return total
def point_light_coeffs(d: Vec, power: float) -> list[float]:
"""The exact coefficients of a delta light of the given power in direction d."""
return [power * y for y in sh9(d)]
def gauss_legendre(n: int) -> list[tuple[float, float]]:
"""Nodes and weights of the n point Gauss-Legendre rule on [-1, 1], by Newton's method."""
out = []
for i in range(1, n + 1):
x = math.cos(math.pi * (i - 0.25) / (n + 0.5))
for _ in range(100):
p0, p1 = 1.0, x
for k in range(2, n + 1):
p0, p1 = p1, ((2 * k - 1) * x * p1 - (k - 1) * p0) / k
dp = n * (x * p1 - p0) / (x * x - 1.0)
dx = p1 / dp
x -= dx
if abs(dx) < 1e-15:
break
p0, p1 = 1.0, x
for k in range(2, n + 1):
p0, p1 = p1, ((2 * k - 1) * x * p1 - (k - 1) * p0) / k
dp = n * (x * p1 - p0) / (x * x - 1.0)
out.append((x, 2.0 / ((1.0 - x * x) * dp * dp)))
return out
def sphere_texels(radiance, n_z: int, n_phi: int) -> list[Texel]:
"""Sample a radiance function on the sphere as texels.
The rule is Gauss-Legendre in z on [-1, 0] and on [0, 1] separately, and
uniform in phi. It integrates exactly a polynomial in (x, y, z) of degree
below min(2 n_z, n_phi) on each hemisphere, so a lighting with a jump at
the horizon still projects exactly when it is a polynomial on each half.
"""
texels = []
rule = gauss_legendre(n_z)
for lo in (-1.0, 0.0):
for t, w in rule:
z = lo + 0.5 * (t + 1.0)
s = math.sqrt(max(0.0, 1.0 - z * z))
for j in range(n_phi):
phi = 2.0 * math.pi * (j + 0.5) / n_phi
d = (s * math.cos(phi), s * math.sin(phi), z)
texels.append((d, radiance(d), 0.5 * w * 2.0 * math.pi / n_phi))
return texels
test_irradiance_environment_map.py 154 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/irradiance-environment-map/. The authors are listed in prov/H7PF86.jsonl.
"""Tests of the irradiance environment map against closed forms and brute force.
The constants are checked against the values printed in the paper, the basis
for orthonormality, the matrix form against the spherical harmonic sum, and
the nine term irradiance against brute force quadrature: exactly for lighting
that is band limited, and within the proved bound 3/32 of the total light for
any lighting that is not negative.
"""
import math
import random
from irradiance_environment_map import (
A_HAT,
C1,
C2,
C3,
C4,
C5,
build,
gauss_legendre,
irradiance,
irradiance_brute_force,
irradiance_matrix,
irradiance_sh,
point_light_coeffs,
project,
sh9,
sphere_texels,
)
def unit(rng: random.Random) -> tuple[float, float, float]:
while True:
v = (rng.uniform(-1, 1), rng.uniform(-1, 1), rng.uniform(-1, 1))
r = math.sqrt(v[0] ** 2 + v[1] ** 2 + v[2] ** 2)
if 0.1 < r <= 1.0:
return (v[0] / r, v[1] / r, v[2] / r)
def test_constants_of_the_paper() -> None:
for got, want in ((C1, 0.429043), (C2, 0.511664), (C3, 0.743125), (C4, 0.886227), (C5, 0.247708)):
# The exact c2 and c3 are 0.5116634 and 0.7431239. The printed values
# 0.511664 and 0.743125 come from the rounded basis constants.
assert abs(got - want) < 2e-6
for got, want in zip(A_HAT, (3.141593, 2.094395, 0.785398)):
assert abs(got - want) < 5e-7
def test_kernel_is_the_clamped_cosine() -> None:
# A_hat_l = 2 pi times the integral of t P_l(t) over [0, 1].
legendre = (lambda t: 1.0, lambda t: t, lambda t: 0.5 * (3 * t * t - 1))
rule = gauss_legendre(6)
for l in range(3):
val = 2 * math.pi * sum(0.5 * w * ((x + 1) / 2) * legendre[l]((x + 1) / 2) for x, w in rule)
assert abs(val - A_HAT[l]) < 1e-12
def test_basis_is_orthonormal() -> None:
texels = sphere_texels(lambda d: 1.0, 3, 8)
assert abs(sum(t[2] for t in texels) - 4 * math.pi) < 1e-12
for i in range(9):
for j in range(9):
s = sum(sh9(d)[i] * sh9(d)[j] * dw for d, _, dw in texels)
assert abs(s - (1.0 if i == j else 0.0)) < 1e-12
def test_matrix_equals_the_harmonic_sum() -> None:
rng = random.Random(7)
for _ in range(50):
coeffs = [rng.uniform(-2, 2) for _ in range(9)]
m = irradiance_matrix(coeffs)
assert all(m[i][j] == m[j][i] for i in range(4) for j in range(4))
for _ in range(5):
n = unit(rng)
assert abs(irradiance(m, n) - irradiance_sh(coeffs, n)) < 1e-12
def test_point_light_check_values() -> None:
d = (0.0, 0.0, 1.0)
m = irradiance_matrix(point_light_coeffs(d, 1.0))
assert abs(irradiance(m, d) - 17 / 16) < 1e-12
assert abs(irradiance(m, (0.0, 0.0, -1.0)) - 1 / 16) < 1e-12
assert abs(irradiance(m, (1.0, 0.0, 0.0)) - 3 / 32) < 1e-12
def test_kernel_of_nine_terms() -> None:
# The addition theorem: sum of A_hat Y(w) Y(n) = 3/32 + t/2 + 15 t^2 / 32 with t = n . w.
rng = random.Random(3)
for _ in range(100):
n, w = unit(rng), unit(rng)
t = sum(a * b for a, b in zip(n, w))
val = irradiance_sh(point_light_coeffs(w, 1.0), n)
assert abs(val - (3 / 32 + t / 2 + 15 * t * t / 32)) < 1e-12
def test_sky_is_exact() -> None:
texels = sphere_texels(lambda d: 1.0 if d[2] > 0 else 0.0, 4, 8)
coeffs = project(texels)
assert abs(coeffs[0] - math.sqrt(math.pi)) < 1e-12
assert abs(coeffs[2] - 0.5 * math.sqrt(3 * math.pi)) < 1e-12
assert all(abs(coeffs[k]) < 1e-12 for k in (1, 3, 4, 5, 6, 7, 8))
m = build(texels)
rng = random.Random(11)
for _ in range(30):
n = unit(rng)
assert abs(irradiance(m, n) - math.pi * (1 + n[2]) / 2) < 1e-12
def test_band_limited_lighting_matches_brute_force() -> None:
rng = random.Random(5)
coeffs = [rng.uniform(-1, 1) for _ in range(9)]
coeffs[0] = 4.0
def radiance(d):
return sum(c * y for c, y in zip(coeffs, sh9(d)))
m = build(sphere_texels(radiance, 3, 8))
fine = sphere_texels(radiance, 24, 96)
for _ in range(8):
n = unit(rng)
assert abs(irradiance(m, n) - irradiance_sh(coeffs, n)) < 1e-12
assert abs(irradiance(m, n) - irradiance_brute_force(fine, n)) < 2e-3
def test_error_bound_for_any_lighting() -> None:
rng = random.Random(13)
texels = sphere_texels(lambda d: 0.0, 6, 12)
for trial in range(10):
lit = [(d, rng.random() ** 6 * 10.0, dw) for d, _, dw in texels]
total = sum(r * dw for _, r, dw in lit)
m = build(lit)
worst = 0.0
for _ in range(20):
n = unit(rng)
err = abs(irradiance(m, n) - irradiance_brute_force(lit, n))
worst = max(worst, err)
assert err <= 3 / 32 * total + 1e-9
assert worst > 0.0
def test_linear_in_the_lighting() -> None:
rng = random.Random(17)
texels = sphere_texels(lambda d: 0.0, 3, 8)
a = [(d, rng.random(), dw) for d, _, dw in texels]
b = [(d, rng.random(), dw) for d, _, dw in texels]
ab = [(d, 2.0 * x[1] + 3.0 * y[1], d2) for (d, _, d2), x, y in zip(texels, a, b)]
ma, mb, mab = build(a), build(b), build(ab)
for i in range(4):
for j in range(4):
assert abs(mab[i][j] - 2.0 * ma[i][j] - 3.0 * mb[i][j]) < 1e-12
History
Reflection mapping goes back to Blinn and Newell (1976), and Miller and Hoffman (1984) convolved environment maps to get diffuse and glossy reflection maps; Greene (1986) stored environments as cube maps and also prefiltered them. These maps were built by direct convolution, which was slow. In 2001, Ramamoorthi and Hanrahan, and at the same time Basri and Jacobs in computer vision, showed that the clamped cosine acts as a low pass filter on the sphere, so that the irradiance is close to a function of the first nine spherical harmonics. Ramamoorthi and Hanrahan wrote it as a quadratic form in the normal that a vertex or pixel shader could evaluate. Spherical harmonics then became the common way to store diffuse lighting in games, in precomputed radiance transfer (Sloan, Kautz and Snyder 2002) and in light probes.
References
- Ravi Ramamoorthi and Pat Hanrahan, An efficient representation for irradiance environment maps, SIGGRAPH 2001, 497 to 500, Section 2 (Equations 3 to 9, the basis and the clamped cosine coefficients), Section 3.1 (prefiltering, Equation 10, and the error statement) and Section 3.2 (Equations 11 to 13, the matrix form and the constants c1 to c5)
- Ronen Basri and David W. Jacobs, Lambertian reflectance and linear subspaces, IEEE International Conference on Computer Vision (ICCV), 2001, the analysis of the clamped cosine kernel and the bound on the average error of a nine term approximation (general locator, cited by Ramamoorthi and Hanrahan for the 3% figure)
- 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 10, Local Illumination (the parts on spherical harmonics and on environment lighting)
Where this leads
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 | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag H7PF86 Edit this page History Report an error Markdown