当我们想要在Vertex Shader中计算光照时,我们需要在视图空间中的法向量。一般情况下,它看起来像下面这样(来自OpenGL超级宝典第5版):
// 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,并且如何通过反转计算来检查它?