/* Etude des transformations geometriques dans OpenGL * 1- On visualise interactivement un cube et les limites du frustum */ #include #include GLdouble oeilZ = 3.0; /* fonction de dessin */ void display() { /* Tout effacer */ glClear( GL_COLOR_BUFFER_BIT ); /* Initialiser les transformations geometriques */ glLoadIdentity(); /* Position camera (transformations inverses) */ glTranslatef( 0,0,-oeilZ ); /* Position de l'objet */ /* Objet dans son repere local */ glBegin( GL_LINE_LOOP ); glVertex3f( -1.0, -1.0, 1.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); glEnd(); glBegin( GL_LINE_LOOP ); glVertex3f( -1.0, -1.0, -1.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); glEnd(); glBegin( GL_LINES ); glVertex3f( -1.0, -1.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); glEnd(); /* Declencher l'affichage */ glFlush(); } /* Prise en compte des touches frappees */ static void key( unsigned char key, int x, int y ) { switch( key ){ case 27: /* touche ESCAPE */ exit(0); case 'p': oeilZ +=0.1; printf("oeilZ: %f\n",oeilZ); glutPostRedisplay(); break; case 'm': oeilZ -=0.1; printf("oeilZ: %f\n",oeilZ); glutPostRedisplay(); break; } } /* Transformation viewport */ static void reshape( GLsizei w, GLsizei h ) { glViewport( 0, 0, w, h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glFrustum( -2, 2, -2, 2, 1, 5 ); glMatrixMode( GL_MODELVIEW ); } /* programme principal */ int main( int argc, char* argv[] ) { /* Initialisations */ glutInit( &argc, argv ); glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); glutInitWindowSize( 400, 400 ); glutCreateWindow("Cube"); glClearColor( 0, 0, 0, 0 ); printf("Tapez m ou p pour vous rapprocher ou vous eloigner\n"); printf("Tapez ESCAPE pour quitter l'application\n"); /* Fonctions appelees dans la boucle */ glutKeyboardFunc( key ); /* touches du clavier */ glutDisplayFunc( display ); /* affichage */ glutReshapeFunc( reshape ); /* redimensionnement */ /* Lancement de la boucle principale */ glutMainLoop(); /* Cette instruction n'est jamais executee */ return 0; }