Sign in to follow this  

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:

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, normalModel, tangentModel; // vertex binormal in world space
dp3 lightDistanceOut.y,,; // ??????????????[/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 this post

Link to post
Share on other sites
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]


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

? Edited by Xashmi

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this