/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///																																  ///
///		FICHIER :	ZooMaterial.cpp																									  ///
///																																  ///
///		NATURE	:	Management of the material of a face for the 3D scene																		  ///
///																																  ///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include	"..\Datas\ZooMaterial.h"



//$LB
int calcfiltertransp(int col, int coef, unsigned char * buf)
{
	int i;
	unsigned char r, g, b;

	coef&=0xff;

	b = (col >>16)&0xFF;
	g = (col >>8) &0xFF;
	r = col       &0xFF;


	//$LB
	for(i=0; i<256; i++)
	{
		buf[i] = ((( i*coef + r*(256-coef) )>>8)&0xFF);

		buf[i + TABCOL_OFFSET1]=(((i*coef + g*(256-coef))>>8)&0xFF);

		buf[i + TABCOL_OFFSET2]=(((i*coef + b*(256-coef))>>8)&0xFF);
	}
	return 0;
}




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ZMaterial Class
///
///		- Classe des matériaux
///		- Méthodes pour modifier les params du matériau
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////
/// Constructor	& Destructor	 										///
///////////////////////////////////////////////////////////////////////////
ZMaterial::ZMaterial(bool accel) : ZData()
{
	type = MAT_TYPE_ID;

	SetDefaultVals();

	modified = true;

	//$LB
	color.SetCoord(1.0f, 1.0f, 1.0f);
	color_24  = 0xFFFFFF;
	color1.SetCoord(1.0f, 1.0f, 1.0f);
	color1_24 = 0xFFFFFF;
	color2.SetCoord(1.0f, 1.0f, 1.0f);
	color2_24 = 0xFFFFFF;


	texture1 = texture2 = lightMap = normalMap = NULL;

	coeffTransp_8 = 1;
	coeffTransp   = 1.0f-((float)coeffTransp_8)/CONST_COLOR;

	transparencyOrder = 0;

	alpha = 0.5f;

	RenderMode = 0x00000000;

	EnableMode(RENDER_LIGHTED);

	accelerated = accel;
	//$LB
	if(accelerated==false)		transpTab24 = (unsigned char*) malloc(TABCOL);
}

ZMaterial::~ZMaterial()
{
	if(texture1)		texture1->DecRef();
	if(texture2)		texture2->DecRef();
	if(lightMap)		lightMap->DecRef();
	if(normalMap)		normalMap->DecRef();
}




///////////////////////////////////////////////////////////////////////////
/// Set the ambient component											///
///////////////////////////////////////////////////////////////////////////
void ZMaterial::SetAmbient(float newVals[4])
{
	for(int i=0; i<4; i++)		ambient[i] = newVals[i];
}
void ZMaterial::SetAmbient(float newVal0, float newVal1, float newVal2, float newVal3)
{
	ambient[0] = newVal0;	ambient[1] = newVal1;
	ambient[2] = newVal2;	ambient[3] = newVal3;
}



///////////////////////////////////////////////////////////////////////////
/// Set the Diffuse component											///
///////////////////////////////////////////////////////////////////////////
void ZMaterial::SetDiffuse(float newVals[4])
{
	for(int i=0; i<4; i++)		diffuse[i] = newVals[i];
}
void ZMaterial::SetDiffuse(float newVal0, float newVal1, float newVal2, float newVal3)
{
	diffuse[0] = newVal0;	diffuse[1] = newVal1;
	diffuse[2] = newVal2;	diffuse[3] = newVal3;
}



///////////////////////////////////////////////////////////////////////////
/// Set the Specular component											///
///////////////////////////////////////////////////////////////////////////
void ZMaterial::SetSpecular(float newVals[4])
{
	for(int i=0; i<4; i++)		specular[i] = newVals[i];
}
void ZMaterial::SetSpecular(float newVal0, float newVal1, float newVal2, float newVal3)
{
	specular[0] = newVal0;	specular[1] = newVal1;
	specular[2] = newVal2;	specular[3] = newVal3;
}



///////////////////////////////////////////////////////////////////////////
/// Set the Emission component											///
///////////////////////////////////////////////////////////////////////////
void ZMaterial::SetEmission(float newVals[4])
{
	for(int i=0; i<4; i++)		emission[i] = newVals[i];
}
void ZMaterial::SetEmission(float newVal0, float newVal1, float newVal2, float newVal3)
{
	emission[0] = newVal0;	emission[1] = newVal1;
	emission[2] = newVal2;	emission[3] = newVal3;
}



///////////////////////////////////////////////////////////////////////////
/// Set the Shininess component											///
///////////////////////////////////////////////////////////////////////////
void ZMaterial::SetShininess(float newVal)
{
	shininess = newVal;
}



///////////////////////////////////////////////////////////////////////////
/// Set the default vals												///
///////////////////////////////////////////////////////////////////////////
void ZMaterial::SetDefaultVals()
{
	ambient[0]  = 1.0f;		ambient[1]  = 1.0f;		ambient[2]  = 1.0f;		ambient[3]  = 1.0f;
	diffuse[0]  = 1.0f;		diffuse[1]  = 1.0f;		diffuse[2]  = 1.0f;		diffuse[3]  = 1.0f;
	specular[0] = 0.0f;		specular[1] = 0.0f;		specular[2] = 0.0f;		specular[3] = 1.0f;
	emission[0] = 0.0f;		emission[1] = 0.0f;		emission[2] = 0.0f;		emission[3] = 1.0f;
	// MS : Nouvelle initilialisation de la variable de brillance
	shininess	= 0.0f;
	lustre      = 0.0f;
}



///////////////////////////////////////////////////////////////////////////
/// Set a texture for the face (PASS 1)									///
///////////////////////////////////////////////////////////////////////////
void ZMaterial::SetTexture1(ZTexture *newTex)
{
	if(newTex!=texture1)
	{
		if(texture1!=NULL)			texture1->DecRef();

		texture1 = newTex;
		if(texture1!=NULL)			texture1->IncRef();
	}
}



///////////////////////////////////////////////////////////////////////////
/// Set a texture for the face (PASS 2)									///
///////////////////////////////////////////////////////////////////////////
void ZMaterial::SetTexture2(ZTexture *newTex)
{
	if(newTex!=texture2)
	{
		if(texture2!=NULL)			texture2->DecRef();

		texture2 = newTex;
		if(texture2!=NULL)			texture2->IncRef();
	}
}

void ZMaterial::SetLightMap(ZTexture *newTex)
{
	if(newTex!=lightMap)
	{
		if(lightMap!=NULL)			lightMap->DecRef();

		lightMap = newTex;
		if(lightMap!=NULL)			lightMap->IncRef();
	}
}

void ZMaterial::SetNormalMap(ZTexture *newTex)
{
	if(newTex!=normalMap)
	{
		if(normalMap!=NULL)			normalMap->DecRef();

		normalMap = newTex;
		if(normalMap!=NULL)			normalMap->IncRef();
	}
}
