/* Auteur: Nicolas JANEY */ /* Avril 2001 */ /* Fonctions de dessin de cylindre */ #include #include #include #include #include #include #include static float anglex = 0.0F ; static float angley = 0.0F ; static int np = 40; struct point3D { float x ; float y ; float z ; } ; struct vecteur3D { float dx ; float dy ; float dz ; } ; void cylindre(double r,double h) { point3D *tab =(point3D *) malloc(2*np*sizeof(point3D)) ; for ( int i = 0 ; i < np ; i++ ) { double a = 3.14159/np*2*i ; double cs = cos(a) ; double sn = sin(a) ; tab[i].x = tab[i+np].x =(float) (r*cs) ; tab[i].y =(float) (h/2.0) ; tab[i+np].y =(float) (-h/2.0) ; tab[i].z = tab[i+np].z =(float) (-r*sn) ; } glBegin(GL_POLYGON) ; for ( i = 0 ; i < np ; i++ ) { glVertex3fv((float *) &tab[i]) ; } glEnd() ; glBegin(GL_POLYGON) ; for ( i = 2*np-1 ; i >= np ; i-- ) { glVertex3fv((float *) &tab[i]) ; } glEnd() ; glBegin(GL_QUAD_STRIP) ; for ( i = 0 ; i <= np ; i++ ) { glVertex3fv((float *) &tab[i%np]); glVertex3fv((float *) &tab[(i%np)+np]); } glEnd() ; free(tab) ; } void cylindreAvecNormales(double r,double h) { float normale[3]; glGetFloatv(GL_CURRENT_NORMAL,normale) ; point3D *tab =(point3D *) malloc(2*np*sizeof(point3D)) ; vecteur3D *norm =(vecteur3D *) malloc(np*sizeof(vecteur3D)) ; for ( int i = 0 ; i < np ; i++ ) { double a = 3.14159/np*2*i ; double cs = cos(a) ; double sn = sin(a) ; tab[i].x = tab[i+np].x =(float) (r*cs) ; tab[i].y =(float) (h/2.0) ; tab[i+np].y =(float) (-h/2.0) ; tab[i].z = tab[i+np].z =(float) (-r*sn) ; norm[i].dx =(float) cs ; norm[i].dy =(float) 0.0 ; norm[i].dz =(float) -sn ; } glBegin(GL_POLYGON) ; for ( i = 0 ; i < np ; i++ ) { glNormal3f(0.0f,1.0f,0.0f); glVertex3fv((float *) &tab[i]) ; } glEnd() ; glBegin(GL_POLYGON) ; for ( i = 2*np-1 ; i >= np ; i-- ) { glNormal3f(0.0f,-1.0f,0.0f); glVertex3fv((float *) &tab[i]) ; } glEnd() ; glBegin(GL_QUAD_STRIP) ; for ( i = 0 ; i <= np ; i++ ) { glNormal3fv((float *) &norm[i%np]); glVertex3fv((float *) &tab[i%np]); glVertex3fv((float *) &tab[(i%np)+np]); } glEnd() ; free(tab) ; free(norm) ; glNormal3fv(normale) ; } void CALLBACK display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(angley,0.0F,1.0F,0.0F) ; glRotatef(anglex,1.0F,0.0F,0.0F) ; glPushMatrix(); float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; float jaune[] = { 1.0F,1.0F,0.0F,1.0F }; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,bleu) ; glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,jaune) ; glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90) ; glTranslatef(0.0F,0.65F,0.0F) ; glNormal3f(0.0f,0.0f,1.0f); cylindre(0.4,0.8) ; glPopMatrix(); glPushMatrix(); float rouge[] = { 1.0F,0.2F,0.2F,1.0F }; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,rouge) ; glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,jaune) ; glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90) ; glTranslatef(0.0F,-0.65F,0.0F) ; cylindreAvecNormales(0.4,0.8) ; glPopMatrix(); glPopMatrix(); glFlush(); auxSwapBuffers(); } void myinit (void) { glClearColor(0.5F,0.5F,0.5F,0.0F); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); float jaune[] = { 0.6F,0.6F,0.0F,1.0F }; glLightfv(GL_LIGHT0,GL_SPECULAR,jaune) ; glEnable(GL_CULL_FACE); glCullFace(GL_BACK); } void CALLBACK myReshape(int w, int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.3,1.3,-1.3/w*h,1.3/w*h,-3.0,3.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void CALLBACK up(void) { anglex++ ; } void CALLBACK down(void) { anglex-- ; } void CALLBACK left(void) { angley++ ; } void CALLBACK right(void) { angley-- ; } void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH); auxInitPosition (0,0,300,300); auxInitWindow("Cylindre") ; myinit(); auxKeyFunc(AUX_UP,up) ; auxKeyFunc(AUX_DOWN,down) ; auxKeyFunc(AUX_LEFT,left) ; auxKeyFunc(AUX_RIGHT,right) ; auxReshapeFunc(myReshape); auxMainLoop(display); }