#include #include #include #include #define PIXEL_CENTER(x) ((long)(x) + 0.5) GLenum rgb, doubleBuffer, windType; GLint windW, windH; GLint boxW, boxH; float color[3]; float angle[4]; // Initialize OpenGL window - prepare the frame-buffer for z-buffering. static void glInit () { // Clear color glPushAttrib(GL_COLOR_BUFFER_BIT); glColorMask(1, 1, 1, 1); glIndexMask(~0); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glPopAttrib(); // Enable Depth testing glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT); } // Function that gets called when the window is resized. static void Reshape(int width, int height) { // Define window size. windW = (GLint)width; windH = (GLint)height; // Viewport mapping glViewport(0, 0, windW, windH); // Reset projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Switch matrix stack to modelview stack. glMatrixMode(GL_MODELVIEW); } // Draw a 1x1x1 box centered about the origin. The different sides of the box with different colors. void drawbox (int mode) { glBegin (mode); glColor3f (1.0, 0.0, 0.0); glVertex3f (-0.5, -0.5, 0.5); glVertex3f (0.5, -0.5, 0.5); glVertex3f (0.5, 0.5, 0.5); glVertex3f (-0.5, 0.5, 0.5); glEnd (); glBegin (mode); glColor3f (0.0, 1.0, 0.0); glVertex3f (-0.5, -0.5, 0.5); glVertex3f (-0.5, 0.5, 0.5); glVertex3f (-0.5, 0.5, -0.5); glVertex3f (-0.5, -0.5, -0.5); glEnd (); glBegin (mode); glColor3f (0.0, 0.0, 1.0); glVertex3f (0.5, -0.5, 0.5); glVertex3f (0.5, 0.5, 0.5); glVertex3f (0.5, 0.5, -0.5); glVertex3f (0.5, -0.5, -0.5); glEnd (); glBegin (mode); glColor3f (1.0, 1.0, 0.0); glVertex3f (-0.5, 0.5, 0.5); glVertex3f (0.5, 0.5, 0.5); glVertex3f (0.5, 0.5, -0.5); glVertex3f (-0.5, 0.5, -0.5); glEnd (); glBegin (mode); glColor3f (0.0, 1.0, 1.0); glVertex3f (-0.5, -0.5, 0.5); glVertex3f (0.5, -0.5, 0.5); glVertex3f (0.5, -0.5, -0.5); glVertex3f (-0.5, -0.5, -0.5); glEnd (); glBegin (mode); glColor3f (1.0, 0.0, 1.0); glVertex3f (-0.5, -0.5, -0.5); glVertex3f (-0.5, 0.5, -0.5); glVertex3f (0.5, 0.5, -0.5); glVertex3f (0.5, -0.5, -0.5); glEnd (); } // Draws the scene at any point in time. static void Draw(void) { int i; // Clear color and depth buffers glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (i=0; i<4; i++) { switch (i) { case 0: // If We Are Drawing The First Scene // Set The Viewport To The Top Left. It Will Take Up Half The Screen Width And Height glViewport (0, windH/2, windW/2, windH/2); glMatrixMode (GL_PROJECTION); // Select The Projection Matrix glLoadIdentity (); // Reset The Projection Matrix // Define a perspective projection with a 45 degree field of view // and looking from the the positive z axis at (0,0,10) towards the origin. gluPerspective (45.0, 1.0, 1.0, 20.0); // Return to model view mode in order to render model glMatrixMode(GL_MODELVIEW); glLoadIdentity (); // Reset The Projection Matrix gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix (); glRotatef (angle[i], 0.0, 1, 0.0); // Rotate box drawbox (GL_POLYGON); glPopMatrix (); // Increment the rotation angle angle[i] += 1.0; if (angle[i] == 360.0) angle[i] = 0.0; break; case 1: glViewport (windW/2, windH/2, windW/2, windH/2); glMatrixMode (GL_PROJECTION); // Select The Projection Matrix glLoadIdentity (); // Reset The Projection Matrix gluPerspective (45.0, 1.0, 1.0, 20.0); // Return to model view mode in order to render model glMatrixMode(GL_MODELVIEW); glLoadIdentity (); // Reset The Projection Matrix gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix (); glRotatef (angle[i], 0.0, 1, 0.0); // Rotate box drawbox (GL_LINES); glPopMatrix (); // Increment the rotation angle angle[i] += 2.0; if (angle[i] == 360.0) angle[i] = 0.0; break; case 2: glViewport (windW/2, 0, windW/2, windH/2); glMatrixMode (GL_PROJECTION); // Select The Projection Matrix glLoadIdentity (); // Reset The Projection Matrix gluPerspective (45.0, 1.0, 1.0, 20.0); // Return to model view mode in order to render model glMatrixMode(GL_MODELVIEW); glLoadIdentity (); // Reset The Projection Matrix gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix (); glRotatef (angle[i], 0.0, 1, 0.0); // Rotate box drawbox (GL_POLYGON); glPopMatrix (); // Increment the rotation angle angle[i] += 4.0; if (angle[i] == 360.0) angle[i] = 0.0; break; case 3: glViewport (0, 0, windW/2, windH/2); glMatrixMode (GL_PROJECTION); // Select The Projection Matrix glLoadIdentity (); // Reset The Projection Matrix gluPerspective (45.0, 1.0, 1.0, 20.0); // Return to model view mode in order to render model glMatrixMode(GL_MODELVIEW); glLoadIdentity (); // Reset The Projection Matrix gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix (); glRotatef (angle[i], 0.0, 1, 0.0); // Rotate box drawbox (GL_LINES); glPopMatrix (); // Increment the rotation angle angle[i] += 6.0; if (angle[i] == 360.0) angle[i] = 0.0; break; } } glFlush(); if (doubleBuffer) glutSwapBuffers(); } static void Key(unsigned char key, int x, int y) { switch (key) { case 27: // esc key exit(1); case 9: //tab key printf ("Tab key pressed\n"); break; case 'M': //m key: toggle between wireframe and smooth surface case 'm': //m key: toggle between wireframe and smooth surface printf ("m key pressed\n"); break; default: return; } glutPostRedisplay(); } static void SpecialKey(int key, int x, int y) { switch (key) { case GLUT_KEY_PAGE_UP: // PAGE UP key pressed printf ("Page up key pressed\n"); break; case GLUT_KEY_PAGE_DOWN: // PAGE DOWN key pressed printf ("Page down key pressed\n"); break; case GLUT_KEY_UP: // Up arrow pressed printf ("Up arrow key pressed\n"); break; case GLUT_KEY_DOWN: // Down arrow pressed printf ("Down arrow key pressed\n"); break; case GLUT_KEY_LEFT: // Left arrow pressed printf ("Left arrow key pressed\n"); break; case GLUT_KEY_RIGHT: // Right arrow pressed printf ("Right arrow key pressed\n"); break; default: return; } glutPostRedisplay(); } // Read input. static GLenum Args(int argc, char **argv) { GLint i, inputfile_count; rgb = GL_TRUE; doubleBuffer = GL_TRUE; inputfile_count = 0; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-ci") == 0) { rgb = GL_FALSE; } else if (strcmp(argv[i], "-rgb") == 0) { rgb = GL_TRUE; } else if (strcmp(argv[i], "-sb") == 0) { doubleBuffer = GL_FALSE; } else if (strcmp(argv[i], "-db") == 0) { doubleBuffer = GL_TRUE; } else { //printf("%s (Bad option).\n", argv[i]); //return GL_FALSE; } } return GL_TRUE; } int main(int argc, char **argv) { glutInit(&argc, argv); if (Args(argc, argv) == GL_FALSE) { exit(1); } // Initialize window size. windW = 512; windH = 512; glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH); // Enable frame buffer for double buffering and z-buffering. windType = (rgb) ? GLUT_RGB : GLUT_INDEX; windType |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; glutInitDisplayMode(windType | GLUT_DEPTH); if (glutCreateWindow("Sample Animation") == GL_FALSE) { exit(1); } angle[0] = angle[1] = angle[2] = angle[3] = 0.0; // Initialize rotation to 0.0 glInit (); glutReshapeFunc(Reshape); // Set reshape function glutIdleFunc(Draw); // Even if no events, redraw to animate glutKeyboardFunc(Key); // Set keyboard function glutSpecialFunc(SpecialKey); // Set special keyboard function (arrow keys) glutDisplayFunc(Draw); // Set Display function glutMainLoop(); return 0; }