17
Mar
08

### 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.

EDIT:
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.

```//*
http://local.wasp.uwa.edu.au/%7Epbourke/surfaces_curves/sphericalh/

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;

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
vertex.xyz = sphericalHarmonics(theta, phi, M0, M1, M2, M3, M4, M5, M6, M7);
colpos = length(vertex.xyz + 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;
}
```

#### 5 Responses to “Spherical Harmonics GLSL Vertex Shader Code”

1. March 18, 2008 at 1:22 pm

Sorry, stupid question, where would I put this code, in QC, to produce results like you have? I’ve been pasting it into the GLSL shader, but it doesn’t seem to work straight off. (I’m on QC 3…)

2. 2 toneburst
March 18, 2008 at 2:39 pm

It’s GLSL shader code, so needs to go in a GLSL shader module.
This is only the Vertex Shader code, though: you need some code in the Fragment Shader to get it to work.

I’m going to post the full Vertex and Fragment Shader programs later, so you may want to hold off until then.

I’m just going through it now commenting everything, so it should be easier to follow.

Cheers,

alx

3. 3 toneburst
March 18, 2008 at 2:40 pm

Not a stupid question at all, in fact…

alx

4. 4 George Toledo
October 10, 2008 at 2:49 am

This was a great starting point for me in understanding how GLSL works and getting me interested in it, and I have been meaning to come back here and thank you. I’ve used this with some tweaks here and there, and modded some other basic shaders with some parts of this code.

5. 5 toneburst
October 10, 2008 at 11:31 am

Hiya,

it’s great to get this kind of feedback, and it’s always nice to hear from people who’re on a similar journey of discovery to mine.

I’d be interested to see what you’ve come up with shader-wise yourself. Most of the stuff I’ve done has been based heavily on other people’s work, so I still have A LOT to learn myself. Glad my musings have been a help to you.

a|x