/* prac4a.c */ /* Purpose: This program constructs a barn, and uses function keys to control translation, rotation and scaling of the shape. */ #ifdef __APPLE__ #include #else #include #endif #include #include #include #define KEY_ESC 27 /* glut doesn't define this one for some reason */ /* Global Constants */ static const float v0[3] = {0.0,0.0,0.0}; static const float v1[3] = {1.0,0.0,0.0}; static const float v2[3] = {1.0,1.0,0.0}; static const float v3[3] = {0.0,1.0,0.0}; static const float v4[3] = {0.0,0.0,1.0}; static const float v5[3] = {1.0,0.0,1.0}; static const float v6[3] = {1.0,1.0,1.0}; static const float v7[3] = {0.0,1.0,1.0}; static const float v8[3] = {0.5,1.25,0.0}; static const float v9[3] = {0.5,1.25,1.0}; /* Functions */ void printHelp( void ) { fprintf(stdout, "\nHouse Transformations\n\n" "Escape key - exit the program\n\n" "F3 - translate 0.5 in x-axis\n" "F4 - translate -0.5 in x-axis\n" "F5 - rotate 45deg. around x-axis\n" "F6 - rotate 45deg. around y-axis\n" "F7 - rotate 45deg. around z-axis\n" "F8 - scale 1.25 in all axes\n" "F9 - scale 0.75 in all axes\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; case GLUT_KEY_F3: /* translate 0.5 in x-axis */ glTranslatef(0.5,0.0,0.0); break; case GLUT_KEY_F4: /* translate -0.5 in x-axis */ glTranslatef(-0.5,0.0,0.0); break; case GLUT_KEY_F5: /* rotate 45 degrees around x-axis */ glRotatef(45.0,1.0,0.0,0.0); break; case GLUT_KEY_F6: /* rotate 45 degrees around y-axis */ glRotatef(45.0,0.0,1.0,0.0); break; case GLUT_KEY_F7: /* rotate 45 degrees around z-axis */ glRotatef(45.0,0.0,0.0,1.0); break; case GLUT_KEY_F8: /* scale 1.25 in all axes */ glScalef(1.25,1.25,1.25); break; case GLUT_KEY_F9: /* scale 0.75 in all axes */ glScalef(0.75,0.75,0.75); break; } glutPostRedisplay(); } 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 drawBarn( GLvoid ) { /* draw rear of house */ glBegin(GL_LINE_LOOP); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glEnd(); /* draw front of house */ glBegin(GL_LINE_LOOP); glVertex3fv(v4); glVertex3fv(v5); glVertex3fv(v6); glVertex3fv(v7); glEnd(); /* draw roof gable at rear and front of house, and connect the two pieces */ glBegin(GL_LINES); glVertex3fv(v2); glVertex3fv(v8); glVertex3fv(v8); glVertex3fv(v3); glVertex3fv(v6); glVertex3fv(v9); glVertex3fv(v9); glVertex3fv(v7); glVertex3fv(v8); glVertex3fv(v9); /* join front frame to back frame (the sides of the house) */ glVertex3fv(v0); glVertex3fv(v4); glVertex3fv(v1); glVertex3fv(v5); glVertex3fv(v3); glVertex3fv(v7); glVertex3fv(v2); glVertex3fv(v6); glEnd(); } GLvoid display( GLvoid ) { glClear( GL_COLOR_BUFFER_BIT ); /* Do all your OpenGL rendering here */ drawBarn(); 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); /* 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] ); init(); glutKeyboardFunc( keyboard ); glutSpecialFunc( specialkeys ); glutDisplayFunc( display ); printHelp( ); glutMainLoop(); return 0; }