Jump to content
  • Advertisement
Sign in to follow this  
MoSin

tangent space and indexed buffer

This topic is 3162 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

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by MoSin
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!


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by MoSin
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...


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.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!