If we send a row major matrix to glMultMatrix() and again get the matrix using glGet() function, we get the same matrix. For example 12th, 13th and 14th elements always consist of translation elements for both row major and column major matrices.
But here's a quote from the book "More OpenGL game programming":
Also note that the product of the projection matrices is transposed before being passed to glTexGenv(). This is necessary because all three matrices store data in column major order. After transposing, the result is in row major order which is what glTexGenfv() expects.
...
The author uses this way to get the projection matrix:
gluPerspective( fovy, aspect_ratio, 1, 100 );
glGetFloatv( GL_PROJECTION_MATRIX, ( GLfloat*)&mat4_projection );
But I'm wondering! Both row major and column major matrices store the data the same way. ( m[0], m[1], etc ). But the way that they deal with rows or columns is different.
I have seen an Open Source example that simply works with row major matrices and pre multiplies them and then sends the result to glMultMatrix().
I guess that something is going wrong in his article.

**0**

# row major vs.column major

Started by ehsan2004, Mar 27 2010 09:30 AM

9 replies to this topic

Sponsor:

###
#2
Members - Reputation: **157**

Posted 28 March 2010 - 01:51 AM

But he just gets the result from OpenGL:

gluPerspective( fovy, aspect_ratio, 1, 100 );

glGetFloatv( GL_PROJECTION_MATRIX, ( GLfloat*)&mat4_projection );

So we don't need to transpose it.

OpenGL always store the translation elements in 12ths, 13th and 14th elements:

array[12] = x

array[13] = y

array[14] = z

array[15] = 1

I have tried it, OpenGL uses row major matrices, but the first row is m[0], m[4], m[8] and m[12] for example.

I guess passing the result to glTexGen() is independent of row major and column major matrices( Assuming that we work with row major ordering ).

The second argument of glTexGen() in his example is GL_EYE_PLANE, So I guess that we need to transpose the matrix to send the correct "plane equation" to this function.

You can find his source code here:

http://glbook.gamedev.net/moglgp/code.asp

Download chapter 5 and search for projective texturing.

[Edited by - ehsan2004 on March 28, 2010 8:51:25 AM]

gluPerspective( fovy, aspect_ratio, 1, 100 );

glGetFloatv( GL_PROJECTION_MATRIX, ( GLfloat*)&mat4_projection );

So we don't need to transpose it.

OpenGL always store the translation elements in 12ths, 13th and 14th elements:

array[12] = x

array[13] = y

array[14] = z

array[15] = 1

I have tried it, OpenGL uses row major matrices, but the first row is m[0], m[4], m[8] and m[12] for example.

I guess passing the result to glTexGen() is independent of row major and column major matrices( Assuming that we work with row major ordering ).

The second argument of glTexGen() in his example is GL_EYE_PLANE, So I guess that we need to transpose the matrix to send the correct "plane equation" to this function.

You can find his source code here:

http://glbook.gamedev.net/moglgp/code.asp

Download chapter 5 and search for projective texturing.

[Edited by - ehsan2004 on March 28, 2010 8:51:25 AM]

###
#4
Members - Reputation: **157**

Posted 28 March 2010 - 02:42 AM

Quote:

Original post by tori

Wavarian, are you sure?

src/mesa/main/matrix.c: * -# 4x4 transformation matrices are stored in memory in column major order.

I read some comments from the OpenGL moderator that both row major and column major matrices use the same 1D array.

But the first row of row major matrices are m[0],m[1], m[2] and m[3] and the first row of column major matrices are m[0], m[4], m[8] and m[12]. After computing the matrix mathes( whether we use row major or column major ) , the values of m[0] to m[15] are the same for both matrices ( OpenGL uses post multiplication and DirectX uses Pre multiplication, so we get the same results ).

###
#5
Crossbones+ - Reputation: **1164**

Posted 28 March 2010 - 02:53 AM

The quote from that book seems to be out of context. It sounds like the author is manually inverting the matrix before sending it to OpenGL rather than expecting OpenGL to invert it or him. Note that there's no way for OpenGL to know if a matrix (given as a 1D array) is row- or column-major, so there's no way for it to know whether to invert your matrix.

