# Tangent space and animation

Hi,

I have a bumpmapped model which uses tangent space to render the bumps. Now if I want to animate my model, do I have to recalculate tangent space for each frame? I guess the must be a faster way. Anyone know?

//Regards, Paw

Your tangent space basis would turn the normal into pre-animated space. You then have to skin the normal you get out of the normal map, just like you skin the position.

Because this is done per fragment, you could skin the tangent bases and interpolate and normalize each basis vector. This makes it no more expensive in the fragment shader, and only a little bit more expensive in the vertex shader.

Ok, so I have to do this for each vector in the tangent basis?

v.x += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;
v.z += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;
v.y += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;

-paw

Do not add the translation part of the matrices. Only use the rotational part.

Oh, and this won’t work right if you use shear or nonlinear scale in your matrix; if so, then you have to use the transpose of the inverse of the matrix instead.

Ok, you mean like this:

void CMD5::BuildVertices(CVector3 *tangents, CVector3 *normals)
{
for (int i = 0; i < m_nMeshes; i++)
{
Mesh& mesh = m_pMeshes[i];

``````  for (int j = 0; j &lt; mesh.nb_vertices; j++)
{
Vertex& v = mesh.vertices[j];

v.x = 0;
v.y = 0;
v.z = 0;

CVector3 &tan  = tangents[j];
CVector3 &norm = normals[j];

for (int k = 0; k &lt; v.weight_count; k++)
{
Weight& w = mesh.weights[v.weight_index + k];

Bone& b = m_Bones[w.bone];

v.x += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;
v.z += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;
v.y += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;

tan.m_vertex += (b.rot * tan.m_vertex + b.rot * tan.m_vertex + b.rot * tan.m_vertex)  * w.t;
tan.m_vertex += (b.rot * tan.m_vertex + b.rot * tan.m_vertex + b.rot * tan.m_vertex) * w.t;
tan.m_vertex += (b.rot * tan.m_vertex + b.rot * tan.m_vertex + b.rot * tan.m_vertex)  * w.t;

norm.m_vertex += (b.rot * norm.m_vertex + b.rot * norm.m_vertex + b.rot * norm.m_vertex) * w.t;
norm.m_vertex += (b.rot * norm.m_vertex + b.rot * norm.m_vertex + b.rot * norm.m_vertex) * w.t;
norm.m_vertex += (b.rot * norm.m_vertex + b.rot * norm.m_vertex + b.rot * norm.m_vertex) * w.t;

}
tan.Normalize();
norm.Normalize();
}
``````

}
}

Something or wrong here.

-Paw

Sorry, I mean like this:

void CMD5::BuildVertices(CVector3 *tangents, CVector3 *normals)
{
for (int i = 0; i < m_nMeshes; i++)
{
Mesh& mesh = m_pMeshes[i];

``````  for (int j = 0; j &lt; mesh.nb_vertices; j++)
{
Vertex& v = mesh.vertices[j];

v.x = 0;
v.y = 0;
v.z = 0;

CVector3 &tang  = tangents[j];
CVector3 &norm = normals[j];

CVector3 newNormal  = CVector3(0, 0, 0);
CVector3 newTangent = CVector3(0, 0, 0);

for (int k = 0; k &lt; v.weight_count; k++)
{
Weight& w = mesh.weights[v.weight_index + k];

Bone& b = m_Bones[w.bone];

v.x += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;
v.z += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;
v.y += (b.rot * w.x + b.rot * w.y + b.rot * w.z + b.pos) * w.t;

newTangent.m_vertex += (b.rot * tang.m_vertex + b.rot * tang.m_vertex + b.rot * tang.m_vertex)  * w.t;
newTangent.m_vertex += (b.rot * tang.m_vertex + b.rot * tang.m_vertex + b.rot * tang.m_vertex)  * w.t;
newTangent.m_vertex += (b.rot * tang.m_vertex + b.rot * tang.m_vertex + b.rot * tang.m_vertex)  * w.t;

newNormal.m_vertex += (b.rot * norm.m_vertex + b.rot * norm.m_vertex + b.rot * norm.m_vertex) * w.t;
newNormal.m_vertex += (b.rot * norm.m_vertex + b.rot * norm.m_vertex + b.rot * norm.m_vertex) * w.t;
newNormal.m_vertex += (b.rot * norm.m_vertex + b.rot * norm.m_vertex + b.rot * norm.m_vertex) * w.t;

}
newNormal.Normalize();
newTangent.Normalize();

norm = newNormal;
tang = newTangent;
}
``````

}
}

Do you see whats wrong here?

-paw

your variable v is set to a reference to the mesh position, and then cleared. Hopefully, the mesh position you clear is not also one you also use for input.

Also, I’m assuming “tangents” and “normals” are also part of the “mesh” struct?

I don’t understand why the “weight” has z, y and z parts that you use as input for position. Is that special to the file format you’re working on?

Originally posted by jwatte:
I don’t understand why the “weight” has z, y and z parts that you use as input for position. Is that special to the file format you’re working on?

This is the doom 3 md5 format.

-SirKnight

Here is a page I just found that may be usefull: http://www.gamasutra.com/features/20030325/fernando_05.shtml

Notice in the vertex program close to the bottom of the page where they do this:

netPosition += weight[i] * bonePosition;
netNormal += weight[i] * boneNormal;

This is the same technique you would use to also skin the tangent and binormal. So it would perhaps look something like this:

netPosition += weight[i] * bonePosition;
netNormal += weight[i] * boneNormal;
netTangent += weight[i] * boneTangent;
netBinormal += weight[i] * boneBinormal;

Of course you would have to compute boneTangent and boneBinormal just like boneNormal was done.

-SirKnight

netPosition += weight[i] * bonePosition;netNormal += weight[i] * boneNormal;netTangent += weight[i] * boneTangent;netBinormal += weight[i] * boneBinormal;

This is done in the code above. The w.t are the weight[i].

-paw