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 vectorsrow major, column vectorscolumn major, row vectorscolumn 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.