





//#include	"..\Scene Graph\ZooObject.h"
#include	"..\Basic\ZooScene.h"














///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ZObject Class
///
///		- Classe des objets
///		- Setting de la PRS et du mesh (referencés)
///		- Bounding box (rectangulaire)
///		- méthodes d'affichage dans différents modes
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////
/// Constructor	& Destructor	 										///
///////////////////////////////////////////////////////////////////////////
ZObject::ZObject(int s3d) : ZNodeGraph(s3d)
{
	type = OBJ_TYPE_ID;

	VertsTransf.resize(0);
	
	visible		= true;
	clickable	= true;

	currentMesh = NULL;


	mesh = NULL;

	multiMeshes = false;
}



ZObject::~ZObject()
{
	if(mesh)		mesh->DecRef();
	CleanMeshList();

	VertsTransf.resize(0);

	ZNodeGraph::~ZNodeGraph();
}





///////////////////////////////////////////////////////////////////////////
///		AddMeshInList													///
///////////////////////////////////////////////////////////////////////////
void ZObject::AddMeshInList(ZMesh *newMesh, float range2)
{
	if( (multiMeshes==false) || (newMesh==NULL) )			return;

	int		tabSize = rangeSQR.Size();

	// Cas de zéro éléments
	if(tabSize==0)
	{
		rangeSQR.Add(range2);
		meshList.Add(newMesh);
		newMesh->IncRef();

		return;
	}

	// Recherche de la position
	for(int i=0; (i < tabSize) && (rangeSQR[i] > range2); i++);

	// Cas de dernière position
	if(i==tabSize)
	{
		rangeSQR.Add(range2);
		meshList.Add(newMesh);
		newMesh->IncRef();

		return;
	}
	else
	{
		rangeSQR.Insert(range2, i);
		meshList.Insert(newMesh, i);
		newMesh->IncRef();
		return;
	}
}



///////////////////////////////////////////////////////////////////////////
///		AddMeshInList													///
///////////////////////////////////////////////////////////////////////////
void ZObject::CleanMeshList()
{
	if(multiMeshes)		currentMesh = NULL;

	for(int i=0; i < meshList.Size(); i++)			meshList[i]->DecRef();
	
	meshList.Clear();
	rangeSQR.Clear();
}





///////////////////////////////////////////////////////////////////////////////////////////////
/// MACRO de détermination d'un vertex partagé - Object definition							///
///////////////////////////////////////////////////////////////////////////////////////////////
#define FIND_SHARED_VERTEX_ZObject											\
	ref    = *vrtRef;														\
	pere   = this;															\
	nbVert = currentMesh->NbVerts;											\
	while(ref >= nbVert)													\
	{																		\
		ref -= nbVert;														\
		pere = (ZObject*) pere->father;										\
		nbVert = pere->GetMesh()->NbVerts;									\
	}










