为什么这个透视投影矩阵计算不能给出正确的结果
Why does this Perspective Projection Matrix Calculation not give the correct result?
我不太明白。我有一个OpenGL应用程序,我正在从旧的固定功能管道转换到可编程管道,并摆脱了我一直在使用的过时的功能…我不擅长数学,而且矩阵对我来说还是个新鲜事物。
然而,我已经得到了我的ModelView矩阵工作正常,但我似乎不能得到透视矩阵工作正确。
我的意思是,我可以使用固定函数弃用的函数,获得矩阵的值,并将其与我生成的计算值相匹配…ModelView吗?没有问题。角度?它不匹配,我不明白为什么。
这两个方法可以通过注释掉预处理器#define USEPROJMATRIX
来切换
typedef float vec_t;
const static vec_t identityMatrix[16] = { 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
const static vec_t zeroMatrix[16] = { 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0 };
const double pi = 3.1415926535897932384626433832795;
float Utils::Radians(const float& degrees)
{
return degrees * (pi / 180);
}
vec_t* Utils::FrustumMatrix(vec_t* out_matrix, const vec_t& left, const vec_t& right, const vec_t& bottom, const vec_t& top, const vec_t& znear, const vec_t& zfar) // Replaced glFrustum()
{
memcpy(out_matrix, zeroMatrix, 16*sizeof(vec_t));
out_matrix[0] = (2.0 * znear) / (right - left);
out_matrix[5] = (2.0 * znear) / (top - bottom);
out_matrix[8] = (right + left) / (right - left);
out_matrix[9] = (top + bottom) / (top - bottom);
out_matrix[10] = -((zfar + znear) / (zfar - znear));
out_matrix[11] = -1.0;
out_matrix[14] = -((2.0 * zfar * znear) / (zfar - znear));
return out_matrix;
}
vec_t* Utils::PerspectiveMatrix(vec_t* out_matrix, const vec_t& yFOVDegrees, const vec_t& aspectRatio, const vec_t& znear, const vec_t& zfar) // Replaces gluPerspective()
{
vec_t range, left, right, bottom, top;
range = tan(Radians(yFOVDegrees / 2)) * znear;
left = -range * aspectRatio;
right = range * aspectRatio;
bottom = -range;
top = range;
return FrustumMatrix(out_matrix, left, right, bottom, top, znear, zfar);
}
#define USEPROJMATRIX
void GameLogic::OnResize(int width = 640, int height = 480)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
#ifndef USEPROJMATRIX
glLoadMatrixf(identityMatrix);
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 1.0f, 1000.0f);
#else
vec_t pMatrix[16];
memcpy(pMatrix, identityMatrix, sizeof(vec_t) * 16);
Utils::PerspectiveMatrix(pMatrix, 45.0f, (GLfloat)width / (GLfloat)height, 1.0f, 1000.0f);
glLoadMatrixf(pMatrix);
#endif
vec_t projectionmatrix[16];
glGetFloatv(GL_PROJECTION_MATRIX, projectionmatrix);
/* Matrix calculation results in projectionmatrix equalling this:
1.8106601, 0.00000000, 0.00000000, 0.00000000,
0.00000000, 2.4142134, 0.00000000, 0.00000000,
0.00000000, 0.00000000, -1.0020020, -1.0000000,
0.00000000, 0.00000000, -2.0020020, 0.0000000
*/
/* GL calculation
1.8106601, 0.00000000, 0.00000000, 0.00000000,
0.00000000, 2.4142137, 0.00000000, 0.00000000,
0.00000000, 0.00000000, -1.0020020, -1.0000000,
0.00000000, 0.00000000, -2.0020020, 0.00000000
*/
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(identityMatrix);
}
我通过直接观察和比较矩阵值来检查我的值。结果值在上面的评论中列出,我不知道为什么它们不匹配。手动计算的矩阵由于没有正确定位而在视觉上扭曲了渲染场景(一个简单的立方体偏移到中心左下方),我相信它也被渲染得更小了。
编辑根据尼古拉斯·博拉斯的回答修正了愚蠢的错误。但是,这仍然不能正常工作。注释中的值也被更新了。 edit2 以上代码现在可以正常工作了,问题解决了。我犯了愚蠢的错误。
这是你在gluPerspective
调用中使用的:
(GLfloat)width / (GLfloat)height
这是你在Utils::PerspectiveMatrix
调用中使用的:
width / height
第一个生成一个浮点值。第二个生成一个整数。我猜你想要第一个。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的for循环不能正确获取argv
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 为什么我不能将 op 结果乘以常量特征张量数组
- 为什么我不能在类中另一个函数的声明中使用静态 constexpr 的结果?
- 为什么"-3 >> 1"不能产生与"-3 / 2"相同的结果?
- C++除法不能给出正确的结果
- 为什么类不能从 decltype 的结果继承?
- 为什么这个透视投影矩阵计算不能给出正确的结果
- 函数的结果不能作为引用传递
- Auto it = vector.begin()结果类型不能转换为const_iterator
- 为什么检查分配操作的结果有效,但不能与其他条件相结合
- md5的实现在字符串上工作得很好,但是在文件上不能给出正确的结果
- 在Qt中,Float的加法不能给出精确的结果
- 二叉树递归加法不能产生正确的结果
- 不能从mysql读取结果
- 我的平方根函数对某些数字不能给出准确的结果
- 浮点限制代码不能产生正确的结果
- CUDA中值滤波器的实现不能产生期望的结果