# Could someone please explain post/pre multiplication & column/row major matrices?

Here’s what I’m guessing: post/pre multiplication refers to which matrix is multiplied by which since AB wouldn’t necessarily result in the same matrix as BA. Is that right? And when I call glMultMatrixd(M), with my matrix mode in modelview, is my modelview matrix multiplied by M or is M multiplied by my modelview? Also, could someone explain column/row major matrices? Thanks in advance.

Hi,

you’re right: matrix operations are NOT commutative (ie. AB != BA for all A, B). OpenGL matrix operations are post-multiplied. For example,

``````glLoadIdentity();
glRotatef(...);
glTranslatef(...);
``````

results in IRT. This means vertices are first translated and THEN rotated, since

v’ = RTv == R(Tv)

Column major and row major describe how a matrix is ‘unpacked’ into a vector. If your matrix is

``````1 2
3 4
``````

then the column major ordering of this matrix is the vector [1 3 2 4], whereas the row major ordering is the vector [1 2 3 4]. The ‘major’ component describes, if you will, the ‘outer loop’ of the packing. Consequently, in column major, we iterate over the columns first and THEN each row in each column (hence, we visit column [1 3] first…)

make sense?

cheers
John

Yeah makes some sense. I don’t see how things could be translated then rotated if glRotatef was called first, but I don’t think that affects me anyway. I’m gonna be doing all my translation and rotation in one matrix then calling glMultMatrix. Thanks for the explanations.

Originally posted by K A Z E:
I don’t see how things could be translated then rotated if glRotatef was called first…
Neither do I.

But that depends on how you look at the transformations. I always tend to look at them exactly the other way round than the rest of the world, and by some coincidence this is the same as the order of operations in OpenGL

Hello,

the reason why the translation is ‘called’ first, if you like, is because OpenGL post multiplies the matrix stack. Here’s a simple example using translation and scalking. Suppose the two orderings are this:

``````glScalef(2, 2, 2);
glTranslatef(1, 1, 1);
``````

and

``````glTranslatef(1, 1, 1);
glScalef(2, 2, 2);
``````

where I assume the modelview matrix is the identify beforehand. Now, my assertion is that in the first instance, points will be translated and THEN scaled, and in the second instance, they’ll be scaled and THEn translated.

Working through the maths for the first case. The modelview matrix will become IST, ie

``````[ 1 0 0 0 ] [ 2 0 0 0 ] [ 1 0 0 1 ]
[ 0 1 0 0 ] [ 0 2 0 0 ] [ 0 1 0 1 ]
[ 0 0 1 0 ] [ 0 0 2 0 ] [ 0 0 1 1 ]
[ 0 0 0 1 ] [ 0 0 0 1 ] [ 0 0 0 1 ]
``````

this results in the matrix

``````     2     0     0     2
0     2     0     2
0     0     2     2
0     0     0     1
``````

for argument’s sake, if we post multiply this matrix by the vector [0 0 0 1] (ie. a point at the origin), we get

[ 2 2 2 1 ], or the point (2, 2, 2)

similarly, in the second case, the modelview matrix is ITS, and thus

``````     2     0     0     1
0     2     0     1
0     0     2     1
0     0     0     1
``````

yields the point (1, 1, 1) from (0, 0, 0).

Clearly, then, in the first case the point is translated to (1, 1, 1) first, and THEN scaled by 2 to end up at (2, 2, 2). Whereas, in the second case, the point (0, 0, 0) is scaled by 2 (to give the same point) and THEN translated to (1, 1, 1).

Why is this?

In the first case, the transformation is given by

S * T * v

where v is the [0 0 0 1] vector. Hence,

S * T * v
= S * (T * v)
= S * [ 1 1 1 1]’
= [ 2 2 2 1 ]’

that is, since the matrix is post multiplied on the stack, the FIRST matrix to affect the vertex is the LAST matrix described.

does that make sense?

cheers,
John