////////////////////////////////////////////////////////////
//                                                        //
//  PC MAGAZIN - PC Underground                           //
//  Wrapper für Textureloader                             //
//  (w)(c)2000 Carsten Dachsbacher                        //
//                                                        //
////////////////////////////////////////////////////////////
#include	<windows.h>
#include	<gl\gl.h>			
#include	<gl\glu.h>			
#include	<gl\glaux.h>			
#include	<assert.h>
#include	<stdio.h>
#include	"Texture.h"

PCUTexture::PCUTexture()
{
	ID = 0;
}

PCUTexture::~PCUTexture()
{
	deleteTexture();
}

bool PCUTexture::createTexture()
{
	deleteTexture();

	glGenTextures(1, &ID);

	ID ++;

	glBindTexture( GL_TEXTURE_2D, ID - 1 );
	glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT );
	glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT );
	glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
	
	return true;
}

void PCUTexture::deleteTexture()
{
	if( ID )
	{
		ID --;
		glDeleteTextures( 1, &ID );
		ID = 0;
	}
}

void PCUTexture::select()
{
	glBindTexture( GL_TEXTURE_2D, ID - 1 );
}

// diese methode lädt NUR TGAs mit 256x256 Pixel und 32 Bit !!!
bool PCUTexture::loadTGA32Bit256x256( char *fileName )
{
	createTexture();

	FILE *f = fopen( fileName, "rb" );
	assert( f );

	unsigned char image[ 256 * 256 * 4 + 4 ];

	fseek( f, 44, SEEK_SET );
	fread( image, 4, 256 * 256, f );

	fclose( f );

	width = height = 256;

	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image+2 );

	return true;
}

bool PCUTexture::loadBMP( char *fileName )
{
	createTexture();

    AUX_RGBImageRec *texture;

    texture = auxDIBImageLoad( fileName );

	if ( !texture )
		return false;

	width = texture->sizeX;
	height = texture->sizeY;

	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, texture->data );

	return true;
}

