Sign in to follow this  

Calculating Normals, Binormals, Tangents

Recommended Posts

I have an array of vertices, texture coordinates and indices. I calculate Normals, Binormals and Tangents using:
[source lang="csharp"]
public static void CalculateTangentBinormal(
Vector3 vertexPosition1, Vector3 vertexPosition2, Vector3 vertexPosition3,
Vector2 vertex1texCoord, Vector2 vertex2texCoord, Vector2 vertex3texCoord,
out Vector3 tangent, out Vector3 binormal)
var vector1 = new[] { vertexPosition2.X - vertexPosition1.X, vertexPosition2.Y - vertexPosition1.Y, vertexPosition2.Z - vertexPosition1.Z };
var vector2 = new[] { vertexPosition3.X - vertexPosition1.X, vertexPosition3.Y - vertexPosition1.Y, vertexPosition3.Z - vertexPosition1.Z };
var tuVector = new[] { vertex2texCoord.X - vertex1texCoord.X, vertex3texCoord.X - vertex1texCoord.X };
var tvVector = new[] { vertex2texCoord.Y - vertex1texCoord.Y, vertex3texCoord.Y - vertex1texCoord.Y };
var den = 1.0f / (tuVector[0] * tvVector[1] - tuVector[1] * tvVector[0]);
tangent.X = (tvVector[1] * vector1[0] - tvVector[0] * vector2[0]) * den;
tangent.Y = (tvVector[1] * vector1[1] - tvVector[0] * vector2[1]) * den;
tangent.Z = (tvVector[1] * vector1[2] - tvVector[0] * vector2[2]) * den;
binormal.X = (tuVector[0] * vector2[0] - tuVector[1] * vector1[0]) * den;
binormal.Y = (tuVector[0] * vector2[1] - tuVector[1] * vector1[1]) * den;
binormal.Z = (tuVector[0] * vector2[2] - tuVector[1] * vector1[2]) * den;
var length = (float)Math.Sqrt(tangent.X * tangent.X + tangent.Y * tangent.Y + tangent.Z * tangent.Z);
tangent.X = tangent.X / length;
tangent.Y = tangent.Y / length;
tangent.Z = tangent.Z / length;
length = (float)Math.Sqrt(binormal.X * binormal.X + binormal.Y * binormal.Y + binormal.Z * binormal.Z);
binormal.X = binormal.X / length;
binormal.Y = binormal.Y / length;
binormal.Z = binormal.Z / length;
public static Vector3 CalculateNormal(Vector3 tangent, Vector3 binormal)
Vector3 normal;
normal.X = tangent.Y * binormal.Z - tangent.Z * binormal.Y;
normal.Y = tangent.Z * binormal.X - tangent.X * binormal.Z;
normal.Z = tangent.X * binormal.Y - tangent.Y * binormal.X;
var length = (float)Math.Sqrt(normal.X * normal.X + normal.Y * normal.Y + normal.Z * normal.Z);
normal.X = normal.X / length;
normal.Y = normal.Y / length;
normal.Z = normal.Z / length;
return normal;
When I render using a shader from NVIDIA's HLSL libray, the lighting looks wrong. The specular "spots" appear to be in random places and the normal mapping is definitely wrong. What's wrong here?

Share this post

Link to post
Share on other sites
So i'm interested to know why you decide to compute the tangent and bitangent (binormal) vectors first and use those for normal? You have the vertices of the triangle, it's probably easier to compute the normal directly, then compute the tangent, then just take the cross product of the tangent and normal to get the bitangent. Be careful that you are getting the same normal (not a flipped normal) when doing these cross products.

Also, could you post the shader code? Remember that once you have the tangent, bitangent, and normal these vectors represent a basis for a space. Your light vector is in object space, but you want the light vector to be in tangent space before performing light calculations, so you must transform the light vector first, then do the specular calculations.

Check out this site: for more info...

Share this post

Link to post
Share on other sites
scyfris, I got that code from somebody's sample code. I rewrote my normal generation code to generate Vertex Normals (if that's the correct term) instead of Surface Normals and will rewrite to generate Tangents and Binormals with what you gave me above. Thanks.

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