加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C和OpenGL矩阵顺序之间的混淆(行主要与列主要)

发布时间:2020-12-16 09:10:23 所属栏目:百科 来源:网络整理
导读:我对矩阵定义感到非常困惑.我有一个矩阵类,它根据以下观察结果保存一个浮点数[16],我假设它是行主要的: float matrixA[16] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };float matrixB[4][4] = { { 0,3 },{ 4,7 },{ 8,11 },{ 12,15 } }; matrixA和matrixB
我对矩阵定义感到非常困惑.我有一个矩阵类,它根据以下观察结果保存一个浮点数[16],我假设它是行主要的:

float matrixA[16] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
float matrixB[4][4] = { { 0,3 },{ 4,7 },{ 8,11 },{ 12,15 } };

matrixA和matrixB在存储器中都具有相同的线性布局(即所有数字都按顺序排列).根据http://en.wikipedia.org/wiki/Row-major_order,这表示行主要布局.

matrixA[0] == matrixB[0][0];
matrixA[3] == matrixB[0][3];
matrixA[4] == matrixB[1][0];
matrixA[7] == matrixB[1][3];

因此,matrixB [0] =第0行,matrixB [1] =第1行等.再次,这表示行主要布局.

当我创建一个如下所示的翻译矩阵时,我的问题/困惑就出现了:

1,transX
0,transY
0,transZ
0,1

其在存储器中布置为,{1,transX,transY,transZ,1}.

然后,当我调用glUniformMatrix4fv时,我需要将转置标志设置为GL_FALSE,表示它是列专业,否则转换如翻译/缩放等不正确应用:

If transpose is GL_FALSE,each matrix is assumed to be supplied in
column major order. If transpose is GL_TRUE,each matrix is assumed to
be supplied in row major order.

为什么我的矩阵似乎是行主要的,需要作为列主要传递给OpenGL?

解决方法

opengl文档中使用的矩阵表示法没有描述OpenGL矩阵的内存中布局

如果你认为放弃/忘记整个“行/列主要”的事情会更容易.这是因为除了行/列专业之外,程序员还可以决定他如何在内存中布置矩阵(相邻元素是否形成行或列),以及符号,这会增加混乱.

OpenGL矩阵有same memory layout as directx matrices.

x.x x.y x.z 0
y.x y.y y.z 0
z.x z.y z.z 0
p.x p.y p.z 1

要么

{ x.x x.y x.z 0 y.x y.y y.z 0 z.x z.y z.z 0 p.x p.y p.z 1 }

> x,y,z是描述矩阵坐标系的3分量矢量(相对于全局坐标系的局部坐标系).
> p是描述矩阵坐标系原点的3分量向量.

这意味着翻译矩阵应该像这样在内存中布局:

{ 1,1 }.

把它留在那,其余的应该很容易.

—引用旧的opengl faq–

9.005 Are OpenGL matrices column-major or row-major?

For programming purposes,OpenGL matrices are 16-value arrays with base vectors laid out contiguously in memory. The translation components occupy the 13th,14th,and 15th elements of the 16-element matrix,where indices are numbered from 1 to 16 as described in section 2.11.2 of the OpenGL 2.1 Specification.

Column-major versus row-major is purely a notational convention. Note that post-multiplying with column-major matrices produces the same result as pre-multiplying with row-major matrices. The OpenGL Specification and the OpenGL Reference Manual both use column-major notation. You can use any notation,as long as it’s clearly stated.

Sadly,the use of column-major format in the spec and blue book has resulted in endless confusion in the OpenGL programming community. Column-major notation suggests that matrices are not laid out in memory as a programmer would expect.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读