PDA

View Full Version : Faster TBN calculation?

10-23-2007, 07:31 PM
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.

Korval
10-23-2007, 07:52 PM
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.

oc2k1
10-23-2007, 07:56 PM
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...