/* Lumieres en OpenGL                           */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Septembre 2012                               */

#include <stdlib.h>
#include <stdio.h>

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

#include "Modules/ModuleAxes.h"
#include "Modules/ModuleCubes.h"

/* Variables et constantes globales             */
/* pour les angles et les couleurs utilises     */

static int mode = 1;
static int l = 0;
static int m = 0;
static int a = 0;
static float ouv = 20.0F;
static float shn = 64.0F;
static float r = 0.0F;
static int aff = 0;
static int c = 0;
static int fct = 180;

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

static void init(void) {
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glEnable(GL_AUTO_NORMAL);
}

static void materialiseLumieres(void) {
  glDisable(GL_LIGHTING);
  if ( ( l == 1 ) || ( l == 3 ) ) {
    glPushMatrix();
    glRotatef(r,0.0F,1.0F,0.0F);
    glTranslatef(4.5F,0.0F,0.0F);
    glColor4f(0.0F,1.0F,0.0F,1.0F);
    glutSolidSphere(0.1,36,36);
    glBegin(GL_LINES);
    glVertex3f(0.0F,0.0F,0.0F);
    glVertex3f(-1.0F,0.0F,0.0F);
    glEnd();
    glPopMatrix(); }
  if ( ( l == 0 ) || ( l == 3 ) ) {
    glPushMatrix();
    glRotatef(r,0.0F,1.0F,0.0F);
    glTranslatef(0.0F,0.0F,2.5F);
    glColor4f(1.0F,0.0F,0.0F,1.0F);
    glutSolidSphere(0.1,36,36);
    glPopMatrix(); }
  glEnable(GL_LIGHTING);
}

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

static const float noir[] = { 0.0F,0.0F,0.0F,1.0F };
static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
static const float vert[] = { 0.0F,1.0F,0.0F,1.0F };
static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
static const float grisClair[] = { 0.8F,0.8F,0.8F,1.0F };
static const float grisFonce[] = { 0.2F,0.2F,0.2F,1.0F };
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };

static void lumiere0(void) {
  const GLfloat light0_position[] = { 0.0,0.0,2.5,1.0 };
  glPushMatrix();
  glRotatef(r,0.0F,1.0F,0.0F);
  glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
  glPopMatrix();
  glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge);
  glLightfv(GL_LIGHT0,GL_SPECULAR,blanc);
  glLightfv(GL_LIGHT0,GL_AMBIENT,noir);
  if ( a ) {
    glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,0.3F);
    glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.0F);
    glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.3F); }
    else {
    glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1.0F);
    glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.0F);
    glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.0F); }
  glEnable(GL_LIGHT0);
}

static void lumiere1(void) {
  const GLfloat light1_position[] = { 1.0,0.0,0.0,0.0 };
  glPushMatrix();
  glRotatef(r,0.0F,1.0F,0.0F);
  glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
  glPopMatrix();
  glLightfv(GL_LIGHT1,GL_DIFFUSE,vert);
  glLightfv(GL_LIGHT1,GL_SPECULAR,blanc);
  glLightfv(GL_LIGHT1,GL_AMBIENT,noir);
  glEnable(GL_LIGHT1);
}

static void lumiere2(void) {
  const GLfloat light2_position[] = { 2.0,10.0,2.0,1.0 };
  const GLfloat light2_direction[] = { -4.0,-10.0,-4.0 };
  glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
  glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,light2_direction);
  glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,ouv);
  glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu);
  glLightfv(GL_LIGHT2,GL_SPECULAR,blanc);
  glLightfv(GL_LIGHT2,GL_AMBIENT,noir);
  glEnable(GL_LIGHT2);
}

static void materiel0(void) {
  glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
  glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
  glMaterialf(GL_FRONT,GL_SHININESS,0.0F);
  glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
  glMaterialfv(GL_FRONT,GL_EMISSION,noir);
}

static void materiel1(void) {
  glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge);
  glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
  glMaterialf(GL_FRONT,GL_SHININESS,0.0F);
  glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
  glMaterialfv(GL_FRONT,GL_EMISSION,noir);
}

static void materiel2(void) {
  glMaterialfv(GL_FRONT,GL_DIFFUSE,jaune);
  glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
  glMaterialf(GL_FRONT,GL_SHININESS,0.0F);
  glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
  glMaterialfv(GL_FRONT,GL_EMISSION,noir);
}

static void materiel3(void) {
  glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
  glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
  glMaterialf(GL_FRONT,GL_SHININESS,shn);
  glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
  glMaterialfv(GL_FRONT,GL_EMISSION,noir);
}

static void materielBase(void) {
  glMaterialfv(GL_FRONT,GL_DIFFUSE,grisClair);
  glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
  glMaterialf(GL_FRONT,GL_SHININESS,0.0);
  glMaterialfv(GL_FRONT,GL_AMBIENT,grisFonce);
  glMaterialfv(GL_FRONT,GL_EMISSION,noir);
}

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

static void lumieres(void) {
  glDisable(GL_LIGHT0);
  glDisable(GL_LIGHT1);
  glDisable(GL_LIGHT2);
  switch (l) {
    case 0 :
      lumiere0();
      break;
    case 1 :
      lumiere1();
      break;
    case 2 :
      lumiere2();
      break;
    case 3 :
      lumiere0();
      lumiere1();
      lumiere2();
      break; }
  glEnable(GL_LIGHTING);
}

