#include #include #include #include #define PIXEL_CENTER(x) ((long)(x) + 0.5) GLint windW, windH; GLint boxW, boxH; float color[3]; float angle; // 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); // 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. glMatrixMode(GL_PROJECTION); glLoadIdentity(); //glFrustum(-10.0, 10.0, -10.0, 10.0, 1.0, 20.0); // Alternate method of specifying viewing paramters. gluPerspective (45.0, 1.0, 1.0, 20.0); // 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 (void) { glBegin (GL_POLYGON); 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 (GL_POLYGON); 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 (GL_POLYGON); 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 (GL_POLYGON); 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 (GL_POLYGON); 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 (GL_POLYGON); 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) { // Clear color and depth buffers glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix (); glRotatef (angle, 0.0, 1, 0.0); // Rotate box drawbox (); glPopMatrix (); glFlush(); // Increment the rotation angle angle += 1.0; if (angle == 360.0) angle = 0.0; glutSwapBuffers(); } // Called when a keyboard key is pressed static void Key(unsigned char key, int x, int y) { switch (key) { case 27: printf ("exit!\n"); exit(1); default: return; } glutPostRedisplay(); } // Called when special keys are pressed - arrows and page-up/down static void SpecialKey(int key, int x, int y) { switch (key) { case GLUT_KEY_PAGE_UP: /* PAGE UP key pressed */ printf ("Page Up pressed\n"); break; case GLUT_KEY_PAGE_DOWN: /* PAGE DOWN key pressed */ printf ("Page Down pressed\n"); break; case GLUT_KEY_UP: /* Up arrow pressed */ printf ("Up Arrow pressed\n"); break; case GLUT_KEY_DOWN: /* Down arrow pressed */ printf ("Down Arrow pressed\n"); break; case GLUT_KEY_LEFT: /* Left arrow pressed */ printf ("Left Arrow pressed\n"); break; case GLUT_KEY_RIGHT: /* Right arrow pressed */ printf ("Right Arrow pressed\n"); break; default: return; } glutPostRedisplay(); } // Read input. static GLenum Args(int argc, char **argv) { GLint i, inputfile_count; inputfile_count = 0; for (i = 1; i < argc; i++) { // Add valid input arguments. /*if (strcmp(argv[i], "?") == 0) { } 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. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); if (glutCreateWindow("Sample Animation") == GL_FALSE) { exit(1); } angle = 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; }