/* walk.c */ /* walk "eye" x, X, y, Y, z, Z */ /* We use the Lookat function in the display callback to point the eye, whose position can be altered by the x,X,y,Y,z, and Z keys. The perspective view is set in the reshape callback */ #include #include static GLfloat nearv=2.0; static GLfloat farv=50.0; /* initial eye location */ static GLdouble eye[]= {0.0, 0.0, 10.0}; void display(void) { char text[]="move eye using x, X, y, Y, z, Z"; char *p; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Update eye position in modelview matrix */ glLoadIdentity(); /* from eye to center of scene */ gluLookAt(eye[0],eye[1],eye[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glColor3f(1.0, 0.0, 0.0); glTranslatef(-1.0, -1.0, 0.0); glutWireSphere(1.0, 12, 12); glColor3f(0.0, 1.0, 1.0); glTranslatef(2.0, 0.0, 0.0); glutWireSphere(1.0, 12, 12); glColor3f(0.0, 1.0, 0.0); glTranslatef(0.0, 2.0, 0.0); glutWireSphere(1.0, 12, 12); glColor3f(0.0, 0.0, 1.0); glTranslatef(-2.0, 0.0, 0.0); glutWireSphere(1.0, 12, 12); glColor3f(1.0, 1.0, 1.0); glTranslatef(-7.0, -7.0, 0.0); glScalef(0.005, 0.005, 1.0); for(p=text; *p; p++) glutStrokeCharacter(GLUT_STROKE_ROMAN, *p); glFlush(); glutSwapBuffers(); } void keys(unsigned char key, int x, int y) { /* Use x, X, y, Y, z, and Z keys to move eye */ if(key == 'x') eye[0]-= 1.0; if(key == 'X') eye[0]+= 1.0; if(key == 'y') eye[1]-= 1.0; if(key == 'Y') eye[1]+= 1.0; if(key == 'z') {eye[2]-= 1.0; if(eye[2]farv) eye[2]=farv;} display(); } void myReshape(int w, int h) { glViewport(0, 0, w, h); /* Use a perspective view */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h / (GLfloat) w, nearv, farv); else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h, 2.0* (GLfloat) w / (GLfloat) h, nearv, farv); /* Or we can use gluPerspective */ /* gluPerspective(45.0, w/h, -10.0, 10.0); */ glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow(argv[0]); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutKeyboardFunc(keys); glEnable(GL_DEPTH_TEST); glutMainLoop(); return 0; }