Row Vector, Column Major Matrix?
Members - Reputation: 217
Posted 05 August 2012 - 08:01 PM
b = a * M;
From what I can tell, storing the matrix elements in column major ordering would be best for this method as it would improve the linearity of memory access.
My current matrix is row major, as that is natural in C, so how do I best go about converting it to column major? Am I on the right track in moving over to column major for row vectors?
My current matrix elements:
// m[ row ][ col ]
T m[ 4 ][ 4 ];
I suppose I have to give up the m[ i ][ j ] syntax, as m[ j ][ i ] is non-standard and would confuse people.
Thanks as usual!
Members - Reputation: 5796
Posted 05 August 2012 - 08:45 PM
Edited by alvaro, 05 August 2012 - 08:47 PM.
Members - Reputation: 287
Posted 05 August 2012 - 10:34 PM
OpenGL performs column-vector multiplications:
M * v
DirectX performs row-vector multiplications:
v * M
OpenGL expects column-vector. DirectX expects row-vector. The row-vector equivalent of a column-vector matrix, is the transpose of said matrix.
Let's look at a simple 2x2 matrix that does a 90 degree rotation.
Column-Major x Vector
v1.x = 0 * v.x - 1 * v.y
v1.y = 1 * v.x + 0 * v.y
Vector x Row-Major
v1.x = v.x * 0 - v.y * 1
v1.y = v.x * 1 + v.y * 0
The algebra for the two equations is the same. Now this is the part where we start getting into confusing territory.
OpenGL expects the data in the matrices you give it to be ordered column-majorly, like so:
0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15
DirectX expects the matrices you give it to be ordered row-majorly, like so:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Which is the transpose of what we saw above. And as already established, row-vector matrices are the transpose of column-vector matrices and thus a double transposition cancels it out. Same data to both.
So pick a style, it will determine your multiplication order, and stick with it. Cause that style will work with DirectX or OpenGL. All you have to do is make sure you are not feeding it the wrong data. In the examples above. If you're looking for the translation component for a frame of reference, it's 11,12,13
Here's a link with that info:
Edited by Kyall, 05 August 2012 - 10:37 PM.