1

A university assignment requires me to use the Vertex Coordinates I have to calculate the Normals and the Tangent from the Normal values so that I can create a Object Space to Texture Space Matrix.

I have the code needed to make the Matrix, and the binormal but I don't have the code for calculating the Tangent. I tried to look online, but the answers usually confuse me. Can you explain to me clearly how it works?

EDIT: I have corrected what I wrote previously as clearly I misunderstood the assignment. Thank you everyone for helping me see that.

  • What do you mean by "the tangent"? A line, or a plane? How do you want to represent the tangent? – Martin Bonner supports Monica Mar 01 '16 at 12:51
  • 2
    The tangent for the tangent-space is nothing you can calculate from the normalmap alone. It is a geometric property depending on the vertex positions and the uv-layout. – BDL Mar 01 '16 at 12:54
  • I find myself very concerned about your university assignment, since as you have stated it it is both impossible and betrays a decidedly erroneous understanding of how tangent-space normal mapping works. Perhaps you have misunderstood what it asked for? – Nicol Bolas Mar 01 '16 at 14:06
  • I have corrected my question as it was clear I did misunderstand the assignment. Thank you for point it out. – Gerhard Plesch Mar 01 '16 at 14:28
  • How is the surface given? Is it an analytic surface, or just some kind of point mesh? – Reto Koradi Mar 02 '16 at 06:44
  • I am not sure what you mean, but I believe it is a point mesh as the vertices are stored in an array to be drawn. – Gerhard Plesch Mar 02 '16 at 07:31

1 Answers1

3

A tangent in the mathematical sense is a property of a geometric object, not of the normalmap. In case of normalmapping, we are in addition searching for a very specific tangent (there are infinitely many in each point, basically every vector in the plane defined by the normal is a tangent).

But let's go one step back: We want a space where the u-direction of the texture is mapped on the tangent direction, the v-direction on the bitangent/binormal and the up-vector of the normalmap to the normal of the object. Thus the tangent for a triangle (v0, v1, v2) with uv-coordinates (uv1, uv2, uv3) can be calculated as:

dv1 = v1-v0
dv2 = v2-v0

duv1 = uv1-uv0
duv2 = uv2-uv0

r = 1.0f / (duv1.x * duv2.y - duv1.y * duv2.x);
tangent = (dv1 * duv2.y   - dv2 * duv1.y) * r;
bitangent = (dv2 * duv1.x   - dv1 * duv2.x) * r;

When having this done for all triangles, we have to smooth the tangents at shared vertices (quite similar to what happens with the normal). There are several algorithms for doing this, depending on what you need. One can, for example, weight the tangents by the surface area of the adjacent triangles or by the incident angle of them.

An implementation of this whole calculation can be found [here] along a more detailed explaination: (http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/)

BDL
  • 21,052
  • 22
  • 49
  • 55
  • 1
    I think the key thing to point out here is that the tangent/bitangent is a property of the *mesh*, not of the texture. Which is what makes the university assignment bizarre. – Nicol Bolas Mar 01 '16 at 14:05