Using spherical harmonics for lighting I faced a problem for a big enough bandwidths. The correctness of an approximation by first n^2
terms became worse and worse starting from n=7
. I look into associated Legendre polynomials definition and found out, that there is a ratio of factorials (l - m)! / (l + m)!
in normalization constant. For n = 7
(l + m)!
can be up to 12!
. I have to use float
(IEEE-754 32-bit floating-point type), due to GPUs nature.
Now I think, that tgamma
from C
/C++
might be more appropriate, then naive calculation of factorial by definition. Even more: maybe there is a good (approximation) formula for ratio of gamma functions (of two big arguments).
Is there a good stable approach to calculate gamma function (for big positive integers) in shaders?
Surely I just can save a lookup table (matrix) for all the possible combinations of values in numerator and denominator, but I want to have alternative (space-efficient) approach.