I am going to implement normal mapping and I found this piece of code.

http://www.3dkingdoms.com/weekly/weekly.php?a=37

I don’t understand the theory starting from line

float cp = …

Where can I find information of how to calculate the tangent and bi-tangent. I have googled normal mapping/bump mapping and found some material(Shaders doing normal mapping). The problem is that they don’t tell you how to calculate the tangent and bitangent before sending it as attributes to the shaders.

cp = cross product. Google it, 2D cross product, or 2D determinant, or look them up in your geometry textbook.

The sign of the cross product is often used to determine the winding direction between two vectors, or in this case to check if they are colinear or degenerate. IIRC:

| a x b | = |a||b|sin(theta)

So if the length of either vector is 0 OR the angle between them is 0, then the cross product is 0. I believe that’s what they’re checking here.

Here’s a try:

We want to find Du, Dv = (du/dx du/dy), (dv/dx dv/dy). These are the tanger and bitangent desired.

*** Is this true? Are u,v coordinates expected to be orthogonal?? ***

So we form E1, E2 and the matrix:

Dv.E1 Du.E1

Dv.E2 Du.E2

For example Dv.E1 is the difference of the v texture coordinates along the direction E1. The . here denotes dot product.

What we want to know is Dv, not Dv.E1.

So:

E1_x E1_y times dv/dx du/dx equals Dv.E1 Du.E1

E2_x E2_y times dv/dy du/dx equals Dv.E2 Du.E2

Call this E D = O in matrix notation.

One we solve for D we are done, since these are the components of the gradient we want.

As far as I can read the algorithm solves:

Oinv * E = Dinv, where

Dinv = Dtranspose since it should be an orthognal basis.

I’m not sure why is doesn’t do:

D = Einv O, which would only involve inverting the coordinate matrix…

I didn’t get the components to match exactly, so I am not 100% on this, but if it is not exact it is at least the idea…

So I read the line starting at

float cp =

as the computation of the determinant of the observation matrix E, which is needed to form Einv…

It is also the crossproduct of the edge vectors of the 2d triangle in u,v space. If the angle between these is zero, then there are some big problems in the texture mapping!!!

Thank my guess was also cross product. cp is a float and not a vector. I found this on wikipedia. Thats why I am confused.

“It has a vector result, a vector which is always perpendicular to both of the vectors being multiplied and the plane containing them.”

cp is a float. How can a float be a vector?

cp is just the magnitude of the cross product. Since you’re crossing 2 2D vectors (XY plane), you know the direction of the cross product is in Z.

You can convince yourself by expanding the vec2s into vec3s by adding a 0 for z, then cross that.

Okay, I was a bit off target above. but another try. I get the same formula that the fellow uses in the code, above, except for the sign of the bitangent…

PDF bitangent/tangent

(I wanted to understand this better anyway Hope it helps… its a bit technical)