I have this code I sorta put together from a few different sites. It works perfect at creating the bitangent... but for what ever reason. the tangent is thrashed.. In other words and to make this more clear, the code is generating BAD tangents!! The tangent is usually pointing down, kinda opposite the normal. I'm expecting it to be aligned with the V axis of the texture coordinate.. The strange thing is, I use the cross of the normal and tangent to get the bitangent, which is fine.
I have tried flipping the coordinates, the UVs.. just about everything.. Im not great in math so.. can some one take a look at my code and see if it looks "legit"? Thank you!
Private Sub ComputeTangentBasis(ByRef p0 As vertex_data, ByRef p1 As vertex_data, ByRef p2 As vertex_data)
Dim tangent, bitangent As Vector3D
Dim n As Vector3D
n.X = p0.nx
n.Y = p0.ny
n.Z = p0.nz
'convert to vector3d type... they are WAY easier to do complex math with!!
Dim v0 = convert_vector3d(p0)
Dim v1 = convert_vector3d(p1)
Dim v2 = convert_vector3d(p2)
Dim edge1 = v1 - v2
Dim edge2 = v2 - v0
Dim deltaU1 = (p1.u - p0.u) * -0.1
Dim deltaU2 = (p2.u - p0.u) * -0.1
Dim deltaV1 = (p1.v - p0.v) * -0.1
Dim deltaV2 = (p2.v - p0.v) * -0.1
Dim dividend = (deltaU1 * deltaV2) - (deltaU2 - deltaV1)
Dim f As Double
If dividend = 0.0 Then
f = 1.0
End If
tangent.X = f * ((deltaV2 * edge1.X) - (deltaV1 * edge2.X))
tangent.Y = f * ((deltaV2 * edge1.Y) - (deltaV1 * edge2.Y))
tangent.Z = f * ((deltaV2 * edge1.Z) - (deltaV1 * edge2.Z))
bitangent = Vector3D.CrossProduct(tangent, n)
'
p0.t.x = tangent.X
p0.t.y = tangent.Y
p0.t.z = tangent.Z
p0.bt.x = bitangent.X
p0.bt.y = bitangent.Y
p0.bt.z = bitangent.Z
'
p1.t.x = tangent.X
p1.t.y = tangent.Y
p1.t.z = tangent.Z
p1.bt.x = bitangent.X
p1.bt.y = bitangent.Y
p1.bt.z = bitangent.Z
'
p2.t.x = tangent.X
p2.t.y = tangent.Y
p2.t.z = tangent.Z
p2.bt.x = bitangent.X
p2.bt.y = bitangent.Y
p2.bt.z = bitangent.Z
End Sub
Here's a pic of the problem.. Norms are red.. Tangents Green... BiTangents.. blueish.