/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2007 */ /* Un programme OpenGL de dessin d'un cube */ /* en fil de fer visualise par differentes */ /* cameras selon des points de vue configures */ /* au moyen de la fonction gluLookAt */ #include #include #include #include #include #include #include "ModuleAxes.h" /* Variables globales */ static int l = 1; static float px = 0.0F; static float py = 0.0F; static float pz = 10.0F; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ void init(void) { const GLfloat l_pos[] = { 1.0F,1.0F,1.0F,0.0F }; const GLfloat c[4] = { 0.5F,0.2F,0.6F,1.0F }; glMaterialfv(GL_FRONT,GL_DIFFUSE,c); glLightfv(GL_LIGHT0,GL_POSITION,l_pos); glEnable(GL_LIGHT0); glColor4fv(c) ; glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection en perspective */ void reshape(int tx,int ty) { /* Configuration du viewport d'affichage */ glViewport(0,0,tx,ty); /* Choix et configuration de la camera */ /* de visualisation */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(15,(double) tx/ty,1.0,100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Scene dessinee */ void scene(void) { glPushMatrix(); glutWireCube(1.3F); glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.6F,0.6F,0.6F,1.0F) ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ; if ( l ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); glPushMatrix(); gluLookAt(px,py,pz, 0.0,0.0,0.0, 0.0,1.0,0.0); axes(); /* Affichage de la scene */ scene(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } /* Fonction executee lors de la frappe */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : l = !l ; glutPostRedisplay(); break; case 'x' : px -= 0.1F; glutPostRedisplay(); break; case 'X' : px += 0.1F; glutPostRedisplay(); break; case 'y' : py -= 0.1F; glutPostRedisplay(); break; case 'Y' : py += 0.1F; glutPostRedisplay(); break; case 'z' : pz -= 0.1F; glutPostRedisplay(); break; case 'Z' : pz += 0.1F; glutPostRedisplay(); break; case 'r' : case 'R' : { static int tf = 0; tf = (tf+1)%3; switch (tf) { case 0 : glutReshapeWindow(200,200); break; case 1 : glutReshapeWindow(400,150); break; case 2 : glutReshapeWindow(100,300); break; } } break; case 0x1B : exit(0); } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(250,250); glutInitWindowPosition(50,50); glutCreateWindow("Cameras avec glutLookAt"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return(0); }