/******************************************************************** viewin.cpp - written by H. Masterman version 1.0 26 March 2003 written to illustrate viewing and navigating. outside looking in ********************************************************************/ #include #include #include #define PI 3.141592653 GLfloat theta = 0.0, phi = 0.0; //stores two angles GLfloat radius = 10.0; GLfloat x,y,z; //point on viewing sphere void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear color buffer and z buffer glLoadIdentity(); // start with an identity in the MODELVIEW transformation x = radius * cos (theta) * cos (phi); y = radius * sin (theta); z = -radius * cos (theta)* sin(phi); gluLookAt(x,y,z,0,0,0,0,1,0); //multiply viewing component of transformation glutSolidTeapot(1.0); glFlush(); glutSwapBuffers(); } void spinCube() { } void mouse(int btn, int state, int x, int y) { exit(0); //exit the program on any mouse click } void special (int key_pressed, int x, int y) { if (key_pressed == GLUT_KEY_LEFT) { phi += 0.1; //radians glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_RIGHT) { phi -= 0.1; //radians glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_UP) { if (theta < (PI/2 - 0.1)) theta += 0.1; glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_DOWN) { if (theta > (-PI/2 + 0.1)) theta -= 0.1; glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_PAGE_UP) { radius += 0.5; glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_PAGE_DOWN) { radius -= 0.5; glutPostRedisplay(); return; } } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0,1.0,30.0); //for perspective viewing glMatrixMode(GL_MODELVIEW); } void main(int argc, char **argv) { glutInit(&argc, argv); /* need both double buffering and z buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH); glutInitWindowSize(700, 700); glutCreateWindow("Viewing Example: Outside Looking In"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMouseFunc(mouse); glutSpecialFunc(special); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glutMainLoop(); }