转 切空间矩阵的推导
来源 http://www.blacksmith-studios.dk/projects/downloads/tangent_matrix_derivation.php
Derivation of the Tangent Space Matrix
Author Jakob Gath (Hel) Edited by S鴕en Dreijer (Halloko)
Contents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
1. Preface 2. Disclaimer 3. What you need to know on beforehand 4. Introduction 5. Theory 5.1. A Practical Example 5.2. The difference between texture space and tangent space 5.3. Choosing the right type of geometry 6. Derivation 6.1.a Exploiting the Properties of a Orthonormal Matrix 6.1.b The inverted tangent space matrix 7. Analysis of the derived matrix 8. Conclusion 9. References |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
1. Preface |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Have you ever wondered what this tangent space everybody's talking about is? Or perhaps you've grown tired of everybody making use of a matrix you don't even know where comes from in the first place! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
2. Disclaimer |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Don't take everything for granted. Don't just blindly believe in everything that's said. If you think something looks weird in the article don't just skip it but instead question it and confront the authors! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
3. What you need to know on beforehand |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
4. Introduction |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
As most graphics programmers probably know diffuse lighting is calculated by performing a dot product between a surface normal and a light vector (if you don't know what we're talking about have a look at [1]. For this dot product to make sense it's required that these two vectors exist in the same space (coordinate system). Usually we choose the surface's coordinate system (in which the surface normals exist) since this means we only have to convert the light vector for each vertex on the surface into this space which means the light vector can be linearly interpolated across the surface. Another option would have been to convert all the surface normals (and there could be a lot) to the light vector's coordinate system. You probably see which method is preferable! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
5. Theory |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
For most graphics APIs world space is defined by the three basis vectors, These are also known as the columns of the identity matrix. If we multiply a vector by the identity matrix it will be converted to the same coordinate system. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
5.1. A Practical Example |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Let's compare the previous section with something that most OpenGL users should be familiar with: the modelview. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin(...); glVertex*(...); ... glEnd(); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In the example code above the modelview matrix is set to the identity matrix. When rendering the vertices following the glBegin() call, their position won't be changed (no translation or rotation is performed) since they're multiplied by the identity matrix. This should be common knowledge to all OpenGL users. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
5.2. The difference between texture space and tangent spaceTexture space: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The dimension of texture space is given by the following OpenGL constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The texture coordinates are referred to as (S, T, R). These coordinates are manipulated by a texture matrix. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
glMatrixMode(GL_TEXTURE); glLoadIdentity(); glBegin(...); glTexCoord*(...); ... glEnd(); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In the example code above the texture matrix is set to the identity matrix (which it is by default). This will in turn mean that the specified texture coordinates (when rendering) won't be changed (translated or rotated). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Tangent space: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Since our use of the tangent space concerns a limited plane (in the shape of a polygon) the corresponding normal map will be of the dimension 2 (in other words, a 2D map). Essentially, this means that each vertex will have two texture coordinates specified - an S and a T component. A normal map is made up so that there for each texel is specified a normal to that texel's surface. Those normals are said to exist in tangent space for which the dimension is 3. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The red, green and blue arrows on the left of figure 1 constitute world space (the axes aren't named since their names don't matter in this discussion, nor do their directions). The three other axes to the right in figure 1 constitute the basis vectors for the triangle's tangent space. v and c denote the vertex and texture coordinates for a given geometric point, respectively. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
5.3. Choosing the right type of geometry |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In figure 1 we've drawn a polygon in the shape of a triangle. By choosing this exact type of geometry the matrix we're seeking will be identical for all three vertices since they will be in the same plane. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
6. DerivationGoal: The basis vectors of the tangent space matrix are to be found |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Let's write out what we know by now:
We further note that,
Some notations:
The vectors are denoted by the (delta) sign since the vector is, essentially, the difference between two sets of numbers. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
From the figure it's easy to see that is formed as a combination of the two unknown basis vectors. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
6.1.a Exploiting the Properties of a Orthonormal Matrix |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
(Section included the 15 dec. 2007) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
6.1.b The inverted tangent space matrix |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
When the inverted matrix has to be calculated we make use of the matrix's determinant
We can then set all this up in a generalized expression for an inverted 3x3 matrix |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Small Note |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Remember that only needs to be calculated once! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
7. Analysis of the derived matrix |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The tangent space matrix only needs to be recalculated if the polygon has been rotated. It should not be recalculated after a translation since the axes of the tangent space coordinate system will still have the same direction after such a transformation.
Calculation of T and B is given by a multiplication of two matrices and a constant and requires 8 operations (6 multiplications and 2 subtractions). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
This is the minimum number of operations required to create a TBN matrix at run-time for the given example. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
8. Conclusion |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In this article we've discussed a robust approach to calculating the tangent space matrix for an arbitrary surface at run-time. We've tried to shed light on where this mysterious matrix comes from. It's important that one remembers to take advantage of all the shortcuts available throughout the calculation of the matrix, such as only calculating the determinant once and storing it for later use. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
9. References |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
[1] - http://www.delphi3d.net/articles/viewarticle.php?article=phong.htm |
Derivation of the Tangent Space Matrix
Author Jakob Gath (Hel) Edited by S鴕en Dreijer (Halloko)
Contents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
1. Preface 2. Disclaimer 3. What you need to know on beforehand 4. Introduction 5. Theory 5.1. A Practical Example 5.2. The difference between texture space and tangent space 5.3. Choosing the right type of geometry 6. Derivation 6.1.a Exploiting the Properties of a Orthonormal Matrix 6.1.b The inverted tangent space matrix 7. Analysis of the derived matrix 8. Conclusion 9. References |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
1. Preface |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Have you ever wondered what this tangent space everybody's talking about is? Or perhaps you've grown tired of everybody making use of a matrix you don't even know where comes from in the first place! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
2. Disclaimer |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Don't take everything for granted. Don't just blindly believe in everything that's said. If you think something looks weird in the article don't just skip it but instead question it and confront the authors! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
3. What you need to know on beforehand |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
4. Introduction |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
As most graphics programmers probably know diffuse lighting is calculated by performing a dot product between a surface normal and a light vector (if you don't know what we're talking about have a look at [1]. For this dot product to make sense it's required that these two vectors exist in the same space (coordinate system). Usually we choose the surface's coordinate system (in which the surface normals exist) since this means we only have to convert the light vector for each vertex on the surface into this space which means the light vector can be linearly interpolated across the surface. Another option would have been to convert all the surface normals (and there could be a lot) to the light vector's coordinate system. You probably see which method is preferable! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
5. Theory |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
For most graphics APIs world space is defined by the three basis vectors, These are also known as the columns of the identity matrix. If we multiply a vector by the identity matrix it will be converted to the same coordinate system. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
5.1. A Practical Example |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Let's compare the previous section with something that most OpenGL users should be familiar with: the modelview. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin(...); glVertex*(...); ... glEnd(); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In the example code above the modelview matrix is set to the identity matrix. When rendering the vertices following the glBegin() call, their position won't be changed (no translation or rotation is performed) since they're multiplied by the identity matrix. This should be common knowledge to all OpenGL users. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
5.2. The difference between texture space and tangent spaceTexture space: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The dimension of texture space is given by the following OpenGL constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The texture coordinates are referred to as (S, T, R). These coordinates are manipulated by a texture matrix. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
glMatrixMode(GL_TEXTURE); glLoadIdentity(); glBegin(...); glTexCoord*(...); ... glEnd(); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In the example code above the texture matrix is set to the identity matrix (which it is by default). This will in turn mean that the specified texture coordinates (when rendering) won't be changed (translated or rotated). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Tangent space: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Since our use of the tangent space concerns a limited plane (in the shape of a polygon) the corresponding normal map will be of the dimension 2 (in other words, a 2D map). Essentially, this means that each vertex will have two texture coordinates specified - an S and a T component. A normal map is made up so that there for each texel is specified a normal to that texel's surface. Those normals are said to exist in tangent space for which the dimension is 3. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The red, green and blue arrows on the left of figure 1 constitute world space (the axes aren't named since their names don't matter in this discussion, nor do their directions). The three other axes to the right in figure 1 constitute the basis vectors for the triangle's tangent space. v and c denote the vertex and texture coordinates for a given geometric point, respectively. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
5.3. Choosing the right type of geometry |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In figure 1 we've drawn a polygon in the shape of a triangle. By choosing this exact type of geometry the matrix we're seeking will be identical for all three vertices since they will be in the same plane. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
6. DerivationGoal: The basis vectors of the tangent space matrix are to be found |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Let's write out what we know by now:
We further note that,
Some notations:
The vectors are denoted by the (delta) sign since the vector is, essentially, the difference between two sets of numbers. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
From the figure it's easy to see that is formed as a combination of the two unknown basis vectors. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
6.1.a Exploiting the Properties of a Orthonormal Matrix |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
(Section included the 15 dec. 2007) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
6.1.b The inverted tangent space matrix |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
When the inverted matrix has to be calculated we make use of the matrix's determinant
We can then set all this up in a generalized expression for an inverted 3x3 matrix |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Small Note |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Remember that only needs to be calculated once! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
7. Analysis of the derived matrix |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The tangent space matrix only needs to be recalculated if the polygon has been rotated. It should not be recalculated after a translation since the axes of the tangent space coordinate system will still have the same direction after such a transformation.
Calculation of T and B is given by a multiplication of two matrices and a constant and requires 8 operations (6 multiplications and 2 subtractions). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
This is the minimum number of operations required to create a TBN matrix at run-time for the given example. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
8. Conclusion |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In this article we've discussed a robust approach to calculating the tangent space matrix for an arbitrary surface at run-time. We've tried to shed light on where this mysterious matrix comes from. It's important that one remembers to take advantage of all the shortcuts available throughout the calculation of the matrix, such as only calculating the determinant once and storing it for later use. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
9. References |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
[1] - http://www.delphi3d.net/articles/viewarticle.php?article=phong.htm |