我按照这篇文章构建了 GluLookAt 类型矩阵。
Method calculateLookAtMatrix(glm::vec3 eye, glm::vec3 center, glm::vec3 up)
glm::vec3 zaxis = glm::normalize(center - eye);
glm::vec3 xaxis = glm::normalize(glm::cross(up, zaxis));
glm::vec3 yaxis = glm::cross(zaxis, xaxis);
lookAtMatrix[0][0] = xaxis.x;
lookAtMatrix[0][1] = yaxis.x;
lookAtMatrix[0][2] = zaxis.x;
lookAtMatrix[1][0] = xaxis.y;
lookAtMatrix[1][1] = yaxis.y;
lookAtMatrix[1][2] = zaxis.y;
lookAtMatrix[2][0] = xaxis.z;
lookAtMatrix[2][1] = yaxis.z;
lookAtMatrix[2][2] = zaxis.z;
lookAtMatrix[3][0] = glm::dot(xaxis, -eye);
lookAtMatrix[3][1] = glm::dot(yaxis, -eye);
lookAtMatrix[3][2] = glm::dot(zaxis, -eye);
lookAtMatrix[3][3] = 1;
我计算那个 CPU 端,并以与透视矩阵相同的方式将其作为统一传递给着色器。
着色器看起来像这样。
gl_Position = lookAtMatrix * position * perspectiveMatrix;
如果将lookAtMatrix 作为单位矩阵传入,则场景会正确渲染,但是使用上面的代码将矩阵设置为...
eye(0.0f,0.0f,10.0f);
center(0.0f,0.0f,0.0f);
up(0.0f,0.0f,1.0f);
导致一个空白的场景。场景是在 X/Y 上绘制的 10x10 网格,因此我一直期待场景的缩小视图。
(我玩过行和列,但我 99.9% 确信它们很好,因为透视矩阵很好)