PDA

View Full Version : Keyframe animation

04-24-2003, 12:31 PM
Hi, I need to learn about keyframe animation and linearly interpolating between keyframes, but I've been unable to find any tutorials or examples around the net.

Can anyone point me to a good example of keyframe animations (source code, obviously)?

Thanks.

Perspective
04-24-2003, 01:19 PM
http://www.gametutorials.com/Tutorials/opengl/OpenGL_Pg5.htm

04-24-2003, 07:26 PM
Thanks for the link. Here's another question:

I have a text file which my program uses to build a table (there are 10 pieces of the table, and the file contains tranlation and scaling information for its corresponding glut cube.) I need to animate the legs, which are made of 2 pieces with a joint in the middle (like the knee of a leg.) Basically, I want to rotate the upper leg around the joint it makes with the table top (that works fine), but then I want to rotate the lower leg around the joint it makes with the upper leg, but in the opposite direction.

How do I get the location of the "knee" joint after a rotation has been applied to it? I can get the original locations from the text file, but when the leg rotates, the location of the joint obviously changes.

Thanks.

nemesis
04-25-2003, 02:45 AM
Apply the rotation to the "knee" joint coordinates as well. This is:

If the rotation center is not (0,0,0), which probably isn't, you will have to translate the local system to the center of the scene, apply the rotation, and translate it back to its original position. Then you will get the coordinates of the rotated "knee".

Er... was that what you were asking? Ô_ô

04-25-2003, 06:13 AM
You may very well be giving me the right information, but let me just clarify (for both of us) what I mean to make sure of it. I have a leg made up of two parts. The upper leg (the thigh of a real leg) rotates at an angle 'a' (through an animation, not all at once) around the joint it makes with the table top, and conversely, the lower leg (the calf of a real leg) rotates at an angle -a around the joint it makes with the upper leg.

Basically, I think I need to make the joint point (in other words, the bottom middle point on the upper leg) the 0,0,0 point for which to rotate the lower leg around.

