- We are going to continue showcasing some of the
features of OpenGL, and in this segment
we are going to talk about animation.
First, let me show you a demo of the program.
In this case, I will set the DEMO is equal to 3.
We now can see that we have a teapot,
and if I hit the P key, the teapot animates.
While it is animating, I can zoom in and zoom out
as before, and I can also stop and restart the animation.
Let's look at the code for this purpose.
We want to put a teapot in the middle that animates.
The first line of code is pushMatrix of modelview.
Then you multiply the modelview matrix on the right,
as in old OpenGL, by a translation corresponding
to the teapotloc in the X coordinates,
no translation in Y and Z.
That's what causes the teapot to animate
from the left to the right.
We'll just need to increase the teapotloc variable.
Notice again that this translation command
where the original matrix input is the identity,
that ensures that glm::translate
returns a translation matrix.
Alright, so the next two transforms setup and center the teapot.
And again, the transforms right multiply
the modelview matrix, which is the top of the stack.
So these transforms, which are multiplied by translation,
by Z is equal to 0.1, just set up and center the teapot.
There's also rotations so that the teapot appears
in the correct orientation.
Notice that all of these transforms are right-multiplying
the top of the stack, so in fact the last transform in code
will be the first transform applied.
So glm::scale sets the scale for the teapot appropriately,
then you rotate the teapot into the correct location,
then you translate it so it appears on the table,
and finally you do what we're interested in,
which is the animation part of it,
which is translation by teapotloc.
Notice here the glUniformMatrix4fv command,
which sets modelviewPos to the correct value
for the modelview matrix, and this a uniform matrix,
and once you've done all this, you pop the modelview matrix
from the stack so the rest of the geometry is not affected
by what you've been doing for the teapot.
And this a very common idiom, that a particular object
is defined between push and pops for the modelview matrix.
We did the same thing for each of the four pillars in drawing.
Here is my animation routine.
So the void animation command simply states that
teapotloc is equal to teapotloc plus 0.005.
That means at each step, it slightly increments
the location of the teapot, which is reported
for the animation and if the teapot goes off the right edge,
you simply bring it back to the left edge.
glutPostRedisplay() just redisplays the scene
and tells OpenGL to call the display function.
drawteapot() in geometry.h
binds the vertex array for the teapot,
draws the elements corresponding to the teapot element
in the scenes and unbinds the array.
The keyboard callback is where p pauses
and restarts the animation.
Notice that I have this animate variable,
which is originally zero,
and then I can toggle it to 1 and back to 0.
In the keyboard, I have escape to quit as before,
but now I added a new case for p
to pause and restart the animation.
It toggles the animate variable; and notice this code.
If the animate variable is on,
it sets glutIdleFunct to the animation.
So even if no keys or nothing has been pressed,
glut will idly call the animation function,
which will move the teapot.
If animate is off, then the glutIdleFunc() is null.
It does nothing.