   How to calculate Tangent, Binormal, Normal vectors? Thanks!
  I read some source code for normal mapping. And i found that Tangent, Binormal, and Normal vectors are input parameters of Vertex Shader.I thought they must be pre-calculated in Application and be filled in Vertex Buffer as components of D3DVERTEXELEMENT9[] (DX9). 1. What i thought is allright? 2. How do we calculate Tangent, Binormal and Normal vectors in Application? 3. Calculate T, B, N vectors in Vertex shader or in Pixel Shader is expensive?Please forgive me my poor english...Thanks all!   
  You're in luck, I just had to write this code last month. Ideally you want to precalculate your TBN vectors and store them in your model format for faster model loading, but you can do it in the application if you need to. One of my failures was to use the world space up vector to generate the TBN, but that doesn't work, I forget why. You need to translate the up vector from the UV space of the polygon to world space. Here's some code.Vector v1 = GetVertex(pV1->v);Vector v2 = GetVertex(pV2->v);Vector v3 = GetVertex(pV3->v);Vector vu1 = GetUV(pV1->vu);Vector vu2 = GetUV(pV2->vu);Vector vu3 = GetUV(pV3->vu);Vector v2v1 = v2 - v1;Vector v3v1 = v3 - v1;float c2c1t = vu2.x - vu1.x;float c2c1b = vu2.y - vu1.y;float c3c1t = vu3.x - vu1.x;float c3c1b = vu3.y - vu1.y;Vector vecNormal = GetNormal(pV1->vn);Vector vecTangent = Vector(c3c1b * v2v1.x - c2c1b * v3v1.x, c3c1b * v2v1.y - c2c1b * v3v1.y, c3c1b * v2v1.z - c2c1b * v3v1.z);//Vector vecBitangent = Vector(-c3c1t * v2v1.x + c2c1t * v3v1.x, -c3c1t * v2v1.y + c2c1t * v3v1.y, -c3c1t * v2v1.z + c2c1t * v3v1.z);Vector vecSmoothBitangent = vecNormal.Cross(vecTangent).Normalized();Vector vecSmoothTangent = vecSmoothBitangent.Cross(vecNormal).Normalized();This code will actually do smooth shading for you, recalculating the TBN based on the smoothed normal. If you don't want that you can just take that part out.Doing it in the shader is possible, and it might be a bit cleaner for your user space code, but I think ultimately you're better off doing it in the CPU, it's much faster. I tried doing it in glsl and it worked, but I forget why I ended up abandoning that and doing it in C++.   ;  I tried doing this myself with mixed results (Previous poster mentioned not to use the global up vector, perhaps thats what caused my problems). I can't recall exactly where this is from now but I took it from one of the Nvidia tools:void ComputeTangentBasis(const Vector& P0, const Vector& P1, const Vector& P2, const Vector& UV0, const Vector& UV1, const Vector& UV2,Vector &normal, Vector &tangent, Vector &binormal){Vector e0 = P1 - P0;Vector e1 = P2 - P0;normal = e0.CrossProduct(e1);//using Eric Lengyel's approach with a few modifications//from Mathematics for 3D Game Programmming and Computer Graphics// want to be able to trasform a vector in Object Space to Tangent Space// such that the x-axis cooresponds to the 's' direction and the// y-axis corresponds to the 't' direction, and the z-axis corresponds// to <0,0,1>, straight up out of the texture map//let P = v1 - v0Vector P = P1 - P0;//let Q = v2 - v0Vector Q = P2 - P0;float s1 = UV1.x - UV0.x;float t1 = UV1.y - UV0.y;float s2 = UV2.x - UV0.x;float t2 = UV2.y - UV0.y;//we need to solve the equation// P = s1*T + t1*B// Q = s2*T + t2*B// for T and B//this is a linear system with six unknowns and six equatinos, for TxTyTz BxByBz//[px,py,pz] = [s1,t1] * [Tx,Ty,Tz]// qx,qy,qzs2,t2Bx,By,Bz//multiplying both sides by the inverse of the s,t matrix gives//[Tx,Ty,Tz] = 1/(s1t2-s2t1) *  [t2,-t1] * [px,py,pz]// Bx,By,Bz  -s2,s1    qx,qy,qz  //solve this for the unormalized T and B to get from tangent to object spacefloat tmp = 0.0f;if(fabsf(s1*t2 - s2*t1) <= 0.0001f){tmp = 1.0f;}else{tmp = 1.0f/(s1*t2 - s2*t1 );}tangent.x = (t2*P.x - t1*Q.x);tangent.y = (t2*P.y - t1*Q.y);tangent.z = (t2*P.z - t1*Q.z);tangent = tangent*tmp;binormal.x = (s1*Q.x - s2*P.x);binormal.y = (s1*Q.y - s2*P.y);binormal.z = (s1*Q.z - s2*P.z);binormal = binormal*tmp;normal.Normalize();tangent.Normalize();binormal.Normalize();};  If you'd like a page explaining the math behind it as well as another example implementation:http://www.terathon.com/code/tangent.html;  very good! Thank you all!But I'm using DX9.TBN matrix is Right Hand, And DirectX9 is Left Hand.We use matTBN(right hand) to translate LightDir(left hand?), ViewDir(left hand?) to Tangent space.That will not cause the error? Thanks!   ;  I think it should just be an issue of reversing the order of those cross products.   