【发布时间】:2015-01-08 00:16:01
【问题描述】:
我目前正在尝试创建一个透视函数,它为 OpenGL 生成一个投影。我已经创建了很多很多方法,最新的复制 GLM 的函数来处理它(我必须使用我自己的 Matrix4 类来完成我的作业)。无论我尝试什么代码,与 GLM 相比,我总是产生不正确的数字,这导致我的立方体仅使用 GLM 进行渲染。
它们都使用相同的变量进行初始化
glm::mat4 ProjectionGLM = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
Matrix4 Projection = Matrix4::Perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
透视函数
Matrix4 Matrix4::Perspective(double fov, double aspect, double zNear, double zFar) {
double fovy = fov * 0.0174532925;
double const tanHalfFovy = tan(fovy / 2.0f);
Matrix4 temp;
temp.data[0][0] = 1.0f / (aspect * tanHalfFovy);
temp.data[1][1] = 1.0f / (tanHalfFovy);
temp.data[2][2] = -(zFar + zNear) / (zFar - zNear);
temp.data[2][3] = -1.0f;
temp.data[3][2] = -(2.0f * zFar * zNear) / (zFar - zNear);
return temp;
代码直接按照 GLM 建模,减去 static_cast 和模板。这与我尝试过的许多其他函数的数学相同。然而得到的矩阵是不同的。
GLM 结果
1.34444320 0 0 0
0 1.79259098 0 0
0 0 -1.00200200 -1.00000000
0 0 -0.200200200 0
我的结果
1.81066012 0 0 0
0 2.41421366 0 0
0 0 -1.00200200 -1.00000000
0 0 -0.200200200 0
知道我的透视函数可能有什么问题,或者 GLM 可能会做哪些不同的事情吗?
链接到我的数学库(仅 .h 和 .cpp 文件) https://mega.co.nz/#!lokhCBaB!5r7GwpWhEZ-tUcXV_ccxmQvZgRwIVU8K7dlIffVqZic
【问题讨论】:
-
GLM 使用弧度,您的代码度数...
-
我在函数的第一行将 FoV 转换为弧度。
-
我查看了该函数,对其进行了编辑:double f = 1.0 / tan(fov * PI / 360); temp.data[0][0] = f / 方面;临时数据[1][1] = f;完全相同的结果矩阵。
标签: c++ opengl math perspectivecamera glm-math