頂點法線的逆轉置


3

在過去的一個月中,我一直在自己學習,並編寫了一個小圖形庫,目前正在致力於頂點法線的轉換,以很快支持照明/陰影。我了解如何將矩陣 M 轉換為頂點,我需要將M的逆轉置(即( M ^-1)^ T)應用於表面法線。但是,我在努力理解如何將此"規則"應用於所有基本轉換(平移,縮放,旋轉)……尤其是平移。

在4D向量類中使用齊次(w)坐標,Translate(x,y,z)不會影響法線,因為其w坐標為0,表示它是向量,而不是點。但是,當我們對Translate的逆進行轉置時,它將對法線產生影響(感覺不正確)。

例如,假設我們創建了一個3D三角形,並希望將基本的Translate(1、2、3)應用於三個頂點的位置。基於用於轉換頂點法線(即逆轉置)的"規則",我們應該將三個頂點的法線乘以Transpose(Translate(-1,-2,-3))。但是,這感覺不正確,因為頂點法線應保持不受平移的影響,而僅受旋轉和縮放的影響。

對於理解如何將逆轉置應用於頂點法線的任何幫助將不勝感激!

2

You are correct, when they are talking about taking the inverse-transpose they are talking about the 3x3 part of the affine transformation leaving the translation since it doesn't affect the normal vector.

Check : Normal / Transforming Normals | Wikipedia

About how or why is it the inverse transpose, a simple explanation, that is also given in the book "Essential Mathematics for Games" is

Suppose we have a Point $P$ and a normal vector $n$ both represented by column matrix. Then we know that $$ n^T P = 0 $$ This is the same as the dot product. Now if we apply a transformation matrix $M$ to point $P$, we'll have to apply some sort of transformation matrix $Q$ to $n$ also. $$ \begin{eqnarray} (Q n)^T M P & = & 0 \\ n^T Q^T M P & = & 0 \end{eqnarray} $$

One solution is $$ \begin{eqnarray} Q^T M & = & I \\ Q & = & {M^{-1}}^T \end{eqnarray} $$ Hence the inverse transpose. This stands for any sort of transformation matrix $M$ be it scaling, rotation etc. Also for pure rotation matrix, since they are orthogonal, the matrix transforming the normals is the same as that of the vertices. as ${M^T}^T = M$


0

You're right, translation is not relevant in this context: it wouldn't affect the normals.

Consider a non-uniformly scaled object:

enter image description here

We want to get the normal matrix to transform the normals in the vertex buffer into vectors representing world space normals from the world matrix M, which encodes the translation, rotation and scaling (and shearing) of the model object.


If you were to interpret the 3x3 of the M in the vertex shader, you would read rotation data with scaling applied. If the world matrix contains non-uniform scaling, your normal vectors will be scaled incorrectly, and you'll get the wrong results.

The reason we take the transpose of the transform is because the rotation matrix, used to create M = TranslationMatrix x RotationMatrix x ScaleMatrix, is an Orthagonal Matrix, which means that the row vectors are orthogonal, unit vectors, i.e. they are linearly-independent and their length is one.

This type of matrices hold a property like the following:

Q^T * Q = Q * Q^T = I

If you remember the inverse rule:

Q^-1 * Q = Q * Q^-1 = I

This also means the following for the orthogonal matrices:

Q^T = Q^-1

This allows us to apply the inverse of the rotations by transposing the matrix.

In summary, taking inverse-transpose of the world matrix does 2 important things:

  1. Undo transformation (Inverse)
    • undo the scaling (and shearing)
    • invert rotations
    • translate the object to the world center (irrelevant)

enter image description here

  1. Un-undo rotations (Transpose)
    • Now, if we take the transpose of our matrix, we will get our rotations back to their original state because of the orthogonal property of the rotation matrix.

enter image description here

We now have our normal matrix ready to transform model-space normals defined in the vertex buffer into world-space normals in the vertex shader.