/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mars 2002 */ /* Une bande de Moebius */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleMatriceVecteur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #ifndef M_PI #define M_PI 3.14159F #endif static int np = 60; static float larg = 3.0F; static float haut = 0.5F; static float rayon = 5.0F; static int t = 2; static int texture = 1; static int light = 1; static int aff = 2; #define LI 64 #define LH 64 GLubyte image[LI][LH][3]; void makeImage(void) { int i,j,c; for( i = 0 ; i < LI ; i++ ) { for( j = 0 ; j < LH ; j++ ) { c =(((i&0x8)==0)^((j&0x8)==0))*255; image[i][j][0] =(GLubyte) 255; image[i][j][1] =(GLubyte) c; image[i][j][2] =(GLubyte) c; } } } void bandeDeMoebius(void) { int i; float fact =(float) t/2; glBegin(GL_QUAD_STRIP); for ( i = 0 ; i <= np ; i++ ) { float angle = i*360.0F/np; matrice m; toRotationZ(m,angle); matrice t; toTranslation(t,rayon,0.0F,0.0F); produitMatriceMatrice(m,t,m); matrice r; angle *= fact; toRotationY(r,angle); produitMatriceMatrice(m,r,m); matrice m1; matrice m2; toTranslation(t,larg,0.0F,haut); produitMatriceMatrice(m,t,m1); toTranslation(t,-larg,0.0F,haut); produitMatriceMatrice(m,t,m2); vecteur p = { 0.0F,0.0F,0.0F,1.0F }; vecteur n = { 0.0F,0.0F,1.0F,0.0F }; vecteur p1; vecteur p2; vecteur n1; vecteur n2; produitMatriceVecteur(m1,p,p1); produitMatriceVecteur(m2,p,p2); produitMatriceVecteur(m1,n,n1); produitMatriceVecteur(m2,n,n2); glTexCoord2f(5.0F*(float) i/np,0.0F); glNormal3f(n1[0],n1[1],n1[2]); glVertex3f(p1[0],p1[1],p1[2]); glTexCoord2f(5.0F*(float) i/np,1.0F); glNormal3f(n2[0],n2[1],n2[2]); glVertex3f(p2[0],p2[1],p2[2]); } glEnd(); glBegin(GL_QUAD_STRIP); for ( i = 0 ; i <= np ; i++ ) { float angle = i*360.0F/np; matrice m; toRotationZ(m,angle); matrice t; toTranslation(t,rayon,0.0F,0.0F); produitMatriceMatrice(m,t,m); matrice r; angle *= fact; toRotationY(r,angle); produitMatriceMatrice(m,r,m); matrice m1; matrice m2; toTranslation(t,larg,0.0F,-haut); produitMatriceMatrice(m,t,m1); toTranslation(t,-larg,0.0F,-haut); produitMatriceMatrice(m,t,m2); vecteur p = { 0.0F,0.0F,0.0F,1.0F }; vecteur n = { 0.0F,0.0F,-1.0F,0.0F }; vecteur p1; vecteur p2; vecteur n1; vecteur n2; produitMatriceVecteur(m1,p,p1); produitMatriceVecteur(m2,p,p2); produitMatriceVecteur(m1,n,n1); produitMatriceVecteur(m2,n,n2); glTexCoord2f(5.0F*(float) i/np,0.0F); glNormal3f(n1[0],n1[1],n1[2]); glVertex3f(p1[0],p1[1],p1[2]); glTexCoord2f(5.0F*(float) i/np,1.0F); glNormal3f(n2[0],n2[1],n2[2]); glVertex3f(p2[0],p2[1],p2[2]); } glEnd(); if ( t%2 == 1 ) { glBegin(GL_QUAD_STRIP); for ( i = 0 ; i <= (2*np) ; i++ ) { float angle = i*360.0F/np; matrice m; toRotationZ(m,angle); matrice t; toTranslation(t,rayon,0.0F,0.0F); produitMatriceMatrice(m,t,m); matrice r; angle *= fact; toRotationY(r,angle); produitMatriceMatrice(m,r,m); matrice m1; matrice m2; toTranslation(t,-larg,0.0F,haut); produitMatriceMatrice(m,t,m1); toTranslation(t,-larg,0.0F,-haut); produitMatriceMatrice(m,t,m2); vecteur p = { 0.0F,0.0F,0.0F,1.0F }; vecteur n = { -1.0F,0.0F,0.0F,0.0F }; vecteur p1; vecteur p2; vecteur n1; vecteur n2; produitMatriceVecteur(m1,p,p1); produitMatriceVecteur(m2,p,p2); produitMatriceVecteur(m1,n,n1); produitMatriceVecteur(m2,n,n2); glTexCoord2f(5.0F*(float) i/np,0.0F); glNormal3f(n1[0],n1[1],n1[2]); glVertex3f(p1[0],p1[1],p1[2]); glTexCoord2f(5.0F*(float) i/np,1.0F); glNormal3f(n2[0],n2[1],n2[2]); glVertex3f(p2[0],p2[1],p2[2]); } glEnd(); } else { glBegin(GL_QUAD_STRIP); for ( i = 0 ; i <= np ; i++ ) { float angle = i*360.0F/np; matrice m; toRotationZ(m,angle); matrice t; toTranslation(t,rayon,0.0F,0.0F); produitMatriceMatrice(m,t,m); matrice r; angle *= fact; toRotationY(r,angle); produitMatriceMatrice(m,r,m); matrice m1; matrice m2; toTranslation(t,-larg,0.0F,haut); produitMatriceMatrice(m,t,m1); toTranslation(t,-larg,0.0F,-haut); produitMatriceMatrice(m,t,m2); vecteur p = { 0.0F,0.0F,0.0F,1.0F }; vecteur n = { -1.0F,0.0F,0.0F,0.0F }; vecteur p1; vecteur p2; vecteur n1; vecteur n2; produitMatriceVecteur(m1,p,p1); produitMatriceVecteur(m2,p,p2); produitMatriceVecteur(m1,n,n1); produitMatriceVecteur(m2,n,n2); glTexCoord2f(5.0F*(float) i/np,0.0F); glNormal3f(n1[0],n1[1],n1[2]); glVertex3f(p1[0],p1[1],p1[2]); glTexCoord2f(5.0F*(float) i/np,1.0F); glNormal3f(n2[0],n2[1],n2[2]); glVertex3f(p2[0],p2[1],p2[2]); } for ( i = 0 ; i <= np ; i++ ) { float angle = i*360.0F/np; matrice m; toRotationZ(m,angle); matrice t; toTranslation(t,rayon,0.0F,0.0F); produitMatriceMatrice(m,t,m); matrice r; angle *= fact; toRotationY(r,angle); produitMatriceMatrice(m,r,m); matrice m1; matrice m2; toTranslation(t,larg,0.0F,haut); produitMatriceMatrice(m,t,m1); toTranslation(t,larg,0.0F,-haut); produitMatriceMatrice(m,t,m2); vecteur p = { 0.0F,0.0F,0.0F,1.0F }; vecteur n = { 1.0F,0.0F,0.0F,0.0F }; vecteur p1; vecteur p2; vecteur n1; vecteur n2; produitMatriceVecteur(m1,p,p1); produitMatriceVecteur(m2,p,p2); produitMatriceVecteur(m1,n,n1); produitMatriceVecteur(m2,n,n2); glTexCoord2f(5.0F*(float) i/np,0.0F); glNormal3f(n1[0],n1[1],n1[2]); glVertex3f(p1[0],p1[1],p1[2]); glTexCoord2f(5.0F*(float) i/np,1.0F); glNormal3f(n2[0],n2[1],n2[2]); glVertex3f(p2[0],p2[1],p2[2]); } glEnd(); } } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); if ( texture ) glEnable(GL_TEXTURE_2D); if ( light ) glEnable(GL_LIGHTING); switch ( aff ) { case 0 : glPolygonMode(GL_FRONT_AND_BACK,GL_POINT); break; case 1 : glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); break; } glPushMatrix(); manipulateurSouris(); manipulateurClavier(); bandeDeMoebius(); glPopMatrix(); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glFlush(); glutSwapBuffers(); } void myinit(void) { glEnable(GL_DEPTH_TEST); makeImage(); glTexImage2D(GL_TEXTURE_2D,0,3,LI,LH,0,GL_RGB,GL_UNSIGNED_BYTE,&image[0][0][0]); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glClearColor(0.0,0.0,0.0,1.0); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LESS); glEnable(GL_TEXTURE_2D); GLfloat shinines[] = { 50.0 }; GLfloat l_pos0[] = { 1.0,1.0,1.0,0.0 }; glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc()); glMaterialfv(GL_FRONT,GL_SHININESS,shinines); glLightfv(GL_LIGHT0,GL_POSITION,l_pos0); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 43 : t++; glutPostRedisplay(); break; case 45 : t--; if ( t < 1 ) t = 1; glutPostRedisplay(); break; case 'd' : np++; glutPostRedisplay(); break; case 'D' : np--; if ( np < 6 ) np = 6; glutPostRedisplay(); break; case 'l' : larg += 0.1F; glutPostRedisplay(); break; case 'L' : larg -= 0.1F; if ( larg < 0.1F ) larg = 0.1F; glutPostRedisplay(); break; case 'h' : haut += 0.1F; glutPostRedisplay(); break; case 'H' : haut -= 0.1F; if ( haut < 0.1F ) haut = 0.1F; glutPostRedisplay(); break; case 'r' : rayon += 0.1F; glutPostRedisplay(); break; case 'R' : rayon -= 0.1F; if ( rayon < 0.1F ) rayon = 0.1F; glutPostRedisplay(); break; case ' ' : texture = 1 - texture; glutPostRedisplay(); break; case 'i' : light = 1 - light; glutPostRedisplay(); break; case 0x0D : aff = (aff+1)%3; glutPostRedisplay(); break; } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(450,300); glutInitWindowPosition(50,50); glutCreateWindow("Bande de Moebius"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-7.0,7.0,-7.0,7.0,-50.0,50.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); return(0); }