/* prac3a.c */ /* Purpose: This program practises triangle fans, independent quadrilaterals and quadrilateral strips, with colours and shading techniques. */ #ifdef __APPLE__ #include #else #include #endif #include #include #include #define KEY_ESC 27 /* glut doesn't define this */ /* Global Definitions */ static float v0[2] = {0.0,0.0}; static float v1[2] = {0.5,0.0}; static float v2[2] = {0.5,1.0}; static float v3[2] = {0.0,1.0}; static float v4[2] = {-0.25,1.0}; static float v5[2] = {-0.5,0.75}; static float v6[2] = {-1.0,0.0}; void printHelp( void ) { fprintf(stdout, "Triangle fans etc.\n\n" "SPACE key - alternate between flat and smooth shading\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; case ' ': { /* toggle between smooth and flat shading */ static GLboolean flat = GL_FALSE; flat = !flat; flat ? glShadeModel(GL_FLAT) : glShadeModel(GL_SMOOTH); } break; } glutPostRedisplay(); } 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(); } } GLvoid display( GLvoid ) { /* Do all your OpenGL rendering here */ glClear( GL_COLOR_BUFFER_BIT ); /* First draw a triangle fan, assigning colours as you wish */ glBegin(GL_TRIANGLE_FAN); glColor3f(1.0,0.25,0.25); glVertex2fv(v0); glColor3f(0.5,0.5,0.0); glVertex2fv(v1); glColor3f(1.0,0.0,0.0); glVertex2fv(v2); glColor3f(1.0,1.0,0.0); glVertex2fv(v3); glColor3f(0.0,1.0,0.0); glVertex2fv(v4); glColor3f(0.0,0.5,0.5); glVertex2fv(v5); glColor3f(0.0,0.0,1.0); glVertex2fv(v6); glEnd(); /* Now draw some quads, choosing a selection of colours */ glBegin(GL_QUADS); glColor3f(1.0,0.25,1.0); glVertex2f(1.0,0.0); glColor3f(1.0,1.0,0.5); glVertex2f(1.5,0.0); glColor3f(1.0,0.0,0.75); glVertex2f(1.5,0.5); glColor3f(0.0,0.0,1.0); glVertex2f(1.0,0.5); glColor3f(0.0,0.5,0.5); glVertex2f(1.0,0.75); glColor3f(1.0,0.75,0.5); glVertex2f(1.75,0.75); glColor3f(1.0,1.0,0.25); glVertex2f(1.85,1.25); glColor3f(0.0,1.0,0.0); glVertex2f(1.5,1.5); glEnd(); /* Now for an arbitrary quad strip */ glBegin(GL_QUAD_STRIP); glColor3f(0.0,1.0,1.0); glVertex2f(-1.75,-0.5); glColor3f(1.0,0.0,1.0); glVertex2f(-1.75,-1.25); glColor3f(0.0,0.5,0.0); glVertex2f(-1.25,-0.75); glColor3f(0.0,0.0,1.0); glVertex2f(-1.5,-1.5); glColor3f(0.5,1.0,0.0); glVertex2f(-0.75,-0.5); glColor3f(0.0,1.0,0.0); glVertex2f(-1.0,-1.5); glColor3f(1.0,1.0,0.0); glVertex2f(-0.5,-0.75); glColor3f(1.0,0.0,0.0); glVertex2f(-0.75,-1.5); glColor3f(0.0,0.0,0.5); glVertex2f(0.0,-0.75); glColor3f(0.0,0.0,0.0); glVertex2f(0.0,-1.75); glEnd(); checkError( "display" ); glFlush(); } void init( void ) { glClearColor( 1.0, 1.0, 1.0, 1.0); glLineWidth(3.0); /* Set a viewing volume for the data we wish to display */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); glutCreateWindow( argv[0] ); printHelp( ); glutKeyboardFunc( keyboard ); glutSpecialFunc( specialkeys ); glutDisplayFunc( display ); init(); glutMainLoop(); return 0; }