//------------------------------------------------------------------------------
// Includes                          
//------------------------------------------------------------------------------

    #include <windows.h>
    #include <stdio.h>
    #include <GL/glut.h>
    #include <jpeglib.h>
    #include <jerror.h>

//------------------------------------------------------------------------------
// Constante & Variable
//------------------------------------------------------------------------------
        
    #define EXIT        {fclose(fichier); return -1;}
    
    GLuint TexNum[1];
 
//------------------------------------------------------------------------------
// Charge une image JPG
//------------------------------------------------------------------------------

int     LoadJPG ( char *filename, int nb )
{
    struct  jpeg_decompress_struct cinfo;       // les infos du fichiers
    struct  jpeg_error_mgr jerr;                // les erreurs
    GLubyte *ligne;                             // une ligne (?)
    GLubyte *image;                             // l'image sous forme de données
    int     ImageSize;                          // Taille de l'image

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    // Lit le fichier et son header, puis le décompresse
    FILE *fichier=fopen(filename, "rb");  
    jpeg_stdio_src(&cinfo, fichier);
    jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress ( &cinfo );
    
    ImageSize = cinfo.image_width * cinfo.image_height * 3;    
    image = (GLubyte *) malloc ( ImageSize );
    ligne=image;
    
    while ( cinfo.output_scanline < cinfo.output_height )
     {
        ligne = image + 3 * cinfo.image_width * cinfo.output_scanline;
        jpeg_read_scanlines ( &cinfo, &ligne, 1 );
     }
     
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);

    // Traitements de la texture par OpenGL
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);        // Construit la texture sous forme de données
    glGenTextures(nb, &TexNum[nb]);              // Génère une ID pour la texture OpenGL                                             
    
    glBindTexture(GL_TEXTURE_2D, TexNum[nb]);    // Pointe la texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    
    // Génère la texture OpenGL en mémoire
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, cinfo.image_width, cinfo.image_height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    
    fclose (fichier);
    free (image);
    return TexNum[nb];
 }
    
//------------------------------------------------------------------------------
// Redimensionnement
//------------------------------------------------------------------------------

void    ReshapeGL ( int Width, int Height )
 {
    glViewport ( 0, 0, Width, Height );
    glMatrixMode ( GL_PROJECTION );
    glLoadIdentity ( );
    gluPerspective ( 45, (float)(Width)/(float)(Height), 1, 100 );
    glMatrixMode ( GL_MODELVIEW );
    glLoadIdentity ( );
 }    

//------------------------------------------------------------------------------
// Initialisation
//------------------------------------------------------------------------------

void    InitGL ( )
 {
    glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );        // Black Background
    glClearDepth(1.0f);                             // Configuration de la profondeur du buffer
    glEnable ( GL_TEXTURE_2D );
 }

//------------------------------------------------------------------------------
// Dessin
//------------------------------------------------------------------------------

void    DrawGL ( )
 {
    static float angle=0;

    glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Efface les buffers
    glLoadIdentity ( );        
    
    gluLookAt ( 0, 0, -15, 0, 0, 0, 0, 1, 0 );
    glRotated ( angle++, 1, 2, 3);

    glBindTexture ( GL_TEXTURE_2D, TexNum[0] );
    glBegin ( GL_QUADS );
        glTexCoord2f ( 0, 0 ); glVertex2d ( 5, 5 );
        glTexCoord2f ( 0, 1 ); glVertex2d ( 5, -5 );
        glTexCoord2f ( 1, 1 ); glVertex2d ( -5, -5 );
        glTexCoord2f ( 1, 0 ); glVertex2d ( -5, 5 );
    glEnd ( );
  
    glutSwapBuffers ( );                           // Dessine la frame à l'écran
    glutPostRedisplay ( );                         // Redémarre DrawGL
 }

//------------------------------------------------------------------------------
// Fonction principale
//------------------------------------------------------------------------------

int main( int argc, char *argv[ ], char *envp[ ] )
 {
    // Création de la fenêtre
    glutInit ( &argc, argv );
    glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize ( 320, 240 );
    glutInitWindowPosition ( 50, 50 );
    glutCreateWindow ( "Load JPG" );
    InitGL ( );

    // Fichiers externes
    LoadJPG ( "tex.jpg", 0 );

    glutReshapeFunc ( ReshapeGL );
    glutDisplayFunc ( DrawGL );
    
    glutMainLoop ( );
    return 0;
 }

//------------------------------------------------------------------------------
// THE END
//------------------------------------------------------------------------------