Programming and Graphics

(Kiana) #1

276 Introduction to C++ Programming and Graphics


void disk()
{
glClear(GLCOLORBUFFERBIT|GLDEPTHBUFFERBIT);
glColor3f(0.8,0.5,0.2);

centerx = centerx+Dx;
centery = centery+Dy;

if(centerx+radius > 1.0) Dx=-Dx;
if(centerx-radius < -1.0) Dx=-Dx;
if(centery+radius > 1.0) Dy=-Dy;
if(centery-radius <-1.0) Dy=-Dy;

glBegin(GLTRIANGLEFAN);
glVertex2f(centerx, centery);
for (int i=0; i <= N; i++)
{
float angle = i*Dtheta;
glVertex2f(centerx+cos(angle)*radius,
centery+sin(angle)*radius);
}
glEnd();
}

//--------- quit

void quit(unsigned char key, int x, int y)
{
if(key == ’q’)
{
cout << "Closing window " << endl;
glutDestroyWindow(win);
exit(0);
}
}

A snapshot of the animation is shown in Figure 8.2.1(b).


Rescaling


If we resize the graphics window, the displayed image will be distorted:
a circle will become an ellipse and a person will instantaneously lose or gain
weight. To prevent this, we run theglutRshapeFunccallback in the double
buffer mode.


The following code contained in the filepacman.ccdraws the image of
a Pac-Man whose shape remains unaltered when the window is resized:

Free download pdf