# Why does OpenGL do matrix pre-mult instead of post-mult?

I always wonder why OpenGL does matrix pre-mult instead of post-mult…this just makes the order of matrix operations unintuitive.
I rewrote the equivalent of glTranslate() and glRotate() using post-mult…it works as expected(no need for reverse order of matrix ops). But I realize by doing this that I may inadvertently be bypassing any hardware TnL that may be present, as the matrix multiplication will be handled by the CPU instead of the GPU.
Is there any way to have the best of both worlds? I’ve heard that it may be possible to implement matrix math in vertex shaders running on the GPU…but is this really possible?

One way had to be chosen, actually they should have left it open but such is life.

Anyway its the differenct between global transfromation and relative transformation.

I believe pre multiplication is global, post is relative. This is why rotation under OpenGL is so confusing to beginners, I believe however it offers more possibilities.

Originally posted by MelvinEng:
Is there any way to have the best of both worlds? I’ve heard that it may be possible to implement matrix math in vertex shaders running on the GPU…but is this really possible?

Sure it is. It actually be quite easy to do. The problem is that the vertex programs operate per-vertex so you’d be doing the calculations once for each vertex (duh!). You can pass lots of parameters to a vertex program, including matrices and rotation, translation, scaling amounts. Then you’d do the calcs in the vertex program and apply them to each vertex. You’d have to do some benchmarking to see if this was slower or faster than doing it yourself in software once, then doing a glLoad/MultMatrix (I’m guessing slower). It’d be fun to find out, anyway.

Hope that helps.

Suppose I have the following sequence of matrix ops…

glMatrixMode(GL_MODELVIEW);
glTranslate(…);
<<draw object A…>>
glRotate(…);
<<draw object B…>>

Am I correct in presuming that the effect of the snippet above does the following?

• translation is applied 1st, then A is drawn
• rotation is applied 1st, followed by translation, then B is drawn

If this is so, then this makes it hard to concatenate matrix ops since concatenation is essentially a post-mult op! (this is really such a drag…)

Howdy,

hmm. I appreciate that you have your POV, but I’m still not convinced that pre-multiply has any advantages over post-multiply. Sure, post-multiply SEEMS backwards if you read source code from top to bottom, but I reckon its more intuitive; and I’ll give you MY opinion.

Incidentially, the code you gave will do what you describe.

I belive post multiplication is better because its like a hierarchy of transformations. I can think of countless examples where this is a good thing—but that isn’t to say that you can’t think of your own countless examples where the reverse is true, too.

The classic example is drawing a solar system. You have something like this (in psuedo code):

glTranslate(suntransform);
glDraw(sun);
glTranslate(earthtransform);
glDraw(earth);
glTranslate(moontransform);
glDraw(moon);

By the time it comes to drawing the moon, then its position is automagically defined relative to the earth, whcih in turn is defined relative to the sun. Ie. as soon as the sun moves, then the earth and consequently the moon will likewise move. More recent transformations are more local.

<thinks> I don’t think this argument will convince you, tho’. Hmm. If you did post multiply then you’ll end up with… well, I suppose I’d argue that you’d get weird effects that seem (to me, at least) counter-intuitive.

Hmm. Can you cite an example where you think post multiply would make your life much MUCH easier?

cheers
JOhn