# Normals For Distorted Sphere

This topic is 3316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a conceptual flaw in this GLSL vertex shader method for calculating a normal for a sphere?
varying vec3 norm;

struct spherical { float r, phi, theta; };

spherical cartesianToSpherical(vec3 cPoint) {
spherical sCoords;
float xyLen = length(cPoint.xy);
sCoords.r = length(cPoint);
sCoords.phi = acos(cPoint.z / sCoords.r);
sCoords.theta = atan(cPoint.x, cPoint.y);
return sCoords;
}

vec3 sphericalToCartesian(spherical sPoint) {
vec3 cCoords;
cCoords.x = sPoint.r * sin(sPoint.phi) * cos(sPoint.theta);
cCoords.y = sPoint.r * sin(sPoint.phi) * sin(sPoint.theta);
cCoords.z = sPoint.r * cos(sPoint.phi);
return cCoords;
}

void main()
{
vec4 vert = gl_Vertex;

spherical sVert = cartesianToSpherical(vert.xyz);

float offset = 0.01;

spherical tanPos = spherical(sVert.r,sVert.phi + offset,sVert.theta);
vec3 tangent = sphericalToCartesian(tanPos);
tangent -= vert.xyz;

spherical bitanPos = spherical(sVert.r,sVert.phi,sVert.theta + offset);
vec3 bitangent = sphericalToCartesian(bitanPos);
bitangent -=vert.xyz;

norm = gl_NormalMatrix * normalize(cross(tangent,bitangent));

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

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

I know how to work out the normal for a sphere the easy way, incidentally. This is just a test. The idea would be to generate 2D/2D perlin noise values based on the cartesian coords of the vertex, tangent and bitangent and use this to displace the points along a line from the point to the centre of the mesh). Hope this makes sense, a|x http://machinesdontcare.wordpress.com

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631781
• Total Posts
3002314
×