Jump to content
  • Advertisement
Sign in to follow this  

Convert vector to tangent space

This topic is 2229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 binormalModel.xyz, normalModel, tangentModel; // vertex binormal in world space
dp3 lightDistanceOut.y, lightDistance.xyz, binormalModel.xyz; // ??????????????

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!