【发布时间】:2013-07-01 10:10:44
【问题描述】:
当我们想在顶点着色器中计算光照时,我们需要视图空间中的法线向量。一般来说,它看起来如下(来自 OpenGL Superbible 5th):
// normalMatrix is retrieved from GLMatrixStack modelViewMatrix
vec3 vEyeNormal = normalMatrix * vNormal
我想在不使用 GLT 库的情况下编写程序。在另一个来源 (http://en.wikibooks.org/wiki/GLSL_Programming/GLUT/Diffuse_Reflection) 中,我发现了以下公式:
vec3 normalDirection = normalize(m_3x3_inv_transp * v_normal);
变量 m_3x3_inv_transp 计算如下:
glm::mat3 m_3x3_inv_transp = glm::transpose(glm::inverse(glm::mat3(mesh.object2world)));
我意识到:
- 法线矩阵只是模型视图矩阵的旋转分量,因为 法线向量的平移是不可接受的。
- OpenGL 操作的顺序是缩放、平移、旋转 (Opengl order of matrix transformations)
- 逆矩阵是撤消最后一次转换 (http://tomdalling.com/blog/modern-opengl/04-cameras-vectors-and-input/)
我的问题是为什么在反转和转置矩阵后我得到 NormalMatrix 以及如何通过反转计算来检查它?
【问题讨论】: