/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Juin 2001 */ /* L'Hemisphere 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] ; } ; static int aff = 0; static facette4 f1 = { {{ -5.0F,-4.0F,-3.0F }, { -4.0F,-4.0F,3.5F }, { 1.0F,-4.0F,4.5F }, { 3.0F,-4.0F,-4.75F }} }; static facette4 f2 = { {{ 4.5F,-0.0F,-5.5F }, { 2.0F,3.0F,-5.0F }, { -2.0F,4.0F,-5.0F }, { -1.0F,-1.0F,-6.0F }} }; 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 projectionSurHemisphere(coord3D *p,coord3D *r,float rayon,coord3D *c) { float x = p->x-c->x; float y = p->y-c->y; float z = p->z-c->z; float d =(float) sqrt(x*x+y*y+z*z); r->x = x*rayon/d+c->x; r->y = y*rayon/d+c->y; r->z = z*rayon/d+c->z; } void ligneEnProjectionSurHemisphere(coord3D *p1,coord3D *p2,float rayon,coord3D *c) { glBegin(GL_LINE_STRIP); for ( int i = 0 ; i <= 20 ; i++ ) { coord3D p,r; p.x = p1->x +i*(p2->x-p1->x)/20; p.y = p1->y +i*(p2->y-p1->y)/20; p.z = p1->z +i*(p2->z-p1->z)/20; projectionSurHemisphere(&p,&r,rayon,c); glVertex3f(r.x,r.y,r.z); } glEnd(); } void ligneEnProjectionSurBaseHemisphere(coord3D *p1,coord3D *p2,float rayon,coord3D *c) { glBegin(GL_LINE_STRIP); for ( int i = 0 ; i <= 20 ; i++ ) { coord3D p,r; p.x = p1->x +i*(p2->x-p1->x)/20; p.y = p1->y +i*(p2->y-p1->y)/20; p.z = p1->z +i*(p2->z-p1->z)/20; projectionSurHemisphere(&p,&r,rayon,c); glVertex3f(r.x,c->y,r.z); } glEnd(); } void facetteEnProjectionSurHemisphere(facette4 *f,float rayon,coord3D *c) { direc3D vs1; direc3D vs2; direc3D vs; vecteur(&f->pt[0],&f->pt[1],&vs1); vecteur(&f->pt[3],&f->pt[2],&vs2); coord3D **p =(coord3D **) calloc(20,sizeof(coord3D *)); int i; for ( i = 0 ; i < 20 ; i++ ) p[i] =(coord3D *) calloc(20,sizeof(coord3D)); for ( i = 0 ; i < 20 ; i++ ) for ( int j = 0 ; j < 20 ; j++ ) { float s =(float) i/19; float t =(float) j/19; coord3D p1; p1.x = f->pt[0].x + s*vs1.x; p1.y = f->pt[0].y + s*vs1.y; p1.z = f->pt[0].z + s*vs1.z; coord3D p2; p2.x = f->pt[3].x + s*vs2.x; p2.y = f->pt[3].y + s*vs2.y; p2.z = f->pt[3].z + s*vs2.z; vecteur(&p1,&p2,&vs); coord3D pp; pp.x = p1.x + t*vs.x; pp.y = p1.y + t*vs.y; pp.z = p1.z + t*vs.z; projectionSurHemisphere(&pp,&p[i][j],rayon,c); } for ( i = 0 ; i < 19 ; i++ ) for ( int j = 0 ; j < 19 ; j++ ) { glBegin(GL_QUADS); glVertex3fv((float *) &p[i][j]); glVertex3fv((float *) &p[i+1][j]); glVertex3fv((float *) &p[i+1][j+1]); glVertex3fv((float *) &p[i][j+1]); glEnd(); } for ( i = 0 ; i < 20 ; i++ ) free(p[i]); free(p); } void facetteEnProjectionSurBaseHemisphere(facette4 *f,float rayon,coord3D *c) { direc3D vs1; direc3D vs2; direc3D vs; vecteur(&f->pt[0],&f->pt[1],&vs1); vecteur(&f->pt[3],&f->pt[2],&vs2); coord3D **p =(coord3D **) calloc(20,sizeof(coord3D *)); int i; for ( i = 0 ; i < 20 ; i++ ) p[i] =(coord3D *) calloc(20,sizeof(coord3D)); for ( i = 0 ; i < 20 ; i++ ) for ( int j = 0 ; j < 20 ; j++ ) { float s =(float) i/19; float t =(float) j/19; coord3D p1; p1.x = f->pt[0].x + s*vs1.x; p1.y = f->pt[0].y + s*vs1.y; p1.z = f->pt[0].z + s*vs1.z; coord3D p2; p2.x = f->pt[3].x + s*vs2.x; p2.y = f->pt[3].y + s*vs2.y; p2.z = f->pt[3].z + s*vs2.z; vecteur(&p1,&p2,&vs); coord3D pp; pp.x = p1.x + t*vs.x; pp.y = p1.y + t*vs.y; pp.z = p1.z + t*vs.z; projectionSurHemisphere(&pp,&p[i][j],rayon,c); p[i][j].y = c->y; } for ( i = 0 ; i < 19 ; i++ ) for ( int j = 0 ; j < 19 ; j++ ) { glBegin(GL_QUADS); glVertex3fv((float *) &p[i][j]); glVertex3fv((float *) &p[i+1][j]); glVertex3fv((float *) &p[i+1][j+1]); glVertex3fv((float *) &p[i][j+1]); glEnd(); } for ( i = 0 ; i < 20 ; i++ ) free(p[i]); free(p); } 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); } 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 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 solidHemisphere(double r,int n1,int n2) { coord3D **p =(coord3D **) calloc(n1,sizeof(coord3D *)); int i; for ( i = 0 ; i < n1 ; i++ ) p[i] =(coord3D *) calloc(n2,sizeof(coord3D)); for ( i = 0 ; i < n1 ; i++ ) { double a1 = i * 3.14159 / 2.0 / (n1-1); double csa1 = cos(a1); double sna1 = sin(a1); for ( int j = 0 ; j < n2 ; j++ ) { double a2 = j * 3.14159 * 2.0 / (n2-1); double csa2 = cos(a2); double sna2 = sin(a2); p[i][j].x = csa2*csa1; p[i][j].z = sna2*csa1; p[i][j].y = sna1; } } glBegin(GL_QUADS); for ( i = 0 ; i < n1-1 ; i++ ) { for ( int j = 0 ; j < n2 ; j++ ) { glNormal3f(p[i+1][j].x,p[i+1][j].y,p[i+1][j].z); glVertex3f(r*p[i+1][j].x,r*p[i+1][j].y,r*p[i+1][j].z); glNormal3f(p[i+1][(j+1)%n2].x,p[i+1][(j+1)%n2].y,p[i+1][(j+1)%n2].z); glVertex3f(r*p[i+1][(j+1)%n2].x,r*p[i+1][(j+1)%n2].y,r*p[i+1][(j+1)%n2].z); glNormal3f(p[i][(j+1)%n2].x,p[i][(j+1)%n2].y,p[i][(j+1)%n2].z); glVertex3f(r*p[i][(j+1)%n2].x,r*p[i][(j+1)%n2].y,r*p[i][(j+1)%n2].z); glNormal3f(p[i][j].x,p[i][j].y,p[i][j].z); glVertex3f(r*p[i][j].x,r*p[i][j].y,r*p[i][j].z); } } glEnd(); for ( i = 0 ; i < n1 ; i++ ) free(p[i]); free(p); } void solidBaseHemisphere(double r,int n2) { coord3D *p =(coord3D *) calloc(n2,sizeof(coord3D)); int j; for ( j = 0 ; j < n2 ; j++ ) { double a2 = j * 3.14159 * 2.0 / (n2-1); double csa2 = cos(a2); double sna2 = sin(a2); p[j].x = r*csa2; p[j].z = r*sna2; p[j].y = 0.0F; } glBegin(GL_POLYGON); glNormal3f(0.0F,-1.0F,0.0F); for ( j = 0 ; j < n2 ; j++ ) { glVertex3f(p[j].x,p[j].y,p[j].z); } glEnd(); free(p); } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); direc3D n1; direc3D n2; coord3D c1; coord3D c2; normale(&f1,&n1); normale(&f2,&n2); centre(&f1,&c1); centre(&f2,&c2); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurVertFonce(0.4F)); glPushMatrix(); glTranslatef(c1.x,-4.0F,c1.z); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); solidHemisphere(5.0,50,50); solidBaseHemisphere(5.0,50); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); glClear(GL_DEPTH_BUFFER_BIT); glPopMatrix(); glColor4fv(couleurRose(0.4F)); traceFacette4(GL_QUADS,&f1); glClear(GL_DEPTH_BUFFER_BIT); { glColor4fv(couleurRoseFonce(0.4F)); 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(couleurBleuCiel(0.4F)); traceFacette4(GL_QUADS,&f2); glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT); glColor4fv(couleurRouge()); traceFacette4(GL_LINE_LOOP,&f1); { glColor4fv(couleurRougeFonce()); setAlignement(CENTER); coord3D cc; centrePondere(&f1,&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()); setAlignement(CENTER); coord3D cc; centrePondere(&f1,&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(couleurBleu()); traceFacette4(GL_LINE_LOOP,&f2); { glColor4fv(couleurBleuFonce()); setAlignement(CENTER); coord3D cc; centrePondere(&f2,&cc,8.0F,1.0F,1.0F,1.0F); placeFontCursor(cc.x,cc.y,cc.z) ; simpleBitmapOutput(REGULAR8x13,"A") ; deplacementCursor(8,3,0) ; simpleBitmapOutput(REGULAR6x10,"j") ; } { glColor4fv(couleurRougeFonce()); 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(couleurNoir()); glBegin(GL_LINES); glVertex3fv((float *) &c1); glVertex3fv((float *) &c2); glEnd(); coord3D c; direc3D d; c.x = (c1.x+c2.x)/2; c.y = (c1.y+c2.y)/2; c.z = (c1.z+c2.z)/2; d.x = c1.x - c2.x; d.y = c1.y - c2.y; d.z = c1.z - c2.z; { glColor4fv(couleurRouge()); glPushMatrix(); glTranslatef(c1.x,c1.y,c1.z); flecheEnVolume(n1.x*5,n1.y*5,n1.z*5,0.1F,0.3F,0.02F); glPopMatrix(); glColor4fv(couleurRougeFonce()); 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,3,0) ; simpleBitmapOutput(REGULAR6x10,"i") ; deplacementCursor(4,-5,0) ; simpleBitmapOutput(DESSIN,"TF") ; } coord3D p; switch(aff) { case 2 : glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()); projectionSurHemisphere(&f2.pt[0],&p,5.0F,&c1); glColor4fv(couleurBleu()); glBegin(GL_LINES); glVertex3fv((float *) &p); p.y = -4.0F; glVertex3fv((float *) &p); glEnd(); afficheSommet(&p); projectionSurHemisphere(&f2.pt[1],&p,5.0F,&c1); glBegin(GL_LINES); glVertex3fv((float *) &p); p.y = -4.0F; glVertex3fv((float *) &p); glEnd(); afficheSommet(&p); projectionSurHemisphere(&f2.pt[2],&p,5.0F,&c1); glBegin(GL_LINES); glVertex3fv((float *) &p); p.y = -4.0F; glVertex3fv((float *) &p); glEnd(); afficheSommet(&p); projectionSurHemisphere(&f2.pt[3],&p,5.0F,&c1); glBegin(GL_LINES); glVertex3fv((float *) &p); p.y = -4.0F; glVertex3fv((float *) &p); glEnd(); afficheSommet(&p); ligneEnProjectionSurBaseHemisphere(&f2.pt[0],&f2.pt[1],5.0F,&c1); ligneEnProjectionSurBaseHemisphere(&f2.pt[1],&f2.pt[2],5.0F,&c1); ligneEnProjectionSurBaseHemisphere(&f2.pt[2],&f2.pt[3],5.0F,&c1); ligneEnProjectionSurBaseHemisphere(&f2.pt[3],&f2.pt[0],5.0F,&c1); glColor4fv(couleurBleuCielFonce(0.4F)); facetteEnProjectionSurBaseHemisphere(&f2,5.0F,&c1); case 1 : glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()); projectionSurHemisphere(&f2.pt[0],&p,5.0F,&c1); afficheSommet(&p); projectionSurHemisphere(&f2.pt[1],&p,5.0F,&c1); afficheSommet(&p); projectionSurHemisphere(&f2.pt[2],&p,5.0F,&c1); afficheSommet(&p); projectionSurHemisphere(&f2.pt[3],&p,5.0F,&c1); afficheSommet(&p); glColor4fv(couleurBleu()); glBegin(GL_LINES); glVertex3fv((float *) &c1); glVertex3fv((float *) &f2.pt[0]); glVertex3fv((float *) &c1); glVertex3fv((float *) &f2.pt[1]); glVertex3fv((float *) &c1); glVertex3fv((float *) &f2.pt[2]); glVertex3fv((float *) &c1); glVertex3fv((float *) &f2.pt[3]); glEnd(); ligneEnProjectionSurHemisphere(&f2.pt[0],&f2.pt[1],5.0F,&c1); ligneEnProjectionSurHemisphere(&f2.pt[1],&f2.pt[2],5.0F,&c1); ligneEnProjectionSurHemisphere(&f2.pt[2],&f2.pt[3],5.0F,&c1); ligneEnProjectionSurHemisphere(&f2.pt[3],&f2.pt[0],5.0F,&c1); glColor4fv(couleurBleuCielFonce(0.4F)); facetteEnProjectionSurHemisphere(&f2,5.0F,&c1); break; } glPopMatrix(); glFlush(); glutSwapBuffers(); } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 32 : aff = (aff+1)%3 ; glutPostRedisplay(); break; } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH); glutInitWindowPosition(10,10); glutInitWindowSize(400,300); glutCreateWindow("L'Hemisphere en radiosite"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-6.0,6.0,-6.0,6.0,-500.0,500.0); setManipulateurDistance(1.0F); setManipulateurClavierAngle(20.0F,-60.0F,0.0F) ; glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); return(0); } /* ************************************************** */