Programming and Graphics

(Kiana) #1

278 Introduction to C++ Programming and Graphics


/*---------- resize ---------------------*/

void resize(int w, int h)
{

//--- Prevent dividing by zero:

if(h==0) h=1;

float ratio = 1.0*w/h;

//--- Reset the coordinate system before modifying

glMatrixMode(GLPROJECTION);
glLoadIdentity();

//--- Viewport is the entire window:

glViewport(0, 0, w, h);

//--- Set the correct perspective.

gluPerspective(45,ratio,1,1000);
glMatrixMode(GLMODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,0.0,0.0,-1.0,0.0,1.0,0.0);

}

/*---------- quit ---------------------*/

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

Figure 8.2.2. shows the images of a Pac-Man in two windows. The first image
is drawn in the primary window generated by the code, while the second image
is drawn after the window has been resized using the window handles. The
resizefunction involves a sequence of carefully designed OpenGL calls.

Free download pdf