/* Une molecule de benzene : C6H6 */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Decembre 2012 */ #include #include #include #include #include #include "Position3D.h" #include "Direction3D.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 const float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; static const float vert[] = { 0.0F,1.0F,0.0F,1.0F }; static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; static float rx = 0.0F; static float ry = 0.0F; static float rz = 0.0F; static int aff = 1; static int niveau = 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 43 : niveau++; glutPostRedisplay(); break; case 45 : niveau--; if ( niveau < 0 ) niveau = 0; glutPostRedisplay(); break; case 0x20 : aff = (aff+1)%2; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } /* 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) { const GLfloat mat_shininess[] = { 50.0 }; glMaterialfv(GL_FRONT,GL_SPECULAR,blanc); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge); glLightfv(GL_LIGHT1,GL_DIFFUSE,vert); glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu); glLightfv(GL_LIGHT0,GL_SPECULAR,blanc); glLightfv(GL_LIGHT1,GL_SPECULAR,blanc); glLightfv(GL_LIGHT2,GL_SPECULAR,blanc); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); glEnable(GL_CULL_FACE); } ////////////////////////////////////////////////// /* Scene dessinee */ static void position3DIntermediaire(Position3D *si, Position3D *sf, Position3D *p) { p->c[0] = (si->c[0]+sf->c[0])/2.0; p->c[1] = (si->c[1]+sf->c[1])/2.0; p->c[2] = (si->c[2]+sf->c[2])/2.0; } static void decompositionRecursiveFacette(int n, Position3D *s1, Position3D *s2, Position3D *s3) { if ( n ) { Position3D s12; Position3D s23; Position3D s31; position3DIntermediaire(s1,s2,&s12); position3DIntermediaire(s2,s3,&s23); position3DIntermediaire(s3,s1,&s31); decompositionRecursiveFacette(n-1,s1,&s12,&s31); decompositionRecursiveFacette(n-1,s2,&s23,&s12); decompositionRecursiveFacette(n-1,s3,&s31,&s23); decompositionRecursiveFacette(n-1,&s31,&s12,&s23); } else { glVertex3f(s1->c[0],s1->c[1],s1->c[2]); glVertex3f(s2->c[0],s2->c[1],s2->c[2]); glVertex3f(s3->c[0],s3->c[1],s3->c[2]); } } static void affichageFacette(int n, Position3D *s1, Position3D *s2, Position3D *s3) { glPushMatrix(); Direction3D *nm = new Direction3D(s1,s2); Direction3D *d = new Direction3D(s1,s3); nm->produitVectoriel(d); nm->normalisation(); glNormal3f(nm->c[0],nm->c[1],nm->c[2]); delete(d); delete(nm); glBegin(GL_TRIANGLES); decompositionRecursiveFacette(niveau,s1,s2,s3); glEnd(); glPopMatrix(); } ////////////////////////////////////////////////// static void scene(void) { glPushMatrix(); Position3D *s1 = new Position3D(4.0,4.0,0.5); Position3D *s2 = new Position3D(-3.0,-2.0,0.0); Position3D *s3 = new Position3D(3.0,-4.0,-0.5); affichageFacette(niveau,s1,s2,s3); delete(s1); delete(s2); delete(s3); glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { //glClearColor(0.5F,0.5F,0.5F,1.0F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); const GLfloat light0_position[] = { 2.5,1.0,1.0,2.0 }; const GLfloat light1_position[] = { 1.0,0.0,1.0,1.5 }; const GLfloat light2_position[] = { 2.2,-2.0,1.0,1.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(25.0F,(float) x/y,1.0,50.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0); } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Decomposition recursive"); init(); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }