/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Juin 2001 */ /* Examen de TD 2000-2001 : Question 3 */ /* Test de planarite de facettes quelconques */ #include #include #include #include #include #include "ModuleReshape.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleFont.h" struct coord3D { float x; float y; float z; } ; struct facette { int ns; coord3D *s; } ; struct direc3D { float x; float y; float z; } ; static coord3D t1[6] = { { 1.0F, 6.0F, 0.0F}, { 7.0F, 2.0F, 0.0F}, { 5.0F,-4.0F, 0.0F}, { 1.0F,-1.0F, 0.0F}, {-3.0F,-5.0F, 0.0F}, {-6.0F, 4.0F, 0.0F} } ; static facette f1 = { 6,t1 }; static coord3D t2[6] = { { 1.0F, 6.0F, 0.0F}, { 7.0F, 2.0F, 0.0F}, { 5.0F,-4.0F, 1.0F}, { 1.0F,-1.0F, 3.0F}, {-3.0F,-5.0F, -2.0F}, {-6.0F, 4.0F, 0.0F} } ; static facette f2 = { 6,t2 }; static int aff = 0; /* ************************************************** */ /* -------------------------------------------------- */ /* Affichage OpenGL d'une facette */ /* -------------------------------------------------- */ void affichageFacette(facette *f) { glBegin(GL_POLYGON); for ( int i = 0 ; i < f->ns ; i++ ) glVertex3fv((float *) &f->s[i]); glEnd(); } /* -------------------------------------------------- */ /* Calcul du produit scalaire entre deux vecteurs */ /* -------------------------------------------------- */ float produitScalaire(direc3D *v1,direc3D *v2) { return(v1->x*v2->x+v1->y*v2->y+v1->z*v2->z); } /* -------------------------------------------------- */ /* Calcul du produit vectoriel entre deux vecteurs */ /* -------------------------------------------------- */ void produitVectoriel(direc3D *v1,direc3D *v2,direc3D *v) { v->x = v1->y*v2->z-v2->y*v1->z; v->y = v1->z*v2->x-v2->z*v1->x; v->z = v1->x*v2->y-v2->x*v1->y; } /* -------------------------------------------------- */ /* Calcul du vecteur entre deux sommets */ /* -------------------------------------------------- */ void vecteur(coord3D *p1,coord3D *p2,direc3D *v) { v->x = p2->x - p1->x; v->y = p2->y - p1->y; v->z = p2->z - p1->z; } /* -------------------------------------------------- */ /* La fonction myinit initialise les fonctionnalites */ /* suivantes : */ /* - une gestion des parties cachees */ /* - normalisation des normales specifiees */ /* -------------------------------------------------- */ void myinit(void) { glDepthFunc(GL_LESS); glEnable(GL_NORMALIZE) ; GLfloat diffuse[] = { 1.0F,0.0F,0.0F,1.0F }; GLfloat specular[] = { 1.0F,0.0F,0.0F,1.0F }; glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,specular); glEnable(GL_LIGHT0); } /* -------------------------------------------------- */ /* Test d'egalite a 0 a epsilon pres */ /* -------------------------------------------------- */ int egalZero(float v) { return(fabs(v) < 0.00001); } /* -------------------------------------------------- */ /* Fonction de test de planarite d'une facette */ /* -------------------------------------------------- */ int testPlanarite(facette *f) { if ( f->ns <= 3 ) return(1); direc3D v1; direc3D v2; direc3D n; vecteur(&f->s[1],&f->s[2],&v1); vecteur(&f->s[2],&f->s[3],&v2); produitVectoriel(&v1,&v2,&n); for ( int i = 2 ; i < f->ns-1 ; i++ ) { direc3D v; vecteur(&f->s[i],&f->s[i+1],&v); float scal = produitScalaire(&n,&v); if ( !egalZero(scal) ) return(0); } return(1); } /* -------------------------------------------------- */ /* Fonction display affichant une facette et testant */ /* sa planarite */ /* -------------------------------------------------- */ void display(void) { glClearColor(0.3F,0.3F,0.3F,0.0F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLfloat position[] = { 0.0F,0.0F,10.0F,1.0F }; glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); glLightfv(GL_LIGHT0,GL_POSITION,position); facette *f; switch (aff) { case 0 : f = &f1; break; case 1 : f = &f2; break; } affichageFacette(f); glPopMatrix(); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glPushMatrix(); float xmin = getXmin(); float ymax = getYmax(); float tpix = getTaillePixel(); setAntialiased(1); setBold(1); setEcartementCaracteres(10.0F); if ( testPlanarite(f) ) strokeOutput(xmin+5*tpix,ymax-24*tpix,1.65F,"Planaire"); else strokeOutput(xmin+5*tpix,ymax-24*tpix,1.65F,"Non planaire"); setAntialiased(0); setBold(0); setEcartementCaracteres(0.0F); glPopMatrix(); glFlush(); glutSwapBuffers(); } /* -------------------------------------------------- */ void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 32 : aff = (aff+1)%2 ; glutPostRedisplay(); break; } } /* -------------------------------------------------- */ void main(void) { glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowPosition(10,10); glutInitWindowSize(300,300); glutCreateWindow("Exercice 3 : Test de planarite de facettes"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-8.0,8.0,-8.0,8.0,-500.0,500.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); } /* ************************************************** */