Skip to main content
GameDev.net gamedev.net
🔒 Locked

matrices, matrices...

Started by Bouga Jan 15, 2005 at 12:55 PM 3 replies 1.1k views
Original Post
Bouga
Bouga
Hi, can you please tell me, if this would work in theory. A normal mapping shader. I have this input data for the shader: lightVector - in world coordinates; normal - in object space coordinates; tangent - in object space coordinates; bitangent - in object space coordinates; WorldToEyeSpaceMatrix - the modelview matrix that the world is rendered with(this matrix does convert vectors from world to eye space, right?); ObjectToEyeSpaceMatrix - the modelviewmatrix that the current object is rendered with(this matrix does convert vectors from object to eye space, right?); Then i do this: EyeSpacelightVector=multiply(WorldToEyeSpaceMatrix,lightVector); EyeSpacenormal=multiply(ObjectToEyeSpaceMatrix,normal); EyeSpacetangent=multiply(ObjectToEyeSpaceMatrix,tangent); EyeSpacebitangent=multiply(ObjectToEyeSpaceMatrix,bitangent); then create the TBN matrix from the EyeSpacetangent, EyeSpacebitangent, EyeSpacenormal; This TBN matrix should now convert vectors from eye space to tangent space, right? then do: TangentSpacelightVector=multiply(TBNSpaceMatrix,EyeSpacelightVector); and do the lighting calculation with TangentSpacelightVector and the normals from the normal map. The reason I'm asking is, I've written a shader that works like this, but the light direction doesnt work as planned. So now I'm thinking - is there something wrong with the shader, or the input values (the appliaction itself is now wrtitten by me, I rely on some other ppl to send the correct data). Thanks.
"A screen buffer is worth a thousand char's" - me
Bouga
Bouga
Anyone? I guess the main part of the question is - if I have 3vectors - tangent, bitangent and normal - in object space, and if I create the TBN matrix from them, I can use it to multiply a vector and transform it from object space into tangent space, right? And if I transform those 3vectors from object to eye space and then create the TBN matrix, can I use it to transform eye space vectors to tangent space?
"A screen buffer is worth a thousand char's" - me
muhkuh
muhkuh
I'm a newby on this too but I do it this way:

In the vertex shader I build up a 3x3 transformation matrix that transforms from object space to tangent space. It works like this:

float3x3 mObjToTangentSpace;mObjToTangentSpace[0]=-IN.binormal;mObjToTangentSpace[1]=-IN.tangent;  mObjToTangentSpace[2]=IN.normal;


Then I use this matrix to transform the vector to the light (L_obj) from object to tangent space. This requires to transform L_world to object space before. For a directional light L_obj is constant and can be computed by the CPU. For a positional light you transform the lights position in world space to object space (by the CPU as it is constant too) and calculate L_obj as the direction from the vertex position in object space to the light's postion in object space.

If you have L in tangent space you can use it as is in the pixel shader or renormalize it there.


You should test your shader on reference data to be sure it does what you want it to do. I use the nvidia FX composer.

Hope that helps.





Bouga
Bouga
Yeah, thanks, I know thats a better & more optimal way to do it; But I still wonder, will the aforementioned approach work?
"A screen buffer is worth a thousand char's" - me
muhkuh
muhkuh
Quote:
Original post by Bouga
Yeah, thanks, I know thats a better & more optimal way to do it; But I still wonder, will the aforementioned approach work?


I don't see why your way shouldn't work (but that doesn't mean anything ;-) ). May be you took the light direction instead of the direction to the light?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.