• Create Account

# Calculating Normals, Binormals, Tangents

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1OpaqueEncounter  Members   -  Reputation: 119

Like
0Likes
Like

Posted 08 August 2012 - 06:34 PM

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; }[/source]
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?

### #2scyfris  Members   -  Reputation: 157

Like
0Likes
Like

Posted 09 August 2012 - 09:52 PM

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.

### #3OpaqueEncounter  Members   -  Reputation: 119

Like
0Likes
Like

Posted 11 August 2012 - 04:56 PM

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.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS