/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Juillet 2001 */ /* Cellules elementaires */ /* de l'hemicube en radiosite */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleReshape.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleFont.h" #include "ModuleFleche.h" struct coord3D { float x; float y; float z; } ; struct direc3D { float x; float y; float z; } ; struct facette4 { coord3D pt[4] ; } ; struct facette3 { coord3D pt[3] ; } ; struct rayon { coord3D o ; direc3D d ; } ; static double ff = 0.0; static int f1; static int f2; static int aff = 0; static float cote = 4.0F; static int disc = 5; static int pi[5] = { 0,0,0,0,0 }; static int pj[5] = { 0,0,0,0,0 }; static double ***fe; static double totfe; static facette4 fi = { {{ -5.0F,-4.0F,-3.0F }, { -4.0F,-4.0F,2.5F }, { 3.5F,-4.0F,3.5F }, { 1.5F,-4.0F,-5.0F }} }; double ***calculFacteursElementaires(int disc) { double ***fe ; int i,j; fe =(double ***) calloc(2,sizeof(double **)); for ( i = 0 ; i < 2 ; i++ ) { fe[i] =(double **) calloc(disc,sizeof(double *)); for ( j = 0 ; j < disc ; j++ ) fe[i][j] =(double *) calloc(disc,sizeof(double)); } double deltaA = 1.0/disc/disc ; double inc = 1.0/disc; double x = 0.5/disc; double z; for ( i = 0 ; i < disc ; i++,x += inc ) { z = 0.5/disc; for ( j = 0 ; j < disc ; j++,z += inc ) { double dn = x*x + z*z + 1.0; fe[0][i][j] = deltaA / (3.14159*dn*dn); } } x = 0.5/disc; for ( i = 0 ; i < disc ; i++,x += inc ) { z = 0.5/disc; for ( j = 0 ; j < disc ; j++,z += inc ) { double dn = x*x + z*z + 1.0; fe[1][i][j] = z*deltaA / (3.14159*dn*dn); } } return(fe); } void liberationFacteursElementaires(int disc,double ***fe) { int i,j ; for ( i = 0 ; i < 2 ; i++ ) { for ( j = 0 ; j < disc ; j++ ) { free(fe[i][j]); } free(fe[i]); } } double sommeTotaleFacteursElementaires(int disc,double ***fe) { int i,j ; double t1 = 0,t2 = 0; for ( i = 0 ; i < disc ; i++ ) for ( j = 0 ; j < disc ; j++ ) { t1 += fe[0][i][j]; t2 += fe[1][i][j]; } return(4*t1+8*t2); } float produitScalaire(direc3D *v1,direc3D *v2) { return(v1->x*v2->x+v1->y*v2->y+v1->z*v2->z); } void produitVectoriel(direc3D *v1,direc3D *v2,direc3D *v) { v->x = v1->y*v2->z-v2->y*v1->z; v->y = v1->z*v2->x-v2->z*v1->x; v->z = v1->x*v2->y-v2->x*v1->y; } void vecteur(coord3D *p1,coord3D *p2,direc3D *v) { v->x = p2->x - p1->x; v->y = p2->y - p1->y; v->z = p2->z - p1->z; } void normalize(direc3D *n) { double d =sqrt(n->x*n->x+n->y*n->y+n->z*n->z); if ( d != 0.0 ) { n->x /= d; n->y /= d; n->z /= d; } } void normale(facette4 *f,direc3D *n) { direc3D v1; direc3D v2; vecteur(&f->pt[0],&f->pt[1],&v1); vecteur(&f->pt[0],&f->pt[2],&v2); produitVectoriel(&v1,&v2,n); normalize(n); } void centre(facette4 *f,coord3D *p) { p->x = (f->pt[0].x+f->pt[1].x+f->pt[2].x+f->pt[3].x)/4.0F; p->y = (f->pt[0].y+f->pt[1].y+f->pt[2].y+f->pt[3].y)/4.0F; p->z = (f->pt[0].z+f->pt[1].z+f->pt[2].z+f->pt[3].z)/4.0F; } void centrePondere(facette4 *f,coord3D *p,float c0,float c1,float c2,float c3) { p->x = (f->pt[0].x*c0+f->pt[1].x*c1+f->pt[2].x*c2+f->pt[3].x*c3)/(c0+c1+c2+c3); p->y = (f->pt[0].y*c0+f->pt[1].y*c1+f->pt[2].y*c2+f->pt[3].y*c3)/(c0+c1+c2+c3); p->z = (f->pt[0].z*c0+f->pt[1].z*c1+f->pt[2].z*c2+f->pt[3].z*c3)/(c0+c1+c2+c3); } void vecteursOrthogonaux(direc3D *d,direc3D *d1,direc3D *d2) { d1->x = d->y; d1->y = -d->x; d1->z = 0.0F; normalize(d1); produitVectoriel(d,d1,d2); } void afficheSommet(coord3D *p) { glPushMatrix(); glTranslatef(p->x,p->y,p->z); glEnable(GL_LIGHTING); glutSolidSphere(0.1,10,10); glDisable(GL_LIGHTING); glPopMatrix(); } void myinit(void) { glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE) ; glEnable(GL_LIGHT0); glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glClearColor(1.0F,1.0F,0.9F,0.0F); fe = calculFacteursElementaires(disc); totfe = sommeTotaleFacteursElementaires(disc,fe); } void traceFacette4(int type,facette4 *f) { glBegin(type); for ( int i = 0 ; i < 4 ; i++ ) glVertex3f(f->pt[i].x,f->pt[i].y,f->pt[i].z); glEnd(); } void afficheElement(coord3D *p,int t,float cote,int disc) { glBegin(GL_QUADS); switch(t) { case 0 : glVertex3f(p->x-cote/disc/2,p->y,p->z-cote/disc/2); glVertex3f(p->x+cote/disc/2,p->y,p->z-cote/disc/2); glVertex3f(p->x+cote/disc/2,p->y,p->z+cote/disc/2); glVertex3f(p->x-cote/disc/2,p->y,p->z+cote/disc/2); break; case 2 : glVertex3f(p->x,p->y-cote/disc/2,p->z-cote/disc/2); glVertex3f(p->x,p->y+cote/disc/2,p->z-cote/disc/2); glVertex3f(p->x,p->y+cote/disc/2,p->z+cote/disc/2); glVertex3f(p->x,p->y-cote/disc/2,p->z+cote/disc/2); break; case 1 : glVertex3f(p->x-cote/disc/2,p->y-cote/disc/2,p->z); glVertex3f(p->x-cote/disc/2,p->y+cote/disc/2,p->z); glVertex3f(p->x+cote/disc/2,p->y+cote/disc/2,p->z); glVertex3f(p->x+cote/disc/2,p->y-cote/disc/2,p->z); break; } glEnd(); } void afficheElementsDuDessus(float cote,coord3D *c,int disc) { float y = c->y+cote; float x = c->x-cote+cote/disc/2 + cote/disc * pi[0]; float z = c->z-cote+cote/disc/2 + cote/disc * pj[0]; coord3D p = { x,y,z }; afficheElement(&p,0,cote,disc); } void afficheElementsDeDerriere(float cote,coord3D *c,int disc) { float x = c->x-cote+cote/disc/2 + cote/disc * pi[1]; float z = c->z-cote; float y = c->y+cote/disc/2 + cote/disc * pj[1]; coord3D p = { x,y,z }; afficheElement(&p,1,cote,disc); } void afficheElementsDeDevant(float cote,coord3D *c,int disc) { float x = c->x-cote+cote/disc/2 + cote/disc * pi[2]; float z = c->z+cote; float y = c->y+cote/disc/2 + cote/disc * pj[2]; coord3D p = { x,y,z }; afficheElement(&p,1,cote,disc); } void afficheElementsDeDroite(float cote,coord3D *c,int disc) { float x = c->x+cote; float z = c->z-cote+cote/disc/2 + cote/disc * pi[3]; float y = c->y+cote/disc/2 + cote/disc * pj[3]; coord3D p = { x,y,z }; afficheElement(&p,2,cote,disc); } void afficheElementsDeGauche(float cote,coord3D *c,int disc) { float x = c->x-cote; float z = c->z-cote+cote/disc/2 + cote/disc * pi[4]; float y = c->y+cote/disc/2 + cote/disc * pj[4]; coord3D p = { x,y,z }; afficheElement(&p,2,cote,disc); } void axes() { setFont(GLUT_BITMAP_8_BY_13,CENTER); glColor4fv(couleurRouge()); flecheEnVolume(1.0F,0.0F,0.0F,0.1F,0.3F,0.02F); glColor4fv(couleurVert()); flecheEnVolume(0.0F,1.0F,0.0F,0.1F,0.3F,0.02F); glColor4fv(couleurBleu()); flecheEnVolume(0.0F,0.0F,1.0F,0.1F,0.3F,0.02F); glPushAttrib(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST); glColor4fv(couleurRougeFonce()); bitmapStringOutput(1.3F,0.0F,0.0F,"x"); glColor4fv(couleurVertFonce()); bitmapStringOutput(0.0F,1.3F,0.0F,"y"); glColor4fv(couleurBleuFonce()); bitmapStringOutput(0.0F,0.0F,1.3F,"z"); glPopAttrib(); } void solidHemicube(double c,int n) { glEnable(GL_LIGHTING); glBegin(GL_QUADS); glNormal3f(0.0F,1.0F,0.0F); glVertex3f(c/2,c/2,c/2); glVertex3f(c/2,c/2,-c/2); glVertex3f(-c/2,c/2,-c/2); glVertex3f(-c/2,c/2,c/2); glEnd(); glBegin(GL_QUADS); glNormal3f(1.0F,0.0F,0.0F); glVertex3f(c/2,c/2,c/2); glVertex3f(c/2,0.0F,c/2); glVertex3f(c/2,0.0F,-c/2); glVertex3f(c/2,c/2,-c/2); glEnd(); glBegin(GL_QUADS); glNormal3f(-1.0F,0.0F,0.0F); glVertex3f(-c/2,c/2,-c/2); glVertex3f(-c/2,0.0F,-c/2); glVertex3f(-c/2,0.0F,c/2); glVertex3f(-c/2,c/2,c/2); glEnd(); glBegin(GL_QUADS); glNormal3f(0.0F,0.0F,1.0F); glVertex3f(-c/2,c/2,c/2); glVertex3f(-c/2,0.0F,c/2); glVertex3f(c/2,0.0F,c/2); glVertex3f(c/2,c/2,c/2); glEnd(); glBegin(GL_QUADS); glNormal3f(0.0F,0.0F,-1.0F); glVertex3f(c/2,c/2,-c/2); glVertex3f(c/2,0.0F,-c/2); glVertex3f(-c/2,0.0F,-c/2); glVertex3f(-c/2,c/2,-c/2); glEnd(); glDisable(GL_LIGHTING); glClear(GL_DEPTH_BUFFER_BIT); glColor4fv(couleurGrisFonce()); glBegin(GL_LINES); glVertex3f(c/2,c/2,c/2); glVertex3f(c/2,c/2,-c/2); glVertex3f(c/2,c/2,-c/2); glVertex3f(-c/2,c/2,-c/2); glVertex3f(-c/2,c/2,-c/2); glVertex3f(-c/2,c/2,c/2); glVertex3f(-c/2,c/2,c/2); glVertex3f(c/2,c/2,c/2); glVertex3f(c/2,c/2,c/2); glVertex3f(c/2,0.0F,c/2); glVertex3f(-c/2,c/2,c/2); glVertex3f(-c/2,0.0F,c/2); glVertex3f(-c/2,c/2,-c/2); glVertex3f(-c/2,0.0F,-c/2); glVertex3f(c/2,c/2,-c/2); glVertex3f(c/2,0.0F,-c/2); glVertex3f(c/2,0.0F,c/2); glVertex3f(c/2,0.0F,-c/2); glVertex3f(c/2,0.0F,-c/2); glVertex3f(-c/2,0.0F,-c/2); glVertex3f(-c/2,0.0F,-c/2); glVertex3f(-c/2,0.0F,c/2); glVertex3f(-c/2,0.0F,c/2); glVertex3f(c/2,0.0F,c/2); glEnd(); int i; glBegin(GL_LINES); for ( i = 1 ; i < 2*n ; i++ ) { glVertex3f(c/2,c/2,-c/2+i*c/2.0F/n); glVertex3f(-c/2,c/2,-c/2+i*c/2.0F/n); } for ( i = 1 ; i < 2*n ; i++ ) { glVertex3f(-c/2+i*c/2.0F/n,c/2,c/2); glVertex3f(-c/2+i*c/2.0F/n,c/2,-c/2); } for ( i = 1 ; i < 2*n ; i++ ) { glVertex3f(c/2,c/2,-c/2+i*c/2.0F/n); glVertex3f(c/2,0.0F,-c/2+i*c/2.0F/n); } for ( i = 1 ; i < 2*n ; i++ ) { glVertex3f(-c/2,c/2,-c/2+i*c/2.0F/n); glVertex3f(-c/2,0.0F,-c/2+i*c/2.0F/n); } for ( i = 1 ; i < 2*n ; i++ ) { glVertex3f(-c/2+i*c/2.0F/n,c/2,c/2); glVertex3f(-c/2+i*c/2.0F/n,0.0F,c/2); } for ( i = 1 ; i < 2*n ; i++ ) { glVertex3f(-c/2+i*c/2.0F/n,c/2,-c/2); glVertex3f(-c/2+i*c/2.0F/n,0.0F,-c/2); } for ( i = 1 ; i < n ; i++ ) { glVertex3f(-c/2,c/2-i*c/2.0F/n,-c/2); glVertex3f(c/2,c/2-i*c/2.0F/n,-c/2); } for ( i = 1 ; i < n ; i++ ) { glVertex3f(-c/2,c/2-i*c/2.0F/n,c/2); glVertex3f(c/2,c/2-i*c/2.0F/n,c/2); } for ( i = 1 ; i < n ; i++ ) { glVertex3f(-c/2,c/2-i*c/2.0F/n,-c/2); glVertex3f(-c/2,c/2-i*c/2.0F/n,c/2); } for ( i = 1 ; i < n ; i++ ) { glVertex3f(c/2,c/2-i*c/2.0F/n,-c/2); glVertex3f(c/2,c/2-i*c/2.0F/n,c/2); } glEnd(); } void solidBaseHemicube(double c) { glEnable(GL_LIGHTING); glBegin(GL_QUADS); glNormal3f(0.0F,-1.0F,0.0F); glVertex3f(c/2,0.0F,c/2); glVertex3f(-c/2,0.0F,c/2); glVertex3f(-c/2,0.0F,-c/2); glVertex3f(c/2,0.0F,-c/2); glEnd(); glDisable(GL_LIGHTING); } double facteurElementaire(int pi,int pj,int aff,int disc) { double ff = 0.0F; int ii,jj; switch (aff) { case 0 : ii = ( pi < disc) ? disc-1-pi : pi-disc ; jj = ( pj < disc) ? disc-1-pj : pj-disc; ff = fe[0][ii][jj]/totfe; break; case 1 : case 2 : case 3 : case 4 : ii = ( pi < disc) ? disc-1-pi : pi-disc ; jj = pj ; ff = fe[1][ii][jj]/totfe; break; } return(ff); } void display(void) { direc3D n1; coord3D c1; normale(&fi,&n1); centre(&fi,&c1); glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); glClear(GL_DEPTH_BUFFER_BIT); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurVertFonce(0.4F)); glPushMatrix(); glTranslatef(c1.x,c1.y,c1.z); glEnable(GL_CULL_FACE); solidHemicube(cote*2,disc); solidBaseHemicube(cote*2); glDisable(GL_CULL_FACE); glClear(GL_DEPTH_BUFFER_BIT); glPopMatrix(); glColor4fv(couleurRose(0.5F)); traceFacette4(GL_QUADS,&fi); glClear(GL_DEPTH_BUFFER_BIT); { glColor4fv(couleurRoseFonce(0.5F)); direc3D d1; direc3D d2; vecteursOrthogonaux(&n1,&d1,&d2); glBegin(GL_POLYGON); glVertex3f(c1.x+d1.x/2+d2.x/2,c1.y+d1.y/2+d2.y/2,c1.z+d1.z/2+d2.z/2); glVertex3f(c1.x+d1.x/2-d2.x/2,c1.y+d1.y/2-d2.y/2,c1.z+d1.z/2-d2.z/2); glVertex3f(c1.x-d1.x/2-d2.x/2,c1.y-d1.y/2-d2.y/2,c1.z-d1.z/2-d2.z/2); glVertex3f(c1.x-d1.x/2+d2.x/2,c1.y-d1.y/2+d2.y/2,c1.z-d1.z/2+d2.z/2); glEnd(); } glClear(GL_DEPTH_BUFFER_BIT); glColor4fv(couleurRouge(0.8F)); traceFacette4(GL_LINE_LOOP,&fi); { glColor4fv(couleurRougeFonce(0.8F)); setAlignement(CENTER); coord3D cc; centrePondere(&fi,&cc,1.0F,12.0F,1.0F,1.0F); placeFontCursor(cc.x,cc.y,cc.z) ; simpleBitmapOutput(REGULAR8x13,"A") ; deplacementCursor(8,3,0) ; simpleBitmapOutput(REGULAR6x10,"i") ; } { glColor4fv(couleurRougeFonce(0.8F)); setAlignement(CENTER); coord3D cc; centrePondere(&fi,&cc,1.0F,1.0F,2.0F,1.0F); placeFontCursor(cc.x,cc.y,cc.z) ; simpleBitmapOutput(REGULAR8x13,"dA") ; deplacementCursor(12,3,0) ; simpleBitmapOutput(REGULAR6x10,"i") ; } { glColor4fv(couleurRougeFonce(0.8F)); direc3D d1; direc3D d2; vecteursOrthogonaux(&n1,&d1,&d2); glBegin(GL_LINE_LOOP); glVertex3f(c1.x+d1.x/2+d2.x/2,c1.y+d1.y/2+d2.y/2,c1.z+d1.z/2+d2.z/2); glVertex3f(c1.x+d1.x/2-d2.x/2,c1.y+d1.y/2-d2.y/2,c1.z+d1.z/2-d2.z/2); glVertex3f(c1.x-d1.x/2-d2.x/2,c1.y-d1.y/2-d2.y/2,c1.z-d1.z/2-d2.z/2); glVertex3f(c1.x-d1.x/2+d2.x/2,c1.y-d1.y/2+d2.y/2,c1.z-d1.z/2+d2.z/2); glEnd(); } glColor4fv(couleurBleuCielFonce(0.5F)); switch ( aff ) { case 0 : afficheElementsDuDessus(cote,&c1,disc); break; case 1 : afficheElementsDeDerriere(cote,&c1,disc); break; case 2 : afficheElementsDeDevant(cote,&c1,disc); break; case 3 : afficheElementsDeDroite(cote,&c1,disc); break; case 4 : afficheElementsDeGauche(cote,&c1,disc); break; } glClear(GL_DEPTH_BUFFER_BIT); { glColor4fv(couleurRouge(0.8F)); glPushMatrix(); glTranslatef(c1.x,c1.y,c1.z); flecheEnVolume(n1.x*cote,n1.y*cote,n1.z*cote,0.1F,0.3F,0.02F); glPopMatrix(); glColor4fv(couleurRougeFonce(0.8F)); setAlignement(LEFT); placeFontCursor(c1.x+n1.x*2.5,c1.y+n1.y*2.5,c1.z+n1.z*2.5) ; deplacementCursor(5,0,0) ; simpleBitmapOutput(REGULAR8x13,"N") ; deplacementCursor(15,2,0) ; simpleBitmapOutput(REGULAR6x10,"i") ; deplacementCursor(4,-5,0) ; simpleBitmapOutput(DESSIN,"TF") ; } { glPushMatrix(); glTranslatef(-3.0F,2.0F,1.25F); axes(); glPopMatrix(); } glPopMatrix(); ff = facteurElementaire(pi[aff],pj[aff],aff,disc); glFlush(); glutSwapBuffers(); glutPostWindowRedisplay(f2); } void key2(unsigned char key,int x,int y) { int *v,i; switch ( key ) { case 'a' : v = &pi[aff]; (*v)++ ; if ( *v >= disc*2 ) *v = 0; glutPostWindowRedisplay(f1); break; case 'A' : v = &pi[aff]; (*v)-- ; if ( *v < 0 ) *v = disc*2-1; glutPostWindowRedisplay(f1); break; case 'b' : v = &pj[aff]; (*v)++ ; if ( *v >= ((aff == 0) ? disc*2 : disc) ) *v = 0; glutPostWindowRedisplay(f1); break; case 'B' : v = &pj[aff]; (*v)-- ; if ( *v < 0 ) *v = ((aff == 0) ? disc*2 : disc)-1; glutPostWindowRedisplay(f1); break; case 43 : liberationFacteursElementaires(disc,fe); disc++ ; fe = calculFacteursElementaires(disc); totfe = sommeTotaleFacteursElementaires(disc,fe); glutPostWindowRedisplay(f1); break; case 45 : liberationFacteursElementaires(disc,fe); disc-- ; if ( disc < 2 ) disc = 2 ; fe = calculFacteursElementaires(disc); totfe = sommeTotaleFacteursElementaires(disc,fe); for ( i = 0 ; i < 5 ; i++ ) { v = &pi[i]; if ( *v > 2*disc-1 ) *v = 2*disc-1; v = &pj[i]; if ( *v > ((i == 0 ) ? 2*disc-1 : disc-1) ) *v = ((i == 0 ) ? 2*disc : disc)-1; } glutPostWindowRedisplay(f1); break; case 32 : aff = (aff+1)%5 ; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostWindowRedisplay(f1); else key2(key,x,y); } void special(int key,int x,int y) { if ( specialManipulateur(key,x,y) ) { glutPostWindowRedisplay(f1); } } void reshape2(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,w,-h,0,-1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void display2() { glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glClearColor(0.0F,0.0F,0.0F,1.0F) ; glPushMatrix(); float pos = 1.0F; glColor4fv(couleurBlanc()); setAlignement(LEFT); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Facteur de forme : %10.8lf",ff) ; pos += 1.0F; glPopMatrix(); glFlush(); glutSwapBuffers(); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH); glutInitWindowPosition(10,10); glutInitWindowSize(300,240); f1 = glutCreateWindow("L'Hemicube en radiosite"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-5.0,5.0,-6.0,4.0,-50.0,50.0); setManipulateurDistance(1.0F); setManipulateurClavierAngle(20.0F,-60.0F,0.0F) ; glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutInitWindowSize(310,30); glutInitWindowPosition(40,360); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f2 = glutCreateWindow("Valeurs"); creationMenuBasique(); glutDisplayFunc(display2); glutReshapeFunc(reshape2); glutKeyboardFunc(key2); glutSpecialFunc(special); glutMainLoop(); return(0); } /* ************************************************** */