/* Auteur: Nicolas JANEY */ /* Avril 2001 */ /* Utilisation d'une courbe de Bezier */ /* pour generer un objet (un tuyau) */ /* par revolution */ #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleFont.h" #include "ModuleMenus.h" #include "ModuleReshape.h" static float dx = 0.0F ; static float dy = 0.0F ; static int cyl = 0 ; static int coul = 0 ; static int pt = 0 ; static int ff = 0 ; static int np1 = 20 ; static int np2 = 20 ; float p = 0.55F ; float pts1[16][3] = { {1.0F,0.0F,1.0F}, {1.0F,p,1.0F}, {p,1.0F,1.0F}, {0.0F,1.0F,1.0F}, {1.0F,0.0F,0.3F}, {1.0F,p,0.3F}, {p,1.0F,0.3F}, {0.0F,1.0F,0.3F}, {1.0F,0.0F,-0.3F}, {1.0F,p,-0.3F}, {p,1.0F,-0.3F}, {0.0F,1.0F,-0.3F}, {1.0F,0.0F,-1.0F}, {1.0F,p,-1.0F}, {p,1.0F,-1.0F}, {0.0F,1.0F,-1.0F} } ; void traceTuyau(float r,float h) { int i; glPushMatrix() ; glScalef(r,r,h/2) ; glColor4fv(couleurBlanc()); glBegin(GL_LINES) ; for ( i = 0 ; i < 90 ; i++ ) { float x =(float) cos(i*3.1459/45) ; float y =(float) sin(i*3.1459/45) ; glVertex3f(x,y,1.0F) ; glVertex3f(x,y,-1.0F) ; } glEnd() ; glBegin(GL_LINE_LOOP) ; for ( i = 0 ; i < 90 ; i++ ) { float x =(float) cos(i*3.1459/45) ; float y =(float) sin(i*3.1459/45) ; glVertex3f(x,y,1.0F) ; } glEnd() ; glBegin(GL_LINE_LOOP) ; for ( i = 0 ; i < 90 ; i++ ) { float x =(float) cos(i*3.1459/45) ; float y =(float) sin(i*3.1459/45) ; glVertex3f(x,y,-1.0F) ; } glEnd() ; glPopMatrix() ; } void points(float *pts) { int i; glDisable(GL_LIGHTING) ; glPointSize(5.0); glColor4fv(couleurCyan()); glBegin(GL_POINTS); for ( i = 0 ; i < 16 ; i++ ) glVertex3fv(&pts[i*3]); glEnd(); glEnable(GL_LIGHTING) ; } void traceQuartTuyauBezierGL(float r,float h,int quart) { int i; glPushMatrix() ; glScalef(r,r,h/2) ; float pts[16][3] ; switch ( quart ) { case 0 : { for ( i = 4 ; i < 12 ; i++ ) { pts[i][0] = pts1[i][0]+dx ; pts[i][1] = pts1[i][1]+dy ; pts[i][2] = pts1[i][2] ; } for ( i = 0 ; i < 4 ; i++ ) { pts[i][0] = pts1[i][0] ; pts[i][1] = pts1[i][1] ; pts[i][2] = pts1[i][2] ; } for ( i = 12 ; i < 16 ; i++ ) { pts[i][0] = pts1[i][0] ; pts[i][1] = pts1[i][1] ; pts[i][2] = pts1[i][2] ; } } break ; case 1 : { for ( i = 4 ; i < 12 ; i++ ) { pts[i][0] = pts1[i][1]+dx ; pts[i][1] = -pts1[i][0]+dy ; pts[i][2] = pts1[i][2] ; } for ( i = 0 ; i < 4 ; i++ ) { pts[i][0] = pts1[i][1] ; pts[i][1] = -pts1[i][0] ; pts[i][2] = pts1[i][2] ; } for ( i = 12 ; i < 16 ; i++ ) { pts[i][0] = pts1[i][1] ; pts[i][1] = -pts1[i][0] ; pts[i][2] = pts1[i][2] ; } } break ; case 2 : { for ( i = 4 ; i < 12 ; i++ ) { pts[i][0] = -pts1[i][0]+dx ; pts[i][1] = -pts1[i][1]+dy ; pts[i][2] = pts1[i][2] ; } for ( i = 0 ; i < 4 ; i++ ) { pts[i][0] = -pts1[i][0] ; pts[i][1] = -pts1[i][1] ; pts[i][2] = pts1[i][2] ; } for ( i = 12 ; i < 16 ; i++ ) { pts[i][0] = -pts1[i][0] ; pts[i][1] = -pts1[i][1] ; pts[i][2] = pts1[i][2] ; } } break ; case 3 : { for ( i = 4 ; i < 12 ; i++ ) { pts[i][0] = -pts1[i][1]+dx ; pts[i][1] = pts1[i][0]+dy ; pts[i][2] = pts1[i][2] ; } for ( i = 0 ; i < 4 ; i++ ) { pts[i][0] = -pts1[i][1] ; pts[i][1] = pts1[i][0] ; pts[i][2] = pts1[i][2] ; } for ( i = 12 ; i < 16 ; i++ ) { pts[i][0] = -pts1[i][1] ; pts[i][1] = pts1[i][0] ; pts[i][2] = pts1[i][2] ; } } break ; } if ( pt ) points((float *) pts) ; glMap2f(GL_MAP2_VERTEX_3,0,1,3,4,0,1,12,4,(float *) pts); glEnable(GL_MAP2_VERTEX_3); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glMapGrid2f(np1,0.0,1.0,np2,0.0,1.0); if ( ff ) glEvalMesh2(GL_LINE,0,np1,0,np2); else glEvalMesh2(GL_FILL,0,np1,0,np2); glPopMatrix() ; } void traceTuyauBezierGL(float r,float h) { glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisMoyen()); glPushMatrix() ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge()); traceQuartTuyauBezierGL(r,h,0) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert()); traceQuartTuyauBezierGL(r,h,1) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu()); traceQuartTuyauBezierGL(r,h,2) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune()); traceQuartTuyauBezierGL(r,h,3) ; glPopMatrix() ; } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); traceTuyauBezierGL(4.0F,9.0F) ; glDisable(GL_LIGHTING); if ( cyl ) traceTuyau(4.0F,9.0F) ; glPopMatrix(); glPushMatrix(); glDisable(GL_DEPTH_TEST); glColor4fv(couleurJaune()); placeFontCursor(-6.5F,-6.5F,0.0F); simpleBitmapOutput("p = %5.2f",p); glPopMatrix(); glFlush(); glutSwapBuffers(); } void reshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if( w <= h ) { float y = 7.F*(float)h/(float)w ; glOrtho(-7.,7.,-y,y,-17.,17.); } else { float x = 7.F*(float)w/(float)h ; glOrtho(-x,x,-7.,7.,-17.,17.); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void initlights(void) { GLfloat pos[] = { 0.0F,0.0F,2.0F,1.0F }; GLfloat shininess[] = { 50.0F }; glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0,GL_AMBIENT,couleurGrisFonce()); glLightfv(GL_LIGHT0,GL_POSITION,pos); glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc()); glMaterialfv(GL_FRONT,GL_SHININESS,shininess); } void myinit(void) { glClearColor(0.0,0.0,0.0,1.0); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LESS); initlights(); } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 43 : { p += 0.01F ; for ( int i = 0 ; i < 4 ; i++ ) { pts1[1+i*4][1] = p ; pts1[2+i*4][0] = p ; } } glutPostRedisplay(); break; case 45 : { p -= 0.01F ; for ( int i = 0 ; i < 4 ; i++ ) { pts1[1+i*4][1] = p ; pts1[2+i*4][0] = p ; } } glutPostRedisplay(); break; case ' ' : cyl = 1-cyl; glutPostRedisplay(); break; case 0x0D : coul = 1-coul; glutPostRedisplay(); break; case 't' : dy += 0.01F ; glutPostRedisplay(); break; case 'v' : dy -= 0.01F ; glutPostRedisplay(); break; case 'g' : dx += 0.01F ; glutPostRedisplay(); break; case 'f' : dx -= 0.01F ; glutPostRedisplay(); break; case 'p' : pt = 1-pt; glutPostRedisplay(); break; case 'a' : ff = 1-ff; glutPostRedisplay(); break; case 'q' : np1++; glutPostRedisplay(); break; case 'Q' : np1--; if ( np1 < 3 ) np1 = 3; glutPostRedisplay(); break; case 's' : np2++; glutPostRedisplay(); break; case 'S' : np2--; if ( np2 < 3 ) np2 = 3; glutPostRedisplay(); break; } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Surface de Bezier : Tuyau courbe"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-7.0,7.0,-7.0,7.0,-500.0,500.0); setManipulateurDistance(1.0F); setManipulateurClavierAngle(10.0F,20.0F,30.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); return(0); }