/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Decembre 2005 */ /* Illustration de la generation */ /* d'une courbe B-Spline par morceaux */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleFont.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #include "Position.h" #include "LignePolygonale.h" static Position **pts; static int aff = 0 ; static int pt = 0 ; static int disc = 5 ; static int sph =1; static int scn = 1; static int nb = 0; static float mnru[4][4] = { { -0.1666F, 0.5F, -0.5F, 0.1666F }, { 0.5F ,-1.0F, 0.5F, 0.0F }, { -0.5F , 0.0F, 0.5F, 0.0F }, { 0.1666F, 0.66666, 0.1666F,0.0F } } ; static float mcro[4][4] = { { -0.5F, 1.5F,-1.5F, 0.5F }, { 1.0F,-2.5F, 2.0F,-0.5F }, { -0.5F, 0.0F, 0.5F, 0.0F }, { 0.0F, 1.0F, 0.0F, 0.0F } } ; static float mbez[4][4] = { { -1.0F, 3.0F,-3.0F, 1.0F }, { 3.0F,-6.0F, 3.0F, 0.0F }, { -3.0F, 3.0F, 0.0F, 0.0F }, { 1.0F, 0.0F, 0.0F, 0.0F } } ; void scene(int n,Position **pts,float *c1,float *c2) { LignePolygonale *lp = new LignePolygonale(n,pts); glColor3fv(c1); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,c1); lp->dessine(sph); LignePolygonale *bsp; switch (aff) { case 0 : bsp = lp->BSpline(new MatriceDeBase(mnru),disc); break; case 1 : bsp = lp->BSpline(new MatriceDeBase(mcro),disc); break; case 2 : bsp = lp->BSpline(new MatriceDeBase(mbez),disc); break; } glColor3fv(c2); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,c2); bsp->dessine(sph); delete(bsp); delete(lp); } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); if ( scn ) { scene(7,pts,couleurRouge(),couleurVert()); } else { if ( nb < 4 ) scene(4,&pts[nb],couleurRouge(),couleurVert()); else { scene(4,&pts[0],couleurRouge(),couleurJaune()); scene(4,&pts[1],couleurRouge(),couleurBleu()); scene(4,&pts[2],couleurRouge(),couleurMagenta()); scene(4,&pts[3],couleurRouge(),couleurCyan()); } } glPopMatrix(); glFlush(); glutSwapBuffers(); } void myinit(void) { glClearColor(0.8,0.8,0.8,1.0); float pt[7][3]; for ( int i = 0 ; i < 7 ; i++ ) { pt[i][0] = pts[i]->x; pt[i][1] = pts[i]->y; pt[i][2] = pts[i]->z; } glShadeModel(GL_FLAT); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); } void special(int k, int x, int y) { switch (k) { case GLUT_KEY_PAGE_UP : pts[pt]->z -= 0.1F ; glutPostRedisplay(); break; case GLUT_KEY_PAGE_DOWN : pts[pt]->z += 0.1F ; glutPostRedisplay(); break; case GLUT_KEY_UP : pts[pt]->y += 0.1F; glutPostRedisplay(); break; case GLUT_KEY_DOWN : pts[pt]->y -= 0.1F; glutPostRedisplay(); break; case GLUT_KEY_LEFT : pts[pt]->x -= 0.1F; glutPostRedisplay(); break; case GLUT_KEY_RIGHT : pts[pt]->x += 0.1F; glutPostRedisplay(); break; } } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 'b' : case 'B' : nb = (nb+1)%5; glutPostRedisplay(); break; case 's' : case 'S' : sph = !sph; glutPostRedisplay(); break; case 43 : disc++ ; glutPostRedisplay(); break; case 45 : disc-- ; if ( disc < 1 ) disc = 1 ; glutPostRedisplay(); break; case 0x0D : aff = (aff+1)%3 ; glutPostRedisplay(); break; case ' ' : scn = !scn; glutPostRedisplay(); break; case 'p' : case 'P' : pt = (pt+1)%7 ; glutPostRedisplay(); break; } } int main(int argc,char **argv) { pts =(Position **) calloc(7,sizeof(Position *)); pts[0] = new Position( 1.0,-3.0,-3.0); pts[1] = new Position(-2.0, 2.0, 3.0); pts[2] = new Position( 2.0, 3.0,-2.0); pts[3] = new Position(-3.0, 0.0,-4.0); pts[4] = new Position(-2.0,-3.0, 2.0); pts[5] = new Position( 3.0,-4.0,-1.0); pts[6] = new Position(-1.0, 1.0,3.0); glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Courbe B-Spline par morceaux"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-5.0,5.0,-5.0,5.0,-50.0,50.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(special); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); return(0); }