static void materiel(void) {
  switch (m) {
    case 0 :
      materielBase();
      break;
    case 1 :
      materiel0();
      break;
    case 2 :
      materiel1();
      break;
    case 3 :
      materiel2();
      break;
    case 4 :
      materiel3();
      break; }
}

/* Scene dessinee                               */

static void cube(double ct) {
  if ( c ) {
    mySolidCube(ct,fct); }
    else {
    glutSolidCube(ct); }
}

static void scene1(void) {
  glPushMatrix();
  lumieres();
  materiel();
  glPushMatrix();
  glTranslatef(2.0F,0.0F,2.0F);
  cube(2.0);
  glPopMatrix();
  materiel();
  glPushMatrix();
  glTranslatef(2.0F,0.0F,-2.0F);
  cube(2.0);
  glPopMatrix();
  materiel();
  glPushMatrix();
  glTranslatef(-2.0F,0.0F,2.0F);
  cube(2.0);
  glPopMatrix();
  materiel();
  glPushMatrix();
  glTranslatef(-2.0F,0.0F,-2.0F);
  cube(2.0);
  glPopMatrix();
  glPopMatrix();
}

static void scene2(void) {
  glPushMatrix();
  lumieres();
  materiel();
  glPushMatrix();
  glTranslatef(2.0F,0.0F,2.0F);
  glutSolidSphere(1.0,fct,fct);
  glPopMatrix();
  materiel();
  glPushMatrix();
  glTranslatef(2.0F,0.0F,-2.0F);
  glutSolidSphere(1.0,fct,fct);
  glPopMatrix();
  materiel();
  glPushMatrix();
  glTranslatef(-2.0F,0.0F,2.0F);
  glutSolidSphere(1.0,fct,fct);
  glPopMatrix();
  materiel();
  glPushMatrix();
  glTranslatef(-2.0F,0.0F,-2.0F);
  glutSolidSphere(1.0,fct,fct);
  glPopMatrix();
  glPopMatrix();
}

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

static void display1(void) {
  glClearColor(0.3F,0.3F,0.3F,0.5F);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  const GLfloat light0_position[] = { 0.0,0.0,0.0,1.0 };
  const GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 };
  const GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 };
  glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
  glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
  glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
  glPolygonMode(GL_FRONT_AND_BACK,( mode ) ? GL_FILL : GL_LINE);
  glPushMatrix();
  dessinAxes();
  if ( aff ) {
    materialiseLumieres(); }
  scene1();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Attention erreur %d\n",error);
}

static void display2(void) {
  glClearColor(0.3F,0.3F,0.3F,0.5F);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,( mode ) ? GL_FILL : GL_LINE);
  glPushMatrix();
  dessinAxes();
  if ( aff ) {
    materialiseLumieres(); }
  scene2();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Attention erreur %d\n",error);
}

/* Fonction executee lorsqu'aucun evenement     */
/* n'est en file d'attente                      */

static void idle(void) {
  r = r + 1.0F;
  glutPostRedisplay();
}

/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */

static void reshape(int x,int y) {
  glViewport(0,0,x,y); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if ( x >= y )
    gluPerspective(26.0F,(float) x/y,1.0,40.0);
    else
    gluPerspective(26.0F*(float) y/x,(float) x/y,1.0,40.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(6.0, 9.0,11.0,
            0.0,-0.5, 0.0,
            0.0, 1.0, 0.0);
}

/* Fonction executee lors de l'appui            */
/* d'une touche alphanumerique du clavier       */

static void keyboard(unsigned char key,int x,int y) {
  switch (key) {
    case 'O' :
      { ouv = ouv+0.2F; }
      glutPostRedisplay();
      break;
    case 'o' :
      { ouv = ouv-0.2F;
        if ( ouv < 0.0F )
          ouv = 0.0F; }
      glutPostRedisplay();
      break;
    case 's' :
      { shn = shn-0.5F;
        if ( shn < 0.0F )
          shn = 0.0F; }
      glutPostRedisplay();
      break;
    case 'S' :
      { shn = shn+0.5F;
        if ( shn > 128.0F )
          shn = 128.0F; }
      glutPostRedisplay();
      break;
    case 'l' :
      { l = (l+1)%4; }
      glutPostRedisplay();
      break;
    case 'm' :
      { m = (m+1)%5; }
      glutPostRedisplay();
      break;
    case 'a' :
      { a = (a+1)%2; }
      glutPostRedisplay();
      break;
    case 'c' :
      { c = (c+1)%2; }
      glutPostRedisplay();
      break;
    case '!' :
      { aff = (aff+1)%2; }
      glutPostRedisplay();
      break;
    case 0x0D :
      { static int anim = 0;
        anim = !anim;
        glutIdleFunc(( anim ) ? idle : NULL); }
      break;
    case 0x20 :
      { static int objet = 1;
        objet = !objet;
        glutDisplayFunc(( objet ) ? display1 : display2); }
      glutPostRedisplay();
      break;
    case 'f' :
      { mode = !mode; }
      glutPostRedisplay();
      break;
    case 43 :
      fct++;
      glutPostRedisplay();
      break;
    case 45 :
      fct--;
      if ( fct < 3 )
        fct = 3;
      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(400,300);
  glutInitWindowPosition(50,50);
  glutCreateWindow("Lumieres et matériel");
  init();
  glutKeyboardFunc(keyboard);
  glutReshapeFunc(reshape);
  //glutIdleFunc(idle);
  glutDisplayFunc(display1);
  glutMainLoop();
  return(0);
}
