float matrixA[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; float matrixB[4][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 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, 0, 0, transX 0, 1, 0, transY 0, 0, 1, transZ 0, 0, 0, 1
其在存储器中布置为,{1,0,0,transX,0,1,0,transY,0,0,1,transZ,0,0,0,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, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, transX, transY, transZ, 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.