Archive for March 17th, 2008


Spherical Harmonics Lit, Supersampled

Thanks to some invaluable assistance from Memo, who sacrificed valuable time off work to help, I finally have a version of the Spherical Harmonics shader that has working lighting (a simple Phong Directional model).

I’ve mixed the previous simple shading with the lighting, in an attempt to make it look like it’s glowing from within. Doesn’t quite work, but looks more intriguing than the lighting on its own.

Spherical Harmonics Lit 01Spherical Harmonics Lit 02
Spherical Harmonics Lit 03Spherical Harmonics Lit 04
Spherical Harmonics Lit 05Spherical Harmonics Lit 06

This formula is great at producing organic, sometimes vaguely obscene-looking (or maybe that’s just me) forms. It’s quite similar to the Superformula really. I’m sure the two share a common origin. They’re certainly both capable of mimicking a variety of natural forms, as well as producing lots of annatural ones.

This implementation still has a few little glitches. The sides of the mesh don’t quite join up correctly. Neither Memo or I are quite sure how to fix that, though I’ve been considering the idea of attempting to use a sphere rather than a plane as the initial mesh. I’m not sure how this would work at the moment, though.
The other problem is, with the lighting and supersampling added, it’s all got a bit slow, predictably. Not sure if I can do anything about that, really…


Spherical Harmonics GLSL Vertex Shader Code

This is a slightly simplified version of the code used to generate the screenshots in the previous post.
I’ve left out the pre-transform matrix, which allows you to draw some parts of the full 3D form, and not others.

After taking advice from Memo, I’ve added 0.5 to the initial vertex positions, to get the values of theta and phi into the corrent ranges.

Spherical Harmonics formula adapted from

GLSL/Quartz Composer conversion alx @ toneburst, 2008

////  CONSTANTS  ////

#define TWOPI 6.28318531
#define PI    3.14159265

// Spherical Harmonics controls
uniform float M0,M1,M2,M3,M4,M5,M6,M7;

// Light position
uniform vec3 LightPosition;

// Passes shading calculation to Fragment Shader
varying float colpos;

////  FUNCTIONS  ////

// Spherical Harmonics
vec3 sphericalHarmonics(float theta, float phi, float m0,float m1,float m2,float m3,float m4,float m5,float m6,float m7)
	vec3 point;
	float r = 0.0;
	r += pow(sin(m0*phi),m1);
	r += pow(cos(m2*phi),m3);
	r += pow(sin(m4*theta),m5);
	r += pow(cos(m6*theta),m7);

	point.x = r * sin(phi) * cos(theta);
	point.y = r * cos(phi);
	point.z = r * sin(phi) * sin(theta);

	return point;

////  MAIN LOOP  ////

void main()
	vec4 vertex = gl_Vertex;

	float theta = (vertex.x + 0.5) * TWOPI;
	float phi = (vertex.y + 0.5) * PI;

	// Spherical Harmonics function = sphericalHarmonics(theta, phi, M0, M1, M2, M3, M4, M5, M6, M7);
	// Shading calculations
	colpos = length( + LightPosition);

	//Transform vertex by modelview and projection matrices
	gl_Position = gl_ModelViewProjectionMatrix * vertex;

	//Forward current color and texture coordinates after applying texture matrix
	gl_FrontColor = gl_Color;
	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

Spherical Harmonics GLSL

Promising results from my attempts to translate code from this page into GLSL/QC:

Spherical Harmonics 01Spherical Harmonics 02
Spherical Harmonics 03Spherical Harmonics 04

With the simple shading, this one has a real airbrushed, Terry-Gilliam-esque look to it (think of the cartoon interludes in Monty Python’s Flying Circus).

Lots of work still to do on this one, obviously, but looking surprisingly cool.


March 2008
« Feb   Apr »


Blog Stats

  • 485,922 hits