C++ glm::ortho() 真的错了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12230312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is glm::ortho() actually wrong?
提问by Jamie Syme
I recently thought it would be a good idea to switch from the old (deprecated) functionality that OpenGL provides, such as matrix operations and the fixed function pipeline.
我最近认为从 OpenGL 提供的旧(已弃用)功能(例如矩阵运算和固定函数管道)切换是个好主意。
I am using GLM as my matrix library to simplify things a bit. The problem is that it may have caused more problems than it simplified...
我使用 GLM 作为我的矩阵库来简化一些事情。问题是它可能引起的问题比它简化的要多……
Perspective projections worked fine with my shaders and setup, but when I tried to switch to orthogonal, everything broke down. My points and simple quads wouldn't display. When I used the old OpenGL matrices, things started working again.
透视投影适用于我的着色器和设置,但当我尝试切换到正交时,一切都崩溃了。我的点和简单的四边形不会显示。当我使用旧的 OpenGL 矩阵时,事情又开始工作了。
I narrowed it all down to the projection matrix. Here is how I called it:
我把它缩小到投影矩阵。这是我如何称呼它的:
glm::mat4 projMat = glm::ortho( 0, 400, 0, 400, -1, 1 );
I compared that to the one supplied by opengl once this is called"
我将它与 opengl 提供的那个进行了比较,一旦被称为“
glOrtho( 0, 400, 0, 400, -1, 1 );
The only differences are the [0][0] element and [1][1] element (which, as far as I know, be equal to "2/width" and "2/height", respectively). From the OpenGL matrix, the values were exactly that! On the glm matrix, though, the values were 0.
唯一的区别是 [0][0] 元素和 [1][1] 元素(据我所知,它们分别等于“2/width”和“2/height”)。从 OpenGL 矩阵来看,这些值正是如此!但是,在 glm 矩阵上,值是 0。
Once I manually switched the values from the glm matrix after I called glm::ortho, everything was working again!
一旦我在调用 glm::ortho 之后手动切换了 glm 矩阵中的值,一切又恢复了!
So my question: is the glm::ortho() function really broken, or am I just using it wrong?
所以我的问题是: glm::ortho() 函数真的坏了,还是我只是用错了?
回答by Tim
It doesn't appear that it should be broken from the source code (v 0.9.3.4)
似乎不应该从源代码中破坏它(v 0.9.3.4)
template <typename valType>
GLM_FUNC_QUALIFIER detail::tmat4x4<valType> ortho
(
valType const & left,
valType const & right,
valType const & bottom,
valType const & top,
valType const & zNear,
valType const & zFar
)
{
detail::tmat4x4<valType> Result(1);
Result[0][0] = valType(2) / (right - left);
Result[1][1] = valType(2) / (top - bottom);
Result[2][2] = - valType(2) / (zFar - zNear);
Result[3][0] = - (right + left) / (right - left);
Result[3][1] = - (top + bottom) / (top - bottom);
Result[3][2] = - (zFar + zNear) / (zFar - zNear);
return Result;
}
My only thought is that this template might be creating a matrix of integers (as you've passed all ints to the function), and thus doing integer division instead of floating point. Can you try appending .f
to all your parameters?
我唯一的想法是这个模板可能正在创建一个整数矩阵(因为您已将所有整数传递给函数),从而进行整数除法而不是浮点数。您可以尝试附加.f
到所有参数吗?
glm::mat4 projMat = glm::ortho( 0.f, 400.f, 0.f, 400.f, -1.f, 1.f );
glm::mat4 projMat = glm::ortho( 0.f, 400.f, 0.f, 400.f, -1.f, 1.f );