/* Auteur: Nicolas JANEY                */
/* nicolas.janey@univ-fcomte.fr         */
/* Janvier 2014                         */
/* Modelisation OpenGL d'une scene      */
/* composee de spheres et de cylindres  */

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

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

#include "Modules/ModuleCouleurs.h"
#include "Modules/ModuleCylindres.h"
#include "Modules/ModuleFont.h"
#include "Modules/ModuleManipulateur.h"
#include "Modules/ModuleMenus.h"
#include "Modules/ModuleReshape.h"

static int wire = 0 ;
static int maille = 0 ;

void init(void) {
  GLfloat light_position0[] = {  30.0F, 30.0F,30.0F,1.0F };
  GLfloat light_position1[] = { -30.0F,-30.0F,30.0F,1.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);
  glDepthFunc(GL_LESS);
  glEnable(GL_NORMALIZE);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);
  setManipulateurClavierAngle(35.0F,20.0F,0.0F);
}

void axes() {
  glPushMatrix() ;
  glColor4fv(couleurJaune()) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(1.0F,0.0F,0.0F) ;
  glEnd() ;
  placeFontCursor(1.05F,0.05F,0.05F);
  simpleBitmapOutput("x");
  glColor4fv(couleurCyan()) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(0.0F,1.0F,0.0F) ;
  glEnd() ;
  placeFontCursor(0.05F,1.05F,0.05F);
  simpleBitmapOutput("y");
  glColor4fv(couleurMagenta()) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(0.0F,0.0F,1.0F) ;
  glEnd() ;
  placeFontCursor(0.05F,0.05F,1.05F);
  simpleBitmapOutput("z");
  if ( maille ) {
    glColor4fv(couleurGrisMoyen()) ;
    for ( int i = -100 ; i < 100 ; i++ ) {
      glBegin(GL_LINES) ;
      glVertex3f((float) i,200.0F,0.0F) ;
      glVertex3f((float) i,-200.0F,0.0F) ;
      glEnd() ;
      glBegin(GL_LINES) ;
      glVertex3f(200.0F,(float) i,0.0F) ;
      glVertex3f(-200.0F,(float) i,0.0F) ;
      glEnd() ; } }
  glPopMatrix() ;
}

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

void element() {
  glPushMatrix();
  glTranslatef(-5.0F,0.0F,5.0F);
  solidCylindre(1.0F,10.0F,10,5);
  for ( int i = 0 ; i < 2 ; i++ ) {
    glTranslatef(0.0F,5.0F,0.0F);
    glutSolidSphere(2.0,24,24);
    glRotatef(-90.0F,0.0F,0.0F,1.0F);
    glTranslatef(0.0F,5.0F,0.0F);
    solidCylindre(1.0F,10.0F,10,5); }
  glPopMatrix();
}

void solidScene() {
  glPushMatrix();
  for ( int i = 0 ; i < 4 ; i++ ) {
    glPushMatrix();
    glRotatef(90.0F*i,1.0F,0.0F,0.0F);
    switch (i) {
      case 0 : glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge()) ;
               break;
      case 1 : glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert()) ;
               break;
      case 2 : glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBlanc()) ;
               break;
      case 3 : glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu()) ;
               break; }
    element();
    glPopMatrix(); }
  glPopMatrix();
}

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

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix() ;
  manipulateurSouris();
  manipulateurClavier();
  glDisable(GL_LIGHTING);
  axes();
  glPolygonMode(GL_FRONT_AND_BACK,(wire) ? GL_LINE : GL_FILL);
  if ( !wire )
    glEnable(GL_LIGHTING);
    else
    glColor4fv(couleurBlanc()) ;
  glEnable(GL_LIGHTING);
  solidScene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

void key(unsigned char key,int x,int y) {
  if ( keyManipulateur(key,x,y) )
    glutPostRedisplay();
    else
    switch ( key ) {
      case 0x0D : wire = !wire;
                  glutPostRedisplay();
                  break;
      case 32   : maille = !maille ;
                  glutPostRedisplay();
                  break; }
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(300,300); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("8 spheres et 12 cylindres");
  init(); 
  creationMenuBasique();
  setParametresOrthoBasique(-10.0,10.0,-10.0,10.0,-50.0,50.0);
  setManipulateurDistance(1.0F);
  glutReshapeFunc(reshapeOrthoBasique);
  glutKeyboardFunc(key);
  glutSpecialFunc(specialBasique);
  glutMotionFunc(motionBasique);
  glutMouseFunc(sourisBasique);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}
