# Board index » delphi » How does OpenGL work with matrices under the hood?

## How does OpenGL work with matrices under the hood?

2004-04-30 11:23:16 AM
delphi249
Hi all, pardon me if this is a silly question (tm)....<G>
when using OpenGL and I want to roate some object (in object coordinates)
and then translate it to some position I do this type of thing:
...
glTranslatef(ObjectX,ObjectY,ObjectZ)
glRotatef(ObjectAngle,0,1,0);
DrawObject.
I am used to doing it this way, but why is it necessary for the translation
to come before the rotation in the code?
Mathematically the rotation should be done before the translation, right?
Does OpenGL do something similar to this in the background? (ignore exact
code, only look at what it is doing)
ModelMatrix: TMatrix3d;
ModelMatrixOperationsIndex: Integer;
ModelMatrixOperationsStack: array[0..MaxModelMatrixOperations - 1] of
TMatrix3d;
begin
ModelMatrixOperationsStack[ModelMatrixOperationsIndex] :=
ArbitaryAxisRotationMatrix(Vector3d(ax,ay,az),Angle);
Inc(ModelMatrixOperationsIndex);
end;
procedure glTranslatef(tx,ty,tz: Single);
begin
ModelMatrixOperationsStack[ModelMatrixOperationsIndex] :=
TranslationMatrix3d(tx,ty,tz);
Inc(ModelMatrixOperationsIndex);
end;
procedure glBegin(...);
begin
while(ModelMatrixOperationsIndex>0)do
begin
Dec(ModelMatrixOperationsIndex);
ModelMatrix :=
MatrixMultiply(ModelMatrix,ModelMatrixOperationsStack[ModelMatrixOperationsI
ndex]);
end;
...
end;
procedure glVertex3f(x,y,z: Single);
begin
NewVertex := TransformVector(Vector3f(x,y,z),ModelMatrix);
...
end;
I am trying to understand why one needs to specify the matrix operations
(rotation, translation, etc) around the other way...
I am itching to know :)
Paul Nicholls (Delphi 5/6 Professional)
"See No Evil, Hear No Evil, EMail No Evil !" - Paul Nicholls
XXXX@XXXXX.COM
Replace '-' with '_' to reply

## Re:How does OpenGL work with matrices under the hood?

##### Quote
I am used to doing it this way, but why is it necessary for the
translation
to come before the rotation in the code?
It is not necessary. The different transformation orders mean different
transformations. No magic here, just imagine it: rotation is always rotation
around the origin. If you rotate first, it rotates the object around its
center (assuming your object center is the origin locally) and then
translates it to a new position. If you translate it first, the rotation
will move it on a larger arc.
As an example if you make an animation and rotate first, your object will
spinning in a fixed place around its center, if you translate first, it will
spin around a different point (determined by the translation amount),
orbiting it.
Regards
Árpád

## Re:How does OpenGL work with matrices under the hood?

"Soós Árpád" <XXXX@XXXXX.COM>writes
##### Quote
>I am used to doing it this way, but why is it necessary for the
translation
>to come before the rotation in the code?

It is not necessary. The different transformation orders mean different
transformations. No magic here, just imagine it: rotation is always
rotation
around the origin. If you rotate first, it rotates the object around its
center (assuming your object center is the origin locally) and then
translates it to a new position. If you translate it first, the rotation
will move it on a larger arc.
As an example if you make an animation and rotate first, your object will
spinning in a fixed place around its center, if you translate first, it
will
spin around a different point (determined by the translation amount),
orbiting it.

Regards

Árpád

I know that matrix transformations are NOT cummulative, it depends on the
order they are done in, as you say. :)
OK, lets talk about spinning an object in a fixed place around it's
centre...
It appears OpenGL wants the translation THEN the rotation instead of what I
would have thought was needed, ie. the rotation THEN the translation...
ie.
glTranslatef(...); translation FIRST
glRotatef(...); rotation SECOND
glRotatef(...); rotation FIRST
glTranslatef(...); translation SECOND
Can you explain why this appears so when doing the code?
Cheers,
Paul.

## Re:How does OpenGL work with matrices under the hood?

Paul Nicholls writes:
##### Quote
Hi all, pardon me if this is a silly question (tm)....<G>

when using OpenGL and I want to roate some object (in object
coordinates) and then translate it to some position I do this type of
thing:

...
glTranslatef(ObjectX,ObjectY,ObjectZ)
glRotatef(ObjectAngle,0,1,0);

DrawObject.

I am used to doing it this way, but why is it necessary for the
translation to come before the rotation in the code?

Mathematically the rotation should be done before the translation,
right?

Depends.
When an object is rotated, it first has to be moved to the origin,
then it is rotated, and afterwards it is moved back.
This because a rotation is always around the origin.
Rene
--
Ing.Buro R.Tschaggelar www.ibrtses.com

## Re:How does OpenGL work with matrices under the hood?

##### Quote
It appears OpenGL wants the translation THEN the rotation instead of what
I
would have thought was needed, ie. the rotation THEN the translation...
Transformations are not executed separately but a single composite
transformation matrix is created using them.
This is calculated by multiplying them. You must specify the individual
transformation matrices in the order of multiplication which is opposite of
the order they are applied.