Does this make sense (I'm very beginner at this)? Are you suggesting that I move the entire scene such that the knee joint is at 0,0,0 in the coordinate system, rotate the lower leg, and then translate back? That makes sense, and it's what I've been trying to do, however, as the upper leg rotates, I lose the new coordinates of the knee joint. I can easily rotate the lower leg around where the knee was originally.

Thanks a ton for your help.

nexusone
04-25-2003, 06:29 AM
You can use the glPush/pop to creat joints for motion.

//Object
glPushMatrix(); // Save matrix 1
glTranslate // Location of object
glRotate // Rotation of object
Draw_base();
//Build first arm
glPushMatrix() // Save matrix 2
glTranslate //Location of arm in relation to base object.
glRotate //Rotation of arm in relation to base object.
Draw_arm_section();
// Draw joint for next section of arm
glPushMatrix(); // Save matrix 3
glTranslate // Location of second section relative to first arm section.
glRotate // Rotate joint select axis of motion relative to first section
Draw_arm_section();
glPopMatrix();
glPopMatrix();
//Here we would start with another arm

glPopMatix(); // Restore matrix to before drawing this object.

A good example is my clock demo on my website, www.angelfire.com/linux/nexusone/ (http://www.angelfire.com/linux/nexusone/)

Originally posted by NeoBender:
You may very well be giving me the right information, but let me just clarify (for both of us) what I mean to make sure of it. I have a leg made up of two parts. The upper leg (the thigh of a real leg) rotates at an angle 'a' (through an animation, not all at once) around the joint it makes with the table top, and conversely, the lower leg (the calf of a real leg) rotates at an angle -a around the joint it makes with the upper leg.

Basically, I think I need to make the joint point (in other words, the bottom middle point on the upper leg) the 0,0,0 point for which to rotate the lower leg around.

Does this make sense (I'm very beginner at this)? Are you suggesting that I move the entire scene such that the knee joint is at 0,0,0 in the coordinate system, rotate the lower leg, and then translate back? That makes sense, and it's what I've been trying to do, however, as the upper leg rotates, I lose the new coordinates of the knee joint. I can easily rotate the lower leg around where the knee was originally.

Thanks a ton for your help.

[This message has been edited by nexusone (edited 04-25-2003).]

04-25-2003, 08:26 AM
Unfortunately, that doesn't work. If I rotate the upper leg, then the lower leg rotates with it, but then rotating the lower leg skews it terribly.

04-25-2003, 09:42 AM
Let me explain better. When I click the button to have the rotation, the following happens to the top leg:

glPushMatrix();
glTranslatef(//whatever the file says)
glScalef(//whatever the file says)
...
glTranslatef(0.0,0.0,0.5); //trans to joint
glRotatef(45.0,0.0,1.0,0.0); //rotate about joint
glTranslatef(0.0,0.0,-0.5); //trans back
...
glutSolidCube(...);

Now, how would I make an opposite rotation to the bottom leg? Just putting -90 for the rotation angle doesn't work.

nexusone
04-25-2003, 10:23 AM
Did you understand my code example, also did you look at my clock example of push/pop useage?

Also remember rotations and translations work in reverse order....

glTranslate
glRotate

is not the same as

glRotate
glTranslate

The order in which you do things is very important!!!!

Originally posted by NeoBender:
Let me explain better. When I click the button to have the rotation, the following happens to the top leg:

glPushMatrix();
glTranslatef(//whatever the file says)
glScalef(//whatever the file says)
...
glTranslatef(0.0,0.0,0.5); //trans to joint
glRotatef(45.0,0.0,1.0,0.0); //rotate about joint
glTranslatef(0.0,0.0,-0.5); //trans back
...
glutSolidCube(...);

Now, how would I make an opposite rotation to the bottom leg? Just putting -90 for the rotation angle doesn't work.

[This message has been edited by nexusone (edited 04-25-2003).]

04-25-2003, 11:07 AM
I can draw the table just fine using the technique you described. But when I start the rotations (by clicking a piece), it messes up.

So, bascially, if I rotate the upper leg 45 degrees, then the lower leg goes with it 45 degrees, and changes from:

|
|

to

\
\

Then, if I apply a rotation directly to the lower leg, it doesn't rotate correctly. I need it to look like:

\
/

04-25-2003, 11:12 AM
That middle one should look like:

\
\

nexusone
04-25-2003, 11:44 AM
If you follow my example code, it should work given you put in the correct rotations for each segment.

Giving each arm segment is draw with an origin of 0,0,0 kept in mind.

[] body
\ Arm section one
\ Arm section two

Remember we keep each object relative to it parent.

Body = Arm_1
Arm_1 = Arm_2

A drawing would make it clear.

back to my code:

//Object
glPushMatrix(); // Save matrix 1
glTranslate // Location of object
glRotate // Rotation of object
Draw_base(); // From this base matrix state we build all arm's and legs.
//Build first arm
glPushMatrix() // Save matrix 2
glTranslate //Location of arm in relation to base object.
glRotate //Rotation of arm in relation to base object now if we did a 45 degree here it also effects out child arm.
Draw_arm_section();
// Draw joint for next section of arm
glPushMatrix(); // Save matrix 3
glTranslate // Location of second section relative to first arm section.
glRotate // Rotate joint select axis of motion relative to first section
Draw_arm_section();
glPopMatrix(); // end second arm
glPopMatrix(); // end first arm
//Here we would start with another arm since the matrix is back relative to the base matrix

glPopMatix(); // Restore matrix to before drawing this object.

If I get a chance will try to write an example.

Originally posted by NeoBender:
I can draw the table just fine using the technique you described. But when I start the rotations (by clicking a piece), it messes up.

So, bascially, if I rotate the upper leg 45 degrees, then the lower leg goes with it 45 degrees, and changes from:

|
|

to

\
\

Then, if I apply a rotation directly to the lower leg, it doesn't rotate correctly. I need it to look like:

\
/

[This message has been edited by nexusone (edited 04-25-2003).]

04-25-2003, 06:16 PM
Your sample isn't needed anymore sir. I got it working nicely, and it's all thanks to you. I was making a really dumb error that I just didn't notice.

Thanks again for all your help and suggestions. I'll name my first born after you.