/* 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 #include "modulefont.h" static GLfloat view_rotx=50.0F ; static GLfloat view_roty=20.0F ; static GLfloat view_rotz=10.0F ; 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) { glPushMatrix() ; glScalef(r,r,h/2) ; glColor3f(1.0,1.0,1.0); glBegin(GL_LINES) ; for ( int 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) { glDisable(GL_LIGHTING) ; glPointSize(5.0); glColor3f(0.0,1.0,1.0); glBegin(GL_POINTS); for ( int i = 0 ; i < 16 ; i++ ) glVertex3fv(&pts[i*3]); glEnd(); glEnable(GL_LIGHTING) ; } void traceQuartTuyauBezierGL(float r,float h,int quart) { glPushMatrix() ; glScalef(r,r,h/2) ; float pts[16][3] ; switch ( quart ) { case 0 : { for ( int 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 ( int 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 ( int 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 ( int 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) { GLfloat diffuse[] = { 0.6F,0.6F,0.6F,1.0F }; GLfloat rouge[] = { 1.0F,0.0F,0.0F,1.0F }; GLfloat vert[] = { 0.0F,1.0F,0.0F,1.0F }; GLfloat bleu[] = { 0.0F,0.0F,1.0F,1.0F }; GLfloat jaune[] = { 1.0F,1.0F,0.0F,1.0F }; glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse); glPushMatrix() ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge); traceQuartTuyauBezierGL(r,h,0) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,vert); traceQuartTuyauBezierGL(r,h,1) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,bleu); traceQuartTuyauBezierGL(r,h,2) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,jaune); traceQuartTuyauBezierGL(r,h,3) ; glPopMatrix() ; } void CALLBACK display(void) { glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); glPushMatrix(); glEnable(GL_DEPTH_TEST); glRotatef(view_rotx,1.0,0.0,0.0); glRotatef(view_roty,0.0,1.0,0.0); glRotatef(view_rotz,0.0,0.0,1.0); glEnable(GL_LIGHTING); traceTuyauBezierGL(4.0F,9.0F) ; glDisable(GL_LIGHTING); if ( cyl ) traceTuyau(4.0F,9.0F) ; glPopMatrix(); glPushMatrix(); glDisable(GL_DEPTH_TEST); glColor3f(1.0,1.0,0.0); glRasterPos2f(-6.5,-6.5); char st[50] ; sprintf(st,"p = %5.2f",p); printString(st); glPopMatrix(); glFlush(); auxSwapBuffers(); } void CALLBACK myReshape(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 ambient[] = { 0.2F,0.2F,0.2F,1.0F }; GLfloat pos[] = { 0.0F,0.0F,2.0F,1.0F }; GLfloat spec[] = { 1.0F,1.0F,1.0F,1.0F }; GLfloat shininess[] = { 50.0F }; glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0,GL_AMBIENT,ambient); glLightfv(GL_LIGHT0,GL_POSITION,pos); glMaterialfv(GL_FRONT,GL_SPECULAR,spec); glMaterialfv(GL_FRONT,GL_SHININESS,shininess); } void myinit(void) { makeRasterFont(); glClearColor(0.0,0.0,0.0,1.0); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LESS); initlights(); } void CALLBACK keyRight(void) { p += 0.01F ; for ( int i = 0 ; i < 4 ; i++ ) { pts1[1+i*4][1] = p ; pts1[2+i*4][0] = p ; } } void CALLBACK keyLeft(void) { p -= 0.01F ; for ( int i = 0 ; i < 4 ; i++ ) { pts1[1+i*4][1] = p ; pts1[2+i*4][0] = p ; } } void CALLBACK keyt(void) { dy += 0.01F ; } void CALLBACK keyv(void) { dy -= 0.01F ; } void CALLBACK keyg(void) { dx += 0.01F ; } void CALLBACK keyf(void) { dx -= 0.01F ; } void CALLBACK keyEnter(void) { coul = 1 - coul ; } void CALLBACK keyEspace(void) { cyl = 1 - cyl ; } void CALLBACK keyp(void) { pt = 1 - pt ; } void CALLBACK keyx(void) { view_rotx += 2 ; } void CALLBACK keyX(void) { view_rotx -= 2 ; } void CALLBACK keyy(void) { view_roty += 2 ; } void CALLBACK keyY(void) { view_roty -= 2 ; } void CALLBACK keyz(void) { view_rotz += 2 ; } void CALLBACK keyZ(void) { view_rotz -= 2 ; } void CALLBACK keya(void) { ff = 1 - ff ; } void CALLBACK keyq(void) { np1++ ; } void CALLBACK keyQ(void) { np1-- ; if ( np1 < 3 ) np1 = 3 ; } void CALLBACK keys(void) { np2++ ; } void CALLBACK keyS(void) { np2-- ; if ( np2 < 3 ) np2 = 3 ; } void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH); auxInitPosition(0,0,300,300); auxInitWindow("Surface de Bezier : Tuyau courbe"); myinit(); auxKeyFunc(AUX_q,keyq) ; auxKeyFunc(AUX_Q,keyQ) ; auxKeyFunc(AUX_s,keys) ; auxKeyFunc(AUX_S,keyS) ; auxKeyFunc(AUX_a,keya) ; auxKeyFunc(AUX_p,keyp) ; auxKeyFunc(AUX_t,keyt) ; auxKeyFunc(AUX_f,keyf) ; auxKeyFunc(AUX_g,keyg) ; auxKeyFunc(AUX_v,keyv) ; auxKeyFunc(AUX_SPACE,keyEspace) ; auxKeyFunc(AUX_RETURN,keyEnter) ; auxKeyFunc(AUX_x,keyx) ; auxKeyFunc(AUX_X,keyX) ; auxKeyFunc(AUX_y,keyy) ; auxKeyFunc(AUX_Y,keyY) ; auxKeyFunc(AUX_z,keyz) ; auxKeyFunc(AUX_Z,keyZ) ; auxKeyFunc(AUX_RIGHT,keyRight) ; auxKeyFunc(AUX_LEFT,keyLeft) ; auxReshapeFunc(myReshape); auxMainLoop(display); }