/* Examen n°1                                   */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Fevrier 2021                                 */

#include <stdio.h>
#include <stdlib.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 const float noir[]        = { 0.0F,0.0F,0.0F,1.0F };

static int mode = 1;
static int anim = 0;
static int question = 0;
static int rz = 0;
static int pz = -900;
static int dz = 1;

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

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

static void init(void) {
  glClearColor(0.0F,0.0F,0.0F,1.0F);
  glColor3f(0.0F,0.0F,0.0F);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHTING);
  glEnable(GL_CULL_FACE);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glLightModelfv(GL_LIGHT_MODEL_AMBIENT,noir);
}

/* Scenes dessinees                             */

static void pyramide() {
}

static void animation() {
  glPushMatrix();
  glPushMatrix();
  glScalef(0.2F, 22.0F, 0.2F);
  glutSolidCube(1.0);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(0.0F, -11.0F, 0.0F);
  glutSolidSphere(1.0, 36, 36);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(0.0F, 11.0F, 0.0F);
  glutSolidSphere(1.0, 36, 36);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(0.0F, pz/100.0F, 0.0F);
  glutSolidSphere(1.0, 36, 36);
  glPopMatrix();
  glPopMatrix();
}

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

static void display(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  if ( mode ) {
    glEnable(GL_LIGHTING);
    glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); }
    else {
    glDisable(GL_LIGHTING);
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); }
  const GLfloat light0_position[] = { 0.0,0.0,1.0,0.0 };
  glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
  switch (question) {
    case 0 :
      pyramide();
      break;
    case 1 :
      glRotatef(rz/10.0F, 0.0F, 0.0F, 1.0F);
      animation();
      printf("%f %f\n", rz/10.0F, pz / 100.0F);
      break; }
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

/* Desallocation de la texture                  */

static void clean(void) {
}

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

/* 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); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  double ratio =(double) tx/ty;
  if ( ratio >= 1.0 )
    gluPerspective(80.0,ratio,0.01,200.0);
    else
    gluPerspective(80.0/ratio,ratio,0.01,200.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

static void idle(void) {
  rz += 1;
  pz += dz;
  if (abs(pz) == 900) {
    dz = -dz;
  }
  glutPostRedisplay();
}

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

static void keyboard(unsigned char key,int x,int y) {
  switch ( key ) {
    case 'm' :
      mode = !mode;
      glutPostRedisplay(); 
      break;
    case ' ' :
      anim = !anim;
      glutIdleFunc(( anim ) ? idle : NULL);
      break;
    case 0x0D :
      question = (question+1)%2;
      glutPostRedisplay(); 
      break;
    case 0x1B :
      exit(0); 
      break; }
}

/* Fonction principale                          */

int main(int argc,char **argv) {

  atexit(clean);

  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(640,640); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Examen 1"); 
  init();
  glutIdleFunc(( anim ) ? idle : NULL);
  glutKeyboardFunc(keyboard);
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}
