# Faster TBN calculation?

I came across this example in DHPOWare’s Parallax mapping example:

vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 t = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
vec3 b = cross(n, t);

The cube model no longer contains a bitangent vertex attribute. The bitangent is now calculated in the vertex shader. Only the tangent vector is calculated and stored in the cube now.
I tried using this in my own code, but the results did not look the same as when I upload the tangent vector array.

Is calculating the tangent vector something you can do in the vert program? I thought it had to be uploaded.

Is calculating the tangent vector something you can do in the vert program? I thought it had to be uploaded.
The NBT triple creates a rotation matrix for transforming model-space lights into the space of a texture on the surface. The Normal is the facing up off of the surface, and the Binormal/Tangent vectors point along the surface, depending on how the texture coordinates are laid out on that surface.

The NBT matrix is not required to be orthonormal. Indeed, for most mappings across arbitrary geometry, it will not be. However, you can approximate it with an orthonormal mapping. That is what the code you posted does. This saves you an attribute, as well as the upload time for that attribute data. What you lose, of course, is visual fidelity.

The tangent can’t be calculated in a vertex shader, because it’s depend the Texture coordinates gradient direction.

It is possible to calculate bitangent in the vertex shader (perpendicular to tangent and normal) if the length is unimportant for the folowing bumpmapping algorithm.
(Maybe you have to change the bitangents direction by swapping the crossproducts arguments)

It is possible to calculate the tangent and bitangent in a geometry shader, but it’s not recommend…

On closer inspection, the error only occurs when texture mapping is backwards on one axis. That seems consistent with the answer you provided. Thank you.

The common solution is to precompute the sign of the bitangent and store it in the w component of the tangent. This way it will always point in the right direction.

It is possible to calculate the tangent and bitangent in a geometry shader, but it’s not recommend…

Why not?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.