//
// Simple test for texture loaders
//
// Author: Alex V. Boreskoff <alexboreskoff@mtu-net.ru>, <steps3d@narod.ru>
//

#include	"libExt.h"
#include	<glut.h>
#include	<stdio.h>
#include	<stdlib.h>

#ifdef	_WIN32
    #include 	<gl\glaux.h>
#endif

#include	"libTexture.h"

unsigned	decalMap;

/////////////////////////////////////////////////////////////////////////////////

void init ()
{
	glClearColor ( 0.0, 0.0, 0.0, 1.0 );
	glEnable     ( GL_DEPTH_TEST );
	glDepthFunc  ( GL_LEQUAL );

	glHint ( GL_POLYGON_SMOOTH_HINT,         GL_NICEST );
	glHint ( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}

void    startOrtho ()
{
    glMatrixMode   ( GL_PROJECTION );                   // select the projection matrix
    glPushMatrix   ();                                  // store the projection matrix
    glLoadIdentity ();                                  // reset the projection matrix
                                                        // set up an ortho screen
    glOrtho        ( 0, 512, 0, 512, -1, 1 );
    glMatrixMode   ( GL_MODELVIEW );                    // select the modelview matrix
    glPushMatrix   ();                                  // store the modelview matrix
    glLoadIdentity ();                                  // reset the modelview matrix
}

void    endOrtho ()
{
    glMatrixMode ( GL_PROJECTION );                     // select the projection matrix
    glPopMatrix  ();                                    // restore the old projection matrix
    glMatrixMode ( GL_MODELVIEW );                      // select the modelview matrix
    glPopMatrix  ();                                    // restore the old projection matrix
}

void display ()
{
	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	startOrtho ();
	glBindTexture ( GL_TEXTURE_2D, decalMap );
	glEnable      ( GL_TEXTURE_2D );

	glBegin ( GL_QUADS );

	glTexCoord2f ( 0, 0 );
	glVertex2f   ( 0, 0 );

	glTexCoord2f ( 0, 1 );
	glVertex2f   ( 0, 511 );

	glTexCoord2f ( 1,   1   );
	glVertex2f   ( 511, 511 );

	glTexCoord2f ( 1, 0 );
	glVertex2f   ( 511, 0 );

	glEnd ();

	endOrtho ();

	glutSwapBuffers ();
}

void reshape ( int w, int h )
{
   glViewport     ( 0, 0, (GLsizei)w, (GLsizei)h );
   glMatrixMode   ( GL_PROJECTION );
   glLoadIdentity ();
   glMatrixMode   ( GL_MODELVIEW );
   glLoadIdentity ();
}

void key ( unsigned char key, int x, int y )
{
	if ( key == 27 || key == 'q' || key == 'Q' )	//	quit requested
    	exit ( 0 );
}

unsigned loadTex ( const char * fileName )
{
/*
	AUX_RGBImageRec * bmp = auxDIBImageLoad ( fileName );

	if ( bmp == NULL )
		return 0;

	unsigned texId;

	glGenTextures ( 1, &texId );
	glBindTexture ( GL_TEXTURE_2D, texId );
	glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );

	gluBuild2DMipmaps ( GL_TEXTURE_2D, 3, bmp->sizeX, bmp->sizeY, GL_RGB, GL_UNSIGNED_BYTE, bmp->data);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	if ( bmp->data != NULL )
			free( bmp -> data );

	free ( bmp );
*/
	return createTexture2D ( false, fileName );
}

int main ( int argc, char * argv [] )
{
								// initialize glut
	glutInit            ( &argc, argv );
	glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
	glutInitWindowSize  ( 512, 512 );


								// create window
	glutCreateWindow ( "Simple test for image loaders" );

								// register handlers
	glutDisplayFunc  ( display );
	glutReshapeFunc  ( reshape );
	glutKeyboardFunc ( key     );

	init           ();
	initExtensions ();

	decalMap = loadTex ( "../../models/face/face.tga" );

    glutMainLoop ();

	return 0;
}
