/* Lignes polygonales 3D lissees et non lissees */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2012 */ /* Une molecule de benzene : C6H6 */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2012 */ #include #include #include #include #include #include "LignePolygonale3D.h" #include "Position3D.h" /* Variables et constantes globales */ static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float gris[] = { 0.7F,0.7F,0.7F,1.0F }; static float rx = 0.0F; static float ry = 0.0F; static float rz = 0.0F; static int n = 100; static int nb = 0; static int aff = 1; /* Fonction executee lors de l'appui */ /* d'une touche de curseur ou d'une touche */ /* page up ou page down */ static void special(int key,int x,int y) { switch(key) { case GLUT_KEY_UP : rx++; glutPostRedisplay(); break; case GLUT_KEY_DOWN : rx--; glutPostRedisplay(); break; case GLUT_KEY_LEFT : ry++; glutPostRedisplay(); break; case GLUT_KEY_RIGHT : ry--; glutPostRedisplay(); break; case GLUT_KEY_PAGE_UP : rz++; glutPostRedisplay(); break; case GLUT_KEY_PAGE_DOWN : rz--; glutPostRedisplay(); break; } } /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ static void init(void) { glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); } /* Scene dessinee */ static void scene(void) { LignePolygonale3D *lp = new LignePolygonale3D(10); lp->set(0,&Position3D(-6.0,-4.0, 1.0)); lp->set(1,&Position3D(-4.5,-3.0,-1.0)); lp->set(2,&Position3D(-2.0, 2.0,-2.0)); lp->set(3,&Position3D(-0.5, 4.0, 1.0)); lp->set(4,&Position3D( 1.5, 5.0, 2.0)); lp->set(5,&Position3D( 4.0, 4.0, 3.0)); lp->set(6,&Position3D( 3.0,-1.0, 1.0)); lp->set(7,&Position3D( 2.0, 3.0,-1.0)); lp->set(8,&Position3D(-1.0, 2.0,-2.0)); lp->set(9,&Position3D(-3.5,-4.0, 0.0)); glColor3f(1.0F,0.0F,1.0F); lp->drawOpenGL(1); switch (aff) { case 0 : { LignePolygonale3D *lpl = new LignePolygonale3D(n,&lp->t[nb],&lp->t[nb+1], &lp->t[nb+2],&lp->t[nb+3], (MatriceDeBase *) &NRUBS); glColor3f(1.0F,1.0F,0.0F); lpl->drawOpenGL(0); delete(lpl); } break; case 1 : { LignePolygonale3D *lpl = new LignePolygonale3D(n,lp,(MatriceDeBase *) &NRUBS); glColor3f(1.0F,1.0F,0.0F); lpl->drawOpenGL(0); delete(lpl); } break; } delete(lp); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); const GLfloat light0_position[] = { 1.0,1.0,1.0,0.0 }; const GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 }; const GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 }; glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glLightfv(GL_LIGHT1,GL_POSITION,light1_position); glLightfv(GL_LIGHT2,GL_POSITION,light2_position); glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE); glPushMatrix(); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); glRotatef(rz,0.0F,0.0F,1.0F); scene(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur OpenGL: %d\n",error); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* -> Ajustement de la camera de visualisation */ static void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30.0F,(float) x/y,1.0,50.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,10.0,15.0,0.0,0.0,0.0,0.0,1.0,0.0); } /* Fonction executee lors de la frappe */ /* d'une touche du clavier */ static void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : nb = (nb+1)%7; glutPostRedisplay(); break; case 0x20 : aff = (aff+1)%2; glutPostRedisplay(); break; case 43 : n++; glutPostRedisplay(); break; case 47 : n--; if ( n == 1 ) n = 2; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(450,300); glutInitWindowPosition(50,50); glutCreateWindow("Une courbe B-Spline"); init(); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return(0); }