# [译]为任意网格计算tangent空间的基向量

2020/05/13 12:08

+BIT祝威+悄悄在此留下版了个权的信息说：

[译]为任意网格计算tangent空间的基向量

# Computing Tangent Space Basis Vectors for an Arbitrary Mesh (Lengyel’s Method)

Modern bump mapping (also known as normal mapping) requires that tangent plane basis vectors be calculated for each vertex in a mesh. This article presents the theory behind the computation of per-vertex tangent spaces for an arbitrary triangle mesh and provides source code that implements the proper mathematics.

+BIT祝威+悄悄在此留下版了个权的信息说：

## Mathematical Derivation 数学推导

[This derivation also appears in Mathematics for 3D Game Programming and Computer Graphics, 3rd ed., Section 7.8.]

[这一推导也出现在Mathematics for 3D Game Programming and Computer Graphics, 3rd ed.，章节7.8。]

+BIT祝威+悄悄在此留下版了个权的信息说：

We want our tangent space to be aligned such that the x axis corresponds to the u direction in the bump map and the y axis corresponds to the v direction in the bump map. That is, if Q represents a point inside the triangle, we would like to be able to write

where P0 is the position of one of the vertices of the triangle, and (u0v0) are the texture coordinates at that vertex. The vectors T and B are the tangent and bitangent vectors aligned to the texture map, and these are what we’d like to calculate.

+BIT祝威+悄悄在此留下版了个权的信息说：

Suppose that we have a triangle whose vertex positions are given by the points P0P1, and P2, and whose corresponding texture coordinates are given by (u0v0), (u1v1), and (u2v2). Our calculations can be made much simpler by working relative to the vertex P0, so we let

+BIT祝威+悄悄在此留下版了个权的信息说：

and

+BIT祝威+悄悄在此留下版了个权的信息说：

（译者注：这其实就是让P0和(u0, v0)归零，将三角形A和B都放到原点上。由于A和B是线性关系，所以这样的平移不会影响其线性关系。）

We need to solve the following equations for T and B.

+BIT祝威+悄悄在此留下版了个权的信息说：

This is a linear system with six unknowns (three for each T and B) and six equations (the xy, and z components of the two vector equations). We can write this in matrix form as follows.

Multiplying both sides by the inverse of the (st) matrix, we have

+BIT祝威+悄悄在此留下版了个权的信息说：

This gives us the (unnormalized) T and B vectors for the triangle whose vertices are P0P1, and P2. To find the tangent vectors for a single vertex, we average the tangents for all triangles sharing that vertex in a manner similar to the way in which vertex normals are commonly calculated. In the case that neighboring triangles have discontinuous texture mapping, vertices along the border are generally already duplicated since they have different mapping coordinates anyway. We do not average tangents from such triangles because the result would not accurately represent the orientation of the bump map for either triangle.

+BIT祝威+悄悄在此留下版了个权的信息说：

Once we have the normal vector N and the tangent vectors T and B for a vertex, we can transform from tangent space into object space using the matrix

To transform in the opposite direction (from object space to tangent space—what we want to do to the light direction), we can simply use the inverse of this matrix. It is not necessarily true that the tangent vectors are perpendicular to each other or to the normal vector, so the inverse of this matrix is not generally equal to its transpose. It is safe to assume, however, that the three vectors will at least be close to orthogonal, so using the Gram-Schmidt algorithm to orthogonalize them should not cause any unacceptable distortions. Using this process, new (still unnormalized) tangent vectors T′ and B′ are given by

+BIT祝威+悄悄在此留下版了个权的信息说：

Normalizing these vectors and storing them as the tangent and bitangent for a vertex lets us use the matrix

to transform the direction to light from object space into tangent space. Taking the dot product of the transformed light direction with a sample from the bump map then produces the correct Lambertian diffuse lighting value.

It is not necessary to store an extra array containing the per-vertex bitangent since the cross product N × T′ can be used to obtain mB′, where m = ±1 represents the handedness of the tangent space. The handedness value must be stored per-vertex since the bitangent B′ obtained from N × T′ may point in the wrong direction. The value of m is equal to the determinant of the matrix in Equation (*). You might find it convenient to store the per-vertex tangent vector T′ as a four-dimensional entity whose w coordinate holds the value of m. Then the bitangent B′ can be computed using the formula

where the cross product ignores the w coordinate. This works nicely for vertex shaders by avoiding the need to specify an additional array containing the per-vertex m values.

## Bitangent versus Binormal 副切线 versus 副法线

+BIT祝威+悄悄在此留下版了个权的信息说：

The term binormal is commonly used as the name of the second tangent direction (that is perpendicular to the surface normal and u-aligned tangent direction). This is a misnomer. The term binormal pops up in the study of curves and completes what is known as a Frenet frame about a particular point on a curve. Curves have a single tangent direction and two orthogonal normal directions, hence the terms normal and binormal. When discussing a coordinate frame at a point on a surface, there is one normal direction and two tangent directions, which should be called the tangent and bitangent.

## Source Code 源代码

The code below generates a four-component tangent T in which the handedness of the local coordinate system is stored as ±1 in the w-coordinate. The bitangent vector B is then given by B = (N × T) · Tw.

+BIT祝威+悄悄在此留下版了个权的信息说：

 1 #include "Vector4D.h"
2
3
4 struct Triangle
5 {
6     unsigned short  index[3];
7 };
8
9
10 void CalculateTangentArray(long vertexCount, const Point3D *vertex, const Vector3D *normal,
11         const Point2D *texcoord, long triangleCount, const Triangle *triangle, Vector4D *tangent)
12 {
13     Vector3D *tan1 = new Vector3D[vertexCount * 2];
14     Vector3D *tan2 = tan1 + vertexCount;
15     ZeroMemory(tan1, vertexCount * sizeof(Vector3D) * 2);
16
17     for (long a = 0; a < triangleCount; a++)
18     {
19         long i1 = triangle->index[0];
20         long i2 = triangle->index[1];
21         long i3 = triangle->index[2];
22
23         const Point3D& v1 = vertex[i1];
24         const Point3D& v2 = vertex[i2];
25         const Point3D& v3 = vertex[i3];
26
27         const Point2D& w1 = texcoord[i1];
28         const Point2D& w2 = texcoord[i2];
29         const Point2D& w3 = texcoord[i3];
30
31         float x1 = v2.x - v1.x;
32         float x2 = v3.x - v1.x;
33         float y1 = v2.y - v1.y;
34         float y2 = v3.y - v1.y;
35         float z1 = v2.z - v1.z;
36         float z2 = v3.z - v1.z;
37
38         float s1 = w2.x - w1.x;
39         float s2 = w3.x - w1.x;
40         float t1 = w2.y - w1.y;
41         float t2 = w3.y - w1.y;
42
43         float r = 1.0F / (s1 * t2 - s2 * t1);
44         Vector3D sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
45                 (t2 * z1 - t1 * z2) * r);
46         Vector3D tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
47                 (s1 * z2 - s2 * z1) * r);
48
49         tan1[i1] += sdir;
50         tan1[i2] += sdir;
51         tan1[i3] += sdir;
52
53         tan2[i1] += tdir;
54         tan2[i2] += tdir;
55         tan2[i3] += tdir;
56
57         triangle++;
58     }
59
60     for (long a = 0; a < vertexCount; a++)
61     {
62         const Vector3D& n = normal[a];
63         const Vector3D& t = tan1[a];
64
65         // Gram-Schmidt orthogonalize
66         tangent[a] = (t - n * Dot(n, t)).Normalize();
67
68         // Calculate handedness
69         tangent[a].w = (Dot(Cross(n, t), tan2[a]) < 0.0F) ? -1.0F : 1.0F;
70     }
71
72     delete[] tan1;
73 }
CalculateTangentArray

Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. Terathon Software, 2001. http://terathon.com/code/tangent.html

+BIT祝威+悄悄在此留下版了个权的信息说：

0
0 收藏

0 评论
0 收藏
0