/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mai 2002 */ /* Examen de TD 2000-2001 : Question 2 */ /* Test de parallelisme entre facettes */ /* triangulaires */ #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleReshape.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleFont.h" struct coord3D { float x; float y; float z; } ; struct facette { coord3D *s; } ; struct direc3D { float x; float y; float z; } ; static coord3D t1[3] = { { 2.0F, 6.0F, -2.0F}, { 10.0F, 2.0F, -1.0F}, { 6.0F,-4.0F, 0.0F} } ; static facette f1 = { t1 }; static coord3D t2[3] = { { -9.0F, 4.0F, 0.0F}, { -1.0F, 0.0F, 1.0F}, { -5.0F,-6.0F, 2.0F} } ; static facette f2 = { t2 }; static int aff = 0; /* ************************************************** */ /* -------------------------------------------------- */ /* Affichage OpenGL d'une facette */ /* -------------------------------------------------- */ void affichageFacette(facette *f) { glBegin(GL_POLYGON); for ( int i = 0 ; i < 3 ; 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) ; glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurRouge()); glLightfv(GL_LIGHT0,GL_SPECULAR,couleurRouge()); 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 testParallelisme(facette *f1,facette *f2) { direc3D v1; direc3D v2; direc3D n; direc3D v; vecteur(&f1->s[0],&f1->s[1],&v1); vecteur(&f1->s[1],&f1->s[2],&v2); produitVectoriel(&v1,&v2,&n); vecteur(&f2->s[0],&f2->s[1],&v); if ( !egalZero(produitScalaire(&n,&v)) ) return(0); else { vecteur(&f2->s[0],&f2->s[2],&v); if ( !egalZero(produitScalaire(&n,&v)) ) 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); affichageFacette(&f1); affichageFacette(&f2); 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 ( testParallelisme(&f1,&f2) ) strokeOutput(xmin+5*tpix,ymax-24*tpix,1.65F,"Parallele"); else strokeOutput(xmin+5*tpix,ymax-24*tpix,1.65F,"Non parallele"); 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 43 : f2.s[1].z += 0.5F ; glutPostRedisplay(); break; case 45 : f2.s[1].z -= 0.5F ; glutPostRedisplay(); break; } } /* -------------------------------------------------- */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH); glutInitWindowPosition(10,10); glutInitWindowSize(350,200); glutCreateWindow("Exercice 3 : Test de parallelisme de facettes"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-7.0,7.0,-7.0,7.0,-50.0,50.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); return(0); } /* ************************************************** */