## Re:How does OpenGL work with matrices under the hood?

"Rene Tschaggelar" <XXXX@XXXXX.COM>writes
##### Quote
Paul Nicholls writes:

>Hi all, pardon me if this is a silly question (tm)....<G>
>
>when using OpenGL and I want to roate some object (in object
>coordinates) and then translate it to some position I do this type of
>thing:
>
>...
>glTranslatef(ObjectX,ObjectY,ObjectZ)
>glRotatef(ObjectAngle,0,1,0);
>
>DrawObject.
>
>I am used to doing it this way, but why is it necessary for the
>translation to come before the rotation in the code?
>
>Mathematically the rotation should be done before the translation,
>right?
>

Depends.
When an object is rotated, it first has to be moved to the origin,
then it is rotated, and afterwards it is moved back.
This because a rotation is always around the origin.

Rene
If the object is already at the origin, how come I have to specify the
translation THEN the rotation when using OpenGL?
Is OpenGL putting the individual matricies on a stack, and then popping them
off and multiplying one at a time with the current model matrix?
This would explain why OpenGL seems to want the transformations specified in
the opposite order...

## Re:How does OpenGL work with matrices under the hood?

"Soós Árpád" <XXXX@XXXXX.COM>writes
##### Quote
>It appears OpenGL wants the translation THEN the rotation instead of
what
I
>would have thought was needed, ie. the rotation THEN the translation...

Transformations are not executed separately but a single composite
transformation matrix is created using them.
This is calculated by multiplying them. You must specify the individual
transformation matrices in the order of multiplication which is opposite
of
the order they are applied.

So I am correct in thinking that OpenGL needs the transformations specified
in the opposite order what they will be applied?

## Re:How does OpenGL work with matrices under the hood?

##### Quote
So I am correct in thinking that OpenGL needs the transformations
specified
in the opposite order what they will be applied?
Yes. See:
www.dcc.unicamp.br/~lmarcos/courses/mc603/redbook/chapter03.html#name2

## Re:How does OpenGL work with matrices under the hood?

"Paul Nicholls" <.>writes news:XXXX@XXXXX.COM...
##### Quote
I am trying to understand why one needs to specify the matrix operations
(rotation, translation, etc) around the other way...
You have kind of answered your own question with the code snippet you
provided us. Indeed, you first translated, then rotated, and *finally* drew
your object. can not you see the pattern here? Things are coded in reverse
order... Think about it... :) Now as to the reason, I must confess I don't
know or can not remember it, but I wouldn't be surprised if it is mentioned
somewhere in the Red Book...
Alan.

## Re:How does OpenGL work with matrices under the hood?

"Paul Nicholls" <.>wrote
##### Quote
Mathematically the rotation should be done before the translation, right?
Remember, though, you are constructing an operator, not applying to the
object.
Let's say A, B and C are operators, V is a vector. What's the new vector if
we apply first operation A, _then_ B, _then_ C?
It's: C * (B * (A* V))
Which means (since our operators are transitive but non-commutative):
(C*B*A)*V
So to construct the combined operator, you multiply them in reverse to the
order that the vector would experience.

## Re:How does OpenGL work with matrices under the hood?

"Soós Árpád" <XXXX@XXXXX.COM>writes
##### Quote
>So I am correct in thinking that OpenGL needs the transformations
specified
>in the opposite order what they will be applied?

Yes. See:

www.dcc.unicamp.br/~lmarcos/courses/mc603/redbook/chapter03.html#name2

Thanks, that was very helpful :-)
Cheers,
Paul.

## Re:How does OpenGL work with matrices under the hood?

"John" <XXXX@XXXXX.COM>writes
##### Quote
"Paul Nicholls" <.>wrote

>Mathematically the rotation should be done before the translation,
right?

Remember, though, you are constructing an operator, not applying to the
object.

Let's say A, B and C are operators, V is a vector. What's the new vector
if
we apply first operation A, _then_ B, _then_ C?

It's: C * (B * (A* V))

Which means (since our operators are transitive but non-commutative):

(C*B*A)*V

So to construct the combined operator, you multiply them in reverse to the
order that the vector would experience.

Ok, I think this makes sense now :-)
Cheers,
Paul.

## Re:How does OpenGL work with matrices under the hood?

"Alan Garny" <XXXX@XXXXX.COM>writes
##### Quote
"Paul Nicholls" <.>writes
>I am trying to understand why one needs to specify the matrix operations
>(rotation, translation, etc) around the other way...

You have kind of answered your own question with the code snippet you
provided us. Indeed, you first translated, then rotated, and *finally*
drew
your object. can not you see the pattern here? Things are coded in reverse
order... Think about it... :) Now as to the reason, I must confess I don't
know or can not remember it, but I wouldn't be surprised if it is mentioned
somewhere in the Red Book...

Alan.

Thanks Alan, after looking at a link that was provided it does indeed say in
the Red Book :-)
Cheers,
Paul.