# tangent space and indexed buffer

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

## Recommended Posts

Hi all I'm working on a deferred renderer and I need to build the tangent space to be able to transform the normal map accordingly. From what I understood, for each triangle, there is a tangent space matrix (t,b,n). I tested some algorithm on a cube and it seemed to work fine. The normal computed was pointing in the correct direction and tangent/bitan, well, were perpendicular (I'm still not sure what it represents exactly). The question I had is what happen when you have a mesh using index buffers. Vertices are there only once... From algorithms I saw, the tangent space is kinda accumulated I think and normalized at the end. I'm not sure I get how this can work. Let's say we take the top right vertex of a face of a unit cube. (0.5,0.5,-0.5). The face we see have a normal of 0,0,-1. The top face 0,1,0 and the right face 1,0,0. The vertex belongs to those three faces. So, if their tangent space are accumulated and then normalized back, what does it represent and how can that be used with a normal map..? Thank you!

##### Share on other sites
Quote:
 Original post by MoSinHi allI'm working on a deferred renderer and I need to build the tangent space to be able to transform the normal map accordingly. From what I understood, for each triangle, there is a tangent space matrix (t,b,n). I tested some algorithm on a cube and it seemed to work fine. The normal computed was pointing in the correct direction and tangent/bitan, well, were perpendicular (I'm still not sure what it represents exactly). The question I had is what happen when you have a mesh using index buffers. Vertices are there only once... From algorithms I saw, the tangent space is kinda accumulated I think and normalized at the end. I'm not sure I get how this can work. Let's say we take the top right vertex of a face of a unit cube. (0.5,0.5,-0.5). The face we see have a normal of 0,0,-1. The top face 0,1,0 and the right face 1,0,0. The vertex belongs to those three faces. So, if their tangent space are accumulated and then normalized back, what does it represent and how can that be used with a normal map..?Thank you!

Tangent space is particular to each triangle in a mesh, each triangle has it's own coordinate system that is its own; because each one is positioned/orientated differently. So the definition of 'tangent space' depends on what triangle you are looking at. It is not something that applies to the model as a whole.

Basically tangent space is a coordinate system where the x and z axes run parallel with the surface of the triangle. The unit Y vector (up vector) in tangent space also corresponds to the surface normal in world space. Also, by having these three vectors (normal,binormal,tangent) one can transform to and from tangent space using either several dot products or one matrix multiply.

As for understanding binornmals and tangents, I usually tend to think of them in terms of Z and X axes of the triangles coordinate system, where the normal is the Y axis.

The reason why tangent space is used for normal mapping is because of efficiency. Normal mapping can either be done by transforming a light direction vector (which is in world space) into to the triangles coordinate system (a.k.a tangent space) in order to do a dot product with the normal from the normal map (which is in tangent space because it's in a texture mapped along the surface) or by doing the reverse- transforming the normal from tangent space into world space. The first method is much more efficient because it can be done in the vertex shader and once for every vertex in the triangle; in the second method we have to transform the normal into world space for every pixel in the pixel shader.

In terms of your model data, if it does not already include normals or tangents or binormals already then you will probably want to compute these offline as it can be expensive to do for each render. Normals can be found from the cross product of two triangle edge vectors. Tangent will be one of the edge vectors themselves while the binormal will be a cross of the tangent and normal.

Hope this helps somewhat! :)

##### Share on other sites
well, I think I understand the concept but since it's per triangle, when you get a model that use index buffer with vertex buffer, you can't store the tangent space per vertex since vertices are represented once? Or maybe I'm missing something there...

##### Share on other sites
Quote:
 Original post by MoSinwell, I think I understand the concept but since it's per triangle, when you get a model that use index buffer with vertex buffer, you can't store the tangent space per vertex since vertices are represented once? Or maybe I'm missing something there...

If you have a normal, binornmal and tangent at each vertex in your mesh (along with the usual color,position,texcoord etc.) then you can convert into tangent space at each vertex using these three vectors. So if your model's vertex buffer does not already include these elements then you need to calculate them and inlcude them in the vertex buffer also. Once you have them then you're good to go for normal mapping.

Once you do the tangent space conversion at each vertex, the result will then be interpolated across the triangle as is normal with per-vertex attributes (color etc.). So you will end up with a tangent space light vector at each pixel on the triangle surface; which is exactly what you need for normal mapping.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633661
• Total Posts
3013220
×