# Spherical Harmonics

Written by Paul Bourke
February 1990

MSWindows interactive viewer: SHDEmo.exe.gz contributed by Wolfgang Wester
(Requires OpenGL and Win95/98/Me or Win NT/2000)

Contribution by Georg Duemlein: pythonSOP

 Formulation The following closed objects are commonly called spherical harmonics although they are only remotely related to the mathematical definition found in the solution to certain wave functions, most notable the eigenfunctions of angular momentum operators. The formula is quite simple, the form used here is based upon spherical (polar) coordinates (radius, theta, phi). r = sin(m0 phi)m1 +   cos(m2 phi)m3 +   sin(m4 theta)m5 +   cos(m6 theta)m7 Where phi ranges from 0 to pi (lines of latitude), and theta ranges from 0 to 2 pi (lines of longitude), and r is the radius. The parameters m0, m1, m2, m3, m4, m5, m6, and m7 are all integers greater than or equal to 0.     Implementation details The images here were created using OpenGL. While the parameters m0, m1, m2, m3, m4, m5, m6, m7 can range from 0 upwards, as the degree increases the objects become increasingly "pointed" and a large number of polygons are required to represent the surface faithfully. All the examples here have a maximum degree of 6. The maximum number of polygons used is 128 x 128 and most were only 64 x 64, that is, the theta and phi angles are split into 64 equal steps each. The C function that computes a point on the surface is ```XYZ Eval(double theta,double phi, int *m) { double r = 0; XYZ p; r += pow(sin(m[0]*phi),(double)m[1]); r += pow(cos(m[2]*phi),(double)m[3]); r += pow(sin(m[4]*theta),(double)m[5]); r += pow(cos(m[6]*theta),(double)m[7]); p.x = r * sin(phi) * cos(theta); p.y = r * cos(phi); p.z = r * sin(phi) * sin(theta); return(p); } ``` The OpenGL snippet that creates the geometry is ``` du = TWOPI / (double)resolution; /* Theta */ dv = PI / (double)resolution; /* Phi */ glBegin(GL_QUADS); for (i=0;i yellow -> green -> cyan -> blue -> magenta -> red. Note it is a circular colour map. There are many other ways of colouring these surfaces, for example a radial intensity variation might look nice. While the objects here all look the same size, there is generally quite a bit of variation in the size arising from applying the above formula directly. Find a normalising term (function of the m0, m1, .... m7) that will make surfaces fit into a unit cube.

Very nice rendering technique by Georg Duemlein (Nov 2008).
For source code see: pythonSOP