/* Courbe de Bezier */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 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 "LignePolygonale3D.h" /* Variables et constantes globales */ /* pour les angles et les couleurs utilises */ static int anim = 0; static float rx = 0.0F; static float ry = 0.0F; static float rz = 0.0F; static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float jaune[] = { 1.0F,1.0F,0.0F,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 LignePolygonale3D *lp = NULL; static int np = 50; static int n = 21; /* Generation d'un nombre aleatoire reel */ /* compris entre -v et v */ double random(double v) { return((v*(rand()%20000)/10000.0)-v); } static void initLp(void) { if ( lp ) delete(lp); lp = new LignePolygonale3D(n); for ( int i = 0 ; i < n ; i++ ) { double fct =(double) 20.0/(n-1); lp->t[i]->c[0] = 5.0*cos((double)i)+random(0.5); lp->t[i]->c[1] = 10.0-(i*fct)+random(fct), lp->t[i]->c[2] = 5.0*sin((double)i)+random(0.5); } } static void clean(void) { if ( lp ) { delete(lp); lp = NULL; } } /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ static void initGL(void) { glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); } /* Scene dessinee */ static void scene(void) { glPushMatrix(); glLineWidth(2.0); glColor3fv(jaune); lp->draw(); LignePolygonale3D *ll = new LignePolygonale3D(lp,np); glColor3fv(rouge); ll->draw(); delete(ll); glLineWidth(1.0); glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { glClearColor(0.5F,0.5F,0.5F,0.5F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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("Attention erreur %d\n",error); } /* Fonction executee lorsqu'aucun evenement */ /* n'est en file d'attente */ static void idle(void) { rx += 0.3355F; ry += 0.6117F; rz += 0.4174F; glutPostRedisplay(); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ static void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30.0F,(float) x/y,30.0,70.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,50.0,0.0,0.0,0.0,0.0,1.0,0.0); } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ static void keyboard(unsigned char key,int x,int y) { switch (key) { case 'p' : n--; if ( n < 2 ) n = 2; initLp(); glutPostRedisplay(); break; case 'P' : n++; initLp(); glutPostRedisplay(); break; case 'm' : initLp(); glutPostRedisplay(); break; case 0x0D : anim = !anim; glutIdleFunc(( anim ) ? idle : NULL); break; case 43 : np++; glutPostRedisplay(); break; case 45 : np--; if ( np < 2 ) np = 2; glutPostRedisplay(); break; case 0x1B : clean(); exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { #ifdef ____CHECKMEM____ _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //_crtBreakAlloc = 431; #endif atexit(clean); initLp(); glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Une courbe de Bezier"); initGL(); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); clean(); return(0); }