 
    
/* Auteur: Nicolas JANEY             */
    /* nicolas.janey@univ-fcomte.fr      */
    /* Janvier 2002                      */
    /* Illustration de la decomposition  */
    /* de la lumiere en differentes      */
    /* composantes au niveau de          */
    /* l'interface entre deux milieux    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #include <GL/glut.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    
    #include "ModuleCouleurs.h"
    #include "ModuleFleche.h"
    #include "ModuleMatriceVecteur.h"
    #include "ModuleFont.h"
    #include "ModuleManipulateur.h"
    #include "ModuleMenus.h"
    #include "ModuleReshape.h"
    
    static vecteur p = { -9.0F,9.0F,0.0F,1.0F } ;
    static vecteur r ;
    static vecteur t ;
    static vecteur i ;
    static vecteur n = { 0.0F,1.0F,0.0F } ;
    static int rtt = 1;
    static float dif ;
    static float nfact=1.0F ;
    static float thetai ;
    static float thetat ;
    static int f1;
    static int f2;
    
    void myinit(void) {
      GLfloat light_position0[] = { 1.0F,0.0F,1.0F,0.0F };
      GLfloat light_position1[] = { -1.0F,0.0F,1.0F,0.0F };
      glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
      glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
      glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc());
      glLightfv(GL_LIGHT0,GL_SPECULAR,couleurNoir());
      glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
      glLightfv(GL_LIGHT1,GL_POSITION,light_position1);
      glEnable(GL_LIGHT0);
      glEnable(GL_LIGHT1);
      glEnable(GL_AUTO_NORMAL);
      glEnable(GL_NORMALIZE);
      glDepthFunc(GL_LESS);
      glEnable(GL_ALPHA_TEST);
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
      glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
    }
    
    void reflexion(vecteur p) {
      glColor4fv(couleurRouge()) ;
      r[0] = -p[0] ;
      r[1] = p[1] ;
      r[2] = -p[2] ;
      normalise(r) ;
      flecheEnVolume(10*r[0],10*r[1],10*r[2],0.35F,1.5F,0.07F);
      placeFontCursor(5*r[0],5*r[1],5*r[2]) ;
      deplacementCursor(5,15,0) ;
      simpleBitmapOutput(1,REGULAR8x13,"R") ; 
      deplacementCursor(2,10,0) ;
      simpleBitmapOutput(1,DESSIN,"TF") ;
    }
    
    void diffusion(vecteur p) {
      vecteur rd ;
      rd[0] = -p[0] ;
      rd[1] = -p[1] ;
      rd[2] = -p[2] ;
      normalise(rd) ;
      dif = -rd[1] ;
      thetai = acos(dif)*180.0F/3.14159F ;
      thetat = asin(nfact*sin(thetai*3.14159F/180.0F))*180.0F/3.14159F;
      glColor4fv(couleurVert()) ;
      for ( int j = 15 ; j < 90 ; j += 30 ) {
        float anglex = j * 3.14159F / 180.0F ;
        float daz = (((j-15)/30)%2 == 0) ? 20.0F : 0.0F ;
        for ( int k = 0 ; k < 360 ; k += 40 ) {
          float anglez = (k+daz) * 3.14159F / 180.0F ;
          float x =(float) (cos(anglez)*cos(anglex)*5*dif) ;
          float z =(float) (sin(anglez)*cos(anglex)*5*dif) ;
          float y =(float) (sin(anglex)*5*dif) ;
          flecheEnVolume(x,y,z,0.1F,0.5F,0.03F); } }
    }
    
    void transmission(vecteur p) {
      i[0] = p[0];
      i[1] = p[1];
      i[2] = p[2];
      normalise(i);
      float ni = produitScalaire(i,n) ;
      float v = 1-nfact*nfact*(1-ni*ni) ;
      if ( v > 0.0F ) {
        rtt = 1;
        float tt = nfact*ni-(float) sqrt(v) ;
        t[0] = tt*n[0] - nfact*i[0] ;
        t[1] = tt*n[1] - nfact*i[1] ;
        t[2] = tt*n[2] - nfact*i[2] ;
        glColor4fv(couleurBleu()) ;
        placeFontCursor(5*t[0],5*t[1],5*t[2]) ;
        deplacementCursor(5,0,0) ;
        simpleBitmapOutput(1,REGULAR8x13,"T") ; 
        deplacementCursor(2,-5,0) ;
        simpleBitmapOutput(1,DESSIN,"TF") ;
        flecheEnVolume(10*t[0],10*t[1],10*t[2],0.35F,1.5F,0.07F); }
        else
        rtt = 0;
    }
    
    void displayRayons(void) {
      glPushMatrix();
      glEnable(GL_LIGHTING);
      glPushMatrix();
      glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune()) ;
      glTranslatef(p[0],p[1],p[2]) ;
      glEnable(GL_CULL_FACE);
      glutSolidSphere(0.5,10,10);
      glDisable(GL_CULL_FACE);
      glDisable(GL_LIGHTING);
      glColor4fv(couleurJaune()) ;
      placeFontCursor(-p[0]/2.0F,-p[1]/2.0F,-p[2]/2.0F) ;
      deplacementCursor(5,-5,0) ;
      simpleBitmapOutput(1,REGULAR8x13,"I") ; 
      deplacementCursor(2,-10,0) ;
      simpleBitmapOutput(1,DESSIN,"TF") ;
      flecheEnVolume(-p[0],-p[1],-p[2],0.35F,1.5F,0.07F);
      glPopMatrix();
      glColor4fv(couleurMagenta()) ;
      placeFontCursor(0.0F,5.0F,0.0F) ;
      deplacementCursor(10,0,0) ;
      simpleBitmapOutput(1,REGULAR8x13,"N") ; 
      deplacementCursor(7,-5,0) ;
      simpleBitmapOutput(1,DESSIN,"TF") ;
      flecheEnVolume(0.0F,10.0F,0.0F,0.35F,1.5F,0.07F);
      diffusion(p);
      reflexion(p);
      transmission(p);
      glPopMatrix();
    }
    
    void display(void) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glEnable(GL_DEPTH_TEST);
      glPushMatrix() ;
      glTranslatef(0.0F,-2.0F,0.0F);
      manipulateurSouris();
      manipulateurClavier();
      displayRayons() ;
      glEnable(GL_LIGHTING);
      glPushMatrix();
      glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBlanc(0.5F)) ;
      glScalef(16.0F,0.0001F,14.0F) ;
      glEnable(GL_CULL_FACE);
      glutSolidCube(1.0) ;
      glDisable(GL_CULL_FACE);
      glPopMatrix();
      glDisable(GL_LIGHTING);
      glPopMatrix() ;
      glDisable(GL_DEPTH_TEST);
      glFlush();
      glutSwapBuffers() ;
      glutPostWindowRedisplay(f2);
    }
    
    void key2(unsigned char key,int x,int y) {
      switch ( key ) {
        case 'n'  : nfact *= 1.01F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case 'N'  : nfact /= 1.01F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case 'm'  : nfact *= 1.001F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case 'M'  : nfact /= 1.001F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case '1'  : p[0] -= 0.2F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case '4'  : p[0] += 0.2F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case '2'  : p[1] -= 0.2F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case '5'  : p[1] += 0.2F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case '3'  : p[2] -= 0.2F ;
                    glutPostWindowRedisplay(f1);
                    break;
        case '6'  : p[2] += 0.2F ;
                    glutPostWindowRedisplay(f1);
                    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();
      glColor4fv(couleurBlanc());
      float pos = 1.0F;
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      simpleBitmapOutput(1,REGULAR8x13,"n = ni/nt    : %6.3f",nfact) ;
      pos += 1.0F;
      glColor4fv(couleurJaune());
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      simpleBitmapOutput(1,REGULAR8x13,"INCIDENT     : %6.3f %6.3f %6.3f",i[0],i[1],i[2]) ;
      pos += 1.0F;
      glColor4fv(couleurMagenta());
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      simpleBitmapOutput(1,REGULAR8x13,"NORMALE      : %6.3f %6.3f %6.3f",n[0],n[1],n[2]) ;
      pos += 1.0F;
      glColor4fv(couleurVert());
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      simpleBitmapOutput(1,REGULAR8x13,"DIFFUSION    : %6.3f",dif) ;
      pos += 1.0F;
      glColor4fv(couleurRouge());
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      simpleBitmapOutput(1,REGULAR8x13,"REFLEXION    : %6.3f %6.3f %6.3f",r[0],r[1],r[2]) ;
      pos += 1.0F;
      glColor4fv(couleurBleu());
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      if ( rtt )
        simpleBitmapOutput(1,REGULAR8x13,"TRANSMISSION : %6.3f %6.3f %6.3f",t[0],t[1],t[2]) ;
        else
        simpleBitmapOutput(1,REGULAR8x13,"TRANSMISSION :  AUCUNE") ;
      pos += 1.0F;
      glColor4fv(couleurJaune());
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      simpleBitmapOutput(1,REGULAR8x13,"THETA I      :  %6.3f degres",thetai) ;
      pos += 1.0F;
      glColor4fv(couleurRouge());
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      simpleBitmapOutput(1,REGULAR8x13,"THETA R      :  %6.3f degres",thetai) ;
      pos += 1.0F;
      glColor4fv(couleurBleu());
      placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
      if ( rtt )
        simpleBitmapOutput(1,REGULAR8x13,"THETA T      :  %6.3f degres",thetat) ;
        else
        simpleBitmapOutput(1,REGULAR8x13,"THETA T      :  AUCUN") ;
      glPopMatrix();
      glutSwapBuffers();
    }
    
    int main(int argc,char **argv) {
      glutInit(&argc,argv);
      glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
      glutInitWindowPosition(50,50); 
      glutInitWindowSize(310,250); 
      f1 = glutCreateWindow("Décomposition au niveau d'une interface");
      myinit(); 
      creationMenuBasique();
      setParametresOrthoBasique(-10.0,10.0,-10.0,10.0,-500.0,500.0);
      setManipulateurDistance(1.0F);
      setManipulateurClavierAngle(25.0F,15.0F,0.0F);
      glutReshapeFunc(reshapeOrthoBasique);
      glutKeyboardFunc(key);
      glutSpecialFunc(specialBasique);
      glutDisplayFunc(display);
      glutMotionFunc(motionBasique);
      glutMouseFunc(sourisBasique);
      glutInitWindowSize(360,190);
      glutInitWindowPosition(60,340);
      glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
      f2 = glutCreateWindow("Valeurs");
      creationMenuBasique();
      glutDisplayFunc(display2);
      glutReshapeFunc(reshape2);
      glutKeyboardFunc(key2);
      glutSpecialFunc(special);
      glutMainLoop();
      return(0);
    }