Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!