Programming and Graphics

(Kiana) #1

8.2 Graphics events 273


Animation


To perform animation, we use two memory spaces holding the graphics,
one called theback bufferand the second called theprimaryoractive buffer.
The computer displays the content of the primary buffer, then the two buffers
are swapped in a process dubbeddouble buffering.


The following code animates the rotation of a circle drawn by a user-
defined function:


#include <math.h>
#include <freeglut.h>

using namespace std;

const float radius = 1.0;
const int N=180;
const float Dtheta = 2*3.1415926/N;
const float radius = 1.0;
float angle=0.0;

void renderscene(void);
void drawCircle();

//--- main:

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUTDEPTH | GLUTDOUBLE | GLUTRGBA);
glutInitWindowPosition(010,020);
glutInitWindowSize(256,256);
glutCreateWindow("GLUT Animation");
glutIdleFunc(renderscene);
glutMainLoop();
return 0;
}

//--- Render the scene:

void renderscene(void)
{
glClear(GLCOLORBUFFERBIT|GLDEPTHBUFFERBIT);
glPushMatrix();
glRotatef(angle,0.0,1.0,0.0);
drawCircle(radius);
glPopMatrix();
glutSwapBuffers();
angle=angle+0.1;
Free download pdf