	program cube3D;

uses
  SysUtils,
  OpenGL,
  glfw,
  textures_OGL;

var
    width, height, running, frames, x, y: Integer;
    t, t0, fps: Double;
    titlestr: string;
    pos_x, pos_z, pos_y, angle, angle2 : real;
    Texture, tex :GLuint;
    jetourne:integer;
    // quelques procedures pour le chargement de texture :
procedure glBindTexture(target: GLEnum; texture: GLuint); Stdcall; External 'OpenGL32.dll';
procedure glGenTextures(n: GLsizei; texture: PGLuint); Stdcall; External 'OpenGL32.dll';

begin

    // Initialise GLFW
    glfwInit;

    // Ouvre une fenetre OpenGL
    if glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW ) <> 1 then
    begin
        glfwTerminate;
        Exit;
    end;

    glfwSetWindowTitle('');;

    // Active la gestion des touches
    glfwEnable( GLFW_STICKY_KEYS );

    // desactive la synchronisation V
    glfwSwapInterval( 0 );

    // initialisation des variables pour la boucle principale
    running := 1;
    frames := 0;
    t0 := glfwGetTime;
    pos_x := 0;
    pos_z := 0;
    pos_y := 0;
    angle := 0;
    angle2:= 0;
    while running <> 0 do
    begin
        // on recupere a nouveau le temps et la position de la souris
        t := glfwGetTime;
        glfwGetMousePos(x, y );

        // calcule et affiche le fps dans le titre
        if ((t-t0) > 1.0) or (frames = 0) then
        begin
            fps := frames / (t-t0);
            //titlestr := Format('Car-cass (%.1f FPS)', [fps]);
            titlestr := 'Car-cass ' +inttostr(x);
            glfwSetWindowTitle( PChar(titlestr) );
            t0 := t;
            frames := 0;
      end;
      inc(frames);

      // Recupere la taille de la fenetre (qui peut etre différente de celle demandée)
      glfwGetWindowSize( width, height );
      if height < 1 then height := 1;

      // quelle partie afficher ? ici -> tout
      glViewport( 0, 0, width, height );

      // on efface le tampon couleur avec du noir
      glClearColor( 0.0, 0.0, 0.0, 0.0 );
      glClear( GL_COLOR_BUFFER_BIT );

      // charge la matrice de projection et d'identité
      glMatrixMode( GL_PROJECTION );
      glLoadIdentity;
      gluPerspective( 65.0, width/height, 1.0, 100.0 );

      // charge la matrice de modele-vue
      glMatrixMode( GL_MODELVIEW );
      glLoadIdentity;
      gluLookAt( 0.0  + pos_x , 0.0 + pos_y , -20.0 + pos_z,    // position des yeux
                 0.0 + pos_x , 0.0 + pos_y , 0.0 + pos_z,     // position du centre la scène visualisée
                 0.0 , 10.0 , 0.0 );                    // vecteur d'orientation vers le haut

      // dessine les carrés
      glTranslatef( 0.0, 0.0, 0.0 );
      glRotatef( 0, 0.0, 1.0, 0.0 );

  glEnable(GL_TEXTURE_2D);
  glGenTextures(1, @Texture);
  glBindTexture(GL_TEXTURE_2D, Texture);
  glfwLoadTexture2D('logo.tga', GLFW_BUILD_MIPMAPS_BIT);

  glShadeModel(GL_SMOOTH);				  // Enable Smooth Shading
	glClearColor(0.0, 0.0, 0.0, 0.0);	// Black Background
	glClearDepth(1.0);					      // Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);				  // Enables Depth Testing

  //glfwLoadTexture2D("logo.tga", 3);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glRotatef(angle, 0.3, 0.5, 0.8);
    glrotatef(angle2, -0.3, 0.5, 0.8);

    glBindTexture(GL_TEXTURE_2D, Texture);
    glrotate(jetourne,1,0,1);
    glBegin(GL_QUADS);
    // Front Face
    glTexCoord2f(0.0, 0.0); glVertex3f(-4.0, -4.0,  4.0);
    glTexCoord2f(1.0, 0.0); glVertex3f( 4.0, -4.0,  4.0);
    glTexCoord2f(1.0, 1.0); glVertex3f( 4.0,  4.0,  4.0);
    glTexCoord2f(0.0, 1.0); glVertex3f(-4.0,  4.0,  4.0);
    // Back Face
    glTexCoord2f(1.0, 0.0); glVertex3f(-4.0, -4.0, -4.0);
    glTexCoord2f(1.0, 1.0); glVertex3f(-4.0,  4.0, -4.0);
    glTexCoord2f(0.0, 1.0); glVertex3f( 4.0,  4.0, -4.0);
    glTexCoord2f(0.0, 0.0); glVertex3f( 4.0, -4.0, -4.0);
    // Top Face
    glTexCoord2f(0.0, 1.0); glVertex3f(-4.0,  4.0, -4.0);
    glTexCoord2f(0.0, 0.0); glVertex3f(-4.0,  4.0,  4.0);
    glTexCoord2f(1.0, 0.0); glVertex3f( 4.0,  4.0,  4.0);
    glTexCoord2f(1.0, 1.0); glVertex3f( 4.0,  4.0, -4.0);
    // Bottom Face
    glTexCoord2f(1.0, 1.0); glVertex3f(-4.0, -4.0, -4.0);
    glTexCoord2f(0.0, 1.0); glVertex3f( 4.0, -4.0, -4.0);
    glTexCoord2f(0.0, 0.0); glVertex3f( 4.0, -4.0,  4.0);
    glTexCoord2f(1.0, 0.0); glVertex3f(-4.0, -4.0,  4.0);
    // Right face
    glTexCoord2f(1.0, 0.0); glVertex3f( 4.0, -4.0, -4.0);
    glTexCoord2f(1.0, 1.0); glVertex3f( 4.0,  4.0, -4.0);
    glTexCoord2f(0.0, 1.0); glVertex3f( 4.0,  4.0,  4.0);
    glTexCoord2f(0.0, 0.0); glVertex3f( 4.0, -4.0,  4.0);
    // Left Face
    glTexCoord2f(0.0, 0.0); glVertex3f(-4.0, -4.0, -4.0);
    glTexCoord2f(1.0, 0.0); glVertex3f(-4.0, -4.0,  4.0);
    glTexCoord2f(1.0, 1.0); glVertex3f(-4.0,  4.0,  4.0);
    glTexCoord2f(0.0, 1.0); glVertex3f(-4.0,  4.0, -4.0);
    glEnd();
    glPopMatrix();

     jetourne:=jetourne+1;







      // swap du buffer
      glfwSwapBuffers;

      // action : touche gauche => reaction
      if (glfwGetKey( GLFW_KEY_LEFT )=1) then
          pos_x := pos_x + 0.5;

      // action : touche droite => reaction
      if (glfwGetKey( GLFW_KEY_RIGHT )=1) then
          pos_x := pos_x - 0.5;

      // action : touche delete => reaction
      if (glfwGetKey( GLFW_KEY_DEL )=1) then
          pos_y := pos_y + 0.5;

      // action : touche F1 => reaction
      if (glfwGetKey(GLFW_KEY_F1) = 1) then
          pos_y := pos_y - 0.5 ;

      // action : touche haut => reaction
      if (glfwGetKey( GLFW_KEY_UP )=1) then
          pos_z := pos_z + 0.5;

      // action : touche bas => reaction
      if (glfwGetKey( GLFW_KEY_DOWN )=1) then
          pos_z := pos_z - 0.5;

      // action : touche space => reaction
      if (glfwGetKey(GLFW_KEY_SPACE) = 1) then
          angle :=  angle + 5;

      // action : touche tab => reaction
      if (glfwGetKey(GLFW_KEY_TAB) = 1) then
          angle := angle - 5 ;

      // action : touche enter => reaction
      if (glfwGetKey(GLFW_KEY_ENTER) = 1) then
          angle2 := angle2 + 5 ;

      // action : touche insert => reaction
      if (glfwGetKey(GLFW_KEY_INSERT) = 1) then
          angle2 := angle2 - 5 ;

      // regarde si on appuie sur ESCP
      running := (not glfwGetKey( GLFW_KEY_ESC )) and
                 glfwGetWindowParam( GLFW_OPENED );
  end;

  // ferme openGL et glfw
  glfwTerminate;

end.
