/* prac7a.c */ /* Purpose: Investigate viewports and aspect ratios */ #ifdef __APPLE__ #include #else #include #endif #include #include #define KEY_ESC 27 /* glut doesn't define this one for some reason */ void printHelp( void ) { fprintf(stdout, "\nprac7a - Viewports and aspect ratios\n\n" "Escape key - exit the program\n\n"); } GLvoid keyboard( GLubyte key, GLint x, GLint y) { switch (key) { case KEY_ESC: /* exit when escape key is pressed */ exit(0); break; } } GLvoid specialkeys( GLint key, GLint x, GLint y) { switch (key) { case GLUT_KEY_F1: /* print Help information */ printHelp (); break; } } GLvoid checkError( const char* const label ) { GLenum error; error = glGetError(); while ( GL_NO_ERROR != error ) { fprintf( stderr,"%s: %s\n", label, gluErrorString(error) ); error = glGetError(); } } void reshape(GLint width, GLint height) { GLdouble aspect; aspect = (GLdouble)width / (GLdouble)height; /* We still want the output to cover the whole window */ glViewport(0, 0, width, height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); /* Get the viewing volume set up to match the aspect ratio of the viewport */ if (width > height) { glOrtho(-2 * aspect, 2 * aspect, -2, 2, -2, 2); } else { glOrtho(-2, 2, -2 / aspect, 2 / aspect, -2, 2); } glMatrixMode( GL_MODELVIEW ); } GLvoid display( GLvoid ) { glClear( GL_COLOR_BUFFER_BIT ); /* Do all your OpenGL rendering here */ glutWireCube(1.0); checkError( "display" ); glFlush(); } GLvoid init( GLvoid ) { glClearColor( 1.0, 1.0, 1.0, 1.0); glColor3f(1.0,0.0,0.0); glLineWidth(3.0); } int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); glutCreateWindow( argv[0] ); init(); glutKeyboardFunc( keyboard ); glutSpecialFunc( specialkeys ); glutDisplayFunc( display ); /* glutReshapeFunc( reshape ); */ printHelp(); glutMainLoop(); return 0; }