Confusion between C and OpenGL matrix order (main and column main)

I am very confused about the definition of a matrix. I have a matrix class that stores a floating point number based on the following observations[16], I assume it is the main line:

< /p>

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 and matrixB both have the same linear layout in memory (that is, all numbers are arranged in order). According to http://en.wikipedia.org/wiki/ Row-major_order, this indicates the main layout of the row.

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

Therefore , matrixB[0] = row 0, matrixB[1] = row 1, etc. Again, this represents the main layout of the rows.

When I create a translation matrix as shown below, my problem /Confusion appeared:

1, 0, 0, transX
0, 1, 0, transY
0, 0, 1, transZ< br />0, 0, 0, 1

It is arranged in the memory as, {1,0,0,transX,0,1,0,transY,0,0,1,transZ,0 ,0,0,1}.

Then, when I call glUniformMatrix4fv, I need to set the transposition flag to GL_FALSE, indicating that it is a column professional, otherwise the transformations such as translation/zooming, etc. are not applied correctly:

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

Why does my matrix seem to be row major and need to be passed as column major OpenGL?

The matrix notation used in the opengl document does not describe the in-memory layout of the OpenGL matrix

< /p>

If you think it will be easier to give up/forget the whole “row/column main” thing. This is because in addition to the row/column profession, the programmer can also decide how he arranges the matrix in memory (relatively Whether adjacent elements form rows or columns), and symbols, which will increase confusion.

OpenGL matrix has the same memory layout as directx matrices.

xx xy xz 0
yx yy yz 0
zx zy zz 0
px py pz 1

or

{ xx xy xz 0 yx yy yz 0 zx zy zz 0 px py pz 1 }

> x, y, z is a 3-component vector describing the matrix coordinate system (local coordinate system relative to the global coordinate system).
> p is a 3-component vector describing the origin of the matrix coordinate system.

This means that the translation matrix should be laid out in memory like this:

 {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, transX, transY, transZ, 1 }.

Leave it there, the rest should It’s easy.

—Quoting the old 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.

I am very confused about the matrix definition. I have a matrix class, which stores a floating point number based on the following observations [16], I assume it is the main line:

< /p>

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 and matrixB have the same linear distribution in the memory Bureau (that is, all numbers are arranged in order). According to http://en.wikipedia.org/wiki/Row-major_order, this indicates the main layout of the row.

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

Therefore, matrixB[0] = row 0, matrixB[1] = row 1, etc. Again, this means that the row is mainly Layout.

When I created a translation matrix as shown below, my question/confusion appeared:

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

It is arranged in the memory as, {1 ,0,0,transX,0,1,0,transY,0,0,1,transZ,0,0,0,1}.

Then, when I call glUniformMatrix4fv, I need to change The transpose flag is set to GL_FALSE, indicating that it is a column major, otherwise the transformations such as translation/zooming etc. are not applied correctly:

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.

Why my matrix seems to be row major Yes, need to be passed to OpenGL as a column?

The matrix notation used in the OpenGL document does not describe the in-memory layout of the OpenGL matrix

If you think you give up/forget The whole “row/column main” thing will be easier. This is because in addition to the row/column profession, the programmer can also decide how he arranges the matrix in memory (whether adjacent elements form a row or column), as well as symbols, This will increase confusion.

OpenGL matrix has the same memory layout as directx matrices.

xx xy xz 0
yx yy yz 0
zx zy zz 0
px py pz 1

or

{ xx xy xz 0 yx yy yz 0 zx zy zz 0 px py pz 1 }

> x, y, z are the 3-component vectors describing the matrix coordinate system (relative to the local coordinate system of the global coordinate system).
> p is the origin of the matrix coordinate system 3-component vector.

This means that the translation matrix should be laid out in memory like this:

{ 1, 0, 0, 0, 0, 1 , 0, 0, 0, 0, 1, 0, transX, transY, transZ, 1 }.

Leave it there, the rest should be easy.

—quote Old 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.

Leave a Comment

Your email address will not be published.