
More tangents and bitangents
Posted Tuesday, 31 May, 2011 - 21:53 by elaverick inOK I've been playing with trying automatically generate tangent and bitangent vectors from the 3 vertices that form a triangle section of a quad, however this keeps returning 0,0,0 each time. Anyone see what's wrong with this?
public static void computeTangentBasis(Vector3 vertexA, Vector3 vertexB, Vector3 vertexC, Vector2 texCoordA, Vector2 texCoordB, Vector2 texCoordC, out Vector3 tangent, out Vector3 bitangent ) { Vector3 Edge1 = Vector3.Subtract(vertexA, vertexB); Vector3 Edge2 = Vector3.Subtract(vertexA, vertexC); Vector2 Edge1uv = Vector2.Subtract(texCoordA, texCoordB); Vector2 Edge2uv = Vector2.Subtract(texCoordA, texCoordC); tangent = Vector3.Normalize(Vector3.Multiply(Edge1, Edge2uv.Y) - (Vector3.Multiply(Edge2, Edge1uv.Y))); bitangent = Vector3.Normalize(Vector3.Multiply(Edge2, Edge1uv.X) - (Vector3.Multiply(Edge1, Edge2uv.X))); }


Comments
Re: More tangents and bitangents
Hi!
Your code looks okay, just the calculations seem to be flipped, i don't know if that is the problem.
I post the thing i use atm.
The class TriangleVertex is just a class that holds vertex attributes like positions(vector3)and texturecoords(vector2). But you understand it anyway. Tell me if it works for you.
It also takes care of the winding of the tangent. You can experiment with that if you wanna flip the tangent or just set the info in the W value.