#include # include # include # include using namespace std; // Globals. float Xvalue, Yvalue, Zvalue; float rotX, rotY,rotZ; bool Hidden; void cube() { // front glColor3f(1.0, 0.0, 0.0); glBegin(GL_POLYGON) ; glVertex3f(0,0,0); glVertex3f(0,1,0); glVertex3f(1,1,0); glVertex3f(1,0,0); glEnd(); //bottom glColor3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON) ; glVertex3f(0,0,0); glVertex3f(0,0,1); glVertex3f(1,0,1); glVertex3f(1,0,0); glEnd(); // top glColor3f(0.0, 0.0, 1.0); glBegin(GL_POLYGON) ; glVertex3f(0,1,0); glVertex3f(0,1,1); glVertex3f(1,1,1); glVertex3f(1,1,0); glEnd(); //left glColor3f(1.0, 1.0, 0.0); glBegin(GL_POLYGON) ; glVertex3f(0,0,0); glVertex3f(0,0,1); glVertex3f(0,1,1); glVertex3f(0,1,0); glEnd(); //left glColor3f(1.0, 0.0, 1.0); glBegin(GL_POLYGON) ; glVertex3f(1,0,0); glVertex3f(1,0,1); glVertex3f(1,1,1); glVertex3f(1,1,0); glEnd(); // top glColor3f(1.0, 0.0, 1.0); glBegin(GL_POLYGON) ; glVertex3f(0,0,1); glVertex3f(0,1,1); glVertex3f(1,1,1); glVertex3f(1,0,1); glEnd(); } // Drawing routine. void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // draw an axis, that stays put. glTranslatef(0.0,0.0,-5.0); glColor3f(0.0,0.0,0.0); glBegin(GL_LINES); glVertex3f(-2,0,0); glVertex3f(2,0,0); glVertex3f(0,-2,0); glVertex3f(0,2,0); glVertex3f(0,0,-2); glVertex3f(0,0,2); glEnd(); glTranslatef(Xvalue, Yvalue, Zvalue); glRotatef(rotX, 1.0, 0.0, 0.0); glRotatef(rotY, 0.0, 1.0, 0.0); glRotatef(rotZ, 0.0, 0.0, 1.0); if(Hidden) { glEnable(GL_DEPTH_TEST); } cube(); if(Hidden) { glDisable(GL_DEPTH_TEST); } glutSwapBuffers(); } void reset(void) { Xvalue = 0; Yvalue = 0; Zvalue = -5.0; rotX = rotY= rotZ = 0; Hidden = true; return; } // Initialization routine. void setup(void) { reset(); glPolygonMode(GL_FRONT_AND_BACK, GL_POLYGON); //glFrontFace(GL_CW); glClearColor(1.0, 1.0, 1.0, 0.0); } // OpenGL window reshape routine. void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode(GL_MODELVIEW); } // Keyboard input processing routine. void keyInput(unsigned char key, int x, int y) { switch (key) { case 'a': rotX -= 10.0; break; case 'A': rotX += 10.0; break; case 'b': rotY -= 10.0; break; case 'B': rotY += 10.0; break; case 'c': rotZ -= 10.0; break; case 'C': rotZ += 10.0; break; case 'h': if (Hidden) { Hidden = false; cout << "Hidden is off" << endl; } else { Hidden=true; cout << "Hidden is on" << endl; } break; case 'r': reset(); break; case 'x': Xvalue -= .1; break; case 'X': Xvalue += .1; break; case 'y': Yvalue -= .1; break; case 'Y': Yvalue += .1; break; case 'Z': Zvalue += .1; break; case 'z': Zvalue -= .1; break; case 27: case 'q': exit(0); break; default: break; } glutPostRedisplay(); } // Callback routine for non-ASCII key entry. void specialKeyInput(int key, int x, int y) { if(key == GLUT_KEY_UP) Yvalue += 0.1; if(key == GLUT_KEY_DOWN) Yvalue -= 0.1; if(key == GLUT_KEY_LEFT) Xvalue -= 0.1; if(key == GLUT_KEY_RIGHT) Xvalue += 0.1; glutPostRedisplay(); } // Main routine. int main(int argc, char **argv) { glutInit(&argc, argv); glutInitContextVersion(3, 0); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Teapot"); glutDisplayFunc(drawScene); glutReshapeFunc(resize); glutKeyboardFunc(keyInput); // Register the callback function for non-ASCII key entry. glutSpecialFunc(specialKeyInput); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }