/* prac8a.c */ /* Purpose: Exploring gluLookAt */ #ifdef __APPLE__ #include #else #include #endif #include #include #define KEY_ESC 27 /* glut doesn't define this one */ /* Global Variables */ static const GLfloat mNearClip = 5.0; /* near clipping plane */ static const GLfloat mFarClip = 15.0; /* far clipping plane */ void printHelp( void ) { fprintf(stdout, "\n Exploring gluLookAt\n\n" "Escape key - exit the program\n\n"); } GLvoid keyboard (GLubyte key, GLint x, GLint y) { switch(key) { case KEY_ESC: /* exit when escape key pressed */ exit (0); break; default : break ; } } GLvoid specialkeys( GLint key, GLint x, GLint y) { switch (key) { case GLUT_KEY_F1: /* print Help information */ printHelp ( ); break; } } GLvoid checkError( const char* const label ) { GLenum error; error = glGetError(); while ( GL_NO_ERROR != error ) { fprintf( stderr,"%s: %s\n", label, gluErrorString(error) ); error = glGetError(); } } void reshape (GLint w, GLint h) { if (w >= h) { glViewport(0, 0, h, h); } else { glViewport(0, 0, w, w); } glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (30, 1.0, mNearClip, mFarClip); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); } /* Displays 6 faces of a cube */ void drawModel (void) { glBegin(GL_QUADS); glColor4f(0.0,0.0,1.0,1.0); glVertex3f(-0.5,-0.5,0.5); glVertex3f(0.5,-0.5,0.5); glVertex3f(0.5,0.5,0.5); glVertex3f(-0.5,0.5,0.5); glEnd(); glBegin(GL_QUADS); glColor4f(1.0,0.0,0.0,1.0); glVertex3f(0.5,-0.5,0.5); glVertex3f(0.5,-0.5,-0.5); glVertex3f(0.5,0.5,-0.5); glVertex3f(0.5,0.5,0.5); glEnd(); glBegin(GL_QUADS); glColor4f(1.0,1.0,0.0,1.0); glVertex3f(0.5,-0.5,-0.5); glVertex3f(-0.5,-0.5,-0.5); glVertex3f(-0.5,0.5,-0.5); glVertex3f(0.5,0.5,-0.5); glEnd(); glBegin(GL_QUADS); glColor4f(0.0,1.0,0.0,1.0); glVertex3f(-0.5,-0.5,-0.5); glVertex3f(-0.5,-0.5,0.5); glVertex3f(-0.5,0.5,0.5); glVertex3f(-0.5,0.5,-0.5); glEnd(); glBegin(GL_QUADS); glColor4f(1.0,0.0,1.0,1.0); glVertex3f(-0.5,-0.5,-0.5); glVertex3f(0.5,-0.5,-0.5); glVertex3f(0.5,-0.5,0.5); glVertex3f(-0.5,-0.5,0.5); glEnd(); glBegin(GL_QUADS); glColor4f(0.0,1.0,1.0,1.0); glVertex3f(-0.5,0.5,0.5); glVertex3f(0.5,0.5,0.5); glVertex3f(0.5,0.5,-0.5); glVertex3f(-0.5,0.5,-0.5); glEnd(); } GLvoid init(GLvoid) { glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glEnable (GL_CULL_FACE); glCullFace(GL_BACK); } GLvoid display(GLvoid) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Translate our origin to midway between the near and far clipping plane */ glTranslatef(0, 0, -(mNearClip+mFarClip)/2); /* Change the values of gluLookat to figure out what it does! :) */ gluLookAt( 1.2,-2,-3, /* camera position */ 0,0,0, /* centre of view */ 0,1,0); /* what direction is up */ drawModel (); glFlush(); } int main(int argc, char** argv) { glutInit (&argc, argv) ; glutInitDisplayMode ( GLUT_RGB | GLUT_DEPTH ) ; glutCreateWindow( argv[0] ); init(); glutDisplayFunc (display) ; glutKeyboardFunc(keyboard) ; glutSpecialFunc (specialkeys); glutReshapeFunc (reshape); printHelp( ); glutMainLoop() ; return 0 ; }