正确的OpenGL矩阵格式? [英] Correct OpenGL matrix format?

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

问题描述

我的问题很简单:Projection和ModelView矩阵的正确格式是什么?

My question simply is: what is the correct format for the Projection and ModelView matrix?

有人告诉我下面的示例矩阵是转置的,而不像OpenGL矩阵那样构建.

I've been told that the following example matrices are transposed and are not built like OpenGL matrices should be.

ModelView Matrix
{(1, 0, 0, 0)
(0, 0.7071068, 0.7071068, 0)
(0, -0.7071068, 0.7071068, 0)
(0, -141.4214, -141.4214, 1)}

Projection Matrix
{(1.931371, 0, 0, 0)
(0, 2.414213, 0, 0)
(0, 0, -1.0002, -1)
(0, 0, -2.0002, 0)}

推荐答案

此答案急需更新.即,无需考虑着色器.

正如@gman在评论中指出的,使用行优先还是列优先取决于您的数学方式.只要它们与您的坐标系和操作顺序相匹配,您就可以选择其中一个(如果您不认为这很令人困惑,甚至可以在不同的时间选择两者).

我将这个答案保留为社区Wiki,以防万一有人有时间去更新它.

OpenGL将矩阵指定为按列优先顺序列出的一维数组,即元素的排序如下:

OpenGL specifies matrices as a one-dimensional array listed in column-major order, ie with elements ordered like this:

m0 m4 m8  m12
m1 m5 m9  m13
m2 m6 m10 m14
m3 m7 m11 m15

因此,如果您使用C或几乎任何其他语言以这种方式初始化数组,则生成的矩阵将看起来像需要转置,因为C代码先从左到右读取,然后从上到下读取(在换句话说,例如按行顺序排列):

So if you initialize an array this way, in C or pretty much any other language, the resulting matrix will look like it needs transposing, because C code reads left-to-right first and then top-to-bottom (in other words, like if it were in row-major order):

int mat[16] = {
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 10, 11,
    12, 13, 14, 15,
}

顺便说一下,OpenGL同时具有 glLoadTransposeMatrix

By the way, OpenGL has both glLoadTransposeMatrix and glMultTransposeMatrix, which you can use instead of glLoadMatrix and glMultMatrix, so this shouldn't be a problem.

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

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