## 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:

Where…

are the associated Legendre polynomials:

etc…

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?

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

April 14, 2011 at 3:09 pm

Hey,

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

April 14, 2011 at 3:51 pm

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 🙂

April 15, 2011 at 6:01 pm

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.

April 16, 2011 at 1:57 am

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

http://www.insomniacgames.com/assets/files/128707735545510.pdf

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

April 17, 2011 at 5:29 pm

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

April 22, 2011 at 8:34 pm

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.

June 20, 2013 at 1:58 pm

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

September 4, 2013 at 5:06 am

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

January 14, 2014 at 5:47 am

http://www.insomniacgames.com/useful-results-in-spherical-harmonics-mainly-2-band/ is the up-to-date IG link. Hope that helps.

August 20, 2014 at 1:54 am

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…

August 20, 2014 at 8:31 am

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.

August 21, 2014 at 5:05 am

thanks,

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 ..

August 18, 2016 at 2:53 am

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.