/* prac2b.c */ /* Purpose: this program constructs a simple house, in 3D. Although this program will only display the house in orthographic mode (so we only see it from the front) the house will be used in subsequent coding exercises, so construct the entire building here. */ #ifdef __APPLE__ #include #else #include #endif #include #include #include #define KEY_ESC 27 /* glut doesn't define this one */ /* Global Definitions */ static float v0[3] = {0.0,0.0,0.0}; static float v1[3] = {1.0,0.0,0.0}; static float v2[3] = {1.0,1.0,0.0}; static float v3[3] = {0.0,1.0,0.0}; static float v4[3] = {0.0,0.0,1.0}; static float v5[3] = {1.0,0.0,1.0}; static float v6[3] = {1.0,1.0,1.0}; static float v7[3] = {0.0,1.0,1.0}; static float v8[3] = {0.5,1.25,0.0}; static float v9[3] = {0.5,1.25,1.0}; void printHelp( void ) { fprintf(stdout, "House construction\n\n" "SPACE key - random colour\n" "Escape key - exit the program\n\n"); } int myrandom(int m) { return rand()%m; } GLvoid keyboard( GLubyte key, GLint x, GLint y) { switch (key) { case KEY_ESC: /* exit when escape key is pressed */ exit(0); break; case ' ': /* change colour first */ glColor3f(myrandom(10)/10.0, myrandom(10)/10.0, myrandom(10)/10.0); 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 ); /* 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(); checkError( "display" ); glFlush(); } void init( void ) { 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(0.25,1.75,0.25,1.75,-2.0,2.0); /* partial barn */ /* Alternative viewing volumes are: glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0); - full barn glOrtho(0.5,1.5,0.5,1.5,-2.0,2.0); - partial barn */ 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; }