/* 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" static int mode = 2; static int disc = 80; static float d = 0.0F; static float larg = 20.0F; static float factz = 4.0F; static float fact = 2.0F; struct coordonnee { float x; float y; float z; }; struct coordonnee **tb; struct coordonnee **no; struct coordonnee **allocationTableau(int n) { struct coordonnee **t =(struct coordonnee **) calloc(n,sizeof(struct coordonnee *)); for ( int i = 0 ; i < n ; i++ ) t[i] =(struct coordonnee *) calloc(n,sizeof(struct coordonnee)); return(t); } void liberationTableau(struct coordonnee **t,int n) { for ( int i = 0 ; i < n ; i++ ) free(t[i]); free(t); } void calculTableau(struct coordonnee **t,int n) { for ( int i = 0 ; i < n ; i++ ) for ( int j = 0 ; j < n ; j++ ) { t[i][j].x = (float) larg*i/(n-1)-larg/2.0F; t[i][j].y = (float) larg*j/(n-1)-larg/2.0F; double dist = fact*sqrt(t[i][j].x*t[i][j].x+t[i][j].y*t[i][j].y); t[i][j].z =(float) factz*sin(dist+d)/(1+dist); } } void norme(struct coordonnee *v) { double d = sqrt(v->x*v->x+v->y*v->y+v->z*v->z); v->x /= d; v->y /= d; v->z /= d; } void calculNormales(struct coordonnee **no,int n) { struct coordonnee v1; struct coordonnee v2; for ( int i = 0 ; i < n ; i++ ) for ( int j = 0 ; j < n ; j++ ) { float x = (float) larg*i/(n-1)-larg/2.0F; float y = (float) larg*j/(n-1)-larg/2.0F; v1.x = 1.0F; v1.y = 0.0F; v2.x = 0.0F; v2.y = 1.0F; double dist = fact*sqrt(x*x+y*y); double cs = cos(d+dist); double sn = sin(d+dist); v1.z = fact*factz*x*(cs/dist-sn/dist/dist)/(1+dist); v2.z = fact*factz*y*(cs/dist-sn/dist/dist)/(1+dist); norme(&v1); norme(&v2); no[i][j].x = v1.y*v2.z - v2.y*v1.z; no[i][j].y = v1.z*v2.x - v2.z*v1.x; no[i][j].z = v1.x*v2.y - v2.x*v1.y; } } void afficheTableauPoints(struct coordonnee **t,struct coordonnee **no,int n) { glBegin(GL_POINTS); for ( int i = 0 ; i < n ; i++ ) for ( int j = 0 ; j < n ; 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) { for ( int i = 0 ; i < n ; i++ ) { glBegin(GL_LINE_STRIP); for ( int j = 0 ; j < n ; 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(); } for ( int j = 0 ; j < n ; j++ ) { glBegin(GL_LINE_STRIP); for ( int i = 0 ; i < n ; i++ ) { 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(); } /* glBegin(GL_LINES); for ( i = 0 ; i < n ; i++ ) { for ( j = 0 ; j < n ; 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); glVertex3f(t[i][j].x+no[i][j].x,t[i][j].y+no[i][j].y,t[i][j].z+no[i][j].z);} } glEnd();*/ } void afficheTableauFaces(struct coordonnee **t,struct coordonnee **no,int n) { for ( int i = 0 ; i < n-1 ; i++ ) { glBegin(GL_QUAD_STRIP); for ( int j = 0 ; j < n ; 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); glNormal3f(no[i+1][j].x,no[i+1][j].y,no[i+1][j].z); glVertex3f(t[i+1][j].x,t[i+1][j].y,t[i+1][j].z); } glEnd(); } } void myinit(void) { GLfloat shinines[] = { 50.0F }; GLfloat l_pos[] = { 1.0F,1.0F,1.0F,0.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); glLightfv(GL_LIGHT0,GL_POSITION,l_pos); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); 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,disc); break; case 1 : afficheTableauLignes(tb,no,disc); break; case 2 : afficheTableauFaces(tb,no,disc); break; } glPopMatrix(); glFlush(); glutSwapBuffers(); } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 43 : fact *= 1.1F; glutPostRedisplay() ; break ; case 45 : fact /= 1.1F; glutPostRedisplay() ; break ; case 'a' : liberationTableau(tb,disc); liberationTableau(no,disc); disc++ ; tb = allocationTableau(disc); no = allocationTableau(disc); calculTableau(tb,disc); calculNormales(no,disc); glutPostRedisplay() ; break ; case 'A' : liberationTableau(tb,disc); liberationTableau(no,disc); disc-- ; if ( disc < 3 ) disc = 3; tb = allocationTableau(disc); no = allocationTableau(disc); calculTableau(tb,disc); calculNormales(no,disc); 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,disc); calculNormales(no,disc); glutPostRedisplay(); } int main(int argc,char **argv) { tb = allocationTableau(disc); no = allocationTableau(disc); calculTableau(tb,disc); calculTableau(no,disc); 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); }