gl_NormalMatrix [英] gl_NormalMatrix

查看:137
本文介绍了gl_NormalMatrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现"gl_NormalMatrix-表示逆转置模型视图矩阵的3x3矩阵".为什么法线矩阵必须是逆转置模型视图矩阵?为什么我不能仅出于此目的使用模型视图矩阵?

解决方案

许多计算是在眼部空间进行的.这与以下事实有关:通常在该空间中进行照明,否则将很难实现依赖于眼睛位置的效果,例如镜面反射光.

因此,我们需要一种将法线转换为眼部空间的方法...

为什么我们不能对法向矢量做同样的事情?法线是3个浮点的向量, modelview 矩阵是4×4.其次,由于法线是矢量,因此我们只希望变换其方向.包含方向的modelview矩阵区域是左上3×3子矩阵.那么为什么不将法线乘以该子矩阵...

让我们看看一个潜在的问题...

在上图中,将 modelview 矩阵应用于所有顶点以及法线,结果显然是错误的:法线不再垂直于曲面.

现在我们知道,不能在所有情况下都应用 modelview 来转换法线向量.那么问题是,我们应该应用什么矩阵?

考虑一个3×3的矩阵G,让我们看看如何计算该矩阵以正确转换法线向量...

转换法线的正确矩阵是M矩阵逆的转置. OpenGL在 gl_NormalMatrix ...

中为我们计算了这一点

I have found that "gl_NormalMatrix - 3x3 Matrix representing the inverse transpose model-view matrix". Why does the matrix for normals have to be the inverse transpose model-view matrix? Why can't I just use the model-view matrix for this purpose?

解决方案

See here:

This section was inspired by the excellent book by Eric Lengyel "Mathematics for 3D Game Programming and Computer Graphics".

Many computations are done in eye space. This has to do with the fact that lighting is commonly performed in this space, otherwise eye position dependent effects, such as specular lights would be harder to implement.

Hence we need a way to transform the normal into eye space...

why can’t we just do the same with a normal vector? A normal is a vector of 3 floats and the modelview matrix is 4×4. Secondly, since the normal is a vector, we only want to transform its orientation. The region of the modelview matrix that contains the orientation is the top left 3×3 submatrix. So why not multiply the normal by this submatrix...

Lets have a look at a potential problem...

In the above figure the modelview matrix was applied to all the vertices as well as to the normal and the result is clearly wrong: the normal is no longer perpendicular to the surface.

So now we know that we can’t apply the modelview in all cases to transform the normal vector. The question is then, what matrix should we apply?

Consider a 3×3 matrix G, and lets see how this matrix could be computed to properly transform the normal vectors...

the correct matrix to transform the normal is the transpose of the inverse of the M matrix. OpenGL computes this for us in the gl_NormalMatrix...

这篇关于gl_NormalMatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