glm中矩阵值的顺序不正确? [英] Incorrect order of matrix values in glm?

查看:290
本文介绍了glm中矩阵值的顺序不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 GLM 库在OpenGL 3和GLSL上进行数学运算. 我需要正交投影才能绘制2D图形,所以我编写了以下简单代码:

I started using GLM library to do mathematics operations over OpenGL 3 and GLSL. I need an orthographic projection to draw 2D graphics, so I writed this simple code:

glm::mat4 projection(1.0);
projection = glm::ortho( 0.0f, 640.0f, 480.0f, 0.0f, 0.0f, 500.0f);

在屏幕上打印glm :: ortho创建的值,我得到:

Printing on screen the values that glm::ortho has created I get:

 0.00313   0.00000   0.00000   0.00000
 0.00000  -0.00417   0.00000   0.00000
 0.00000   0.00000  -0.00200   0.00000
-1.00000   1.00000  -1.00000   1.00000

我知道这不是OpenGL中值的正确顺序,因为将此矩阵乘以位置矢量会忽略所有转换值.

As I know this is not the correct order for the values in OpenGL, because multiplying this matrix by a position vector will ignore all translation values.

我用我的着色器和一些基元测试了该矩阵,但我只得到了一个空白屏幕.但是,如果我手动修改矩阵,如下所示就可以了:

I tested that matrix with my shader and some primitives and I only get a blank screen. But if I modify by hand the matrix as follows it works ok:

 0.00313   0.00000   0.00000  -1.00000
 0.00000  -0.00417   0.00000   1.00000
 0.00000   0.00000  -0.00200  -1.00000
 0.00000   0.00000   0.00000   1.00000

此外,查看"glm/gtc/matrix_transform.inl"文件中的函数"ortho":

Moreover, looking at the function "ortho" in the "glm/gtc/matrix_transform.inl" file:

template <typename valType>
inline 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;
}

我用以下代码替换了最后3行初始化行,并且工作正常:

I have replaced the last 3 initialization lines by the following code and also worked ok:

    Result[0][3] = - (right + left) / (right - left);
    Result[1][3] = - (top + bottom) / (top - bottom);
    Result[2][3] = - (zFar + zNear) / (zFar - zNear);

这是我用于测试的最小顶点着色器(请注意,此刻uni_MVP只是上面解释的投影矩阵):

This is a minimal vertex shader that I'm using for test (note that at this moment the uni_MVP is only the projection matrix explained above):

uniform mat4 uni_MVP;

in  vec2 in_Position;

void main(void)
{
  gl_Position = uni_MVP * vec4(in_Position.xy,0.0, 1.0);
}

我认为这不是错误,因为所有功能都以相同的方式工作. 也许是我的C ++编译器出现了一个问题,该问题使多维数组的顺序反转了? 在不修改所有GLM源代码的情况下如何解决此问题?

I thik that this is not a bug, because all functions works the same way. Maybe is an issue of my C++ compiler that inverts the order of multidimensional arrays? How can I solve this without modifying all GLM source code?

我正在使用最新版本的GLM库(0.9.1),并将其在Windows Vista上运行Code :: Blocks和MinGW.

I'm using the last version of GLM library (0.9.1) with Code::Blocks and MinGW running on Windows Vista.

推荐答案

首先,它被称为转置,而不是反转.反转意味着完全不同的东西.其次,这就是应该的样子. OpenGL以列主要顺序访问矩阵,即矩阵元素必须具有以下索引:

First, it's called transposition, not inversion. Inversion means something completely different. Second, this is exactly how it's supposed to be. OpenGL accesses matrices in column major order, i.e. the matrix elements have to following indices:

 0  4  8  12
 1  5  9  13
 2  6 10  14
 3  7 11  15

但是,通常的C/C ++多维数组通常按以下方式编号:

However your usual C/C++ multidimensional arrays you normally number like this:

 0  1  2  3
 4  5  6  7
 8  9 10 11
12 13 14 15

即行和列索引已转置.较早版本的OpenGL带有一些扩展,该扩展允许以转置形式提供矩阵,以免人们重写代码.它称为 GL_ARB_transpose_matrix http://www.opengl.org /registry/specs/ARB/transpose_matrix.txt

i.e. row and column indices are transposed. Older versions of OpenGL sported some extension that allows to supply matrices in transposed form, to spare people rewriting their code. It's called GL_ARB_transpose_matrix http://www.opengl.org/registry/specs/ARB/transpose_matrix.txt

使用着色器,甚至比必须使用新功能还要容易. glUniformMatrix的参数为GLboolean transpose,您有3个猜测.

With shaders it's even easier than having to use new functions. glUniformMatrix has a parameter GLboolean transpose, you've got 3 guesses what it does.

这篇关于glm中矩阵值的顺序不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