55

我正在研究一些着色器,我需要转换法线。

我在一些教程中阅读了转换法线的方式是将它们与模型视图矩阵的倒数的转置相乘。但我找不到解释为什么会这样,这背后的逻辑是什么?

4

5 回答 5

51

It flows from the definition of a normal.

Suppose you have the normal, N, and a vector, V, a tangent vector at the same position on the object as the normal. Then by definition N·V = 0.

Tangent vectors run in the same direction as the surface of an object. So if your surface is planar then the tangent is the difference between two identifiable points on the object. So if V = Q - R where Q and R are points on the surface then if you transform the object by B:

V' = BQ - BR
   = B(Q - R)
   = BV

The same logic applies for non-planar surfaces by considering limits.

In this case suppose you intend to transform the model by the matrix B. So B will be applied to the geometry. Then to figure out what to do to the normals you need to solve for the matrix, A so that:

(AN)·(BV) = 0

Turning that into a row versus column thing to eliminate the explicit dot product:

[tranpose(AN)](BV) = 0

Pull the transpose outside, eliminate the brackets:

transpose(N)*transpose(A)*B*V = 0

So that's "the transpose of the normal" [product with] "the transpose of the known transformation matrix" [product with] "the transformation we're solving for" [product with] "the vector on the surface of the model" = 0

But we started by stating that transpose(N)*V = 0, since that's the same as saying that N·V = 0. So to satisfy our constraints we need the middle part of the expression — transpose(A)*B — to go away.

Hence we can conclude that:

 transpose(A)*B = identity
 => transpose(A) = identity*inverse(B)
 => transpose(A) = inverse(B)
 => A = transpose(inverse(B))
于 2012-11-30T23:29:50.557 回答
40

我最喜欢的证明如下,其中 N 是法线,V 是切向量。由于它们是垂直的,它们的点积为零。M 是任何 3x3 可逆变换 (M -1 * M = I)。N' 和 V' 是经过 M 变换的向量。

在此处输入图像描述

为了获得一些直觉,请考虑下面的剪切变换。

在此处输入图像描述

请注意,这不适用于切向量。

于 2014-10-21T01:40:43.523 回答
36

Take a look at this tutorial:

https://paroj.github.io/gltut/Illumination/Tut09%20Normal%20Transformation.html

You can imagine that when the surface of a sphere stretches (so the sphere is scaled along one axis or something similar) the normals of that surface will all 'bend' towards each other. It turns out you need to invert the scale applied to the normals to achieve this. This is the same as transforming with the Inverse Transpose Matrix. The link above shows how to derive the inverse transpose matrix from this.

Also note that when the scale is uniform, you can simply pass the original matrix as normal matrix. Imagine the same sphere being scaled uniformly along all axes, the surface will not stretch or bend, nor will the normals.

于 2012-11-30T23:29:15.870 回答
3

如果模型矩阵由平移、旋转和缩放组成,则不需要进行逆转置来计算法线矩阵。只需将法线除以平方比例并乘以模型矩阵,我们就完成了。您可以将其扩展到具有垂直轴的任何矩阵,只需计算您正在使用的矩阵的每个轴的平方比例。

我在我的博客中写了详细信息:https ://lxjk.github.io/2017/10/01/Stop-Using-Normal-Matrix.html

于 2017-10-01T23:57:48.107 回答
-2

不明白为什么在与模型矩阵相乘之前不将方向向量的第 4 个元素归零。不需要反转或转置。将方向向量视为两点之间的差。将这两个点与模型的其余部分一起移动 - 它们与模型的相对位置仍然相同。取两点之差得到新方向,第四个元素抵消为零。便宜很多。

于 2014-03-14T23:59:47.663 回答