Sign in to follow this  

How to bumpmap a gluSphere

Recommended Posts

I just built bumpmapping into my engine, I am using a GLSL implementation. My vertex shader needs the normal, and a vertex tangent at every vertex point to compute the tangent space matrix for calculations. I am curious if there is an easy way or method to get this vertex tangent into the shader when using something like glusphere that is generating all the geometry for me? Or am I going to need to roll my own sphere/sphere texture generation code? How does everyone else deal with this? Thanks!

Share this post

Link to post
Share on other sites
GLU primitives don't come with tangent vectors (they do have normals, though). You have to calculate them yourself but in the case of spheres you don't need a general triangle tangent vector calculator.

If you decide to let all tangents lie parallel to the XZ plane, you can easily calculate tangents as derivates of vertex circular (projected to XZ plane) position.

From calculus, we know the derivative of the sine is the cosine, and the derivative of the cosine is minus the sine, therefore:

vtx_pos_x = cos( angle )
vtx_pos_y = irrelevant
vtx_pos_z = -sin( angle )


vtx_tangent_x = -sin( angle )
vtx_tangent_y = 0
vtx_tangent_z = -cos( angle )

The bitangent is calculated as the cross-product of vertex tangent and normal vector.

<edit spelling>

Share this post

Link to post
Share on other sites
Right I follow that, is there any easy way to hook into the GLU code to add these vertex attributes, IE do they have any kind of callback or anything?

If not I'm not sure how I can even specify a vertex attribute short of taking SGI's source and adding my own stuff to it?

Share this post

Link to post
Share on other sites
I wanted to thank Krek for his help, I've got it semi working, I wanted to post my code for determining the vertex tangent and see if I'm way off base or what:

float angle = acos(gl_Vertex.x);
if (gl_Vertex.z > 0.0) {
angle = (2.0 * PI) - angle;

// derivative in the x z plane creates our texture tangent
vertexTangentTemp = normalize(vec3(-sin(angle), 0, -cos(angle)));

The ball looks mostly good but there seem to be some kind of boundry problems going on. For example there are visible 'lines' running around the sphere that the specular highlights seem to get trapped in. Also when I roll the ball towards the light down the z axis the specular seems to slightly wobble side to side when it should stay dead center. Here is an example image of the specular getting trapped in the line (on the left), versus it looking (as best i can tell) proper, on the right.

Would the lines be caused by the texturing somehow? I seem to remember seeing on another of my spheres a visible line where the left and right edges of the texture were meeting up, rather than there being a nice smooth transition from one side of the texture to the other.

Thanks for all help in advance!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this