/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2005 */ /* Recherche des intersections entre */ /* des rayons lumineux et des spheres */ #include #include #include #include #include #include #include "Position.h" #include "Direction.h" #include "Sphere.h" #include "Droite.h" #include "Rayon.h" #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleAxes.h" #include "ModuleFleche.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #include "ModuleFont.h" static int question = 0; static int exemple = 0; static int aff = 1; static int f1 ; static int f2 ; static Position *sp1 = new Position(-1.3F,0.7F,-1.5F); static Position *sp2 = new Position(1.9F,-0.4F, 0.8F); static Position *sp3 = new Position(-0.7F,-1.2F, 1.8F); static Sphere *s1 = new Sphere(sp1,1.2F); static Sphere *s2 = new Sphere(sp2,0.9F); static Sphere *s3 = new Sphere(sp3,0.86721912F); static Sphere *s = new Sphere(); static Position *dp1 = new Position(-1.3F,1.6F,-1.5F); static Position *dp2 = new Position(0.5F,-0.7F, 0.6F); static Position *dp3 = new Position(0.2F,-0.2F, 0.1F); static Direction *dd1 = new Direction(0.8F,-0.5F,0.5F); static Direction *dd2 = new Direction(1.7F,-0.9F, 1.1F); static Direction *dd3 = new Direction(-1.2F,-0.6F, 0.9F); static Droite *d1 = new Droite(dp1,dd1); static Droite *d2 = new Droite(dp2,dd2); static Droite *d3 = new Droite(dp3,dd3); static Droite *d = new Droite(); static Rayon *r1 = new Rayon(dp1,dd1); static Rayon *r2 = new Rayon(dp2,dd2); static Rayon *r3 = new Rayon(dp3,dd3); static Rayon *r = new Rayon(); static Sphere *ts[3] = { s1,s2,s3 }; static Droite *td[3] = { d1,d2,d3 }; static Rayon *tr[3] = { r1,r2,r3 }; void scene4(void) { glDisable(GL_LIGHTING); axes(); glEnable(GL_LIGHTING); glPushMatrix(); r1->dessine(couleurBleu()); r2->dessine(couleurMagenta()); r3->dessine(couleurVert()); if ( aff ) { glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBlanc()); for ( int i = 0 ; i < 3 ; i++ ) for ( int j = 0 ; j < 3 ; j++ ) { Position *pt = ts[i]->intersection(tr[j]); if ( pt ) { pt->dessine(); delete(pt); } } } glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge(0.5F)); s1->dessine(); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurJaune(0.5F)); s2->dessine(); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurCyan(0.5F)); s3->dessine(); glPopMatrix(); } void display4() { glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); scene4(); glPopMatrix(); glFlush(); glutSwapBuffers(); glutPostWindowRedisplay(f2); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } void scene3(void) { glDisable(GL_LIGHTING); axes(); glEnable(GL_LIGHTING); glPushMatrix(); r->dessine(couleurBlanc()); Position *pt = s->intersection(r); if ( pt ) { glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge(0.5F)); pt->dessine(); delete(pt); } glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurJaune(0.5F)); s->dessine(); glPopMatrix(); } void affecteExemple(void) { switch ( exemple ) { case 0 : s = s1; d = d1; r = r1; break; case 1 : s = s1; d = d2; r = r2; break; case 2 : s = s1; d = d3; r = r3; break; case 3 : s = s2; d = d1; r = r1; break; case 4 : s = s2; d = d2; r = r2; break; case 5 : s = s2; d = d3; r = r3; break; case 6 : s = s3; d = d1; r = r1; break; case 7 : s = s3; d = d2; r = r2; break; case 8 : s = s3; d = d3; r = r3; break; } } void display3() { glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); affecteExemple(); scene3(); glPopMatrix(); glFlush(); glutSwapBuffers(); glutPostWindowRedisplay(f2); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } void scene2(void) { glDisable(GL_LIGHTING); axes(); glEnable(GL_LIGHTING); glPushMatrix(); d1->dessine(couleurBleu()); d2->dessine(couleurMagenta()); d3->dessine(couleurVert()); if ( aff ) { glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBlanc()); for ( int i = 0 ; i < 3 ; i++ ) for ( int j = 0 ; j < 3 ; j++ ) { Position **pts = ts[i]->intersection(td[j]); if ( pts ) { pts[0]->dessine(); delete(pts[0]); if ( pts[1] != NULL ) { pts[1]->dessine(); delete(pts[1]); } free(pts); } } } glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge(0.5F)); s1->dessine(); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurJaune(0.5F)); s2->dessine(); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurCyan(0.5F)); s3->dessine(); glPopMatrix(); } void display2() { glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); scene2(); glPopMatrix(); glFlush(); glutSwapBuffers(); glutPostWindowRedisplay(f2); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } void scene1(void) { glDisable(GL_LIGHTING); axes(); glEnable(GL_LIGHTING); glPushMatrix(); d->dessine(couleurBlanc()); if ( aff ) { Position **pts = s->intersection(d); if ( pts ) { glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge(0.5F)); pts[0]->dessine(); delete(pts[0]); if ( pts[1] != NULL ) { pts[1]->dessine(); delete(pts[1]); } free(pts); } } glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurJaune(0.5F)); s->dessine(); glPopMatrix(); } void display1() { glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); affecteExemple(); scene1(); glPopMatrix(); glFlush(); glutSwapBuffers(); glutPostWindowRedisplay(f2); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } void displayV4(void) { glClearColor(0.0F,0.0F,0.0F,1.0F) ; glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); float pos = 1.0F; glColor4fv(couleurRouge()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; s1->print("SPHERE 1 : P (%7.3f %7.3f %7.3f) R %5.3f"); glColor4fv(couleurBleu()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; r1->p->print("RAYON 1 : P (%7.3f %7.3f %7.3f)"); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; r1->d->print(" D (%7.3f %7.3f %7.3f)"); pos += 1.0F; glColor4fv(couleurJaune()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; s2->print("SPHERE 2 : P (%7.3f %7.3f %7.3f) R %5.3f"); glColor4fv(couleurMagenta()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; r2->p->print("RAYON 2 : P (%7.3f %7.3f %7.3f)"); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; r2->d->print(" D (%7.3f %7.3f %7.3f)"); pos += 1.0F; glColor4fv(couleurCyan()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; s3->print("SPHERE 3 : P (%7.3f %7.3f %7.3f) R %5.3f"); glColor4fv(couleurVert()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; r3->p->print("RAYON 3 : P (%7.3f %7.3f %7.3f)"); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; r3->d->print(" D (%7.3f %7.3f %7.3f)"); glPopMatrix(); glFlush(); glutSwapBuffers(); } void displayV3(void) { glClearColor(0.0F,0.0F,0.0F,1.0F) ; glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); float pos = 1.0F; glColor4fv(couleurJaune()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; s->print("SPHERE : P (%7.3f %7.3f %7.3f) R %5.3f"); glColor4fv(couleurBlanc()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d->p->print("DROITE : P (%7.3f %7.3f %7.3f)"); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d->d->print(" D (%7.3f %7.3f %7.3f)"); int inter = s->testIntersection(r); pos += 1.0F; glColor4fv(couleurRouge()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"INTERSECTIONS ? : %s %d", ((inter == 0) ? "NON" : "OUI"),inter); pos += 1.0F; Position *pt = s->intersection(r); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; if ( pt ) pt->print("POSITION : %7.3f %7.3f %7.3f"); delete(pt); glPopMatrix(); glFlush(); glutSwapBuffers(); } void displayV2(void) { glClearColor(0.0F,0.0F,0.0F,1.0F) ; glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); float pos = 1.0F; glColor4fv(couleurRouge()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; s1->print("SPHERE 1 : P (%7.3f %7.3f %7.3f) R %5.3f"); glColor4fv(couleurBleu()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d1->p->print("DROITE 1 : P (%7.3f %7.3f %7.3f)"); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d1->d->print(" D (%7.3f %7.3f %7.3f)"); pos += 1.0F; glColor4fv(couleurJaune()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; s2->print("SPHERE 2 : P (%7.3f %7.3f %7.3f) R %5.3f"); glColor4fv(couleurMagenta()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d2->p->print("DROITE 2 : P (%7.3f %7.3f %7.3f)"); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d2->d->print(" D (%7.3f %7.3f %7.3f)"); pos += 1.0F; glColor4fv(couleurCyan()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; s3->print("SPHERE 3 : P (%7.3f %7.3f %7.3f) R %5.3f"); glColor4fv(couleurVert()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d3->p->print("DROITE 3 : P (%7.3f %7.3f %7.3f)"); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d3->d->print(" D (%7.3f %7.3f %7.3f)"); glPopMatrix(); glFlush(); glutSwapBuffers(); } void displayV1(void) { glClearColor(0.0F,0.0F,0.0F,1.0F) ; glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); float pos = 1.0F; glColor4fv(couleurJaune()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; s->print("SPHERE : P (%7.3f %7.3f %7.3f) R %5.3f"); glColor4fv(couleurBlanc()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d->p->print("DROITE : P (%7.3f %7.3f %7.3f)"); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; d->d->print(" D (%7.3f %7.3f %7.3f)"); int inter = s->testIntersection(d); glColor4fv(couleurRouge()); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"INTERSECTIONS ? : %s %d", ((inter == 0) ? "NON" : "OUI"),inter); if (inter ) { Position **pts = s->intersection(d); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; pts[0]->print("INTERSECTION 1 : %8.4f %8.4f %8.4f"); delete(pts[0]); if ( pts[1] != NULL ) { pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; pts[1]->print("INTERSECTION 2 : %8.4f %8.4f %8.4f"); delete(pts[1]); } free(pts); } glPopMatrix(); glFlush(); glutSwapBuffers(); } void special(int key,int x,int y) { if ( specialManipulateur(key,x,y) ) { glutPostWindowRedisplay(f1); } } void key(unsigned char k,int x,int y) { if ( keyManipulateur(k,x,y) ) glutPostWindowRedisplay(f1); else switch ( k ) { case 'a' : { aff = !aff; glutPostWindowRedisplay(f1); } break; case 0x0D : { question = (question+1)%4; exemple = 0; int win = glutGetWindow(); switch ( question ) { case 1 : glutSetWindow(f1); glutDisplayFunc(display1); glutSetWindow(f2); glutReshapeWindow(470,130); glutDisplayFunc(displayV1); break; case 0 : glutSetWindow(f1); glutDisplayFunc(display2); glutSetWindow(f2); glutReshapeWindow(490,190); glutDisplayFunc(displayV2); break; case 3 : glutSetWindow(f1); glutDisplayFunc(display3); glutSetWindow(f2); glutReshapeWindow(470,110); glutDisplayFunc(displayV3); break; case 2 : glutSetWindow(f1); glutDisplayFunc(display4); glutSetWindow(f2); glutReshapeWindow(490,190); glutDisplayFunc(displayV4); break; } glutSetWindow(win); glutPostWindowRedisplay(f1); } break; case ' ' : { exemple = (exemple+1)%9; glutPostWindowRedisplay(f1); } break; } } void myInit() { glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); glShadeModel(GL_SMOOTH); glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glClearColor(0.5F,0.5F,0.5F,1.0F); } void reshapeV(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(); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitWindowSize(270,270); glutInitWindowPosition(50,50); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f1 = glutCreateWindow("Intersection rayon-sphere"); myInit(); creationMenuBasique(); setParametresOrthoBasique(-2.5,2.5,-2.5,2.5,-50.0,50.0); setManipulateurDistance(1.0F); setManipulateurSourisAngle(10.0F,-20.0F,0.0F); glutReshapeFunc(reshapeOrthoBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutKeyboardFunc(key); glutDisplayFunc(display2); glutSpecialFunc(special); glutInitWindowSize(490,190); glutInitWindowPosition(60,360); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f2 = glutCreateWindow("Valeurs"); creationMenuBasique(); glutDisplayFunc(displayV2); glutReshapeFunc(reshapeV); glutKeyboardFunc(key); glutSpecialFunc(special); glutMainLoop(); return(0); }