//Camera.C Toby Jones March 2001 //This is a program that takes ketboard commands and allows a person to //fly around the world. //All commands are outlined in the keyboard function #include #include #include #include //to enable where you are looking at and where you are going //to be controlled by shperical coordinates double theta = 3.141592654/2; double phi = 0; double mag = 5; //camera position float px = 0; float py = 0; float pz = 5; void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); } void Display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glLoadIdentity(); //position, direction and orientation of the camera gluLookAt(px, py, pz, //camera position px + mag*cos(theta)*cos(phi), //looking at x py+ mag*sin(phi) , //looking at y pz - mag*sin(theta)*cos(phi), //looking at z 0.0, 1.0, 0.0); //up direction //draw a cube glScalef(1.0, 1.0, 1.0); glutWireCube(1.0); glFlush(); } void Keyboard (unsigned char key, int p, int o) { switch (key) { //temporary variable to allow moving right and left double temp1, temp2; case 'u': //move straight up py +=1; glutPostRedisplay(); break; case 'd': //move straight down py -=1; glutPostRedisplay(); break; case 'r': //move directly right in respect to where you are px += sin(theta); pz += cos(theta); glutPostRedisplay(); break; case 'l': //move directly left in respect to where you are px -= sin(theta); pz -= cos(theta); glutPostRedisplay(); break; case 'b': //move directly backward WRT where you're looking pz +=sin(theta)*cos(phi); px -=cos(theta)*cos(phi); py -=sin(phi); glutPostRedisplay(); break; case 'f': //move directly forward WRT where you're looking pz-= sin(theta)*cos(phi); px +=cos(theta)*cos(phi); py +=sin(phi); glutPostRedisplay(); break; case 'L': //Look left theta +=.1; glutPostRedisplay(); break; case 'R': //Look right theta -=.1; glutPostRedisplay(); break; case 'U': //Look up phi+=.1; glutPostRedisplay(); break; case 'D': //Look down phi-=.1; glutPostRedisplay(); break; case 'Q': //Quit case 'q': exit (EXIT_SUCCESS); } } void reshape(int w, int h) { //This is the important stuff. This allows the world to be projected like //you think it should. If you move the camera it changes the projections glViewport(0,0,(GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //This call creates a window to project in of size(-x, x, -y, y, near, far) glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode(GL_MODELVIEW); } //usual main int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(Display); glutReshapeFunc(reshape); glutKeyboardFunc (Keyboard); glutMainLoop(); return 0; }