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…

Assuming points on a unit sphere are defined in Cartesian coordinates as:

Then the first three bands of the SH basis are simply:

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:

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):
Wow.

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:

The definition of A isn’t exactly straight forward, but luckily smart people have already done the hard work for us:

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:

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?

Related

13 Responses to “Spherical harmonics, WTF?”

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.

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 🙂

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.

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.

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?
Thanks

[…] 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 […]

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.

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.