Convert vector to tangent space

Recommended Posts

Hello. Recently I implemented normal mapping and it works:

But althought I did it by myself some things not clear. Here's the vertex shader code:

[CODE]
worldPosition = mul4x4(pos, MODEL_MATRIX); // vertex position in world sopace
lightDistance = LIGHT_WORLD_POSTION - worldPosition; // vertex-light distance in world space

op = mul4x4(worldPosition, PROJECTION_MATRIX);

lightDistanceOut.w = ZERO_VECTOR;

tangentModel = mul4x4(tangent, ROTATION_MATRIX); // vertex tangent in world space
dp3 lightDistanceOut.x, lightDistance, tangentModel; // ??????????????

normalModel = mul4x4(normal, ROTATION_MATRIX); // vertex normal in world space
dp3 lightDistanceOut.z, lightDistance, normalModel; // ??????????????

crs binormalModel.xyz, normalModel, tangentModel; // vertex binormal in world space
dp3 lightDistanceOut.y, lightDistance.xyz, binormalModel.xyz; // ??????????????[/CODE]
This code convert vector to light to tangent space. My questions are:
1. Initialy I have tangent and normal in model space and first of all I convert them to world space by multiplying by model's rotation matrix. Is it correct?
2. Now, when I have T, B, N vectors in world space (is it in world or maybe I'm wrong?) I can construct matrix with rows as T, B, N vectors. But If I multiply vector to light by that matrix I get incorrect results. And in code above all this dot products is just shortcut for multiplication by matrix rows, not by columns (i.e. by inverse/transpose of my TBN matrix) - and it works. But why?

Share on other sites
Xashmi    115
This looks wrong:

[source lang="c"]lightDistanceOut.w = ZERO_VECTOR;[/source]

Assuming w is a scalar component should not this be

[source lang="c"]lightDistanceOut.w = 0.0;[/source]

alternatively

[source lang="c"]lightDistanceOut = ZERO_VECTOR;[/source]

? Edited by Xashmi