/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Avril 2001 */ /* Fonctions de dessin de cylindre et cone */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" static int np = 40; void cylindre(double h,double r,int n) { double *cs =(double *) calloc(n+1,sizeof(double)) ; double *sn =(double *) calloc(n+1,sizeof(double)) ; double *ncs =(double *) calloc(n+1,sizeof(double)) ; double *nsn =(double *) calloc(n+1,sizeof(double)) ; cs[0] = cs[n] = r ; sn[0] = sn[n] = 0.0 ; ncs[0] = ncs[n] = 1.0 ; nsn[0] = nsn[n] = 0.0 ; int i; for ( i = 1 ; i < n ; i++ ) { double a = 3.14159/n*2*i ; ncs[i] = cos(a) ; nsn[i] = sin(a) ; cs[i] = r*ncs[i] ; sn[i] = r*nsn[i] ; } glBegin(GL_POLYGON) ; glNormal3f(0.0,1.0,0.0) ; for ( i = 0 ; i < n ; i++ ) { glVertex3d(cs[i],h/2,sn[i]); } glEnd() ; glBegin(GL_POLYGON) ; glNormal3f(0.0,-1.0,0.0) ; for ( i = 0 ; i < n ; i++ ) { glVertex3d(cs[i],-h/2,sn[i]); } glEnd() ; glBegin(GL_QUADS) ; for ( i = 0 ; i < n ; i++ ) { glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(cs[i],h/2,sn[i]); glNormal3d(ncs[i+1],0.0,nsn[i+1]) ; glVertex3d(cs[i+1],h/2,sn[i+1]); glVertex3d(cs[i+1],-h/2,sn[i+1]); glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(cs[i],-h/2,sn[i]); } glEnd() ; free(sn) ; free(cs) ; free(nsn) ; free(ncs) ; } void cone(double h,double r,int n) { double *cs =(double *) calloc(n+1,sizeof(double)) ; double *sn =(double *) calloc(n+1,sizeof(double)) ; double *ncs =(double *) calloc(n+1,sizeof(double)) ; double *nsn =(double *) calloc(n+1,sizeof(double)) ; cs[0] = cs[n] = r ; sn[0] = sn[n] = 0.0 ; ncs[0] = ncs[n] = 1.0 ; nsn[0] = nsn[n] = 0.0 ; int i; for ( i = 1 ; i < n ; i++ ) { double a = 3.14159/n*2*i ; ncs[i] = cos(a) ; nsn[i] = sin(a) ; cs[i] = r*ncs[i] ; sn[i] = r*nsn[i] ; } glBegin(GL_POLYGON) ; glNormal3f(0.0,-1.0,0.0) ; for ( i = 0 ; i < n ; i++ ) { glVertex3d(cs[i],-h/2,sn[i]); } glEnd() ; glBegin(GL_QUADS) ; for ( i = 0 ; i < n ; i++ ) { glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(0,h/2,0); glNormal3d(ncs[i+1],0.0,nsn[i+1]) ; glVertex3d(0,h/2,0); glVertex3d(cs[i+1],-h/2,sn[i+1]); glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(cs[i],-h/2,sn[i]); } glEnd() ; /* glBegin(GL_TRIANGLES) ; for ( i = 0 ; i < n ; i++ ) { glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(0.0,h/2,0.0); glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(cs[i],-h/2,sn[i]); glNormal3d(ncs[i+1],0.0,nsn[i+1]) ; glVertex3d(cs[i+1],-h/2,sn[i+1]); }*/ glEnd() ; free(sn) ; free(cs) ; free(nsn) ; free(ncs) ; } void display(void) { glClearColor(0.5F,0.5F,0.5F,0.0F); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()) ; glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,couleurJaune()) ; glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90) ; glTranslatef(0.0F,0.7F,0.0F) ; cylindre(0.8,0.4,np) ; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge()) ; glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,couleurJaune()) ; glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90) ; glTranslatef(0.0F,-1.1F,0.0F) ; cone(1.4,0.4,np) ; glPopMatrix(); glFlush(); glutSwapBuffers(); } void myinit(void) { glShadeModel(GL_SMOOTH); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0,GL_SPECULAR,couleurJauneClair()) ; } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 43 : np++ ; glutPostRedisplay(); break ; case 45 : np-- ; if ( np < 3 ) np = 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("Cylindre et cone"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-1.3F,1.3F,-1.3F,1.3F,-500.0,500.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); return(0); }