Sign in to follow this  
jamesleighe

Row Vector, Column Major Matrix?

Recommended Posts

I use row vectors, so multiplication goes like:

Vector a;
Vector b;
Matrix M;

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!

Share this post


Link to post
Share on other sites
C++ is extremely good at hiding implementation details. You can make a matrix class which stores things internally however is most convenient for fast multiplication (if that's an important operation to optimize for in your case) in a manner that makes the user not have to worry about it. Just provide an appropriate operator()(int i, int j) so the user doesn't have to use an unfamiliar index order. Edited by alvaro

Share this post


Link to post
Share on other sites
I'm not sure if I can explain this well, but I'll try:

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:
0 -1
1 0

Row Major
0 1
-1 0

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:
[url="http://www.mindcontrol.org/~hplus/graphics/matrix-layout.html"]http://www.mindcontr...rix-layout.html[/url] Edited by Kyall

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this