BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How does OpenGL work with matrices under the hood?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Graphics
View previous topic :: View next topic  
Author Message
Paul Nicholls
Guest





PostPosted: Fri Apr 30, 2004 3:23 am    Post subject: How does OpenGL work with matrices under the hood? Reply with 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:

glLoadIdentity;
....
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;

procedure glRotatef(Angle,ax,ay,az: Single); overload;
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 :)

Thanks in advance,
Paul Nicholls (Delphi 5/6 Professional)
"See No Evil, Hear No Evil, EMail No Evil !" - Paul Nicholls
[email]paul-nicholls (AT) hotmail (DOT) com[/email]

Replace '-' with '_' to reply


Back to top
Paul Nicholls
Guest





PostPosted: Fri Apr 30, 2004 6:37 am    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote



"Soós Árpád" <sarpad (AT) webmail (DOT) hu> wrote

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

instead of

glRotatef(...); rotation FIRST
glTranslatef(...); translation SECOND

Can you explain why this appears so when doing the code?

Cheers,
Paul.




Back to top
Rene Tschaggelar
Guest





PostPosted: Fri Apr 30, 2004 6:39 am    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote



Paul Nicholls wrote:

Quote:
Hi all, pardon me if this is a silly question (tm)....
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:

glLoadIdentity;
...
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 http://www.ibrtses.com
Your newsgroups @ http://www.talkto.net

Back to top
Paul Nicholls
Guest





PostPosted: Fri Apr 30, 2004 7:04 am    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote

"Rene Tschaggelar" <ee123123wqe12 (AT) sd242323 (DOT) com> wrote

Quote:
Paul Nicholls wrote:

Hi all, pardon me if this is a silly question (tm)....
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:

glLoadIdentity;
...
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...



Back to top
Paul Nicholls
Guest





PostPosted: Fri Apr 30, 2004 7:05 am    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote

"Soós Árpád" <sarpad (AT) webmail (DOT) hu> wrote

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?



Back to top
Alan Garny
Guest





PostPosted: Fri Apr 30, 2004 9:06 am    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote

"Paul Nicholls" <.> wrote

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't you see the pattern here? Things are coded in reverse
order... Think about it... Smile Now as to the reason, I must confess I don't
know or can't remember it, but I wouldn't be surprised if it is mentioned
somewhere in the Red Book...

Alan.



Back to top
John
Guest





PostPosted: Fri Apr 30, 2004 10:49 am    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote

"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.



Back to top
Paul Nicholls
Guest





PostPosted: Sun May 02, 2004 11:04 pm    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote

"Soós Árpád" <sarpad (AT) webmail (DOT) hu> wrote

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

Yes. See:


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



Thanks, that was very helpful :-)

Cheers,
Paul.



Back to top
Paul Nicholls
Guest





PostPosted: Sun May 02, 2004 11:05 pm    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote

"John" <John (AT) home (DOT) com> wrote

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.



Back to top
Paul Nicholls
Guest





PostPosted: Sun May 02, 2004 11:06 pm    Post subject: Re: How does OpenGL work with matrices under the hood? Reply with quote

"Alan Garny" <someone (AT) somewhere (DOT) com> wrote

Quote:
"Paul Nicholls" <.> wrote

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't you see the pattern here? Things are coded in reverse
order... Think about it... Smile Now as to the reason, I must confess I don't
know or can't 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.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Graphics All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.