/* Un programme OpenGL de deplacement            */
/* d'une camera au sein d'une scene              */
/* par utilisation de la souris et du clavier    */
/*                                               */
/* Auteur: Nicolas JANEY                         */
/* nicolas.janey@univ-fcomte.fr                  */
/* Mars 2021                                     */

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

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

#include "Scene.h"

/* Variables globales                           */

#ifndef M_PI
#define M_PI 3.14159
#endif

static float px = 0.0F;
static float py = 0.5F;
static float pz = 0.0F;
static float angle = 0.0F;
static float ouv = 50.0F;
static double dd = 0.0;
static double dg = 0.0;

static int mouvement = 0;
static int button = 0;
static int mx;
static int my;

static void redisplay(void) {
  glutPostRedisplay();
}

void scene(void) {
  int i;
  const GLfloat c1[4] = { 1.0F,1.0F,1.0F,1.0F };
  const GLfloat c2[4] = { 0.1F,0.7F,0.6F,1.0F };
  const GLfloat c3[4] = { 1.0F,1.0F,0.0F,1.0F };
  glPushMatrix();
  glNormal3f(0.0F,1.0F,0.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,c1);
  glColor4fv(c1);
  glBegin(GL_QUADS);
  for ( i = 0 ; i < 20 ; i++ ) {
    for ( int j = 0 ; j < 20 ; j++ ) {
      if ( (i+j)%2 == 1 ) {
        glVertex3f(i-10.0F,0.0F,j-10.0F);
        glVertex3f(i-9.0F,0.0F,j-10.0F);
        glVertex3f(i-9.0F,0.0F,j-9.0F);
        glVertex3f(i-10.0F,0.0F,j-9.0F); } } }
  glEnd();
  glMaterialfv(GL_FRONT,GL_DIFFUSE,c2);
  glColor4fv(c2);
  glBegin(GL_QUADS);
  for ( i = 0 ; i < 20 ; i++ ) {
    for ( int j = 0 ; j < 20 ; j++ ) {
      if ( (i+j)%2 == 0 ) {
        glVertex3f(i-10.0F,0.0F,j-10.0F);
        glVertex3f(i-9.0F,0.0F,j-10.0F);
        glVertex3f(i-9.0F,0.0F,j-9.0F);
        glVertex3f(i-10.0F,0.0F,j-9.0F); } } }
  glEnd();
  glMaterialfv(GL_FRONT,GL_DIFFUSE,c3);
  glColor4fv(c3);
  srand(0);
  for ( i = 0 ; i < 100 ; i++ ) {
    float x = -10.0F+((float) (rand()%1000)/50.0F); 
    float z = -10.0F+((float) (rand()%1000)/50.0F);
    glPushMatrix();
    glTranslatef(x,1.0F,z);
    glRotatef(rand()%360,0.0F,1.0F,0.0F);
    glScalef(0.1F,2.0F,0.1F);
    glutSolidCube(1.0);
    glPopMatrix(); }
  glPopMatrix();
}

void init(void) {
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_LIGHT2);
  glEnable(GL_LIGHT3);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glClearColor(0.8F,0.8F,0.9F,1.0F);
}

void lumieres(void) {
  const GLfloat blanc[] = { 0.3F,0.3F,0.3F,1.0F };
  const GLfloat l_pos0[] = { 4.0F,3.0F,4.0F,1.0F };
  glLightfv(GL_LIGHT0,GL_POSITION,l_pos0);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
  const GLfloat l_pos1[] = { -4.0F,3.0F,4.0F,1.0F };
  glLightfv(GL_LIGHT1,GL_POSITION,l_pos1);
  glLightfv(GL_LIGHT1,GL_DIFFUSE,blanc);
  const GLfloat l_pos2[] = { -4.0F,3.0F,-4.0F,1.0F };
  glLightfv(GL_LIGHT2,GL_POSITION,l_pos2);
  glLightfv(GL_LIGHT2,GL_DIFFUSE,blanc);
  const GLfloat l_pos3[] = { 4.0F,3.0F,-4.0F,1.0F };
  glLightfv(GL_LIGHT3,GL_POSITION,l_pos3);
  glLightfv(GL_LIGHT3,GL_DIFFUSE,blanc);
}

/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */
/* Configuration d'une camera de visualisation  */
/* en projection en perspective                 */

void reshape(int tx,int ty) {
  /* Configuration du viewport d'affichage      */
  glViewport(0,0,tx,ty);
  /* Choix et configuration de la camera        */
  /* de visualisation                           */
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(ouv,(double) tx/ty,0.1,100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

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

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  gluLookAt(px,py,pz,
            px+sin(angle),py,pz+cos(angle),
            0.0,1.0,0.0);
  /* Affichage de la scene                      */
  lumieres();
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Attention, erreur OpenGL %d\n",error);
}

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

void keyboard(unsigned char key,int x,int y) {
  switch ( key ) {
    case 43 :
      redisplay();
      break;
    case 45 :
      redisplay();
      break;
    case 0x1B :
      exit(0); }
}

/* Fonction executee lors de l'appui            */
/* d'une touche de la souris                    */

void mouse(int bouton,int etat,int x,int y) {
  button = bouton;
  mx = x;
  my = y; 
  if ( etat == GLUT_DOWN ) {
    mouvement = 1; }
  if ( etat == GLUT_UP ) {
    mouvement = 0; } 
}

/* Fonction executee lors du deplacement        */
/* de la souris devant la fenetre               */
/* avec le bouton appuye                        */

void motion(int x,int y) {
  switch ( button ) {
    case GLUT_LEFT_BUTTON :
      if ( mouvement == 1 ) {
        px += 0.04F*sin(angle)*(my-y)/5.0F;
        pz += 0.04F*cos(angle)*(my-y)/5.0F;
        dd += 0.04F*(my-y)/5.0F;
        dg += 0.04F*(my-y)/5.0F;
        angle += (x-mx)/200.0F;
        dd += 0.01F*(x-mx)/5.0F;
        dg -= 0.01F*(x-mx)/5.0F;
        mx = x; 
        my = y; 
        redisplay(); }
      break; }
}

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

void special(int key,int x,int y) {
  switch ( key ) {
    case GLUT_KEY_UP :
      px += 0.009F*sin(angle);
      pz += 0.009F*cos(angle);
      dd += 0.009F;
      dg += 0.009F;
      redisplay();
      break;
    case GLUT_KEY_DOWN :
      px -= 0.009F*sin(angle);
      pz -= 0.009F*cos(angle);
      dd -= 0.009F;
      dg -= 0.009F;
      redisplay();
      break;
    case GLUT_KEY_RIGHT :
      angle -= 0.01F;
      dd -= 0.005F;
      dg += 0.005F;
      redisplay();
      break;
    case GLUT_KEY_LEFT :
      angle += 0.01F;
      dd += 0.005F;
      dg -= 0.005F;
      redisplay();
      break; }
}

/* Fonction principale                          */

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
  glutInitWindowSize(1260,560); 
  glutInitWindowPosition(20,50); 
  glutCreateWindow("Deplacement dans une scène");
  init();
  glutKeyboardFunc(keyboard);
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  glutSpecialFunc(special);
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}
