Spherical Harmonic Lighting

Introduction
Spherical Harmonics(SH) functions are a set of orthogonal basis functions defined in spherical coordinates using imaginary numbers. In this post, we use the following conversion between spherical and cartesian coordinates:
Since we are dealing with real value functions, we only need to deal with real spherical harmonics functions which in the form of:
The index l of the SH function is called the band index which is an integer >= 0 and index m is an integer with range -l<=m<=l , so there will be (2l + 1) functions in a given band. You may refer to the Appendix A2 of Stupid Spherical Harmonics(SH) Trick to look up the evaluated value of the SH basis function for a pair of (l, m).

The linear combination of SH basis functions with scalar values can be used to approximate a function as below:
With an approximation up to band l = - 1, which n×n coefficients are needed.
So the remaining problem to approximate a function is to compute the coefficient c which can be solved either analytically or numerically by Monte Carlo Integration.

Monte Carlo Integration
To compute a definite integral numerically, we can consider the Monte Carlo Estimator:
When the number of samples, N, is large enough, the estimator F will equal to the definite integral because considering the expected value of F:
When number of samples,N, is large enough, by the law of large numbers, the estimator F will converge to the definite integral. Therefore, we can calculate the coefficient of the SH basis functions by using Monte Carlo Estimator.

Properties of Spherical Harmonics Function
There are 2 important properties properties of SH functions:
First, it is rotationally invariant. 
Where the rotated function g is still a SH function which its coefficients can be computed by using the coefficients of f. For details of rotating a general SH functions, you can refer to the section 'Rotating Spherical Harmonics' in Spherical Harmonics Lighting: The Gritty Details.

Second, when integrating 2 SH projected functions over the spherical domain, the results will equals to dot product of their SH coefficients (due to the SH basis functions are orthogonal):
This is a nice property that we can calculate the integration over the spherical domain by a dot product of the SH coefficients.

Lighting with SH functions
When performing lighting calculation, we need to solve the rendering equation:
For shading lambert diffuse surface without shadow, we can simplify the rendering equation into:
To solve this integral, we can project the functions L(x, ω) and max(N.ω, 0) into SH functions using Monte Carlo Integration, then by the property 2 described above, the integral equals to dot product of the SH coefficients of the 2 SH projected functions.

Zonal Harmonics
If a SH projected function is rotational symmetric about a fixed axis, it is called Zonal Harmonics(ZH). If this axis is the z-axis, this will make the ZH function only depends on θ, which will result in only one non-zero coefficient in each band with m= 0. Then rotation of the ZH function can be greatly simplified. When the ZH function is rotated to a new axis d, the coefficients of the rotated SH function will equals to:
,which is faster than the general SH rotation. The ZH function is well suit to approximate the function max(N.ω, 0) in the above diffuse surface rendering equation since the SH projected L(xω) is usually done in world space while the shading surface can be re-oriented to the same space to perform lighting calculation.

WebGL Demo
Below is a webGL demo (which need a webGL enabled browser such as Chrome) using the cube map on the right as light source and projected to SH function using Monte Carlo Integration.

Both the white and the blue color on the model is reflected from the sun and the blue sky using SH coefficients generated from the cube map and the ZH coefficients projected from max(N.ω, 0) which rotated to world space according the surface normal. The approximation is done up to band l=2.  You can drag in the viewport to rotate the camera.
Your browser does not support the canvas tag/WebGL. This is a static example of what would be seen.
The source code of the webGL can be downloaded here.

Conclusion
SH functions can be used to approximate the rendering equation with only a few coefficients and a simple dot product to evaluate lighting during run time. But it also has its disadvantage while SH can only approximate low frequency function as it needs large number of bands to represent high frequency details.

Reference
[1] Spherical Harmonics Lighting: The Gritty Details: http://www.research.scea.com/gdc2003/spherical-harmonic-lighting.pdf
[2] Stupid Spherical Harmonics(SH) Trick: http://www.ppsloan.org/publications/StupidSH36.pdf
[3] Physically Based Rendering: http://www.amazon.com/gp/product/0123750792/ref=pd_lpo_k2_dp_sr_1?pf_rd_p=486539851&pf_rd_s=lpo-top-stripe-1&pf_rd_t=201&pf_rd_i=012553180X&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=09AG8FQQWKJHC2AEFPD1
[4] Sky box texture downloaded from: http://www.codemonsters.de/home/content.php?show=cubemaps

Dual Quaternion

