/* Test de planarite pour un polygone */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2012 */ #define ____CHECKMEM____ #ifdef ____CHECKMEM____ #define _CRTDBG_MAP_ALLOC #ifdef _DEBUG #define _AFXDLL #include #endif #include #include #else #include #endif #include #include #include #include #include #include "Position3D.h" #include "Direction3D.h" #include "Polygone3D.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 scn = 1; static Position3D *p1 = new Position3D(-3.0, 7.0, 1.0); static Position3D *p2 = new Position3D(-5.0,-2.0, 2.0); static Position3D *p3 = new Position3D(-2.0,-6.0, 3.0); static Position3D *p4 = new Position3D( 6.0,-3.0, 4.0); static Position3D *p5 = new Position3D( 5.0, 4.0, 5.0); static void clean(void) { delete(p5); delete(p4); delete(p3); delete(p2); delete(p1); } /* Fonction executee lors de la frappe */ /* d'une touche du clavier */ static void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x20 : aff = (aff+1)%2; glutPostRedisplay(); break; case 0x0D : scn = !scn; glutPostRedisplay(); break; case 0x1B : clean(); 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) { glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); } static void scene(void) { if ( scn ) { p4->c[2] = -2.0; p5->c[2] = 5.0; } else { Direction3D *d12 = new Direction3D(p1,p2); Direction3D *d13 = new Direction3D(p1,p3); d12->produitVectoriel(d13); double a = d12->c[0]; double b = d12->c[1]; double c = d12->c[2]; double d = -a*p1->c[0]-b*p1->c[1]-c*p1->c[2]; delete(d12); delete(d13); p4->c[2] = (-d-a*p4->c[0]-b*p4->c[1])/c; p5->c[2] = (-d-a*p5->c[0]-b*p5->c[1])/c; } printf("P1 : "); p1->print(); printf("\n"); printf("P2 : "); p2->print(); printf("\n"); printf("P3 : "); p3->print(); printf("\n"); printf("P4 : "); p4->print(); printf("\n"); printf("P5 : "); p5->print(); printf("\n"); Polygone3D *p = new Polygone3D(); p->add(p1); p->add(p2); p->add(p3); p->add(p4); p->add(p5); if ( p->testPlanarite() ) printf("Facette planaire\n"); else printf("Facette non planaire\n"); glPushMatrix(); p->draw(); glPopMatrix(); delete(p); } /* 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); 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(50.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) { #ifdef ____CHECKMEM____ _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //_crtBreakAlloc = 332; #endif glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Test planarite polygone"); init(); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }