
#include "RegistredTexture.h"
#include "MemoryManager/DebugNew.h"
using namespace RenderSystem;

RegistredTexture::RegistredTexture()
{
	this->AnisotropyLevel = 1;
	this->magTexFilter = GL_NEAREST;
	this->minTexFilter = GL_NEAREST;
	this->sRepeat = GL_CLAMP;
	this->tRepeat = GL_CLAMP;
	this->uRepeat = GL_CLAMP;
	this->useMipMap = false;
	this->downSizingLevel = 0;
	this->name = NULL;
	this->isInVideoMemory = false;
}

RegistredTexture::~RegistredTexture()
{
	this->unloadFromVideoMem();
	if(this->name!=NULL)
	{
		delete [] this->name;
		this->name = NULL;
	}
}


void RegistredTexture::setProperties(int _downSizingLevel,int _magTexFilter,int _minTexFilter,int _AnisotropyLevel,int _sRepeat,int _tRepeat,int _uRepeat)
{
	if(_downSizingLevel>=0)
		this->downSizingLevel = _downSizingLevel;
	else
		this->downSizingLevel = 0;
	switch(_magTexFilter)
	{
	case GL_NEAREST:
		this->magTexFilter = GL_NEAREST;
		break;
	case GL_LINEAR:
		this->magTexFilter = GL_LINEAR;
		break;
	default:
		this->magTexFilter = GL_NEAREST;
		break;
	}
	switch(_minTexFilter)
	{
	case GL_NEAREST:
		this->useMipMap = false;
		this->minTexFilter = GL_NEAREST;
		break;
	case GL_LINEAR:
		this->useMipMap = false;
		this->minTexFilter = GL_LINEAR;
		break;
	case GL_NEAREST_MIPMAP_NEAREST:
		this->useMipMap = true;
		this->minTexFilter = GL_NEAREST_MIPMAP_NEAREST;
		break;
	case GL_NEAREST_MIPMAP_LINEAR:
		this->useMipMap = true;
		this->minTexFilter = GL_NEAREST_MIPMAP_LINEAR;
		break;
	case GL_LINEAR_MIPMAP_NEAREST:
		this->useMipMap = true;
		this->minTexFilter = GL_LINEAR_MIPMAP_NEAREST;
		break;
	case GL_LINEAR_MIPMAP_LINEAR:
		this->useMipMap = true;
		this->minTexFilter = GL_LINEAR_MIPMAP_LINEAR;
		break;
	default:
		this->useMipMap = false;
		this->minTexFilter = GL_NEAREST;
		break;
	}
	if(_AnisotropyLevel<1)
	{
		this->AnisotropyLevel = 1;//le plus bas
	}
	else if(_AnisotropyLevel>EXT_MAX_texture_filter_anisotropy)
	{
		this->AnisotropyLevel = EXT_MAX_texture_filter_anisotropy;//le plus haut
	}
	else
	{
		this->AnisotropyLevel = _AnisotropyLevel;//niveau demander valide
	}

	this->sRepeat = _sRepeat;
	this->tRepeat = _tRepeat;
	this->uRepeat = _uRepeat;
}

Texture* RegistredTexture::getTexture(){return NULL;};
unsigned int RegistredTexture::getId(){return 0;};
unsigned int RegistredTexture::getTarget(){return 0;};
void RegistredTexture::bindTexture(){};
void RegistredTexture::glDisableTexture(){};
void RegistredTexture::glEnableTexture(){};
bool RegistredTexture::loadInVideoMem(){return false;};
void RegistredTexture::unloadFromVideoMem(){};

void RegistredTexture::setName(const char* _name)
{
	if(this->name!=NULL)
		delete [] this->name;

	this->name = new char[strlen(_name)+1];
	strcpy(this->name,_name);
}


///////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////Texture2D/////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

RegistredTexture_2D::RegistredTexture_2D()
{
	bool generateAlpha=false;
	unsigned char rBlended=0;
	unsigned char gBlended=0;
	unsigned char bBlended=0;
	unsigned char alphaBlendValue=0;
	unsigned char alphaDefault=255;
	alphaMap = NULL;
}
RegistredTexture_2D::~RegistredTexture_2D()
{
	if(this->alphaMap!=NULL)
	{
		delete [] this->alphaMap;
		this->alphaMap = NULL;
	}
}

unsigned int RegistredTexture_2D::getTarget()
{
	return this->tex2d.getTarget();
}
unsigned int RegistredTexture_2D::getId()
{
	return this->tex2d.getId();
}
void RegistredTexture_2D::bindTexture()
{
	this->tex2d.bindTexture();
}
void RegistredTexture_2D::glEnableTexture()
{
	this->tex2d.glEnableTexture();
}
void RegistredTexture_2D::glDisableTexture()
{
	this->tex2d.glDisableTexture();
}

bool RegistredTexture_2D::loadInVideoMem()
{
	this->unloadFromVideoMem();	//effacera la texture en mémoire vidéo si elle est déjà présente : réallocation

	bool ret;
	if(this->alphaMap==NULL)
	{
		ret = tex2d.loadBMP(this->name,this->useMipMap,this->magTexFilter,this->minTexFilter,this->sRepeat,
			this->tRepeat,this->downSizingLevel,generateAlpha,rBlended,gBlended,bBlended,alphaBlendValue,alphaDefault,this->AnisotropyLevel);
	}
	else
	{
		ret = tex2d.loadLocalHeight(this->name,this->alphaMap,this->useMipMap,this->magTexFilter,this->minTexFilter,
			this->sRepeat,this->tRepeat,this->downSizingLevel,this->AnisotropyLevel);
	}
	this->isInVideoMemory = true;

	return ret;
}
void RegistredTexture_2D::unloadFromVideoMem()
{
	if(isInVideoMemory)
	{
		//on efface de la mémoire vidéo si la texture est chargé bien sur
		tex2d.~Texture2D();
		this->isInVideoMemory = false;
	}
}

void RegistredTexture_2D::setAlphaMap(const char* _alphamap)
{
	if(this->alphaMap!=NULL)
		delete [] this->alphaMap;//on efface l'ancienne

	if(_alphamap==NULL)
	{
		this->alphaMap = NULL;//pas d'alpha map
	}
	else
	{
		this->alphaMap = new char[strlen(_alphamap)+1];//une alphamap
		strcpy(this->alphaMap,_alphamap);
	}
}

Texture* RegistredTexture_2D::getTexture()
{
	return &this->tex2d;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////Texture3D/////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////





///////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////TextureCubeMap///////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////