During the last couple of days, I have had the indescribable joy of learning in practice about how tiny typos, understatements or mistakes in scientific papers can turn a *lots-of-typing-but-that’s-it* kind of an algorithm into a purgatory. And hell do I mean it ðŸ™‚

Rotating spherical functions expressed in spherical harmonics coefficients have been described by some academic people as simple, but don’t take their word – it’s a cynical irony.. unless all you intend to do is a little bit of shifting on the Z axis, in which case I will humbly agree and pass you the microphone. If your plans are more ill-willed or if you are striving for a generic XYZ solution which works for any reasonable number of bands, well then – bad news: prepare for a bumpy ride, things will hit the roof.

Now there are some very important works on the net that you’ll have definitely bumped upon, if you ever showed interest in the subject. These include papers like:

* “*Rapid and stable determination of rotation matrices between sphericalÂ harmonics by direct recursion*” byÂ *IvanicÂ *et al;

* “*Spherical Harmonics: The Gritty Details*” by *Green* which is an excellent starting point Â **but **..bear with me :);

* Â “*Fast Approximation to Spherical Harmonic Rotation*” byÂ *KrivanekÂ *et al;

What you quickly learn from reading these papers is that naive XYZ rotation of SH coefficients is a no-go if you intend to do it on a per-fragment manner – it’s just too many calculations and operating instructions for hardware of the man on the street. Instead, you need to hack your way through, tabulating data and doing a bit of a wacky stuff.

My intent today is not to give you an overview of available techniques – you can read about them by yourself, assuming you do not quickly get worn-out when shown integrals (or even double ones, I dare you!). Since this blog is – at least for the time being – mostly about showing off with stuff that is a far cry from anything that’s visually pleasible ðŸ˜‰ and giving Â heads-up to people about rather unobvious coding problems I had faced in my programming career, please: let’s have a closer look at theÂ **butÂ **word in the bullet-point list above.

*Robin Green*, who is the person behind that “but”t-ed article, did an excellent job giving a quick walkthrough for various topics connected with working with Spherical Harmonics. Really, hats off. What I’m missing, however, is an errata which is basically nowhere to be found on the net.

One thing that I have spotted so far in there which is** notÂ **flawless is the last appendix which contains various formulas necessary to calculate rotation matrices for all SH bands. If you implemented the algorithm, you’ll have noticed that your implementation accesses illegal array elements during the very first iteration! It seems as if the assumption:

**-l <= m’ <= l**

does not hold at all! Depending on how you implement the algorithm, in the worst case you may not even notice it and spend countless hours, trying to debug rotations, going mad, simultaneously observing your hair slowly receding to oblivion.

Â ~~What seems to be aÂ solution in this case (seems, as I have not yet had a chance to visually verify this) is toÂ ~~**recursivelyÂ **calculate the rotation SH coefficients for all *<l, m, m’>Â *triple combinations that are.. well, illegal. ðŸ™‚ What I know at this moment is that this approach does not cause a stack overflow and seems to be the way taken by other people as well (see thisÂ as an example).

Â Â Â Â **Update:Â **

Finally got the shaders right. From the looks of it, the right way to cope with the bug in the aforementioned paper is to **use zeroes for coefficients that are outside the allowed domain**. If you try to do the calculations anyway, you’ll end up with invalid floating-point values due to divide-by-zero operation getting into your way. Hope this saves somebody’s day ðŸ™‚