/* prac4b.c */ /* Purpose: This program constructs a cube, 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 */ /* 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}; /* Functions */ void printHelp( void ) { fprintf(stdout, "\nCube Transformations\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); } } 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 drawCube( GLvoid ) { glBegin(GL_LINE_LOOP); /* Front face of cube */ glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glEnd(); glBegin(GL_LINE_LOOP); /* Back face of cube */ glVertex3fv(v4); glVertex3fv(v5); glVertex3fv(v6); glVertex3fv(v7); glEnd(); glBegin(GL_LINES); /* join front frame to back frame: */ glVertex3fv(v0); glVertex3fv(v4); glVertex3fv(v1); glVertex3fv(v5); glVertex3fv(v3); glVertex3fv(v7); glVertex3fv(v2); glVertex3fv(v6); glEnd(); } GLvoid display( GLvoid ) { GLfloat ww; GLfloat wh; glClear( GL_COLOR_BUFFER_BIT ); /* Do all your OpenGL rendering here */ /* Get current window size */ ww = glutGet(GLUT_WINDOW_WIDTH); wh = glutGet(GLUT_WINDOW_HEIGHT); /* First viewport: */ glViewport(0,wh/2,ww/2,wh/2); glScalef(1.25,1.0,1.0); glRotatef(30,1,0,0); glRotatef(85,0,1,0); drawCube(); /* 2nd Viewport: */ glViewport(ww/2,wh/2,ww/2,wh/2); /* Need to undo previous transformations */ glRotatef(-85,0,1,0); glRotatef(-30,1,0,0); glScalef(4/5.0,1,1); /* New transformations: */ glTranslatef(0,0,0.5); glRotatef(60,0,0,1); glRotatef(45,0,1,0); glRotatef(-30,1,0,0); drawCube(); /* 3rd Viewport: */ glViewport(0,0,ww/2,wh/2); /* undo previous transformations */ glRotatef(30,1,0,0); glRotatef(-45,0,1,0); glRotatef(-60,0,0,1); glTranslatef(0,0,-0.5); /* New transformations: */ glScalef(1.0,2.0,1.5); glTranslatef(0,0.5,0); glRotatef(-35,0,0,1); glRotatef(65,1,0,0); glRotatef(20,0,1,0); drawCube(); /* 4th Viewport: */ glViewport(ww/2,0,ww/2,wh/2); /* undo previous transformations */ glRotatef(-20,0,1,0); glRotatef(-65,1,0,0); glRotatef(35,0,0,1); glTranslatef(0,-0.5,0); glScalef(1,0.5,2/3.0); /* New transformations: */ glRotatef(45,0,1,0); glRotatef(30,0,0,1); drawCube(); checkError( "display" ); glFlush(); } GLvoid init( GLvoid ) { glClearColor( 1.0, 1.0, 1.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); 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 ); printHelp( ); glutMainLoop(); return 0; }