#include #include // Define a constant for the value of PI #define GL_PI 3.1415f // Rotation amounts static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; // Called to draw scene void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the matrix state and do the rotations glPushMatrix(); glRotatef(45, 0.5f, 1.0f, 0.5f); glPushMatrix(); glScalef(1.2, 0.7, 2.0); glColor3f(0.0, 0.0, 1.0); glutSolidCube(30); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, 21.0, 0.0); glRotatef(yRot, 0.0f, 1.0f, 0.0f); glScalef(1.0, 0.7, 1.7); glColor3f(0.0, 1.0, 0.0); glutSolidCube(20); glPopMatrix(); glPushMatrix(); glRotatef(yRot, 0.0f, 1.0f, 0.0f); glTranslatef(0.0, 17.0, 15.0); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glScalef(1.0, 1.0, 7.0); glTranslatef(0.0, 0.0, 2.5); glColor3f(1.0, 0.0, 0.0); glutSolidCube(5); glPopMatrix(); // Restore the matrix state glPopMatrix(); // Display the results glutSwapBuffers(); } // This function does any needed initialization on the rendering // context. void SetupRC() { glEnable(GL_DEPTH_TEST); // Hidden surface removal glFrontFace(GL_CCW); // Counter clock-wise polygons face out glEnable(GL_CULL_FACE); // Do not calculate inside of jet glShadeModel(GL_SMOOTH); glClearColor(1.0f, 1.0f, 1.0f, 1.0f ); } void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_UP) xRot-= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 5.0f; if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; if(xRot > 10.0f) xRot = 10.0f; if(xRot < -50.0f) xRot = -50.0f; if(yRot > 356.0f) yRot = 0.0f; if(yRot < -1.0f) yRot = 355.0f; // Refresh the Window glutPostRedisplay(); } void ChangeSize(int w, int h) { GLfloat nRange = 80.0f; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Color Tank"); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0; }