/* Auteur: Nicolas JANEY                  */
/* nicolas.janey@univ-fcomte.fr           */
/* Mars 2021                              */
/* Module de dessin d'objets geometriques */
/* en OpenGL                              */

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

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

#include "ObjetsOpenGL.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif

/* Modelisation geometrique d'un cube               */
/* avec placage de textures sur chacune             */
/* de ses faces                                     */
/* ct : la taille du cube                           */
/* textureID : le tableau des 6 handles de texture  */
/*             a utiliser                           */

void solidCube(double ct,unsigned int *textureID) {
  float c =(float) ct/2.0F;
  /* Activation premiere texture                */
  glBindTexture(GL_TEXTURE_2D,textureID[0]);
  glBegin(GL_QUADS);
  glNormal3f(0.0F,0.0F,-1.0F);
  glTexCoord2f(0.0F,0.0F);
  glVertex3f( c, c,-c);
  glTexCoord2f(0.0F,1.0F);
  glVertex3f( c,-c,-c);
  glTexCoord2f(1.0F,1.0F);
  glVertex3f(-c,-c,-c);
  glTexCoord2f(1.0F,0.0F);
  glVertex3f(-c, c,-c);
  glEnd();
  /* Activation deuxieme texture                */
  glBindTexture(GL_TEXTURE_2D,textureID[1]);
  glBegin(GL_QUADS);
  glNormal3f(0.0F,0.0F,1.0F);
  glTexCoord2f(0.0F,0.0F);
  glVertex3f(-c,-c, c);
  glTexCoord2f(1.0F,0.0F);
  glVertex3f( c,-c, c);
  glTexCoord2f(1.0F,1.0F);
  glVertex3f( c, c, c);
  glTexCoord2f(0.0F,1.0F);
  glVertex3f(-c, c, c);
  glEnd();
  /* Activation troisieme texture               */
  glBindTexture(GL_TEXTURE_2D,textureID[2]);
  glBegin(GL_QUADS);
  glNormal3f(-1.0F,0.0F,0.0F);
  glTexCoord2f(0.0F,0.0F);
  glVertex3f(-c, c,-c);
  glTexCoord2f(0.0F,1.0F);
  glVertex3f(-c,-c,-c);
  glTexCoord2f(1.0F,1.0F);
  glVertex3f(-c,-c, c);
  glTexCoord2f(1.0F,0.0F);
  glVertex3f(-c, c, c);
  glEnd();
  /* Activation quatrieme texture               */
  glBindTexture(GL_TEXTURE_2D,textureID[3]);
  glBegin(GL_QUADS);
  glNormal3f(1.0F,0.0F,0.0F);
  glTexCoord2f(0.0F,0.0F);
  glVertex3f( c, c, c);
  glTexCoord2f(0.0F,1.0F);
  glVertex3f( c,-c, c);
  glTexCoord2f(1.0F,1.0F);
  glVertex3f( c,-c,-c);
  glTexCoord2f(1.0F,0.0F);
  glVertex3f( c, c,-c);
  glEnd();
  /* Activation cinquieme texture               */
  glBindTexture(GL_TEXTURE_2D,textureID[4]);
  glBegin(GL_QUADS);
  glNormal3f(0.0F,-1.0F,0.0F);
  glTexCoord2f(0.0F,0.0F);
  glVertex3f(-c,-c, c);
  glTexCoord2f(0.0F,1.0F);
  glVertex3f(-c,-c,-c);
  glTexCoord2f(1.0F,1.0F);
  glVertex3f( c,-c,-c);
  glTexCoord2f(1.0F,0.0F);
  glVertex3f( c,-c, c);
  glEnd();
  /* Activation sixieme texture                 */
  glBindTexture(GL_TEXTURE_2D,textureID[5]);
  glBegin(GL_QUADS);
  glNormal3f(0.0F,1.0F,0.0F);
  glTexCoord2f(0.0F,0.0F);
  glVertex3f( c, c, c);
  glTexCoord2f(0.0F,1.0F);
  glVertex3f( c, c,-c);
  glTexCoord2f(1.0F,1.0F);
  glVertex3f(-c, c,-c);
  glTexCoord2f(1.0F,0.0F);
  glVertex3f(-c, c, c);
  glEnd();
}

/* Modelisation geometrique d'une sphère            */
/* avec placage de textures                         */
/* rayon : la rayon de la sphere                    */
/* nlat : le nombre de facettes en latitude         */
/* nlon : le nombre de facettes en longitude        */
/* textureID : le handle de la texture a utiliser   */

void solidSphere(float rayon,int nlat,int nlon,unsigned int textureID) {
  glBindTexture(GL_TEXTURE_2D,textureID);
  for ( int i = 0 ; i < nlat ; i++ ) {
    float a1 = -M_PI/2.0F + i*M_PI/nlat ;
    float a2 = a1 + M_PI/nlat ;
    float cs1 = cos(a1);
    float cs2 = cos(a2);
    float sn1 = sin(a1);
    float sn2 = sin(a2);
    float t1 = 1.0F-(float) i/nlat;
    float t2 = 1.0F-(float) (i+1)/nlat;
    glBegin(GL_QUAD_STRIP);
    for ( int j = 0 ; j <= nlon ; j++ ) {
      float a = j*2*M_PI/nlon;
      float x1 = cs1*cos(a);
      float z1 = cs1*sin(a);
      float x2 = cs2*cos(a);
      float z2 = cs2*sin(a);
      float s = 1.0F-(float) j/nlon;
      glTexCoord2f(s,t1);
      glNormal3f(x1,sn1,z1);
      glVertex3f(rayon*x1,rayon*sn1,rayon*z1);
      glTexCoord2f(s,t2);
      glNormal3f(x2,sn2,z2);
      glVertex3f(rayon*x2,rayon*sn2,rayon*z2); }
    glEnd(); }
}
