Jump to content
  • Advertisement
Sign in to follow this  
bela

OpenGL row/column-major confusion

This topic is 3563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! According to the documentation, Direct3D uses row major matrices, while OpenGL uses column major ones. I did the following test (a rotation of 90 degrees around axis X):
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(90, 1, 0, 0);
GLfloat mtxGL[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mtxGL);

Checked the result in debugger:
mtxGL:
		[0]	1.0000000	float
		[1]	0.00000000	float
		[2]	0.00000000	float
		[3]	0.00000000	float
		[4]	0.00000000	float
		[5]	1.2167964e-008	float
		[6]	1.0000000	float
		[7]	0.00000000	float
		[8]	0.00000000	float
		[9]	-1.0000000	float
		[10]	1.2167964e-008	float
		[11]	0.00000000	float
		[12]	0.00000000	float
		[13]	0.00000000	float
		[14]	0.00000000	float
		[15]	1.0000000	float

D3DXMATRIX mtxD3D;
D3DXMatrixRotationX(&mtxD3D, 90/180.0f*3.1415f);

Checked the result in debugger:
(float*)&mtxD3D,16:
		[0]	1.0000000	float
		[1]	0.00000000	float
		[2]	0.00000000	float
		[3]	0.00000000	float
		[4]	0.00000000	float
		[5]	4.6328703e-005	float
		[6]	1.0000000	float
		[7]	0.00000000	float
		[8]	0.00000000	float
		[9]	-1.0000000	float
		[10]	4.6328703e-005	float
		[11]	0.00000000	float
		[12]	0.00000000	float
		[13]	0.00000000	float
		[14]	0.00000000	float
		[15]	1.0000000	float

So they seem to be stored in the same way. I checked translation too, and found the same result. I expected the D3D and OpenGL matrices to be the transposes of each other, as D3D is row major and OpenGL is column major. I know that one has to transpose matrices before passing between the two APIs, so where is the catch? :) Thanks

Share this post


Link to post
Share on other sites
Advertisement
OpenGL uses column vectors as well as column major order for matrices, while D3D uses row vectors as well as row major order for matrices. Both conventions together means in fact that the memory layout of matrices is the same for openGL as well as D3D. But notice that this comes from the convention how the 2D matrix is linearized to be stored in computer memory.

Mathematically a matrix is "still" 2D, and the transpose operation has actually be performed to convert matrices/vectors between OpenGL and D3D.

Share this post


Link to post
Share on other sites
Thanks for your reply!

I'm trying hard but still don't get it. Given that the memory layout is the same, I don't see why one would have to transpose the matrices.
Could you please describe it in a bit more detail or point me to a paper that contains an explanation?

Thanks in advance,
bela

Share this post


Link to post
Share on other sites
Quote:
Original post by bela
Thanks for your reply!

I'm trying hard but still don't get it. Given that the memory layout is the same, I don't see why one would have to transpose the matrices.
Could you please describe it in a bit more detail or point me to a paper that contains an explanation?

Thanks in advance,
bela
The first step is to understand that there are two issues involved - matrix 'majorness' and vector notation convention - and they are orthogonal (that is, they are fully independent of each other).

The issue of vector notation convention is purely mathematical (that is, we can discuss it without any reference to or concern with the details of how a computer works). The choice to be made here is whether vectors are represented using column matrices (i.e. 'column vectors'), or using row matrices (i.e. 'row vectors').

Matrix-vector multiplication using row vectors looks like this:
[ x' y' ] = [ x y ][ a b ]
[ c d ]
While with column vectors it looks like this:
[ x' ] = [ a b ][ x ]
[ y' ] [ c d ][ y ]
This follows naturally from the definition of matrix multiplication (and has some additional implications involving the order in which transform matrices must be multiplied to achieve a given effect).

Now we can turn our attention to 'matrix majorness'. This really has nothing to do with math; it's purely a programming-related issue.

Basically, when presented with the problem of storing a matrix in memory, we have to decide whether to store it by rows, or by columns. If we store it by rows, we get something like this (the numbers indicate the location of the element in memory relative to the beginning of the memory block in which the matrix is stored):
[ 0 1 2 ]
[ 3 4 5 ]
[ 6 7 8 ]
Or, we can store by column, like this:
[ 0 3 6 ]
[ 1 4 7 ]
[ 2 5 8 ]
One other thing we have to factor in (I should have mentioned this earlier) is that transform matrices must be built differently, depending on whether they are intended to be used with row vectors or with column vectors. For example, a translation matrix that will be used with row vectors looks like this:
[ 1 0 0 0 ]
[ 0 1 0 0 ]
[ 0 0 1 0 ]
[ x y z 1 ]
While a translation matrix that will be used with column vectors looks like this:
[ 1 0 0 x ]
[ 0 1 0 y ]
[ 0 0 1 z ]
[ 0 0 0 1 ]
(I won't go into the 'why' of this, but please ask if it's not clear.)

So now we have two choices to make: matrix majorness, and vector notation convention. The two choices are (strictly speaking - we're not concerning ourselves here with practical issues such as vectorization and so forth) orthogonal; that is, any of the four combinations:
row major, row vectors
row major, column vectors
column major, row vectors
column major, column vectors
Is valid.

Now we can finally bring it all home. There are four possible combinations here, but we'll only concern ourselves with numbers 1 and 4, above, as these are the conventions used by DirectX and OpenGL, respectively. (There's probably some nuance here regarding whether OpenGL 'really' specifies a notational convention, but most OpenGL references use column vectors, which is good enough for our purposes.)

So, let's write out our translation matrix again, first row major with row vectors, and then column major with column vectors (on the left will be the matrix, and on the right will be the layout in memory):
[ 1 0 0 0 ]    [  0  1  2  3  ]
[ 0 1 0 0 ] [ 4 5 6 7 ]
[ 0 0 1 0 ] [ 8 9 10 11 ]
[ x y z 1 ] [ 12 13 14 15 ]

[ 1 0 0 x ] [ 0 4 8 12 ]
[ 0 1 0 y ] [ 1 5 9 13 ]
[ 0 0 1 z ] [ 2 6 10 14 ]
[ 0 0 0 1 ] [ 3 7 11 15 ]
As you can see, in each case the same location in memory corresponds to the same transform element, and this is why it is not necessary (in practice) to transpose a matrix when going from one API to another. All the API cares about is that the transform is laid out in memory in a certain way (specifically, that the elements of the basis vectors are contiguous).

Now, one might very well say that OpenGL and DirectX matrices are transposes of each other, but if so they would be speaking mathematically, not in terms of programming. If you were to write out a 'DirectX matrix', you would need to transpose it in order for it to 'look like' an OpenGL matrix, but this is a notation issue only; as far as the computer is concerned, they look the same.

This is all a bit confusing (and this took a long time to type!), so please post back if you have any further questions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!