Spherical Harmonics is widely used in Computer Graphics. They are analogue to Fourier basis on a sphere, consists of a set of orthogonal functions to represent functions defined on the surface of a sphere.

However, they are very tricky to implement due to lots of constants and integral functions.

Here is a real-time visualization that forks iq’s raytracing of SH functions, but adding the fourth order of them.


Applications: Irradiance

One can easily use the equation above and extract high order of spherical harmonics using prefilter.c, then apply them for irradiance

First, convert the coordinate systems from panorama:

x = 2.0 * i / width – 1.0;
y = 2.0 * j / width – 1.0;

theta = y * PI;
phi = x * PI / 2.0;

x = sin(phi) * sin(theta);
y = cos(theta);
z = sin(phi) * cos(theta);

The delta angle is given by

domega = (2 * PI / width) * (2 * PI / width) * sinc(theta);

Then one can update the integration using the fomulas