非均匀缩放的法线矩阵 [英] normal matrix for non uniform scaling
问题描述
我正在尝试为OpenGL 2.0上的GLSL着色器计算法线矩阵.
Im trying to calculate the normal matrix for my GLSL shaders on OpenGL 2.0.
理论是:法线矩阵是ModelView的左上3x3矩阵,已转置和倒置.
The theory is : a normal matrix is the top left 3x3 matrix of the ModelView, transposed and inverted.
这似乎是正确的,因为我一直在正确渲染场景,直到我从maya导入模型并发现不均匀的比例为止.加载的模型有一个奇怪的灯光,而我的过程模型是正确的,所以我把钱花在了正常的矩阵计算上.
It seems to be correct as I have been rendering my scenes correctly, until I imported a model from maya and found non-uniform scales. Loaded models have a weird lighting, while my procedural ones are correct, so I put my money on the normal matrix calculation.
它是如何用非均匀比例尺计算的?
How is it computed with non uniform scale?
推荐答案
您已经弄清楚,需要使用转置的倒置矩阵来转换法线.对于缩放矩阵,这很容易计算.
You already figured out that you need the transposed inverted matrix for transforming the normals. For a scaling matrix, that's easy to calculate.
一个非均匀的3x3缩放矩阵如下所示:
A non-uniform 3x3 scaling matrix looks like this:
[ sx 0 0 ]
[ 0 sy 0 ]
[ 0 0 sz ]
,其中sx
,sy
和sz
是3个坐标方向的比例因子.
with sx
, sy
and sz
being the scaling factors for the 3 coordinate directions.
这是相反的:
[ 1 / sx 0 0 ]
[ 0 1 / sy 0 ]
[ 0 0 1 / sz ]
转置它不会改变任何东西,因此这已经是您的常规转换矩阵.
Transposing it changes nothing, so this is already your normal transformation matrix.
请注意,与例如旋转不同,此变换矩阵在应用于归一化矢量时将不会保持对矢量进行归一化.因此,在将此矩阵应用到着色器中之后,必须将其重新规格化,然后才能将其用于光照计算.
Note that, unlike for example a rotation, this transformation matrix will not keep vectors normalized when it is applied to a normalized vector. So after applying this matrix in your shader, you will have to re-normalize the result before using it for lighting calculations.
这篇关于非均匀缩放的法线矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!