# How to bumpmap a gluSphere

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

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

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

then:

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 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 on other sites
Nope, but because you are using GLSL, you can do the tangent/binormal calculation in the vertex shader, but only in this case of it being a sphere.

##### 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. http://www.malevolenceguild.org/spheres.jpg

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!

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631715
• Total Posts
3001868
×