Spherical harmonics, WTF?

April 14, 2011

I never really “got” spherical harmonics, there was something about them that just didn’t click for me. A little late to the party, I spent a few evenings recently reading over all the introductory papers I could find. Several times. This post is mostly just a brain dump, made in an effort to get everything straighted out in my head. I did stumble across a few curiosities though…

Real valued spherical harmonics can be defined as:
Y^m_l(\theta, \phi) = \Phi^m(\phi) \, N^{|m|}_l \, P^{|m|}_l(cos\,\theta)

P^m_l are the associated Legendre polynomials:
P^0_0(x) = 1
P^0_1(x) = x
P^1_1(x) = -\sqrt{1-x^2}
P^0_2(x) = \frac{1}{2}(3x^2-1)
P^1_2(x) = -3x\sqrt{1-x^2}
P^2_2(x) = 3(1-x^2)

\Phi^m(x) = \begin{cases}  \sqrt{2} cos(mx), & m > 0 \\  1, & m = 0 \\  \sqrt{2} sin(|m|x), & m < 0  \end{cases}

N^m_l = \sqrt{ \frac{2l+1}{4\pi} \, \frac{(l-m)!}{(l+m)!}}

Assuming points on a unit sphere are defined in Cartesian coordinates as:
x = sin\theta \, cos\phi
y = sin\theta \, sin\phi
z = cos\theta

Then the first three bands of the SH basis are simply:
Y^0_0(\theta, \phi) = \sqrt{\frac{1}{4\pi}}
Y^{-1}_1(\theta, \phi) = -\sqrt{\frac{3}{4\pi}} y
Y^0_1(\theta, \phi) = \sqrt{\frac{3}{4\pi}} z
Y^1_1(\theta, \phi) = -\sqrt{\frac{3}{4\pi}} x
Y^{-2}_2(\theta, \phi) = \sqrt{\frac{15}{4\pi}} xy
Y^{-1}_2(\theta, \phi) = -\sqrt{\frac{15}{4\pi}} yz
Y^0_2(\theta, \phi) = \sqrt{\frac{5}{16\pi}} (3z^2-1)
Y^1_2(\theta, \phi) = -\sqrt{\frac{15}{4\pi}} zx
Y^2_2(\theta, \phi) = \sqrt{\frac{15}{16\pi}} (x^2-y^2)

Note the change in sign of odd m harmonics, which is consistent with the above definitions of x, y, z and P. In many sources the basis function constants are all positive, which can be explained by assuming that they’re defined using the Condon-Shortley phase. That took me a while to figure out.

Projecting incident radiance L into the SH basis is done using the following integral:
L^m_l = \int^{2\pi}_0 \int^{\pi}_0 \, L(\theta, \phi) \, Y^m_l(\theta, \phi) \, sin(\theta) d\theta d\phi

This is actually a spectacularly bad approximation for low numbers of SH bands. For example, here’s Paul Debevec’s light probe of Grace cathedral:

And here’s that same probe’s projection into three SH bands (negative values have been clamped to zero):


Fortunately, while spherical harmonics aren’t generally good at representing incident radiance, they totally kick arse at representing irradiance. (Very roughly speaking, incident radiance is the the amount of light falling on a surface from a particular direction, while irradiance is the total sum of light falling on a surface from all directions.)

In SH form the conversion from radiance L to irradiance E is marvelously simple:
E^m_l = \hat{A}_l \, L^m_l

The definition of A isn’t exactly straight forward, but luckily smart people have already done the hard work for us:
\hat{A}_0 = 3.141593
\hat{A}_1 = 2.094395
\hat{A}_2 = 0.785398
\hat{A}_3 = 0
\hat{A}_4 = -0.130900
\hat{A}_5 = 0
\hat{A}_6 = 0.049087

The fact that terms after 2 fall off very quickly is what makes it possible to approximate irradiance fairly accurately with only three bands.

Given a set of spherical harmonic irradiance coefficients, the diffuse illumination for a particular direction is calculated by:
E(\theta, \phi) = \sum_{l, m} \hat{A}_l \, L^m_l Y^m_l(\theta, \phi)

Condon-Shortley phase aside, something else that confused me were the results from An Efficient Representation for Irradiance Environment Maps. As far as I can tell, the gamma is not correct for some (possibly all) of the images in that paper, which made comparing results from my own code frustrating. The problems are compounded by the fact that the authors chose to apply some undefined tone mapping operator to some images, but not others.

Here’s the Grace cathedral probe again, this time with an exposure of -2.5 stops:

Below is the result I got from performing a diffuse convolution of the probe using Monte Carlo integration and 1024 samples per pixel (I was too impatient to wait for a brute force convolution to finish). The exposure is set to -2.5 stops. It’s very close to the result of applying a diffuse convolution in HDR Shop:

And here’s the result of projecting the light probe into three SH bands and converting from the coefficients from radiance to irradiance. Again, the exposure is -2.5 stops. It’s pretty close to the Monte Carlo result, which is reassuring:

Now, let’s compare these results to the those from the irradiance environment maps paper. First, their brute force diffuse convolution:

