# Tangent, Bitangent & Normal

Hi,

I’m working on converting some HLSL code to GLSL. I have an HLSL vertex shader that distorts a mesh via a number of different surface formulae. The formulae output vertex position (as a vec4), and bitangent and tangent vec3s (all presumably in Object coordinates).

I’ve been able to transform the vertex coordinates by multiplication with the gl_ModelViewProjectionMatrix. What I’m having trouble trying to do is figure out how to create the normal from the Bi and Tan variables, and transform it into the correct coordinate space, so I can use it to apply lighting effects.

I’m currently not applying any transformation to Bi and Tan, and using the following code to attempt to calculate the normal:

normal = normalize(gl_NormalMatrix * cross(Tan,Bi));

which doesn’t seem to do the job, as I get a black unlit surface with my Phong Directional lighting.
I’ve tried setting normal to the mesh’s original normal using

normal = normalize(gl_NormalMatrix * gl_Normal);

(where normal is a varying vec3)
This results in a lit surface (though obviously it’s not correctly-lit, as the normals from the original plane mesh are incorrect for the distorted mesh).
I infer from this that it’s definitely a problem with the way the normal is calculated.

Anyone know what might be going wrong?

Maybe the resulting normal is facing the wrong way?

Try:

normal = normalize(gl_NormalMatrix * cross(Bi,Tan));

Hi -NiCo-

Good thought!
Unfortunately, I just tried that, and it doesn’t fix it, sadly… alx

Are you sure the Bi and Tan variables have the correct values? not at all, sadly. I’m pretty sure the original HLSL code worked.
I know the vertex position code works. Without actually seeing a visible effect though, I can’t really tell if the BI and Tan values are correct. alx

Incidentally, in the original HLSL code, Bi and Tan were multiplied by the
WORLDVIEWPROJECTION matrix before the normal calculation was performed with the code

n = normalize(cross(In.Tan,In.Bi));

I’ve tried multiplying Bi and Tan with the gl_NormalMatrix before calculating the normal, but this doesn’t do it, either.

Don’t know if this throws any light on the situation at all…

alx

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.