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