mathopenglmatrix3dlighting

In a TBN Matrix are the normal, tangent, and bitangent vectors always perpendicular?


This is related to a problem described in another question (images there):

Opengl shader problems - weird light reflection artifacts

I have a .obj importer that creates a data structure and calculates the tangents and bitangents. Here is the data for the first triangle in my object:

Data Structure

My understanding of tangent space is that the normal points outward from the vertex, the tangent is perpendicular (orthogonal?) to the normal vector and points in the direction of positive S in the texture, and the bitangent is perpendicular to both. I'm not sure what you call it but I thought that these 3 vectors formed what would look like a rotated or transformed x,y,z axis. They wouldn't be 3 randomly oriented vectors, right?

Also my understanding: The normals in a normal map provide a new normal vector. But in tangent space texture maps there is no built in orientation between the rgb encoded normal and the per vertex normal. So you use a TBN matrix to bridge the gap and get them in the same space (or get the lighting in the right space).

But then I saw the object data... My structure has 270 vertices and all of them have a 0 for the Tangent Y. Is that correct for tangent data? Are these tangents in like a vertex normal space or something? Or do they just look completely wrong? Or am I confused about how this works and my data is right?

To get closer to solving my problem in the other question I need to make sure my data is right and my understanding on how tangent space lighting math works.


Solution

  • The tangent and bitangent vectors point in the direction of the S and T components of the texture coordinate (U and V for people not used to OpenGL terms). So the tangent vector points along S and the bitangent points along T.

    So yes, these do not have to be orthogonal to either the normal or each other. They follow the direction of the texture mapping. Indeed, that's their purpose: to allow you to transform normals from model space into the texture's space. They define a mapping from model space into the space of the texture.

    The tangent and bitangent will only be orthogonal to each other if the S and T components at that vertex are orthogonal. That is, if the texture mapping has no shearing. And while most texture mapping algorithms will try to minimize shearing, they may not eliminate it in all cases. In these situations, an accurate matrix will include non-orthogonal tangents and bitangents at the locations of shearing.