Introduction
Continue to the last post which introduce Dual Number, it can be extended to Dual Quaternion. Dual Quaternion is similar to Quaternion with all the real numbers replaced by dual numbers:
Dual quaternion can also be regrouped into the following form by:
which is in terms of 2 quaternions (real part and dual part).

Arithmetic operations
Dual quaternion can perform arithmetic operations as below:

Addition:
Multiplication:
Conjugate:
Norm:
note that the norm of a dual quaternion is a dual number.

Dual Quaternion as Rigid Transform
Unit Dual Quaternion can represent a rigid transformation (i.e. rotation & translation) like matrix. Like ordinary quaternion, dual quaternion can represent a rotation using ordinary quaternion with the dual part equals to zero:
To representation a translation (tx, ty, tz), the following dual quaternion can be used:
Then, we can combine the above 2 transform into 1 dual quaternion (which is also unit dual quaternion) to represent a rotation followed by a translation
With the above arithmetic operations, we can transform a point p(px, py, pz), using unit dual quaternion like ordinary unit quaternion:


Dual Quaternion - Matrix Conversion
Sometimes it is convenient to have methods to convert between Unit Dual Quaternion and Matrix. Assume we have ordinary quaternion - matrix conversion functions.

Converting Dual Quaternion to Matrix:
Given any unit dual quaternion which is composed of a rotation followed by a translation, we have:
Therefore, we can find the rotation matrix by considering the real part of the dual quaternion while the translation (i.e. tx, ty, tz) can be solved by using system of linear equations by equating coefficients.

Converting Matrix to Dual Quaternion:
Given a rigid transform matrix, we can decompose the rotation matrix into real quaternion as usual and the translation dual quaternion can also be obtained by:

then multiply the translation dual quaternion with rotation dual quaternion will give the answer.

Blending Rigid Transform using Dual Quaternion
Using dual quaternion to represent transform is better when blending multiple transformations, which can be applied to skinning a mesh. From that paper, it suggests using an fast approximation for blending called Dual quaternion Linear Blending (DLB):
note that the norm in the denominator is a dual number, which can be treated as 1 divided by the norm and gives another dual number (using dual number division) so that it can be multiply by a dual quaternion.

Conclusion
Dual quaternion is an alternative way to represent a rigid transform other than matrix. And it may gives a faster accumulation of transformation if joint inferences per vertex is large enough according to "Spherical Skinning with Dual-Quaternions and QTangents".



Reference:
[1] Dual Quaternion for Rigid Transformation Blending: http://www.jarmilakavanova.cz/ladislav/papers/sdq-i3d07/sdq-i3d07.pdf
[2] Spherical Skinning with Dual-Quaternions and QTangents: http://www.crytek.com/sites/default/files/izfrey_siggraph2011.pdf
[3] Estimating 3-D location parameters using dual number quaternions: https://pwww2.cse.tamu.edu/volzfest/proceedings/paper-dedications/Shao_Lejun.pdf
[4] Dual quaternion as a tool for rigid body motion: http://www.ingegneriameccanica.org/papers/pennestrivalentini_paper.pdf

Dual Number

Introduction
Recently, I read the "Spherical Skinning with Dual-Quaternions and QTangents" from Crytek. It raised my interest on the topic of "Dual Number" (which is related to Dual Quaternion). Dual number, just like imaginary number, has the form of:
where the real number a is called real part and the real number b is called dual part.


Arithmetic operations
Dual number can perform the arithmetic operations as below:

Addition:
Multiplication:
Division:

Finding derivative using Dual Number
The interesting part of dual number is when it is applied to Taylor Series. When substituting a dual number into a differentiable function using the Taylor Series:
This gives a very nice property that we can find the first derivative, f'(a), by consider the dual part of f(a+bε), which can be evaluated using dual number arithmetic.
For example, given a function
we want to find the first derivative of f(x) at x = 2, i.e. f'(2). We can find it by using dual number arithmetic where f'(2) will equals to the dual part of  f(2+ε) according to Taylor Series.
Therefore, f'(2)= 8/9, you can verify this by finding f'(x) and substitute 2 into it, which will give the same answer.

Conclusion
By using dual number, we can find the derivative of a function using dual arithmetic. Hence, we can also find the tangent to an arbitrary point, p, on a given parametric curve which is equals to the normalized dual part of the point p. For those who are interested in finding out more about dual number, I recommend to read the presentation "Dual Numbers: Simple Math, Easy C++ Coding, and Lots of Tricks" by Gino van den Bergen in GDC Europe 2009.

Reference:
[1] http://en.wikipedia.org/wiki/Dual_number
[2] http://www.gdcvault.com/play/10103/Dual-Numbers-Simple-Math-Easy