/* Auteur: Nicolas JANEY */ /* Avril 2001 */ /* Utilisation d'une courbe de Bezier */ /* pour generer un objet (un vase) */ /* par revolution */ #include #include #include #include #include #include static GLfloat view_rotx=50.0F ; static GLfloat view_roty=20.0F ; static GLfloat view_rotz=10.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[10][2] = { {0.0F,-5.0F}, {5.0F,-5.0}, {4.5F,-4.0F}, {2.0F,-2.0F}, {1.5F,0.0F}, {0.2F,1.0F}, {0.2F,2.0F}, {1.5F,4.0F}, {1.5F,5.0F}, {0.5F,5.0F} } ; 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 < 40 ; i++ ) glVertex3fv(&pts[i*3]); glEnd(); glEnable(GL_LIGHTING) ; } void traceQuartVaseBezierGL(void) { glPushMatrix() ; float pts[10][4][3] ; for ( int i = 0 ; i < 10 ; i++ ) { pts[i][0][0] = pts1[i][0] ; pts[i][0][1] = pts1[i][1] ; pts[i][0][2] = 0.0F ; pts[i][1][0] = pts1[i][0] ; pts[i][1][1] = pts1[i][1] ; pts[i][1][2] = p*pts1[i][0] ; pts[i][2][0] = p*pts1[i][0] ; pts[i][2][1] = pts1[i][1] ; pts[i][2][2] = pts1[i][0] ; pts[i][3][0] = 0.0F ; pts[i][3][1] = pts1[i][1] ; pts[i][3][2] = pts1[i][0] ; } glMap2f(GL_MAP2_VERTEX_3, 0.0F,1.0F, 3,4, 0.0F,1.0F, 12,10, (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); if ( pt ) points((float *) pts) ; glPopMatrix() ; } void traceVaseBezierGL(void) { 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); traceQuartVaseBezierGL() ; glRotatef(90.0F,0.0F,1.0F,0.0F) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,vert); traceQuartVaseBezierGL() ; glRotatef(90.0F,0.0F,1.0F,0.0F) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,bleu); traceQuartVaseBezierGL() ; glRotatef(90.0F,0.0F,1.0F,0.0F) ; if ( coul ) glMaterialfv(GL_FRONT,GL_DIFFUSE,jaune); traceQuartVaseBezierGL() ; 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); traceVaseBezierGL() ; glDisable(GL_LIGHTING); 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,-1.0F,0.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) { glClearColor(0.0,0.0,0.0,1.0); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LESS); initlights(); } void CALLBACK keyEnter(void) { coul = 1 - coul ; } 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("Vase par revolution"); 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_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) ; auxReshapeFunc(myReshape); auxMainLoop(display); }