/* demoDisplayList.c : */ /* Purpose: construct a box and the letter O using Display lists. */ #ifdef __APPLE__ #include #else #include #endif #include #include #include #define KEY_ESC 27 /* glut doesn't define this one */ #define PI 3.14159265 GLuint BOX; GLuint OSHAPE; void printHelp( void ) { fprintf(stdout, "Display List Demo\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; /* update the viewport */ glViewport(0,0,width,height); /* reset viewing volume */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* match VV with aspect ratio of Viewport */ if (width > height) { glOrtho(-4*aspect, 4*aspect, -4, 4, -2, 2); } else { glOrtho(-4, 4, -4/aspect, 4/aspect, -2, 2); } glMatrixMode(GL_MODELVIEW); } GLvoid createBoxList( GLvoid ) { BOX = glGenLists(1); glNewList(BOX, GL_COMPILE); glBegin( GL_POLYGON ); glColor3f(1.0,0.0,0.0); glVertex2f(-1.0,-1.0); glVertex2f(1.0,-1.0); glVertex2f(1.0,1.0); glVertex2f(-1.0,1.0); glEnd(); glEndList(); } GLvoid createOshapeList( GLvoid ) { OSHAPE = glGenLists(1); glNewList(OSHAPE, GL_COMPILE); { float angle; int i; glBegin( GL_QUAD_STRIP ); for (i=0; i<=12; i++) /* 12 vertices around our letter O */ { angle = PI / 6.0 * i; /* 30 degrees in radians */ glVertex2f(0.4*cos(angle), 0.4*sin(angle) ); glVertex2f(0.5*cos(angle), 0.5*sin(angle) ); } glEnd(); } glEndList(); } GLvoid display( GLvoid ) { /* Do all your OpenGL rendering here */ glClear( GL_COLOR_BUFFER_BIT ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glCallList(BOX); glCallList(OSHAPE); /* To display another O-shape, larger and to one side: */ glTranslatef(2.0,0.0,0.0); glScalef(2.0,2.0,1.0); glCallList(OSHAPE); checkError( "display" ); glFlush(); } void init( void ) { glClearColor( 1.0, 1.0, 1.0, 1.0); glPolygonMode(GL_FRONT,GL_LINE); createBoxList(); createOshapeList(); } int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); glutCreateWindow( argv[0] ); printHelp(); glutDisplayFunc( display ); glutReshapeFunc( reshape ); glutKeyboardFunc( keyboard ); glutSpecialFunc( specialkeys ); init(); glutMainLoop(); return 0; }