#define RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC							\
																			\
	/**** GOURAUD COLOR ****/												\
																			\
	if(shadeList->gour.size())												\
	{																		\
		glShadeModel(GL_SMOOTH);											\
		facelist = &(shadeList->gour);										\
																			\
		glBegin(GL_TRIANGLES);												\
																			\
			for(i=0; i< facelist->size(); i++)								\
			{																\
				curFace = (*facelist)[i];									\
				vrtRef	= curFace->VertRef;									\
				UV		= curFace->UV1;										\
																			\
				if(curFace->dependency)										\
				{															\
					/* Vertex number 0 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glColor3fv(pere->GetMesh()->Verts[ref].color1.vals);	\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 1 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glColor3fv(pere->GetMesh()->Verts[ref].color1.vals);	\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glColor3fv(pere->GetMesh()->Verts[ref].color1.vals);	\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
				}															\
				else														\
				{															\
					/* Vertex number 2 */									\
					glColor3fv((*Verts)[*vrtRef].color1.vals);				\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					glColor3fv((*Verts)[*vrtRef].color1.vals);				\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					glColor3fv((*Verts)[*vrtRef].color1.vals);				\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
				}															\
			}																\
																			\
		glEnd();															\
	}																		\
																			\
	/**** FLAT COLOR ****/													\
																			\
	if(shadeList->flat.size() || shadeList->no.size())						\
	{																		\
		glShadeModel(GL_FLAT);												\
																			\
		glBegin(GL_TRIANGLES);												\
																			\
			/** Face color **/												\
			facelist = &(shadeList->flat);									\
																			\
			for(i=0; i< facelist->size(); i++)								\
			{																\
				curFace = (*facelist)[i];									\
				vrtRef	= curFace->VertRef;									\
				UV		= curFace->UV1;										\
				glColor3fv(curFace->GetMaterial()->color1.vals);			\
																			\
				if(curFace->dependency)										\
				{															\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
				}															\
				else														\
				{															\
					/* Vertex number 2 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
				}															\
			}																\
																			\
			/** Default color **/											\
			facelist = &(shadeList->no);									\
																			\
			for(i=0; i< facelist->size(); i++)								\
			{																\
				curFace = (*facelist)[i];									\
				vrtRef	= curFace->VertRef;									\
				UV		= curFace->UV1;										\
				glColor3f(1.0f, 1.0f, 1.0f);								\
																			\
				if(curFace->dependency)										\
				{															\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
				}															\
				else														\
				{															\
					/* Vertex number 2 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
				}															\
			}																\
																			\
		glEnd();															\
	}











///////////////////////////////////////////////////////////////////////////
/// Direct rendering for dynamic objects (CLASSIC mode)					///
///////////////////////////////////////////////////////////////////////////
/// Input	: NONE
/// Output	: 
///		- Direct render the object on the screen (glBegin/glEnd)
/// Return	: NONE

void ZObject::RenderPass1Classic()
{
	int				i, j;

	vector<ZVert>	*Verts = &(currentMesh->Verts);

	FaceList		*facelist;
	ZFace			*curFace;
	ZUV				*UV;
	int				*vrtRef;
	
	ZTexture		*curTex;

	int				nbVert;
	int				ref;
	ZObject			*pere;

	FacesBySHADE	*shadeList;


	// ************* Treatment for non texturing ************** 

	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glDisable(GL_ALPHA_TEST);

	// ******* GOURAUD COLOR *******
	

	if(currentMesh->FacesFlat.gour.size())
	{
		glShadeModel(GL_SMOOTH);
		facelist = &(currentMesh->FacesFlat.gour);

		glBegin(GL_TRIANGLES);

			for(i=0; i< facelist->size(); i++)
			{
				curFace = (*facelist)[i];
				vrtRef	= curFace->VertRef;

				if(curFace->dependency)
				{
					// Vertex number 0
					FIND_SHARED_VERTEX_ZObject
					glColor3fv(pere->GetMesh()->Verts[ref].color1.vals);
					glVertex3fv(pere->VertsTransf[ref].vals);
					vrtRef++;

					// Vertex number 1
					FIND_SHARED_VERTEX_ZObject
					glColor3fv(pere->GetMesh()->Verts[ref].color1.vals);
					glVertex3fv(pere->VertsTransf[ref].vals);
					vrtRef++;

					// Vertex number 2
					FIND_SHARED_VERTEX_ZObject
					glColor3fv(pere->GetMesh()->Verts[ref].color1.vals);
					glVertex3fv(pere->VertsTransf[ref].vals);
				}
				else
				{
					// Vertex number 0
					glColor3fv((*Verts)[*vrtRef].color1.vals);
					glVertex3fv(VertsTransf[*vrtRef].vals);
					vrtRef++;

					// Vertex number 1
					glColor3fv((*Verts)[*vrtRef].color1.vals);
					glVertex3fv(VertsTransf[*vrtRef].vals);
					vrtRef++;

					// Vertex number 2
					glColor3fv((*Verts)[*vrtRef].color1.vals);
					glVertex3fv(VertsTransf[*vrtRef].vals);
				}
			}

		glEnd();
	}

	

	// ******* FLAT COLOR *******
	
	if(currentMesh->FacesFlat.flat.size() || currentMesh->FacesFlat.no.size())
	{
		glShadeModel(GL_FLAT);

		glBegin(GL_TRIANGLES);

			// **** Face color ****
			facelist = &(currentMesh->FacesFlat.flat);

			for(i=0; i< facelist->size(); i++)
			{
				curFace = (*facelist)[i];
				vrtRef	= curFace->VertRef;
				glColor3fv(curFace->GetMaterial()->color1.vals);

				if(curFace->dependency)
				{
					// Vertex number 0
					FIND_SHARED_VERTEX_ZObject
					glVertex3fv(pere->VertsTransf[ref].vals);
					vrtRef++;
			
					// Vertex number 1
					FIND_SHARED_VERTEX_ZObject
					glVertex3fv(pere->VertsTransf[ref].vals);
					vrtRef++;

					// Vertex number 2
					FIND_SHARED_VERTEX_ZObject
					glVertex3fv(pere->VertsTransf[ref].vals);
				}
				else
				{
					// Vertex number 0
					glVertex3fv(VertsTransf[*vrtRef].vals);
					vrtRef++;
			
					// Vertex number 1
					glVertex3fv(VertsTransf[*vrtRef].vals);
					vrtRef++;

					// Vertex number 2
					glVertex3fv(VertsTransf[*vrtRef].vals);
				}
			}

			// *** Default color ***
			facelist = &(currentMesh->FacesFlat.no);

			for(i=0; i< facelist->size(); i++)
			{
				curFace = (*facelist)[i];
				vrtRef	= curFace->VertRef;
				glColor3fv(curFace->GetMaterial()->color.vals);

				if(curFace->dependency)
				{
					// Vertex number 0
					FIND_SHARED_VERTEX_ZObject
					glVertex3fv(pere->VertsTransf[ref].vals);
					vrtRef++;
			
					// Vertex number 1
					FIND_SHARED_VERTEX_ZObject
					glVertex3fv(pere->VertsTransf[ref].vals);
					vrtRef++;

					// Vertex number 2
					FIND_SHARED_VERTEX_ZObject
					glVertex3fv(pere->VertsTransf[ref].vals);
				}
				else
				{
					// Vertex number 0
					glVertex3fv(VertsTransf[*vrtRef].vals);
					vrtRef++;
			
					// Vertex number 1
					glVertex3fv(VertsTransf[*vrtRef].vals);
					vrtRef++;

					// Vertex number 2
					glVertex3fv(VertsTransf[*vrtRef].vals);
				}
			}

		glEnd();
	}






	// ************* Treatment by type of texture ************** 

	glEnable(GL_TEXTURE_2D);

	// ******* CLASSICAL MAPPING *******

	for(j=0; j<currentMesh->FacesTex1.size(); j++)
	{
		curTex = currentMesh->FacesTex1[j].tex;
		curTex->PutGLparams();


		///////////////////////////////////////////
		///				clamp u v				///
		///////////////////////////////////////////

		shadeList = &currentMesh->FacesTex1[j].ClampUV;

		if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
		{
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

			RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC
		}

		///////////////////////////////////////////
		///				clamp u 				///
		///////////////////////////////////////////
		
		shadeList = &currentMesh->FacesTex1[j].ClampU;

		if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
		{
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

			RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC
		}


		///////////////////////////////////////////
		///				clamp V 				///
		///////////////////////////////////////////
		
		shadeList = &currentMesh->FacesTex1[j].ClampV;

		if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
		{
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

			RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC
		}

		///////////////////////////////////////////
		///				No clamp  				///
		///////////////////////////////////////////
		
		shadeList = &currentMesh->FacesTex1[j].NoClamp;

		if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
		{
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

			RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC
		}

		///////////////////////////////////////////
		///				End clamping			///
		///////////////////////////////////////////
	}


	// ******* COLOR TRANSP MAPPING *******

	if(currentMesh->FacesColTransp.size())
	{
		glEnable(GL_ALPHA_TEST);
		glAlphaFunc(GL_GREATER, CONST_ALPHA_TEST);

		for(j=0; j<currentMesh->FacesColTransp.size(); j++)
		{
			curTex = currentMesh->FacesColTransp[j].tex;
			curTex->PutGLparams();


			///////////////////////////////////////////
			///				clamp u v				///
			///////////////////////////////////////////

			shadeList = &currentMesh->FacesColTransp[j].ClampUV;

			if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
			{
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

				RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC
			}

			///////////////////////////////////////////
			///				clamp u 				///
			///////////////////////////////////////////

			shadeList = &currentMesh->FacesColTransp[j].ClampU;

			if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
			{
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

				RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC
			}


			///////////////////////////////////////////
			///				clamp V 				///
			///////////////////////////////////////////

			shadeList = &currentMesh->FacesColTransp[j].ClampV;

			if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
			{
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

				RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC
			}

			///////////////////////////////////////////
			///				No clamp  				///
			///////////////////////////////////////////

			shadeList = &currentMesh->FacesColTransp[j].NoClamp;

			if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
			{
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

				RENDER_SHADE_LIST_TEXTURED_PASS1_CLASSIC
			}
		}

		glDisable(GL_ALPHA_TEST);
	}


	// ************* ENVIRONMENTAL MAPPING *************

	if( currentMesh->FacesEnv.size() )
	{
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		

		for(j=0; j<currentMesh->FacesEnv.size(); j++)
		{
			curTex = currentMesh->FacesEnv[j].tex;
			curTex->PutGLparams();

			shadeList = &currentMesh->FacesEnv[j].NoClamp;

			if(shadeList->gour.size())
			{
				// --------------> TEXTURED <---------------
				// ---> PASS for smooth color rendering <---

				glShadeModel(GL_SMOOTH);
				facelist = &(shadeList->gour);

				glBegin(GL_TRIANGLES);

					glColor3f(1.0f, 1.0f, 1.0f);

					for(i=0; i< facelist->size(); i++)
					{
						curFace = (*facelist)[i];
						vrtRef	= curFace->VertRef;

						if(curFace->dependency)
						{
							// Vertex number 0
							FIND_SHARED_VERTEX_ZObject
							glNormal3fv(pere->VertNormTransf[ref].vals);
							glVertex3fv(pere->VertsTransf[ref].vals);
							vrtRef++;

							// Vertex number 1
							FIND_SHARED_VERTEX_ZObject
							glNormal3fv(pere->VertNormTransf[ref].vals);
							glVertex3fv(pere->VertsTransf[ref].vals);
							vrtRef++;

							// Vertex number 2
							FIND_SHARED_VERTEX_ZObject
							glNormal3fv(pere->VertNormTransf[ref].vals);
							glVertex3fv(pere->VertsTransf[ref].vals);
						}
						else
						{
							// Vertex number 0
							glNormal3fv(VertNormTransf[*vrtRef].vals);
							glVertex3fv(VertsTransf[*vrtRef].vals);
							vrtRef++;

							// Vertex number 1
							glNormal3fv(VertNormTransf[*vrtRef].vals);
							glVertex3fv(VertsTransf[*vrtRef].vals);
							vrtRef++;

							// Vertex number 2
							glNormal3fv(VertNormTransf[*vrtRef].vals);
							glVertex3fv(VertsTransf[*vrtRef].vals);
						}
					}

				glEnd();
			}

			if(shadeList->flat.size())
			{
				// --------------> TEXTURED <---------------
				// ---> PASS for uniform color rendering <---

				glShadeModel(GL_FLAT);

				glBegin(GL_TRIANGLES);

					glColor3f(1.0f, 1.0f, 1.0f);

					facelist = &(shadeList->flat);

					for(i=0; i< facelist->size(); i++)
					{
						curFace = (*facelist)[i];
						vrtRef	= curFace->VertRef;
						glNormal3fv(FaceNormTransf[curFace->index].vals);

						if(curFace->dependency)
						{
							// Vertex number 0
							FIND_SHARED_VERTEX_ZObject
							glVertex3fv(pere->VertsTransf[ref].vals);
							vrtRef++;
					
							// Vertex number 1
							FIND_SHARED_VERTEX_ZObject
							glVertex3fv(pere->VertsTransf[ref].vals);
							vrtRef++;

							// Vertex number 2
							FIND_SHARED_VERTEX_ZObject
							glVertex3fv(pere->VertsTransf[ref].vals);
							vrtRef++;
						}
						else
						{
							// Vertex number 0
							glVertex3fv(VertsTransf[*vrtRef].vals);
							vrtRef++;
					
							// Vertex number 1
							glVertex3fv(VertsTransf[*vrtRef].vals);
							vrtRef++;

							// Vertex number 2
							glVertex3fv(VertsTransf[*vrtRef].vals);
						}
					}

				glEnd();
			}
		}

		glDisable(GL_TEXTURE_GEN_S);							// Enable Texture Coord Generation For S (NEW)
		glDisable(GL_TEXTURE_GEN_T);							// Enable Texture Coord Generation For T (NEW)
	}





#ifdef	_SHOW_NORMALS_

		ZVector3	centre;
		ZVector3	normale;

		glLineWidth(2.0);
		
		glShadeModel(GL_FLAT);
		glDisable(GL_TEXTURE_2D);

		glBegin(GL_LINES);

		for(j=0; j < currentMesh->Faces.size(); j++)
		{
			glColor3f(0.0f, 1.0f, 1.0f);

			curFace = &(currentMesh->Faces[j]);
			vrtRef	= curFace->VertRef;

			normale = FaceNormTransf[curFace->index];
			normale.Normalize();
			normale *= 0.1f;

			if(curFace->dependency)
			{
				// Vertex number 0
				FIND_SHARED_VERTEX_ZObject
				centre.x = pere->VertsTransf[ref].x;
				centre.y = pere->VertsTransf[ref].y;
				centre.z = pere->VertsTransf[ref].z;
				vrtRef++;

				// Vertex number 1
				FIND_SHARED_VERTEX_ZObject
				centre.x += pere->VertsTransf[ref].x;
				centre.y += pere->VertsTransf[ref].y;
				centre.z += pere->VertsTransf[ref].z;
				vrtRef++;

				// Vertex number 2
				FIND_SHARED_VERTEX_ZObject
				centre.x += pere->VertsTransf[ref].x;
				centre.y += pere->VertsTransf[ref].y;
				centre.z += pere->VertsTransf[ref].z;
				glVertex3fv(pere->VertsTransf[ref].vals);
			}
			else
			{
				// Vertex number 0
				centre.x = VertsTransf[*vrtRef].x;
				centre.y = VertsTransf[*vrtRef].y;
				centre.z = VertsTransf[*vrtRef].z;
				vrtRef++;

				// Vertex number 1
				centre.x += VertsTransf[*vrtRef].x;
				centre.y += VertsTransf[*vrtRef].y;
				centre.z += VertsTransf[*vrtRef].z;
				vrtRef++;

				// Vertex number 2
				centre.x += VertsTransf[*vrtRef].x;
				centre.y += VertsTransf[*vrtRef].y;
				centre.z += VertsTransf[*vrtRef].z;
			}

			centre *= 0.33333333f;
			glVertex3fv(centre.vals);
			centre += normale;
			glVertex3fv(centre.vals);
		}

		glEnd();

		glEnable(GL_TEXTURE_2D);

#endif


}






#define RENDER_SHADE_LIST_TEXTURED_PASS2_CLASSIC							\
																			\
	/* Gouraud */															\
																			\
	if(shadeList->gour.size())												\
	{																		\
		glShadeModel(GL_SMOOTH);											\
		facelist = &(shadeList->gour);										\
																			\
		glBegin(GL_TRIANGLES);												\
																			\
			for(i=0; i< facelist->size(); i++)								\
			{																\
				curFace = (*facelist)[i];									\
				vrtRef	= curFace->VertRef;									\
				UV		= curFace->UV2;										\
				alpha	= curFace->GetMaterial()->alpha;					\
																			\
				if(curFace->dependency)										\
				{															\
					/* Vertex number 0 */									\
					FIND_SHARED_VERTEX_ZObject								\
					color	= &(pere->GetMesh()->Verts[ref].color2);		\
					glTexCoord2fv(UV->vals);								\
					glColor4f(color->x, color->y, color->z, alpha);			\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 1 */									\
					FIND_SHARED_VERTEX_ZObject								\
					color	= &(pere->GetMesh()->Verts[ref].color2);		\
					glTexCoord2fv(UV->vals);								\
					glColor4f(color->x, color->y, color->z, alpha);			\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					color	= &(pere->GetMesh()->Verts[ref].color2);		\
					glTexCoord2fv(UV->vals);								\
					glColor4f(color->x, color->y, color->z, alpha);			\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
				}															\
				else														\
				{															\
					/* Vertex number 0 */									\
					color	= &((*Verts)[*vrtRef].color2);					\
					glColor4f(color->x, color->y, color->z, alpha);			\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 1 */									\
					color	= &((*Verts)[*vrtRef].color2);					\
					glColor4f(color->x, color->y, color->z, alpha);			\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					color	= &((*Verts)[*vrtRef].color2);					\
					glColor4f(color->x, color->y, color->z, alpha);			\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
				}															\
			}																\
																			\
		glEnd();															\
	}																		\
																			\
	/* Flat */																\
																			\
	if(shadeList->flat.size() || shadeList->no.size())						\
	{																		\
		glShadeModel(GL_FLAT);												\
																			\
		glBegin(GL_TRIANGLES);												\
																			\
			/* COLOR 1 */													\
																			\
			facelist = &(shadeList->flat);									\
																			\
			for(i=0; i< facelist->size(); i++)								\
			{																\
				curFace = (*facelist)[i];									\
				vrtRef	= curFace->VertRef;									\
				UV		= curFace->UV2;										\
				color	= &(curFace->GetMaterial()->color2);				\
				glColor4f(color->x, color->y, color->z, curFace->GetMaterial()->alpha);		\
																			\
				if(curFace->dependency)										\
				{															\
					/* Vertex number 0 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 1 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
				}															\
				else														\
				{															\
					/* Vertex number 0 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 1 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
				}															\
			}																\
																			\
			/* DEFAULT color */												\
																			\
			facelist = &(shadeList->no);									\
																			\
			for(i=0; i< facelist->size(); i++)								\
			{																\
				curFace = (*facelist)[i];									\
				vrtRef	= curFace->VertRef;									\
				UV		= curFace->UV2;										\
				color	= &(curFace->GetMaterial()->color);					\
				glColor4f(1.0f, 1.0f, 1.0f, curFace->GetMaterial()->alpha);	\
																			\
				if(curFace->dependency)										\
				{															\
					/* Vertex number 0 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 1 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					FIND_SHARED_VERTEX_ZObject								\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(pere->VertsTransf[ref].vals);				\
				}															\
				else														\
				{															\
					/* Vertex number 0 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 1 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
					vrtRef++;												\
					UV++;													\
																			\
					/* Vertex number 2 */									\
					glTexCoord2fv(UV->vals);								\
					glVertex3fv(VertsTransf[*vrtRef].vals);					\
				}															\
			}																\
																			\
		glEnd();															\
	}







///////////////////////////////////////////////////////////////////////////
/// Direct rendering for dynamic objects (CLASSIC mode)					///
///////////////////////////////////////////////////////////////////////////
/// Input	: NONE
/// Output	: 
///		- Direct render the object on the screen (glBegin/glEnd)
/// Return	: NONE
void ZObject::RenderPass2Classic()
{

	vector<ZVert>	*Verts = &(currentMesh->Verts);
	int				i, j;

	FaceList		*facelist;

	ZFace			*curFace;
	ZVector3		*color;
	ZUV				*UV;
	int				*vrtRef;
	
	ZTexture		*curTex;

	float			alpha;

	int				nbVert;
	int				ref;
	ZObject			*pere;

	FacesBySHADE	*shadeList;


	// --------------------------------------------------------
	// ------------- Treatment by type of texture -------------
	// --------------------------------------------------------

	glEnable(GL_TEXTURE_2D);

	for(j=0; j<currentMesh->FacesTex2.size(); j++)
	{
		curTex = currentMesh->FacesTex2[j].tex;
		curTex->PutGLparams();
		if(curTex->A)				{	glEnable(GL_ALPHA_TEST);	glAlphaFunc(GL_NOTEQUAL, 0);	}
		else						{	glDisable(GL_ALPHA_TEST);	}

		///////////////////////////////////////////
		///				clamp u v				///
		///////////////////////////////////////////
		
		shadeList = &currentMesh->FacesTex2[j].ClampUV;

		if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
		{
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

			RENDER_SHADE_LIST_TEXTURED_PASS2_CLASSIC
		}

		///////////////////////////////////////////
		///				clamp u					///
		///////////////////////////////////////////
		
		shadeList = &currentMesh->FacesTex2[j].ClampU;

		if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
		{
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

			RENDER_SHADE_LIST_TEXTURED_PASS2_CLASSIC
		}

		///////////////////////////////////////////
		///				clamp V					///
		///////////////////////////////////////////
		
		shadeList = &currentMesh->FacesTex2[j].ClampV;

		if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
		{
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

			RENDER_SHADE_LIST_TEXTURED_PASS2_CLASSIC
		}

		///////////////////////////////////////////
		///				No clamp 				///
		///////////////////////////////////////////
		
		shadeList = &currentMesh->FacesTex2[j].NoClamp;

		if(shadeList->flat.size() || shadeList->gour.size() || shadeList->no.size())
		{
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

			RENDER_SHADE_LIST_TEXTURED_PASS2_CLASSIC
		}

	}
	

}







///////////////////////////////////////////////////////////////////////////
/// Direct rendering for dynamic objects (CLASSIC mode)					///
///////////////////////////////////////////////////////////////////////////
/// Input	: NONE
/// Output	: 
///		- Direct render the object on the screen (glBegin/glEnd)
/// Return	: NONE
void ZObject::RenderPass3Classic()
{
	vector<ZVert>	*Verts = &(currentMesh->Verts);
	int				i, j;

	FaceList		*facelist;

	ZFace			*curFace;
	int				*vrtRef;
	
	ZMaterial		*curMat;

	int				nbVert;
	int				ref;
	ZObject			*pere;

	ZMatrix			matx;
	matx = worldMat;
	matx._14 = matx._24 = matx._34 = matx._41 = matx._42 = matx._43 = matx._44 = 0.0f;



	// --------------> LIGHTING <---------------
	// ---> PASS for SMOOTH light rendering <---

	if(currentMesh->FacesMatGour.size())
	{
		for(j=0; j < currentMesh->FacesMatGour.size(); j++)
		{
			curMat = currentMesh->FacesMatGour[j].mat;
			curMat->PutGLparams();

			facelist = &(currentMesh->FacesMatGour[j].faces);

			glBegin(GL_TRIANGLES);

				for(i=0; i< facelist->size(); i++)
				{
					curFace = (*facelist)[i];
					vrtRef	= curFace->VertRef;

					if(curFace->dependency)
					{
						// Vertex number 0
						FIND_SHARED_VERTEX_ZObject
						if(pere->normalsTransformed)		glNormal3fv(pere->VertNormTransf[ref].vals);
						else								glNormal3fv( (matx*pere->GetMesh()->Verts[ref].Normal).vals );
						glVertex3fv(pere->VertsTransf[ref].vals);
						vrtRef++;

						// Vertex number 1
						FIND_SHARED_VERTEX_ZObject
						if(pere->normalsTransformed)		glNormal3fv(pere->VertNormTransf[ref].vals);
						else								glNormal3fv( (matx*pere->GetMesh()->Verts[ref].Normal).vals );
						glVertex3fv(pere->VertsTransf[ref].vals);
						vrtRef++;

						// Vertex number 2
						FIND_SHARED_VERTEX_ZObject
						if(pere->normalsTransformed)		glNormal3fv(pere->VertNormTransf[ref].vals);
						else								glNormal3fv( (matx*pere->GetMesh()->Verts[ref].Normal).vals );
						glVertex3fv(pere->VertsTransf[ref].vals);
					}
					else
					{
						// Vertex number 0
						glNormal3fv(VertNormTransf[*vrtRef].vals);
						glVertex3fv(VertsTransf[*vrtRef].vals);
						vrtRef++;

						// Vertex number 1
						glNormal3fv(VertNormTransf[*vrtRef].vals);
						glVertex3fv(VertsTransf[*vrtRef].vals);
						vrtRef++;

						// Vertex number 2
						glNormal3fv(VertNormTransf[*vrtRef].vals);
						glVertex3fv(VertsTransf[*vrtRef].vals);
					}
				}

			glEnd();
		}
	}


	// --------------> LIGHTING <---------------
	// ----> PASS for FLAT light rendering <----

	if(currentMesh->FacesMatFlat.size())
	{
		for(j=0; j < currentMesh->FacesMatFlat.size(); j++)
		{
			curMat = currentMesh->FacesMatFlat[j].mat;
			curMat->PutGLparams();

			facelist = &(currentMesh->FacesMatFlat[j].faces);

			glBegin(GL_TRIANGLES);

				for(i=0; i< facelist->size(); i++)
				{
					curFace = (*facelist)[i];
					vrtRef	= curFace->VertRef;

					if(curFace->dependency)
					{
						// Vertex number 0
						FIND_SHARED_VERTEX_ZObject
						glNormal3fv(FaceNormTransf[curFace->index].vals);
						glVertex3fv(pere->VertsTransf[ref].vals);
						vrtRef++;

						// Vertex number 1
						FIND_SHARED_VERTEX_ZObject
						glNormal3fv(FaceNormTransf[curFace->index].vals);
						glVertex3fv(pere->VertsTransf[ref].vals);
						vrtRef++;

						// Vertex number 2
						FIND_SHARED_VERTEX_ZObject
						glNormal3fv(FaceNormTransf[curFace->index].vals);
						glVertex3fv(pere->VertsTransf[ref].vals);
					}
					else
					{
						// Vertex number 0
						glNormal3fv(FaceNormTransf[curFace->index].vals);
						glVertex3fv(VertsTransf[*vrtRef].vals);
						vrtRef++;

						// Vertex number 1
						glNormal3fv(FaceNormTransf[curFace->index].vals);
						glVertex3fv(VertsTransf[*vrtRef].vals);
						vrtRef++;

						// Vertex number 2
						glNormal3fv(FaceNormTransf[curFace->index].vals);
						glVertex3fv(VertsTransf[*vrtRef].vals);
					}
				}

			glEnd();
		}
	}

}





///////////////////////////////////////////////////////////////////////////
/// Direct rendering for dynamic objects (CLASSIC mode)					///
///////////////////////////////////////////////////////////////////////////
/// Input	: NONE
/// Output	: 
///		- Direct render the object on the screen (glBegin/glEnd)
/// Return	: NONE
void ZObject::RenderPass4Classic()
{
	vector<ZVert>	*Verts = &(currentMesh->Verts);
	int				i, j;

	FaceList		*facelist;

	ZFace			*curFace;
	int				*vrtRef;
	
	int				nbVert;
	int				ref;
	ZObject			*pere;


	// --------------> LIGHTING <---------------
	// ----> PASS for FLAT light rendering <----

	if(currentMesh->FacesMatNo.size())
	{
		glShadeModel(GL_FLAT);

		for(j=0; j < currentMesh->FacesMatNo.size(); j++)
		{
			if(currentMesh->FacesMatNo[j].mat->IsOnMode(RENDER_TRANSP)==false /*|| currentMesh->FacesMatNo[j].mat->GetCoeffTransp()==0*/)
			{
				facelist = &(currentMesh->FacesMatNo[j].faces);

				glBegin(GL_TRIANGLES);

					for(i=0; i< facelist->size(); i++)
					{
						glColor3f(0.5f, 0.5f, 0.5f);
						curFace = (*facelist)[i];
						vrtRef	= curFace->VertRef;

						if(curFace->dependency)
						{
							// Vertex number 0
							FIND_SHARED_VERTEX_ZObject
							glVertex3fv(pere->VertsTransf[ref].vals);
							vrtRef++;
					
							// Vertex number 1
							FIND_SHARED_VERTEX_ZObject
							glVertex3fv(pere->VertsTransf[ref].vals);
							vrtRef++;

							// Vertex number 2
							FIND_SHARED_VERTEX_ZObject
							glVertex3fv(pere->VertsTransf[ref].vals);
						}
						else
						{
							// Vertex number 0
							glVertex3fv(VertsTransf[*vrtRef].vals);
							vrtRef++;
					
							// Vertex number 1
							glVertex3fv(VertsTransf[*vrtRef].vals);
							vrtRef++;

							// Vertex number 2
							glVertex3fv(VertsTransf[*vrtRef].vals);
						}
					}

				glEnd();
			}
		}
	}

}









///////////////////////////////////////////////////////////////////////////
/// Render the object in wired mode										///
///////////////////////////////////////////////////////////////////////////
/// Input	: NONE
/// Output	: 
///		- Render the BBOX of the object
/// Return	: NONE
void ZObject::RenderWired(float lineWidth)
{
	vector<ZVert>	*vert = &(currentMesh->Verts);
	int				i, j;

	FaceList		*listFace;

	ZFace			*curFace;
	ZVector3		*pos[3];
	int				*vrtRef;
	
	ZMaterial		*curMat;

	int				nbVert;
	int				ref;
	ZObject			*pere;
	

	glLineWidth(lineWidth);

	// --------------------------------------------------------
	// ------------- Treatment by Material (color) ------------
	// --------------------------------------------------------
	glBegin(GL_LINES);

		for(j=0; j < currentMesh->FacesMatGour.size(); j++)
		{
			curMat = currentMesh->FacesMatGour[j].mat;
			glColor3fv(curMat->color.vals);		

			// --- List of gouro faces ---
			listFace = &(currentMesh->FacesMatGour[j].faces);

			for(i=0; i< listFace->size(); i++)
			{
				curFace = (*listFace)[i];
				vrtRef	= curFace->VertRef;

				if(curFace->dependency)
				{
					// Vertex number 0
					FIND_SHARED_VERTEX_ZObject
					pos[0] = &(pere->VertsTransf[ref]);
					vrtRef++;

					// Vertex number 1
					FIND_SHARED_VERTEX_ZObject
					pos[1] = &(pere->VertsTransf[ref]);
					vrtRef++;

					// Vertex number 2
					FIND_SHARED_VERTEX_ZObject
					pos[2] = &(pere->VertsTransf[ref]);

					glVertex3fv(pos[0]->vals);
					glVertex3fv(pos[1]->vals);

					glVertex3fv(pos[1]->vals);
					glVertex3fv(pos[2]->vals);

					glVertex3fv(pos[2]->vals);
					glVertex3fv(pos[0]->vals);
				}
				else
				{
					glVertex3fv(VertsTransf[curFace->VertRef[0]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[1]].vals);

					glVertex3fv(VertsTransf[curFace->VertRef[1]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[2]].vals);

					glVertex3fv(VertsTransf[curFace->VertRef[2]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[0]].vals);
				}
			}
		}


		for(j=0; j < currentMesh->FacesMatFlat.size(); j++)
		{
			curMat = currentMesh->FacesMatFlat[j].mat;
			glColor3fv(curMat->color.vals);		

			// --- List of gouro faces ---
			listFace = &(currentMesh->FacesMatFlat[j].faces);

			for(i=0; i< listFace->size(); i++)
			{
				curFace = (*listFace)[i];
				vrtRef	= curFace->VertRef;

				if(curFace->dependency)
				{
					// Vertex number 0
					FIND_SHARED_VERTEX_ZObject
					pos[0] = &(pere->VertsTransf[ref]);
					vrtRef++;

					// Vertex number 1
					FIND_SHARED_VERTEX_ZObject
					pos[1] = &(pere->VertsTransf[ref]);
					vrtRef++;

					// Vertex number 2
					FIND_SHARED_VERTEX_ZObject
					pos[2] = &(pere->VertsTransf[ref]);

					glVertex3fv(pos[0]->vals);
					glVertex3fv(pos[1]->vals);

					glVertex3fv(pos[1]->vals);
					glVertex3fv(pos[2]->vals);

					glVertex3fv(pos[2]->vals);
					glVertex3fv(pos[0]->vals);
				}
				else
				{
					glVertex3fv(VertsTransf[curFace->VertRef[0]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[1]].vals);

					glVertex3fv(VertsTransf[curFace->VertRef[1]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[2]].vals);

					glVertex3fv(VertsTransf[curFace->VertRef[2]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[0]].vals);
				}
			}
		}


		for(j=0; j < currentMesh->FacesMatNo.size(); j++)
		{
			curMat = currentMesh->FacesMatNo[j].mat;
			glColor3fv(curMat->color.vals);		

			// --- List of gouro faces ---
			listFace = &(currentMesh->FacesMatNo[j].faces);

			for(i=0; i< listFace->size(); i++)
			{
				curFace = (*listFace)[i];
				vrtRef	= curFace->VertRef;

				if(curFace->dependency)
				{
					// Vertex number 0
					FIND_SHARED_VERTEX_ZObject
					pos[0] = &(pere->VertsTransf[ref]);
					vrtRef++;

					// Vertex number 1
					FIND_SHARED_VERTEX_ZObject
					pos[1] = &(pere->VertsTransf[ref]);
					vrtRef++;

					// Vertex number 2
					FIND_SHARED_VERTEX_ZObject
					pos[2] = &(pere->VertsTransf[ref]);

					glVertex3fv(pos[0]->vals);
					glVertex3fv(pos[1]->vals);

					glVertex3fv(pos[1]->vals);
					glVertex3fv(pos[2]->vals);

					glVertex3fv(pos[2]->vals);
					glVertex3fv(pos[0]->vals);
				}
				else
				{
					glVertex3fv(VertsTransf[curFace->VertRef[0]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[1]].vals);

					glVertex3fv(VertsTransf[curFace->VertRef[1]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[2]].vals);

					glVertex3fv(VertsTransf[curFace->VertRef[2]].vals);
					glVertex3fv(VertsTransf[curFace->VertRef[0]].vals);
				}
			}
		}

	glEnd();

}



///////////////////////////////////////////////////////////////////////////
/// Transform the vertices with his matrix (in PRS)						///
///////////////////////////////////////////////////////////////////////////
/// Input	: matrix of the object in the World referencial
/// Output	: 
///		- compute the vertices transformated and store them in 'VertsTransf'
/// Return	: NONE
void ZObject::TransformAllVerts()
{
	if(currentMesh)
	{
		if(currentMesh->NbVerts<1)
		{
			VertsTransf.resize(0);
			return;
		}

		if(VertsTransf.size() != currentMesh->NbVerts)		VertsTransf.resize(currentMesh->NbVerts);

		vector<ZVert>		*vertInit;	
		vertInit	= &(currentMesh->Verts);

		VertsTransf[0] = worldMat * ((*vertInit)[0]);
		for(int i=1; i<currentMesh->NbVerts; i++)		VertsTransf[i] =  worldMat * ((*vertInit)[i]);
	}
	else
		VertsTransf.resize(0);
}



void ZObject::TransformNormals()
{
	if(currentMesh)
	{
		int i;

		ZMatrix		mat = worldMat;
		mat._14 = mat._24 = mat._34 = mat._41 = mat._42 = mat._43 = mat._44 = 0.0f;

		if( (currentMesh->NbFaces<1) || (currentMesh->NbVerts<1) )
		{
			FaceNormTransf.resize(0);
			VertNormTransf.resize(0);
			return;
		}

		if(FaceNormTransf.size() != currentMesh->NbFaces)		FaceNormTransf.resize(currentMesh->NbFaces);
		if(VertNormTransf.size() != currentMesh->NbVerts)		VertNormTransf.resize(currentMesh->NbVerts);

		ZFace		*Face;
		ZVert		*Vert;
		Vert = &(currentMesh->Verts[0]);
		Face = &(currentMesh->Faces[0]);

		if(currentMesh->dependency)
		{
			// Utlise les normales déjà calculées pour le mesh DEPENDENCY
			for(i=0; i<currentMesh->NbFaces; i++, Face++)		FaceNormTransf[i] = Face->Normal;
			for(i=0; i<currentMesh->NbVerts; i++, Vert++)		VertNormTransf[i] = Vert->Normal;
		}
		else
		{
			// Calcule les normales transformées à partir des normales statiques du mesh
			for(i=0; i<currentMesh->NbFaces; i++, Face++)		FaceNormTransf[i] = mat * Face->Normal;
			for(i=0; i<currentMesh->NbVerts; i++, Vert++)		VertNormTransf[i] = mat * Vert->Normal;
		}

		if(((ZScene*)scene3D)->accelerated==false)
		{
			// Normalisation pour le rendu Software
			for(i=0; i<currentMesh->NbFaces; i++)				FaceNormTransf[i].Normalize();
			for(i=0; i<currentMesh->NbVerts; i++)				VertNormTransf[i].Normalize();
		}
	}
	else
	{
		FaceNormTransf.resize(0);
		VertNormTransf.resize(0);
	}
}







///////////////////////////////////////////////////////////////////////////
/// Render the BBOX of the object										///
///////////////////////////////////////////////////////////////////////////
/// Input	: NONE
/// Output	: 
///		- Render the BBOX of the object
/// Return	: NONE
void ZObject::RenderBOX(const ZVector3 &color, float lineWidth)
{
	glColor3f(color.x, color.y, color.z);
	glLineWidth(lineWidth);

	glLoadMatrixf(worldMat.matxArray);		// load the world Matrix

	ZVector3	vect0, vect1;
	vect0.SetCoord(currentMesh->xmin, currentMesh->ymin, currentMesh->zmin);
	vect1.SetCoord(currentMesh->xmax, currentMesh->ymax, currentMesh->zmax);

	glBegin(GL_LINE_STRIP);
		glVertex3f(vect0.x, vect1.y, vect1.z);			glVertex3f(vect1.x, vect1.y, vect1.z);
		glVertex3f(vect1.x, vect1.y, vect0.z);			glVertex3f(vect0.x, vect1.y, vect0.z);
		glVertex3f(vect0.x, vect1.y, vect1.z);			glVertex3f(vect0.x, vect0.y, vect1.z);
		glVertex3f(vect1.x, vect0.y, vect1.z);			glVertex3f(vect1.x, vect0.y, vect0.z);
		glVertex3f(vect0.x, vect0.y, vect0.z);			glVertex3f(vect0.x, vect0.y, vect1.z);
	glEnd();

	glBegin(GL_LINES);
		glVertex3f(vect0.x, vect1.y, vect0.z);			glVertex3f(vect0.x, vect0.y, vect0.z);
		glVertex3f(vect1.x, vect1.y, vect0.z);			glVertex3f(vect1.x, vect0.y, vect0.z);
		glVertex3f(vect1.x, vect1.y, vect1.z);			glVertex3f(vect1.x, vect0.y, vect1.z);
	glEnd();
}
