/* prac5b.c */ /* Purpose: Investigate the difference in the ordering of translate, rotate, and scale. */ #ifdef __APPLE__ #include #else #include #endif #include #include #include #define KEY_ESC 27 /* glut doesn't define this one for some reason */ /* Global Variables */ static char transformations[3] = {'r', 't', 's'}; /* Functions */ void printHelp( void ) { fprintf(stdout, "\nOrder of Transformations\n\n" "Escape key - exit the program\n\n" "a - swap order of first two transformations\n" "s - swap order of last two transformations\n"); } /* Swap the order of the transformations */ void swap(unsigned short index1, unsigned short index2) { char temp; temp = transformations[index1]; transformations[index1] = transformations[index2]; transformations[index2] = temp; } GLvoid keyboard( GLubyte key, GLint x, GLint y) { switch (key) { case KEY_ESC: /* exit when escape key is pressed */ exit(0); break; case 'a': /* swap the first two transformations */ swap(0, 1); break; case 's': /* swap the last two transformations */ swap(1, 2); 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(); } } void drawAxes(void) { static const GLfloat max = 20.0; glBegin(GL_LINES); glColor3f(0.0, 0.0, 1.0); glVertex2f(-max, 0.0); glVertex2f(max, 0.0); glColor3f(0.8, 0.0, 1.0); glVertex2f(0.0, -max); glVertex2f(0.0, max); glEnd(); } void drawTriangle(void) { glBegin(GL_TRIANGLES); glVertex2f(0.0, 1.0); glVertex2f(-1.0, -1.0); glVertex2f(1.0, -1.0); glEnd(); } GLvoid display( GLvoid ) { int index; glClear( GL_COLOR_BUFFER_BIT ); /* Do all your OpenGL rendering here */ /* Set up the transformations. We want to start from scratch every time, not build up the transformations. */ glLoadIdentity(); drawAxes(); /* Draw the untransformed triangle */ glColor3f(1.0,1.0,0.0); drawTriangle(); for (index = 0; index < 3; ++index) { switch (transformations[index]) { case 'r': glRotatef(60, 0, 0, 1); printf("Rotate\t"); break; case 't': glTranslatef(3.0, 0, 0); printf("Translate\t"); break; case 's': glScalef(3.0, 0.5, 1.0); printf("Scale \t"); break; } printf("*\t"); } printf("triangle\n"); /* Draw the transformed triangle */ glColor3f(1.0,0.0,0.0); drawTriangle(); checkError( "display" ); glFlush(); } GLvoid init( GLvoid ) { glClearColor( 1.0, 1.0, 1.0, 1.0); glLineWidth(3.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-3.0, 13.0, -3.0, 12.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; }