/* 64 cubes de coté 0.5 places en un cube       */
/* de 4 x 4 x 4 cubes (taille globale = 6.5)    */
/* et colores en fonction de leur position      */
/* dans ce cube                                 */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Fevrier 2021                                 */

#include <stdio.h>
#include <math.h>

#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>

/* Variables et constantes globales             */

static const float blanc[]       = { 1.0F,1.0F,1.0F,1.0F };

static int aff = 1;
static int question = 0;

/* Fonction d'initialisation des parametres     */
/* OpenGL ne changeant pas au cours de la vie   */
/* du programme                                 */

static void init(void) {
  glClearColor(0.25F,0.25F,0.25F,1.0F);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
}

/* Scene dessinee                               */

static void scene() {
  const GLfloat light0_direction[] = { 0.0,0.0,1.0,0.0 };
  glLightfv(GL_LIGHT0,GL_POSITION,light0_direction);
  glPushMatrix();
  glTranslatef(0.0F,0.0F,-100.0F);
  for ( int x = -3 ; x <= 3 ; x += 2 )
    for ( int y = -3 ; y <= 3 ; y += 2 )
      for ( int z = -3 ; z <= 3 ; z += 2 ) {
        glPushMatrix();
        glTranslatef((float) x,(float) y,(float) z);
        float couleur[4] = { (x+3)/6.0F,(y+3)/6.0F,(z+3)/6.0F,1.0F };
        glMaterialfv(GL_FRONT,GL_DIFFUSE,couleur);
        glutSolidCube(0.5);
        glPopMatrix(); }
  glPopMatrix();
}

//////////////////////////////////////////////////

static int wtx;                    // Resolution horizontale du viewport
static int wty;                    // Resolution verticale du viewport

static double px = 0.0;            // Position en x de la caméra
static double py = 0.0;            // Position en y de la caméra
static double pz = -25.0;          // Position en z de la caméra

/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin                      */

static void displayOrthographique(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  double ratio =(double) wtx / wty;
  if ( ratio >= 1.0 )
    glOrtho(-4.0F*ratio,4.0F*ratio,-4.0F,4.0F,90.0F,110.0F);
    else
    glOrtho(-4.0F,4.0F,-4.0F/ratio,4.0F/ratio,90.0F,110.0F);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glPushMatrix();
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

/* Configuration des caractéristiques           */
/* virtuelle de visualisation en perspective    */
/* tx : Résolution horizontale du viewport      */
/* ty : Résolution verticale du viewport        */
/* distance : Distance en -z entre la caméra    */
/*            et le centre de visualisation     */
/* dZ : Déport en z des plan de clipping        */
/*      near et far de la caméra par rapport    */
/*      au centre de visualisation              */
/* taille : La taille de la scène               */
/* facteur : Facteur correctif multiplicatif    */
/*           sur l'ouverture verticale          */

static void setCameraPerspective(int tx,int ty,double distance,double dZ,double taille,double facteur) {
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  double ratio =(double) tx / ty;
  double angle = atan2(taille/2.0,distance-taille/2.0)*2*180.0/3.14159*facteur;
  printf("angle = %lf\n",angle);
  double cmin = distance-dZ;
  if ( cmin < 0.0 )
    cmin = 0.01;
  if ( ratio >= 1.0 )
    gluPerspective(angle,ratio,cmin,distance+dZ);
    else
    gluPerspective(angle/ratio,ratio,cmin,distance+dZ);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

static void displayPerspectiveB(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  setCameraPerspective(wtx,wty,100.0,3.5,6.5,4.0/3.25);
  glPushMatrix();
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

static void displayPerspectiveC(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  setCameraPerspective(wtx,wty,50.0,3.5,6.5,4.0/3.25);
  glPushMatrix();
  gluLookAt(0.0,0.0,-50.0,0.0,0.0,-100.0,0.0,1.0,0.0);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

static void displayPerspectiveD1(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  setCameraPerspective(wtx,wty,10.0,3.5,6.5,4.0/3.25*0.97);
  glPushMatrix();
  gluLookAt(0.0,0.0,-90.0,0.0,0.0,-100.0,0.0,1.0,0.0);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

static void displayPerspectiveD2(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  setCameraPerspective(wtx,wty,5.0,3.5,6.5,4.0/3.25*0.87);
  glPushMatrix();
  gluLookAt(0.0,0.0,-95.0,0.0,0.0,-100.0,0.0,1.0,0.0);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

static void displayPerspectiveD3(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  setCameraPerspective(wtx,wty,sqrt(5.0*5.0+5.0*5.0+5.0*5.0),6.0,6.5,1.25);
  glPushMatrix();
  gluLookAt(-5.0,-5.0,-95.0,0.0,0.0,-100.0,0.0,1.0,0.0);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

static void displayPerspectiveD4(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  setCameraPerspective(wtx,wty,sqrt(10.0*10.0+15.0*15.0+10.0*10.0),6.0,6.5,1.5);
  glPushMatrix();
  gluLookAt(-10.0,-15.0,-90.0,0.0,0.0,-100.0,0.0,1.0,0.0);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

static void displayPerspectiveD5(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  setCameraPerspective(wtx,wty,sqrt(10.0*10.0+15.0*15.0+20.0*20.0),6.0,6.5,1.6);
  glPushMatrix();
  gluLookAt(-10.0,-15.0,-120.0,0.0,0.0,-100.0,0.0,1.0,0.0);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

static void displayInteractif(void) {
  printf("%8.2lf%8.2lf%8.2lf\n",px,py,pz);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  double ratio =(double) wtx / wty;
  double angle = 10.0;
  if ( ratio >= 1.0 )
    gluPerspective(angle,ratio,0.01,1000.0);
    else
    gluPerspective(angle/ratio,ratio,0.01,1000.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glPushMatrix();
  gluLookAt(px,py,pz,0.0,0.0,-100.0,0.0,1.0,0.0);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */
/* -> Ajustement de la camera de visualisation  */

static void reshape(int tx,int ty) {
  glViewport(0,0,tx,ty); 
  wtx = tx;
  wty = ty;
}

/* Fonction executee lors dde l'utilisation     */
/* d'une touche non alphanumérique du clavier   */

static void special(int key,int x,int y) {
  switch(key) {
    case GLUT_KEY_UP :
      py += 0.25F;
      glutPostRedisplay();
      break;
    case GLUT_KEY_DOWN :
      py -= 0.25F;
      glutPostRedisplay();
      break;
    case GLUT_KEY_RIGHT :
      px += 0.25F;
      glutPostRedisplay();
      break;
    case GLUT_KEY_LEFT :
      px -= 0.25F;
      glutPostRedisplay();
      break;
    case GLUT_KEY_PAGE_UP :
      pz -= 0.25F;
      glutPostRedisplay();
      break;
    case GLUT_KEY_PAGE_DOWN :
      pz += 0.25F;
      glutPostRedisplay();
      break; }
}

//////////////////////////////////////////////////

/* Fonction executee lors de la frappe          */
/* d'une touche du clavier                      */

static void keyboard(unsigned char key,int x,int y) {
  switch ( key ) {
    case 0x20 :
      aff = (aff+1)%2;
      glutPostRedisplay(); 
      break;
    case 0x0D :
      question = (question+1)%9;
      switch(question) {
        case 0 :
          glutDisplayFunc(displayOrthographique);
          glutPostRedisplay();
          break; 
        case 1 :
          glutDisplayFunc(displayPerspectiveB);
          glutPostRedisplay();
          break; 
        case 2 :
          glutDisplayFunc(displayPerspectiveC);
          glutPostRedisplay();
          break; 
        case 3 :
          glutDisplayFunc(displayPerspectiveD1);
          glutPostRedisplay();
          break; 
        case 4 :
          glutDisplayFunc(displayPerspectiveD2);
          glutPostRedisplay();
          break; 
        case 5 :
          glutDisplayFunc(displayPerspectiveD3);
          glutPostRedisplay();
          break; 
        case 6 :
          glutDisplayFunc(displayPerspectiveD4);
          glutPostRedisplay();
          break; 
        case 7 :
          glutDisplayFunc(displayPerspectiveD5);
          glutPostRedisplay();
          break; 
        case 8 :
          glutDisplayFunc(displayInteractif);
          glutPostRedisplay();
          break; }
      glutPostRedisplay(); 
      break;
    case 0x1B :
      exit(0); 
      break; }
}

/* Fonction principale                          */

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(360,360); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("64 cubes"); 
  init();
  glutKeyboardFunc(keyboard);
  glutSpecialFunc(special);
  glutReshapeFunc(reshape);
  glutDisplayFunc(displayOrthographique);
  glutMainLoop();
  return(0);
}
