• Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

# Row Vector, Column Major Matrix?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1James Leighe  Members   -  Reputation: 217

Like
0Likes
Like

Posted 05 August 2012 - 08:01 PM

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!

### #2Álvaro  Members   -  Reputation: 5796

Like
3Likes
Like

Posted 05 August 2012 - 08:45 PM

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, 05 August 2012 - 08:47 PM.

### #3Kyall  Members   -  Reputation: 287

Like
1Likes
Like

Posted 05 August 2012 - 10:34 PM

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:
http://www.mindcontr...rix-layout.html

Edited by Kyall, 05 August 2012 - 10:37 PM.

I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS