/* 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 */ #include #include #include #include #include #include #include "ModuleAxes.h" /* Variables globales */ static int l = 1; static int tc = 0; static float zoom = 1.0F; static float px = 0.0F; static float py = 0.0F; static float pz = 0.0F; static float rx = 20.0F; static float ry = -35.0F; static int clic = 0; static int mx; static int my; /* 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 orthographique */ void reshape0(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(); glOrtho((double) -1.2*tx/ty,(double) 1.2*tx/ty, -1.2,1.2, -5.0,5.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection orthographique */ void reshape1(int tx,int ty) { /* Configuration du viewport d'affichage */ glViewport(0,0,tx,ty); /* Choix et configuration de la camera */ /* de visualisation */ /* Placement de la scene devant la camera */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum((double) -0.65*tx/ty,(double) 0.65*tx/ty, -0.65,0.65, 5.0,15.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0F,0.0F,-10.0F); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection orthographique */ void reshape2(int tx,int ty) { /* Configuration du viewport d'affichage */ glViewport(0,0,tx,ty); /* Choix et configuration de la camera */ /* de visualisation */ /* Placement de la scene devant la camera */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(14.7,(double) tx/ty,5.0,15.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0F,0.0F,-10.0F); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection orthographique */ void reshape3(int tx,int ty) { /* Configuration du viewport d'affichage */ glViewport(0,0,tx,ty); /* Choix et configuration de la camera */ /* de visualisation */ /* Placement de la scene devant la camera */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum((double) -0.555*tx/ty,(double) 0.555*tx/ty, -0.555,0.555, 2.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0F,0.0F,-5.0F); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection orthographique */ void reshape4(int tx,int ty) { /* Configuration du viewport d'affichage */ glViewport(0,0,tx,ty); /* Choix et configuration de la camera */ /* de visualisation */ /* Placement de la scene devant la camera */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(31.0,(double) tx/ty,2.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0F,0.0F,-5.0F); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection orthographique */ void reshape5(int tx,int ty) { /* Configuration du viewport d'affichage */ glViewport(0,0,tx,ty); /* Choix et configuration de la camera */ /* de visualisation */ /* Placement de la scene devant la camera */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum((double) -0.325*tx/ty,(double) 0.325*tx/ty, -0.325,0.325, 0.5,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0F,0.0F,-2.5F); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection orthographique */ void reshape6(int tx,int ty) { /* Configuration du viewport d'affichage */ glViewport(0,0,tx,ty); /* Choix et configuration de la camera */ /* de visualisation */ /* Placement de la scene devant la camera */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(66.0,(double) tx/ty,0.5,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0F,0.0F,-2.5F); } typedef void (*fonctionIntInt) (int,int); static fonctionIntInt reshape[7] = { reshape0, reshape1, reshape2, reshape3, reshape4, reshape5, reshape6 }; /* 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); /* Gestion de l'interactivite */ glPushMatrix(); { glTranslatef(px,py,pz); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); glScalef(zoom,zoom,zoom); } /* Affichage de la scene */ axes(); 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 'x' : px -= zoom/50.0F; glutPostRedisplay(); break; case 'X' : px += zoom/50.0F; glutPostRedisplay(); break; case 'y' : py -= zoom/50.0F; glutPostRedisplay(); break; case 'Y' : py += zoom/50.0F; 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 0x0D : l = !l ; glutPostRedisplay(); break; case 0x20 : tc = (tc+1)%7 ; glutReshapeFunc(reshape[tc]); reshape[tc](glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT)); glutPostRedisplay(); break; case 0x1B : exit(0); } } /* Fonction executee lors de la frappe */ /* d'une touche non alphanumerique du clavier */ void special(int key,int x,int y) { switch ( key ) { case GLUT_KEY_PAGE_UP : zoom *= 1.01F; glutPostRedisplay(); break; case GLUT_KEY_PAGE_DOWN : zoom /= 1.01F; glutPostRedisplay(); break; case GLUT_KEY_UP : rx -= 1.0F; glutPostRedisplay(); break; case GLUT_KEY_DOWN : rx += 1.0F; glutPostRedisplay(); break; case GLUT_KEY_RIGHT : ry += 1.0F; glutPostRedisplay(); break; case GLUT_KEY_LEFT : ry -= 1.0F; glutPostRedisplay(); break; } } /* Fonction executee lors d'un clic de souris */ /* dans la fenetre */ void mouse(int bouton,int etat,int x,int y) { if ( bouton == GLUT_LEFT_BUTTON ) { if ( etat == GLUT_DOWN ) { clic = 1; mx = x; my = y; } if ( etat == GLUT_UP ) { clic = 0; } } } /* Fonction executee lors d'un deplacement */ /* de la souris sur la fenetre */ /* avec un bouton appuye */ void motion(int x,int y) { if ( clic ) { ry += (x-mx); rx += (y-my); mx = x; my = y; glutPostRedisplay(); } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(200,200); glutInitWindowPosition(50,50); glutCreateWindow("Cameras OpenGL"); init(); glutReshapeFunc(reshape0); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutMouseFunc(mouse); glutMotionFunc(motion); glutDisplayFunc(display); glutMainLoop(); return(0); }