And now their SH approximation:

My guess is that they either applied a gamma curve of 2.2 for some reason, or didn’t correctly account for sRGB colour space when performing the HDR to LDR conversion. Or am I doing it wrong?

Here are the papers that I cribbed from:

Ramamoorthi & Hanrahan’s paper that introduced SH to the rendering community: An Efficient Representation for Irradiance Environment Maps

Their earlier paper actually contains a more rigorous treatment of spherical harmonics: On the relationship between radiance and irradiance: determining the illumination from images of a convex Lambertian object

Robin Green’s great introduction to the topic: Spherical Harmonic Lighting: The Gritty Details

And Volker Schönefeld wrote my favourite introduction, the way he describes SH in terms of separate functions of theta and phi made everything fall into place: Spherical Harmonics


13 Responses to “Spherical harmonics, WTF?”

  1. David Says:


    I went through the same struggles. Having his images not be gamma correct made me doubt my results. Though, there are a couple things you can do to make sure your results are correct.

    For example, if your image is all white then if you sample your normal anywhere you can make sure that the convolution is going to be 1.0

    I hate the Ravi Rammaoorthi paper that everyone references because it really provides no explanation behind the math. It kept me severely confused for a long time. Though, the second paper you linked and his phd thesis http://graphics.stanford.edu/papers/ravir_thesis/
    are really good.

    I learned a lot from his PhD thesis and it cleared up a lot of issues for me.

    -= Dave

  2. Tom Madams Says:

    Thanks for the link to the thesis, I hadn’t found that and it looks pretty comprehensive. It’ll make for good bedtime reading.

    I did just what you described with the all white images. What’s nice about that is that when you convert to an irradiance map, the values become pi, which is what you’d expect from the integral of a Lambertian BRDF. That was the moment I decided that I was right and the paper was wrong 🙂

  3. Robin Green Says:

    Been doing a lot of looking into spherical basis functions generally and have come to the conclusion that SH functions are pretty much the wrong thing to use in most all cases. The Ravi Rampossibletospell’s paper is truly obtuse, right up there with Schröder & Sweldens implenetrable paper on “Spherical Wavelets”. It’s heartening to see just how quickly the astrophysics world note the cite and it’s deficiencies and move on to better things.

  4. Miles Says:

    Also Insomniac’s “Useful results in Spherical Harmonics” is a nice summary:


    @Robin can you provide any pointers to alternatives or what the astrophysics world uses instead?

  5. David Neubelt Says:

    I can’t speak for Robin but I’ve been doing the same research and came to the same conclusion.

    I’d refer you to look into spherical radial basis functions. http://www.cse.cuhk.edu.hk/~ttwong/papers/srbf/srbf.html

  6. Robin Green Says:

    Hoping to drop the results in a future Siggraph and GDC paper and tutorial. It’s been a long, 4-year journey into the depths of basis functions, Riesz and Hilbert spaces and the fundamental nature of orthogonality. A short post wouldn’t cover it.

  7. I am looking into spherical harmonic lighting for my masters project (C++, OpenGL). I have found Robin’s paper quite useful so far. I get uto the point of computing the SH but I am a bit lost when it comes to the lighting equation. Can somebody point me to some code that will make things clearer?

  8. […] to 5 skipping that zeroed out 4th order, then obtaining the coefficients can be difficult. Spherical harmonics, WTF? on the I’m doing it wrong blog has the required numbers multiplied by pi. The origin of the […]

  9. ekflame Says:

    hello~ I’m studying SH Light with http://www.cs.columbia.edu/~cs4162/slides/spherical-harmonic-lighting.pdf

    hmm… I have two question about it.. first is..

    1) Equation 4. Mapping [0..1,0..1] random numbers into spherical coordinates.

    in above E4. theta is mapped to ‘2arccos( root( 1 – deltaX) ).. but I can’t understand where come from that… why don’t use phi*deltaX ?

    2) Equation 7. Integrating the product of two SH functions by evaluating a dot product of their coefficients.

    I have known, funcY ( SH basis function ) is orthonormal, so P0xP1 = 0. P1xP1->1 but I can’t prove Equation 7….

    I need help… thank you…

    • Tom Madams Says:

      1) If you use π*ξ_x you won’t get a uniform sampling of points over the hemisphere, you’ll get a much higher density of points near the top of your hemisphere. Equation 4 corrects for this; try plotting the points, it should be clear to see.

      2) Hmm, I’m not sure I can help you here. Sorry, it’s been a long time since I thought about this and my maths is quite rusty.

      • ekflame Says:


        1) I guess it too… but I don’t know what that equation come from…

        2) I solved it, I misunderstood , y1(s)y1(s) = 1, y1(s)y2(0) = 0.
        It’s not on multiplying, it’s on integrating…

        thanks for your help. about question (1), I need more thinking ..

  10. Raul Says:

    It should be noted that the values reported in Figure 2 of the Siggraph paper are scaled by multiplying by 10 for the Grace Cathedral and dividing by 10 for the Eucalyptus Grove (rnl_probe) and St. Peters Basilica.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: