/* Placage d'une texture sur un rectangle       */
/* ou sur un cylindre                           */
/*                                              */
/* 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 float rx = 0.0F;
static float ry = 0.0F;
static float rz = 0.0F;

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

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

static void init(void) {
  glClearColor(1.0F,1.0F,1.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);
}

/* Dessin d'un cylindre                         */

#ifndef M_PI
  #define M_PI 3.14159
#endif

static void solidCylinder(double hauteur,double rayon,int ns) {
  glPushMatrix();
  hauteur /= 2.0F;
  glBegin(GL_QUAD_STRIP);
  for ( int i = 0 ; i <= ns ; i++ ) {
    float rp =(float) i/ns;
    float a = 2.0*M_PI*rp;
    float cs = cos(a);
    float sn = -sin(a); 
    glNormal3f(cs,0.0F,sn);
    float x = rayon*cs;
    float z = rayon*sn;
    glTexCoord2f(rp,1.0F);
    glVertex3f(x,hauteur,z);
    glTexCoord2f(rp,0.0F);
    glVertex3f(x,-hauteur,z); }
  glEnd();
  glPopMatrix();
}

/* Scene dessinee : rectangle                    */

static void marche(void) {
  float hauteur = 0.125F;
  glPushMatrix();
  float x1 = 1.0;
  float z1 = 0.0;
  double angle = 20.0/180.0*3.14159;
  float x2 = 1.0*cos(angle);
  float z2 = 1.0*sin(angle);
  glBegin(GL_TRIANGLES);
  glNormal3f(0.0F,-1.0F,0.0F);
  glVertex3f(0.0F,0.00F,0.0F);
  glVertex3f(  x1,0.00F,  z1);
  glVertex3f(  x2,0.00F,  z2);
  glNormal3f(0.0F,1.0F,0.0F);
  glVertex3f(0.0F,0.125F,0.0F);
  glVertex3f(  x2,0.125F,  z2);
  glVertex3f(  x1,0.125F,  z1);
  glEnd();
  glBegin(GL_QUADS);
  glNormal3f(0.0F,0.0F,-1.0F);
  glVertex3f(0.0F,0.00F,0.0F);
  glVertex3f(0.0F,0.125F,0.0F);
  glVertex3f(  x1,0.125F,  z1);
  glVertex3f(  x1,0.00F,  z1);
  glEnd();
  glBegin(GL_QUADS);
  glNormal3f( -z2,0.00F,  x2);
  glVertex3f(  x2,0.00F,  z2);
  glVertex3f(  x2,0.125F,  z2);
  glVertex3f(0.0F,0.125F,0.0F);
  glVertex3f(0.0F,0.00F,0.0F);
  glEnd();
  glBegin(GL_QUADS);
  glNormal3f(cos(angle/2.0F),0.0F,sin(angle/2.0F));
  glVertex3f(  x2,0.125F,  z2);
  glVertex3f(  x2,0.00F,  z2);
  glVertex3f(  x1,0.00F,  z1);
  glVertex3f(  x1,0.125F,  z1);
  glEnd();
  glPopMatrix();
}

/* Scene dessinee : cylindre                    */

static void escalier(void) {
  glPushMatrix();
  glPushMatrix();
  glTranslatef(0.0F,1.5F,0.0F);
  solidCylinder(3.0,0.1,24);
  glPopMatrix();
  for ( int i = 0 ; i < 24 ; i++ ) {
    glPushMatrix();
    glRotatef(15.0F*i,0.0F,1.0F,0.0F);
    glTranslatef(0.0F,0.125F*i,0.0F);
    marche();
    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);
  glRotatef(rx,1.0F,0.0F,0.0F);
  glRotatef(rz,0.0F,0.0F,1.0F);
  glRotatef(ry,0.0F,1.0F,0.0F);
  switch (question) {
    case 0 :
      marche();
      break;
    case 1 :
      glTranslatef(0.0F,-1.5F,0.0F);
      escalier();
      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(20.0,ratio,0.01,200.0);
    else
    gluPerspective(20.0/ratio,ratio,0.01,200.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0.0,0.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

static void idle(void) {
  rx += 0.51F;
  ry += 0.28F;
  rz += 0.37F;
  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(480,360); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Un escalier en colimaçon"); 
  init();
  glutIdleFunc(( anim ) ? idle : NULL);
  glutKeyboardFunc(keyboard);
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}
