/******************************************************************** viewout.cpp - written by H. Masterman version 1.0 26 March 2003 written to illustrate viewing and navigating. inside looking out ********************************************************************/ #include #include #include #define PI 3.141592653 GLfloat theta = 0.0, phi = 0.0; //stores two angles GLfloat radius = 1.0; GLfloat xo = -10.0; GLfloat yo = 0.0; GLfloat zo = 0.0; // initial viewer position 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(xo,yo,zo,xo+x,yo+y,zo+z,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 mouse_x, int mouse_y) { if (key_pressed == GLUT_KEY_LEFT) //turn left { phi += 0.1; //radians glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_RIGHT) //turn right { phi -= 0.1; //radians glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_UP) //look up { if (theta < (PI/2 - 0.1)) theta += 0.1; glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_DOWN) //look down { if (theta > (-PI/2 + 0.1)) theta -= 0.1; glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_PAGE_UP) //walk ahead { xo+=x; zo+=z; glutPostRedisplay(); return; } if (key_pressed == GLUT_KEY_PAGE_DOWN) //walk backward { xo-=x; zo-=z; 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 In"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMouseFunc(mouse); glutSpecialFunc(special); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glutMainLoop(); }