###
#6
Crossbones+ - Reputation: **4437**

Posted 28 March 2010 - 03:39 AM

Regardless whether you have a D3D matrix

{ 0, 1, 2, 3, 4, ..., 15 }

This means that you need not re-order to use that for either D3D or OpenGL (but you need to do so for e.g. Collada!) because if you use it in D3D you would do a row vector multiplication

Maybe the following mathematical rule was used:

(

BTW: Whether the elements 12, 13, 14 store the translation depends additionally on the conventation that the homogeneous co-ordinate is store at the 4th place in a 4D vector. Both D3D as well as OpenGL use this convention, but other APIs/formats may use another one.

[ 0 1 2 3 ]or a OpenGL matrix (the transpose of the above one)

[ 4 5 6 7 ]

[ 8 9 10 11 ]

[ 12 13 14 15 ]

[ 0 4 8 12 ]in linear memory (i.e. from lower address to higher address) the layout is

[ 1 5 9 13 ]

[ 2 6 10 14 ]

[ 3 7 11 15 ]

{ 0, 1, 2, 3, 4, ..., 15 }

This means that you need not re-order to use that for either D3D or OpenGL (but you need to do so for e.g. Collada!) because if you use it in D3D you would do a row vector multiplication

[ 0 1 2 3 ], and in OpenGL a column vector multiplication

[ x y z w ] * [ 4 5 6 7 |

[ 8 9 10 11 ]

[ 12 13 14 15 ]

[ 0 4 8 12 ] [ x [so that the results are identical, aren't they? The quintessence is, that row vectors together with row major order on the one hand, and column vectors together with column major order on the other hand produce the same layout in memory.

[ 1 5 9 13 ] * [ y ]

[ 2 6 10 14 ] [ z ]

[ 3 7 11 15 ] [ w ]

Quote:

Original post by ehsan2004

I have seen an Open Source example that simply works with row major matrices and pre multiplies them and then sends the result to glMultMatrix().

Maybe the following mathematical rule was used:

(

**A*****B**)^{T}=**B**^{T}***A**^{T}BTW: Whether the elements 12, 13, 14 store the translation depends additionally on the conventation that the homogeneous co-ordinate is store at the 4th place in a 4D vector. Both D3D as well as OpenGL use this convention, but other APIs/formats may use another one.

###
#7
Members - Reputation: **157**

Posted 28 March 2010 - 03:39 AM

But I don't see any matrix inversion there:

For example, he has also calculated the texture matrix for the shader( When using shader ), But he has not transposed the results:

You can find his source code here:

http://glbook.gamedev.net/moglgp/code.asp

Download chapter 5 and search for projective texturing.

mat4(const mat4 &m) {

mat[0] = m[0]; mat[4] = m[4]; mat[8] = m[8]; mat[12] = m[12];

mat[1] = m[1]; mat[5] = m[5]; mat[9] = m[9]; mat[13] = m[13];

mat[2] = m[2]; mat[6] = m[6]; mat[10] = m[10]; mat[14] = m[14];

mat[3] = m[3]; mat[7] = m[7]; mat[11] = m[11]; mat[15] = m[15];

}

mat4 operator*(const mat4 &m) const {

mat4 ret;

ret[0] = mat[0] * m[0] + mat[4] * m[1] + mat[8] * m[2] + mat[12] * m[3];

ret[1] = mat[1] * m[0] + mat[5] * m[1] + mat[9] * m[2] + mat[13] * m[3];

ret[2] = mat[2] * m[0] + mat[6] * m[1] + mat[10] * m[2] + mat[14] * m[3];

ret[3] = mat[3] * m[0] + mat[7] * m[1] + mat[11] * m[2] + mat[15] * m[3];

ret[4] = mat[0] * m[4] + mat[4] * m[5] + mat[8] * m[6] + mat[12] * m[7];

ret[5] = mat[1] * m[4] + mat[5] * m[5] + mat[9] * m[6] + mat[13] * m[7];

ret[6] = mat[2] * m[4] + mat[6] * m[5] + mat[10] * m[6] + mat[14] * m[7];

ret[7] = mat[3] * m[4] + mat[7] * m[5] + mat[11] * m[6] + mat[15] * m[7];

ret[8] = mat[0] * m[8] + mat[4] * m[9] + mat[8] * m[10] + mat[12] * m[11];

ret[9] = mat[1] * m[8] + mat[5] * m[9] + mat[9] * m[10] + mat[13] * m[11];

ret[10] = mat[2] * m[8] + mat[6] * m[9] + mat[10] * m[10] + mat[14] * m[11];

ret[11] = mat[3] * m[8] + mat[7] * m[9] + mat[11] * m[10] + mat[15] * m[11];

ret[12] = mat[0] * m[12] + mat[4] * m[13] + mat[8] * m[14] + mat[12] * m[15];

ret[13] = mat[1] * m[12] + mat[5] * m[13] + mat[9] * m[14] + mat[13] * m[15];

ret[14] = mat[2] * m[12] + mat[6] * m[13] + mat[10] * m[14] + mat[14] * m[15];

ret[15] = mat[3] * m[12] + mat[7] * m[13] + mat[11] * m[14] + mat[15] * m[15];

return ret;

}

For example, he has also calculated the texture matrix for the shader( When using shader ), But he has not transposed the results:

void CProjector :: SetupTexMatrix()

{

float offset[16] = { 0.5f, 0, 0, 0,

0, 0.5f, 0, 0,

0, 0, 0, 0,

0.5f,0.5f, 0.0f, 1 };

mat4 m_offset = mat4(offset);

mat4 m_result = m_offset * mat4_projection * mat4_view;

glMatrixMode(GL_TEXTURE);

glLoadMatrixf((GLfloat*) &m_result);

glMatrixMode(GL_MODELVIEW);

}

You can find his source code here:

http://glbook.gamedev.net/moglgp/code.asp

Download chapter 5 and search for projective texturing.

###
#8
Members - Reputation: **157**

Posted 28 March 2010 - 03:44 AM

I'm agree with haegarr.

Again I add the comments of the author :

Also note that the product of the projection matrices is transposed before being passed to glTexGenv(). This is necessary because all three matrices store data in column major order. After transposing, the result is in row major order which is what glTexGenfv() expects.

...

I don't agree with the author. I guess It's about passing the correct equation to glTexGen(), It's not about the DirectX or OpenGL matrices.

Again I add the comments of the author :

Also note that the product of the projection matrices is transposed before being passed to glTexGenv(). This is necessary because all three matrices store data in column major order. After transposing, the result is in row major order which is what glTexGenfv() expects.

...

I don't agree with the author. I guess It's about passing the correct equation to glTexGen(), It's not about the DirectX or OpenGL matrices.

###
#9
Members - Reputation: **157**

Posted 28 March 2010 - 03:53 AM

Quote:

This means that you need not re-order to use that for either D3D or OpenGL (but you need to do so for e.g. Collada!)

Yes, I don't know why Collada has used that ordering. I also transposed the bind shape matrices because Collada stores the transpose of the standard row major ordering! (For example regarding the Collada spec, translation elements are stored in m[3], m[7] and m[11] !

I remember that someone reported the bug to Collada forum, but we still see the same rule in Collada specification.

###
#10
Crossbones+ - Reputation: **4437**

Posted 28 March 2010 - 04:43 AM

Quote:I'm not sure whether that is really a bug. In fact, you have 2 binary criteria (if neglecting esoteric ones), allowing for 4 combinations:

Original post by ehsan2004

Yes, I don't know why Collada has used that ordering. I also transposed the bind shape matrices because Collada stores the transpose of the standard row major ordering! (For example regarding the Collada spec, translation elements are stored in m[3], m[7] and m[11] !

I remember that someone reported the bug to Collada forum, but we still see the same rule in Collada specification.

row vectors and row major order

row vectors and column major order

column vectors and row major order

column vectors and column major order

No one is really superior w.r.t. the others. OpenGL has picked one, D3D another, and Collada a third.