/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2003 */ /* Animation d'un maillage de facettes */ #include #include #include #include #include #include #include "ModuleMenus.h" #include "ModuleReshape.h" #include "ModuleManipulateur.h" #include "ModuleCouleurs.h" #ifndef M_PI #define M_PI 3.14159 #endif static int mode = 2; static int disci = 150; static int discj = 36; static float d = 0.0F; static float larg = 20.0F; static float factz = 5.0F; static float fact = 5.0F; struct coordonnee { float x; float y; float z; }; struct coordonnee **tb; struct coordonnee **no; struct coordonnee **allocationTableau(int n,int m) { struct coordonnee **t =(struct coordonnee **) calloc(n,sizeof(struct coordonnee *)); for ( int i = 0 ; i < n ; i++ ) t[i] =(struct coordonnee *) calloc(m,sizeof(struct coordonnee)); return(t); } void liberationTableau(struct coordonnee **t,int n,int m) { for ( int i = 0 ; i < n ; i++ ) free(t[i]); free(t); } void calculTableau(struct coordonnee **t,int n,int m) { for ( int j = 0 ; j < m ; j++ ) { float a =(float) j*2.0F*M_PI/m; float sn = sin(a); float cs = cos(a); for ( int i = 0 ; i < n ; i++ ) { float r =(float) larg/2.0F*i/(n-1); t[i][j].x = r*cs; t[i][j].y = r*sn; double dist = fact*r; t[i][j].z =(float) factz*sin(dist+d)/(1+dist); } } } void calculNormales(struct coordonnee **no,int n,int m) { for ( int j = 0 ; j < m ; j++ ) { float a =(float) j*2.0F*M_PI/m; float csa = cos(a); float sna = sin(a); for ( int i = 0 ; i < n ; i++ ) { double dist =(float) larg/2.0F*i/(n-1); double cs = cos(d+fact*dist); double sn = sin(d+fact*dist); float dx = factz*fact*(sn/(1+fact*dist)-cs)/(1+fact*dist); float dz = 1.0F; no[i][j].x = dx*csa; no[i][j].y = dx*sna; no[i][j].z = dz; } } } void afficheTableauPoints(struct coordonnee **t,struct coordonnee **no,int n,int m) { glBegin(GL_POINTS); for ( int i = 0 ; i < n ; i++ ) for ( int j = 0 ; j < m ; j++ ) { glNormal3f(no[i][j].x,no[i][j].y,no[i][j].z); glVertex3f(t[i][j].x,t[i][j].y,t[i][j].z); } glEnd(); } void afficheTableauLignes(struct coordonnee **t,struct coordonnee **no,int n,int m) { for ( int i = 0 ; i < n ; i++ ) { glBegin(GL_LINE_STRIP); for ( int j = 0 ; j <= m ; j++ ) { int jj = j%m; glNormal3f(no[i][jj].x,no[i][jj].y,no[i][jj].z); glVertex3f(t[i][jj].x,t[i][jj].y,t[i][jj].z); } glEnd(); } for ( int j = 0 ; j <= m ; j++ ) { int jj = j%m; glBegin(GL_LINE_STRIP); for ( int i = 0 ; i < n ; i++ ) { glNormal3f(no[i][jj].x,no[i][jj].y,no[i][jj].z); glVertex3f(t[i][jj].x,t[i][jj].y,t[i][jj].z); } glEnd(); } /* glBegin(GL_LINES); for ( i = 0 ; i < n ; i++ ) { for ( j = 0 ; j < m ; j++ ) { glVertex3f(t[i][j].x,t[i][j].y,t[i][j].z); glVertex3f(t[i][j].x+no[i][j].x/2,t[i][j].y+no[i][j].y/2,t[i][j].z+no[i][j].z/2);} } glEnd();*/ } void afficheTableauFaces(struct coordonnee **t,struct coordonnee **no,int n,int m) { for ( int i = 0 ; i < n-1 ; i++ ) { glBegin(GL_QUAD_STRIP); for ( int j = 0 ; j <= m ; j++ ) { int jj = j%m; glNormal3f(no[i][jj].x,no[i][jj].y,no[i][jj].z); glVertex3f(t[i][jj].x,t[i][jj].y,t[i][jj].z); glNormal3f(no[i+1][jj].x,no[i+1][jj].y,no[i+1][jj].z); glVertex3f(t[i+1][jj].x,t[i+1][jj].y,t[i+1][jj].z); } glEnd(); } } void myinit(void) { GLfloat shinines[] = { 50.0F }; glClearColor(0.5F,0.5F,0.8F,1.0F) ; glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBlanc()); glMaterialfv(GL_FRONT,GL_SPECULAR,couleurGrisFonce()); glMaterialfv(GL_FRONT,GL_SHININESS,shinines); 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,couleurVert()) ; float dir1[4] = { -1.0F,1.0F,1.0F,0.0F }; glLightfv(GL_LIGHT1,GL_POSITION,dir1) ; glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBleu()) ; float dir2[4] = { 0.0F,-1.0F,1.0F,0.0F }; glLightfv(GL_LIGHT2,GL_POSITION,dir2) ; glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurRouge()) ; glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); switch ( mode ) { case 0 : afficheTableauPoints(tb,no,disci,discj); break; case 1 : afficheTableauLignes(tb,no,disci,discj); break; case 2 : afficheTableauFaces(tb,no,disci,discj); break; } glPopMatrix(); glFlush(); glutSwapBuffers(); } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 'a' : liberationTableau(tb,disci,discj); liberationTableau(no,disci,discj); disci++ ; tb = allocationTableau(disci,discj); no = allocationTableau(disci,discj); calculTableau(tb,disci,discj); calculNormales(no,disci,discj); glutPostRedisplay() ; break ; case 'A' : liberationTableau(tb,disci,discj); liberationTableau(no,disci,discj); disci-- ; if ( disci < 3 ) disci = 3; tb = allocationTableau(disci,discj); no = allocationTableau(disci,discj); calculTableau(tb,disci,discj); calculNormales(no,disci,discj); glutPostRedisplay() ; break ; case 'b' : liberationTableau(tb,disci,discj); liberationTableau(no,disci,discj); discj++ ; tb = allocationTableau(disci,discj); no = allocationTableau(disci,discj); calculTableau(tb,disci,discj); calculNormales(no,disci,discj); glutPostRedisplay() ; break ; case 'B' : liberationTableau(tb,disci,discj); liberationTableau(no,disci,discj); discj-- ; if ( discj < 3 ) discj = 3; tb = allocationTableau(disci,discj); no = allocationTableau(disci,discj); calculTableau(tb,disci,discj); calculNormales(no,disci,discj); glutPostRedisplay() ; break ; case 43 : fact *= 1.1F; glutPostRedisplay() ; break ; case 45 : fact /= 1.1F; glutPostRedisplay() ; break ; case 'c' : factz *= 1.1F; glutPostRedisplay() ; break ; case 'C' : factz /= 1.1F; glutPostRedisplay() ; break ; case ' ' : mode = (mode+1)%3 ; glutPostRedisplay() ; break ; } } void idle(void) { d -=0.05; calculTableau(tb,disci,discj); calculNormales(no,disci,discj); glutPostRedisplay(); } int main(int argc,char **argv) { tb = allocationTableau(disci,discj); no = allocationTableau(disci,discj); calculTableau(tb,disci,discj); calculTableau(no,disci,discj); glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(450,300); glutInitWindowPosition(50,50); glutCreateWindow("Animation d'un maillage de facettes"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-10.0,10.0,-10.0,10.0,-500.0,500.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutIdleFunc(idle); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); return(0); }