How do I make animations in C ++

Download the Visual Studio project

Our first animation is a spinning Archimedean spiral. If you fix the center of the rotating spiral for a while and then look at another object, a post-motion effect occurs.

For the implementation, the example uses the spiral class CSpiral, which manages its own rotation and can also draw itself independently.

The following attributes can be found in the spiral.h file.


They are initialized in the constructor as usual. The attributes are protected or declared. This means that we cannot access these elements from the outside. However, these elements can be accessed normally within the functions of the class. The two public functions

rotate (degrees);
render ();

also do this extensively. Protected attributes can therefore be changed indirectly by calling up publicly accessible () functions. Let's take a look at the implementation of the function in the spiral.cpp file.

CSpiral :: rotate (degrees)
m_rotation + = degrees;

It is called in the function of the main.cpp file to move our animation one frame at a time. In our case, the variable is protected by its status, for example, from being implemented in any way.

Rotation and drawing of the spiral take place in the function. First, let's take a look at the rotation:

CSpiral :: render ()
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();

glRotatef (m_rotation, 0.0f, 0.0f, 1.0f);

As indicated in the last example, there are several matrices in OpenGL. The modelview matrix contains geometric transformations such as rotation, scaling and translation that are applied to each vector to be drawn. So this is the right starting point to let our spiral, i.e. the entire geometry, rotate.

With we switch OpenGL to a mode that allows the model view matrix to be changed. Then we save the previous matrix on the stack. At the end of the render function, we fetch this saved matrix from the stack again:

glPopMatrix ();


So we're undoing any changes to the matrix that happened between push and pop. In our case, this change is the rotation of the spiral.

With the help of the command we can rotate the geometry around any axis. The center of the rotation is always the origin. In our example we rotate the spiral by degrees around the positive z-axis (0, 0, 1).

The functions or can be used to scale or move geometries.

The actual drawing of the Archimedean spiral takes place in the function. First we choose a line width of 5 pixels and start a line.

glLineWidth (5);

glBegin (GL_LINE_STRIP);

Then we calculate and set the points of the line using a for loop.

(i = 0; i     {
fader = (i) / (m_steps-1);

angle = fader * 60.0f * PI;
radius = fader * m_radius;

x = cosf (angle) * radius;
y = sinf (angle) * radius;
glVertex2f (x, y);

Whoever still remembers renderCircle () from the last example: Here we are basically drawing circular paths with an increasing radius.

First we scale the value of between zero and one. Then we use this to run an angle from 0 to 60 * PI. One revolution of a circle corresponds to 2 * PI. So the angle makes thirty complete revolutions when going through the for loop.

We let the radius by which we remove our points from the origin increase linearly from zero to. The (x, y) coordinates then result from the circular functions sine and cosine, scaled by the corresponding radius.

After processing the loop that sets our points, we finish our line

glEnd ();

The main.cpp file differs little from the one in the previous example. The differences are:

  • There is a global variable that can be accessed by all functions in main.cpp.
  • In the function, the animation of the spiral is controlled via its function, so our flip book is moved one step further at a time.
  • The spiral is drawn there by calling the function.
  • At the end of the function, another drawing is initiated with the help of the command. In this way, at the next opportunity, GLUT jumps back into our function to draw the next picture.

Independent programming

  • What is the difference between a rotation followed by a translation versus a translation followed by a rotation? Program both variants using different sequences of and and interpret the result.
  • As mentioned above, the rotation around an axis always takes place around the origin. What combination of shifts and rotations can be used to move the center of rotation to any point within the object?
  • Program a logarithmic spiral. With the logarithmic spiral, the radius does not grow linearly, but exponentially according to the formula radius = b * ea * fader with two constants a and b. ex can be calculated by the function.
  • One question: what happens if more than sixty frames per second are displayed? How could one keep the speed of the spiral constant under these circumstances? Later more.