/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2005 */ /* Fonctions de dessin de cylindre */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #include "ModuleAxes.h" #ifndef M_PI #define M_PI 3.14159 #endif static float anglex = 0.0F; static float angley = 0.0F; static float anglez = 0.0F; static int np = 40; void solidCylinderSansNormales1(float r,float h,int n){ int i; glPushMatrix(); glBegin(GL_QUADS); for( i = 0 ; i < n ; i++ ){ float ai=(2*M_PI*i)/n; float aj=(2*M_PI*(i+1))/n; float si=sin(ai); float sj=sin(aj); float ci=cos(ai); float cj=cos(aj); float xi=r*ci; float zi=r*si; float xj=r*cj; float zj=r*sj; glVertex3f(xi,h/2,zi); glVertex3f(xi,-h/2,zi); glVertex3f(xj,-h/2,zj); glVertex3f(xj,h/2,zj); } glEnd(); glBegin(GL_POLYGON); for( i = 0 ; i < n ; i++ ){ float ai=(2*M_PI*i)/n; float si=sin(ai); float ci=cos(ai); glVertex3f(r*ci,-h/2,r*si); } glEnd(); glBegin(GL_POLYGON); for( i = 0 ; i < n ; i++ ){ float ai=(2*M_PI*i)/n; float si=-sin(ai); float ci=cos(ai); glVertex3f(r*ci,h/2,r*si); } glEnd(); glPopMatrix(); } void solidCylinderAvecNormales1(float r,float h,int n){ int i; glPushMatrix(); glBegin(GL_QUADS); for( i = 0 ; i < n ; i++ ){ float ai=(2*M_PI*i)/n; float aj=(2*M_PI*(i+1))/n; float si=sin(ai); float sj=sin(aj); float ci=cos(ai); float cj=cos(aj); float xi=r*ci; float zi=r*si; float xj=r*cj; float zj=r*sj; glNormal3f(ci,0,si); glVertex3f(xi,h/2,zi); glVertex3f(xi,-h/2,zi); glNormal3f(cj,0,sj); glVertex3f(xj,-h/2,zj); glVertex3f(xj,h/2,zj); } glEnd(); glNormal3f(0.0F,-1.0F,0.0F); glBegin(GL_POLYGON); for( i = 0 ; i < n ; i++ ){ float ai=(2*M_PI*i)/n; float si=sin(ai); float ci=cos(ai); glVertex3f(r*ci,-h/2,r*si); } glEnd(); glNormal3f(0.0F,1.0F,0.0F); glBegin(GL_POLYGON); for( i = 0 ; i < n ; i++ ){ float ai=(2*M_PI*i)/n; float si=-sin(ai); float ci=cos(ai); glVertex3f(r*ci,h/2,r*si); } glEnd(); glPopMatrix(); } void solidCylinderSansNormales2(float r,float h,int n){ int i; glPushMatrix(); glBegin(GL_QUAD_STRIP); for( i = 0 ; i <= n ; i++ ){ float a = (2*M_PI*i)/n; float sn = sin(a); float cs = cos(a); float x = r*cs; float z = -r*sn; glVertex3f(x,h/2,z); glVertex3f(x,-h/2,z); } glEnd(); glBegin(GL_POLYGON); for( i = 0 ; i < n ; i++ ){ float ai = (2*M_PI*i)/n; float si = -sin(ai); float ci = cos(ai); glVertex3f(r*ci,h/2,r*si); } glEnd(); glBegin(GL_POLYGON); for( i = 0 ; i < n ; i++ ){ float ai = (2*M_PI*i)/n; float si = sin(ai); float ci = cos(ai); glVertex3f(r*ci,-h/2,r*si); } glEnd(); glPopMatrix(); } void solidCylinderAvecNormales2(float r,float h,int n){ int i; glPushMatrix(); glBegin(GL_QUAD_STRIP); for( i = 0 ; i <= n ; i++ ){ float a = (2*M_PI*i)/n; float sn = sin(a); float cs = cos(a); float x = r*cs; float z = -r*sn; glNormal3f(cs,0.0F,-sn); glVertex3f(x,h/2,z); glVertex3f(x,-h/2,z); } glEnd(); glNormal3f(0.0F,-1.0F,0.0F); glBegin(GL_POLYGON); for( i = 0 ; i <= n ; i++ ){ float ai=(2*M_PI*i)/n; float si=sin(ai); float ci=cos(ai); glVertex3f(r*ci,-h/2,r*si); } glEnd(); glNormal3f(0.0F,1.0F,0.0F); glBegin(GL_POLYGON); for( i = 0 ; i <= n ; i++ ){ float ai=(2*M_PI*i)/n; float si=-sin(ai); float ci=cos(ai); glVertex3f(r*ci,h/2,r*si); } glEnd(); glPopMatrix(); } void solidCylinderSansNormales3(float r,float h,int n){ float *cs =(float *) calloc(n,sizeof(float)); float *sn =(float *) calloc(n,sizeof(float)); int i; for( i = 0 ; i < n ; i++ ){ float a = (2*M_PI*i)/n; sn[i] = sin(a); cs[i] = cos(a); } glPushMatrix(); glBegin(GL_QUAD_STRIP); for( i = 0 ; i <= n ; i++ ){ float x = r*cs[i%n]; float z = -r*sn[i%n]; glVertex3f(x,h/2,z); glVertex3f(x,-h/2,z); } glEnd(); glBegin(GL_POLYGON); for( i = 0 ; i < n ; i++ ){ float si = -sn[i]; float ci = cs[i]; glVertex3f(r*ci,h/2,r*si); } glEnd(); glBegin(GL_POLYGON); for( i = 0 ; i < n ; i++ ){ float si = sn[i]; float ci = cs[i]; glVertex3f(r*ci,-h/2,r*si); } glEnd(); free(cs); free(sn); glPopMatrix(); } void solidCylinderAvecNormales3(float r,float h,int n){ float *cs =(float *) calloc(n,sizeof(float)); float *sn =(float *) calloc(n,sizeof(float)); int i; for( i = 0 ; i < n ; i++ ){ float a = (2*M_PI*i)/n; sn[i] = sin(a); cs[i] = cos(a); } glPushMatrix(); glBegin(GL_QUAD_STRIP); for( i = 0 ; i <= n ; i++ ){ float x = r*cs[i%n]; float z = -r*sn[i%n]; glNormal3f(cs[i%n],0.0F,-sn[i%n]); glVertex3f(x,h/2,z); glVertex3f(x,-h/2,z); } glEnd(); glBegin(GL_POLYGON); glNormal3f(0.0F,1.0F,0.0F); for( i = 0 ; i < n ; i++ ){ float si = -sn[i]; float ci = cs[i]; glVertex3f(r*ci,h/2,r*si); } glEnd(); glBegin(GL_POLYGON); glNormal3f(0.0F,-1.0F,0.0F); for( i = 0 ; i < n ; i++ ){ float si = sn[i]; float ci = cs[i]; glVertex3f(r*ci,-h/2,r*si); } glEnd(); free(cs); free(sn); glPopMatrix(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); axes(); glPushMatrix(); glTranslatef(1.7F,1.7F,0.0F) ; glRotatef(anglex,1.0F,0.0F,0.0F); glRotatef(angley,0.0F,1.0F,0.0F); glRotatef(anglez,0.0F,0.0F,1.0F); solidCylinderAvecNormales1(0.9F,1.8F,np) ; glPopMatrix(); glPushMatrix(); glTranslatef(-1.7F,-1.7F,0.0F) ; glRotatef(angley,0.0F,1.0F,0.0F); glRotatef(anglez,0.0F,0.0F,1.0F); glRotatef(anglex,1.0F,0.0F,0.0F); solidCylinderAvecNormales3(0.9F,1.8F,np) ; glPopMatrix(); glPushMatrix(); glTranslatef(1.7F,-1.7F,0.0F) ; glNormal3f(0.0F,0.0F,1.0F); glRotatef(anglez,0.0F,0.0F,1.0F); glRotatef(anglex,1.0F,0.0F,0.0F); glRotatef(angley,0.0F,1.0F,0.0F); solidCylinderSansNormales3(0.9F,1.8F,np) ; glPopMatrix(); glPushMatrix(); glTranslatef(-1.7F,1.7F,0.0F) ; glNormal3f(0.0F,0.0F,1.0F); glRotatef(angley,0.0F,1.0F,0.0F); glRotatef(anglex,1.0F,0.0F,0.0F); glRotatef(anglez,0.0F,0.0F,1.0F); solidCylinderSansNormales1(0.9F,1.8F,np) ; glPopMatrix(); glPopMatrix(); glFlush(); glutSwapBuffers(); } void myinit (void) { glClearColor(0.75F,0.75F,0.75F,0.0F); glShadeModel(GL_SMOOTH); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); float dir0[4] = { 1.0F,1.0F,1.0F,0.0F }; glLightfv(GL_LIGHT0,GL_POSITION,dir0) ; glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc()) ; float dir1[4] = { -1.0F,1.0F,1.0F,0.0F }; glLightfv(GL_LIGHT1,GL_POSITION,dir1) ; glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc()) ; float dir2[4] = { 0.0F,-1.0F,1.0F,0.0F }; glLightfv(GL_LIGHT2,GL_POSITION,dir2) ; glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurBlanc()) ; } void idle(void) { anglex += 0.10F; angley += 0.11F; anglez += 0.12F; glutPostRedisplay(); } void key(unsigned char key,int x,int y) { static int aff = 1; static int anim = 1; if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 'a' : anim = !anim; glutIdleFunc((anim) ? idle : NULL); break; case ' ' : switchAffichagePlan(); glutPostRedisplay(); break; case 43 : np++ ; glutPostRedisplay(); break ; case 45 : np-- ; if ( np < 3 ) np = 3 ; glutPostRedisplay(); break ; case 0x0D : aff = !aff; glPolygonMode(GL_FRONT_AND_BACK, (aff) ? GL_FILL : GL_LINE); glutPostRedisplay(); } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(250,250); glutInitWindowPosition(50,50); glutCreateWindow("Cylindres par facettes"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-3.5F,3.5F,-3.5F,3.5F,-5.0,5.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return(0); }