I have a basic tangent space normal mapping shader going. It works OK for basic diffuse lighting, but is having serious handedness problems for specular lighting. This is using objects exported from Blender, and it seems Blender’s triangle-ization step on obj export switches handedness a lot. In fact, if you bake a normal map from blender, you can see that it compensates for this in the normal map. But I’m hand painting my normal map, plus I need to get tangent space specular lighting working.
I’m getting myself really confused about all this :-(. I’m using the technique from here, the same math but rewritten for my own environment: http://www.terathon.com/code/tangent.html
I calculate both tangent and bitangent in C++ code like that, then send them as user attributes to the VS.
One problem I think exists: these lines “tan1[i1] += sdir;” which calculate the tangent and bitangent are going to add in sdir/tdir from opposite windings, aren’t they? It seems like that won’t work in such a case, unless I’m missing something.
On that theory, I’m trying to calculate the handedness in the C++ code that calculate tangent/bitangent, and add in the right sense of sdir/tdir. But it isn’t working right; my specular lighting is still bad across some of the triangle boundaries (but works across others).
Am I even right about the problem here? And if I am, is it possible to fix this right there in CalculateTangentArray?
My hacked up attempt to do this is like this, right before sdir/tdir get added to the two tangent vectors which are accumulating for each vertex shared by several faces:
bool rightHand = (sdir.cross(tdir)).dot(norms[tri]) >= 0.0;
if (!rightHand) {
tdir *= -1.0;
sdir *= -1.0;
}
But that’s clearly wrong. I’ve also tried only reversing sdir, and that’s wrong too :). But I’m not sure why.
Any help would be really awesome! Thanks a ton.
Oh, I also have txcoords that wrap 4 times around my object. Is that going to cause any problems for this technique, such as when the txcoord wraps from 1 back to 0?