HomeMultimedia Page 3 - Animation in OpenGL for Game Programming using SDL
Animation APIs in OpenGL - Multimedia
Animation is the backbone of immersive experience in any game. This applies to both 2-D and 3-D games without exception. Hence, in order to create a seamless and immersive game environment, one must first understand the whys and wherefores of animation. One of the advantages that OpenGL has over other graphics and gaming toolkits is that in OpenGL, animation APIs are not low-level.
In OpenGL there are two functions that correspond to rotation and translation. They are:
glRotate*()
glTranslate*()
The * indicates that there is more than one form of the function. In the case of the above functions there are two -- one with decimal parameters having d instead of * and the float version having f instead of *. Let's get down to details:
glRotate*() is a function that rotates an object at the angle specified around specified axes. It terms of a matrix it "computes a matrix that performs a counterclockwise rotation of angle degrees about the vector from the origin through the point (x, y, z)." For the time being it is sufficient to understand that the rotation takes place according to the degrees specified for the axis given. The parameters are the angle through which the object has to be rotated and all the axes. The axes around which the rotation is to take place are given a value of 1 and others are given a 0. For example, if an object has to be rotated 10 degrees around the y-axis, then the statement would be
glRotatef(10.0f,0,1,0);
One important point about glRotate*() is that it follows the right-hand coordinate space rule; hence if the axis points towards the user, then the rotation would be counter-clockwise.
glTranslate*() moves the object to the point specified by (x, y, z) passed as parameters, from the current coordinates. In terms of a matrix, "The current matrix is multiplied by this translation matrix, with the product replacing the current matrix." The distance to be moved is either positive or negative according to the parameters passed. For example, to move an object 2 units along the x-axis, the statement would be
glTranslatef(1.0f,0.0f,0.0f);
By setting the value of the x, y or z parameter to the required number of units, the object can be moved that many units parallel to the axis having a value greater or smaller than 0.
Though the above APIs form the basis of animation, they cannot animate an object by themselves. It is here that logic comes into picture. Let's see how to achieve it in the next section.