	program cube3D;

uses
  SysUtils,
  OpenGL,
  glfw,
  textures_OGL;

var
    running: boolean;
    Texture: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
    jetourne:=0;
    // Initialise GLFW
   
    running:=TRUE;
    while (running ) do
    begin
        // on recupere a nouveau le temps et la position de la souris
        //t := glfwGetTime;  permet de chopper le temps
         glfwInit;

    // Ouvre une fenetre OpenGL
    glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW );

    glfwSetWindowTitle('');;

    // desactive la synchronisation V
    glfwSwapInterval( 0 );


    //t0 := glfwGetTime;
      ;

      glfwSetWindowTitle('logo texture ToK');
      // Recupere la taille de la fenetre (qui peut etre différente de celle demandée)

      // quelle partie afficher ? ici -> tout
      glViewport( 0, 0, 1000, 800 );

      // 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, (1000/800), 1.0, 100.0 );

      // charge la matrice de modele-vue
      glMatrixMode( GL_MODELVIEW );
      glLoadIdentity;
      gluLookAt( 0.0   , 0.0  , -20.0 ,    // position des yeux
                 -5.0 , 5.0  , 0.0 ,     // position du centre la scène visualisée
                 0.0 , 1.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();


    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;

      // regarde si on appuie sur ESCP
      if glfwGetKey(GLFW_KEY_ESC) = GLFW_PRESS then
        begin
        running:=FALSE;
        glfwTerminate;
        end;
  end;

  // ferme openGL et glfw
  glfwTerminate;

end.
