/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///																																  ///
///		FICHIER :	ZooMaterial.h																									  ///
///																																  ///
///		NATURE	:	Management of the material of a face for the 3D scene																		  ///
///																																  ///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



#ifndef __ZOOMATERIAL_H__
#define __ZOOMATERIAL_H__



#include	"..\Basic\ZooStd.h"
#include	"..\Datas\ZooTexture.h"



//$LB
int calcfiltertransp(int col,int coef, unsigned char * buf);



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ZMaterial Class
///
///		- Classe des matériaux
///		- Méthodes pour modifier les params du matériau
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ZMaterial : public ZData
{

protected:

	ZTexture*		texture1;										// pointer to the first texture
	ZTexture*		texture2;										// pointer to the second texture
	ZTexture*		lightMap;										// pointer to the lightMap
	ZTexture*		normalMap;										// pointer to the normalMap

	//$LB
	//int			color_16;										// and the same color in hexa form
	int             color_24;
	int				coeffTransp_8;									// transparency coefficient in integer value (0 to 255)

public:

	// Propriétés d'éclairage
	GLfloat			ambient[4], diffuse[4], specular[4], emission[4];
	GLfloat			shininess;
	float			lustre ;

	// Propriétés de rendu
	int				RenderMode;										// the integer which contain all the flags

	ZVector3		color;											// défault color of the material

	float			alpha;											// coefficient d'application du multitexturing

	ZVector3		color1,  color2;								// the two colors which will be mix with the textures
	//$LB int				color1_16, color2_16;							// and the same colors in hexa form
	int             color1_24, color2_24, ambient_24, diffuse_24, specular_24,emission_24;

	float			coeffTransp;									// transparency coefficient in float value (0 to 1)

  bool			modified;										// flag of material's properties modification

	int				transparencyOrder;

	//$LB short			*transpTab;										// table de transparence pour le mode SOFT
	unsigned char *   transpTab24;

	bool			accelerated;
	
	///////////////////////////////////////////////////////////////////////////
	/// Constructor					 										///
	///////////////////////////////////////////////////////////////////////////
	ZMaterial(bool accel);


	///////////////////////////////////////////////////////////////////////////
	/// Destructor					 										///
	/////////s/////////////////////////////////////////////////////////////////
	~ZMaterial();

	void SetDefaultColor(int col);
	int  GetDefaultColor();

	void SetCoeffTransp(int coef);
	int  GetCoeffTransp();

	
	///////////////////////////////////////////////////////////////////////////
	/// Set the default vals												///
	///////////////////////////////////////////////////////////////////////////
	void SetDefaultVals();

	void SetAmbient(float newVals[4]);
	void SetAmbient(float newVal0, float newVal1, float newVal2, float newVal3);

	void SetDiffuse(float newVals[4]);
	void SetDiffuse(float newVal0, float newVal1, float newVal2, float newVal3);

	void SetSpecular(float newVals[4]);
	void SetSpecular(float newVal0, float newVal1, float newVal2, float newVal3);

	void SetEmission(float newVals[4]);
	void SetEmission(float newVal0, float newVal1, float newVal2, float newVal3);

	void SetShininess(float newVal);
	void SetShininess(float newVal0, float newVal1, float newVal2, float newVal3);


	///////////////////////////////////////////////////////////////////////////
	/// Gestion des options	de rendu										///
	///////////////////////////////////////////////////////////////////////////
	bool EnableMode(int mode);
	bool DisableMode(int mode);
	bool IsOnMode(int mode);


	///////////////////////////////////////////////////////////////////////////
	/// Set the textures													///
	///////////////////////////////////////////////////////////////////////////
	void SetTexture1(ZTexture *newTex);
	void SetTexture2(ZTexture *newTex);
	void SetLightMap(ZTexture *newTex);
	void SetNormalMap(ZTexture *newTex);

	
	///////////////////////////////////////////////////////////////////////////
	/// Get the textures													///
	///////////////////////////////////////////////////////////////////////////
	ZTexture* GetTexture1();
	ZTexture* GetTexture2();
	ZTexture* GetLightMap();
	ZTexture* GetNormalMap();



	///////////////////////////////////////////////////////////////////////////
	/// Put the GL parameters												///
	///////////////////////////////////////////////////////////////////////////
	void PutGLparams();
};





inline void ZMaterial::SetDefaultColor(int col)
{
	color_24 = col;

	if(accelerated==false)		calcfiltertransp(color_24, coeffTransp_8, transpTab24);
}


inline int ZMaterial::GetDefaultColor()
{
	return color_24;
}


inline void ZMaterial::SetCoeffTransp(int coef)
{
	coeffTransp_8 = coef;

	if(accelerated==false)		calcfiltertransp(color_24, coeffTransp_8, transpTab24);
}


inline int ZMaterial::GetCoeffTransp()
{
	return coeffTransp_8;
}



///////////////////////////////////////////////////////////////////////////
/// Get the textures													///
///////////////////////////////////////////////////////////////////////////
inline ZTexture* ZMaterial::GetTexture1()
{
	return texture1;
}

inline ZTexture* ZMaterial::GetTexture2()
{
	return texture2;
}

inline ZTexture* ZMaterial::GetLightMap()
{
	return lightMap;
}

inline ZTexture* ZMaterial::GetNormalMap()
{
	return normalMap;
}



///////////////////////////////////////////////////////////////////////////
/// Gestion des options													///
///////////////////////////////////////////////////////////////////////////
inline bool ZMaterial::EnableMode(int mode)
{
	if(mode > MAXIMUM_MODE)			return false;					// test if the mode is valid

	RenderMode |= mode;												// set the mode

	modified = true;

	return true;
}

inline bool ZMaterial::DisableMode(int mode)
{
	if(mode > MAXIMUM_MODE)			return false;					// test if the mode is valid
	
	RenderMode &= (0xffffffff - mode);								// set the mode

	modified = true;

	return true;
}

inline bool ZMaterial::IsOnMode(int mode)
{
	if(mode > MAXIMUM_MODE)		return false;

	return ((RenderMode & mode) !=0);
}


///////////////////////////////////////////////////////////////////////////
/// Put the GL parameters												///
///////////////////////////////////////////////////////////////////////////
inline void ZMaterial::PutGLparams()
{
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,   ambient);
	  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,   diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  specular);
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION,  emission);
	// MS : Ajout de la shininess (brillance)
	glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess);
}


#endif