OpenGL Program for Simple Animation
OpenGL Program for Simple Animation
(Revolution) in C
OpenGL is a cross-language, cross-platform API for rendering 2D and 3D Vector
Graphics. Using this, we can make a lot of design as well as animations. Below is
the simple animation made using OpenGL.
Approach :
To make a picture moving, we need to understand the working procedure of a
function used to display i.e glClear(GL_COLOR_BUFFER_BIT). Its task is to
clear screen with default value after a certain time (normally, after 1/30 sec or 1/60
sec). So, if any change of coordinate happens, then it will appear to be moving as
human eye can distinguish image only which is separated by 1/16 second
(persistence of vision).
Now, the coordinates of circle are X = r*cos(?) and Y = r*sin(?) or for ellipse X =
rx*cos(?) and Y = ry*cos(?) where rx and ry are radius in X- and Y- direction
and ? is the angle.
If we vary ? from 0 to 2*pi (360 degree) at very small increase (say of 1 degree) and
draw point on that coordinate, we can make a complete circle or ellipse. We can also
make semi-circle or any arc of circle or ellipse by varying the starting and ending
value of ? (angle).
These concepts are used to draw the following Animation:
// C Program to illustrate
// OpenGL animation for revolution
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
// global declaration
int x, y;
float i, j;
// Initialization function
void myInit (void)
{
// Reset background color with black (since all three argument is 0.0)
glClearColor(0.0, 0.0, 0.0, 1.0);
// Driver Program
int main (int argc, char** argv)
{
glutInit(&argc, argv);
// Display mode which is of RGB (Red Green Blue) type
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Name to window
glutCreateWindow("Revolution");
// Call to myInit()
myInit();
glutDisplayFunc(display);
glutMainLoop();
}