Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoMeshSimplifier.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoMeshSimplifier.cpp	(revision 2291)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoMeshSimplifier.cpp	(revision 2300)
@@ -42,8 +42,144 @@
 
 //-------------------------------------------------------------------------
+//	Erase submeshes that have not indices.
+//-------------------------------------------------------------------------
+void	MeshSimplifier::eraseVoidSubMeshes(Mesh	*geoMesh)
+{
+	SubMesh	*geosubmesh;
+	size_t	valid_submesh_count;
+	size_t	submesh;
+
+	valid_submesh_count	=	0;
+
+	//	For each submesh.
+	for	(size_t	i	=	0; i < geoMesh->mSubMeshCount;	i++)
+	{
+		//	Debug.
+		cout	<<	"Indices of submesh "
+					<<	i
+					<<	": "
+					<<	geoMesh->mSubMesh[i].mIndexCount
+					<<	endl;
+
+		if ((geoMesh->mSubMesh[i].mIndexCount > 0)
+				||
+				(indexMeshLeaves == i))
+		{
+			valid_submesh_count++;
+		}
+	}
+
+	//	Reassign submesh count.
+	geoMesh->mSubMeshCount	=	valid_submesh_count;
+
+	//	Reserve memory for valid submeshes.
+	geosubmesh	=	new	SubMesh[valid_submesh_count];
+
+	submesh	=	0;
+	
+	//	For each submesh.
+	for (size_t i = 0; i < geoMesh->mSubMeshCount; i++)
+	{
+		//	If leaves submesh is found.
+		if (indexMeshLeaves == i)
+		{
+			geosubmesh[i].mStripCount	=	0;
+			geosubmesh[i].mStrip			=	NULL;
+
+			geosubmesh[i].mSharedVertexBuffer	=	false;
+
+			strcpy(	geosubmesh[i].mMaterialName,
+					mInitialMesh->mSubMesh[i].mMaterialName);
+
+			//	Copy submesh bones.
+			if (!mInitialMesh->mSubMesh[i].mBones.empty())
+			{
+				for (	size_t j = 0;
+						j < mInitialMesh->mSubMesh[i].mBones.size();
+						j++)
+				{
+					geosubmesh[i].mBones.push_back(mInitialMesh->
+							mSubMesh[i].mBones[j]);
+				}
+			}
+
+			//	Leaves mesh.
+			geosubmesh[i].mIndexCount	=	
+				mInitialMesh->mSubMesh[i].mIndexCount;
+
+			geosubmesh[i].mIndex	=
+				new Geometry::Index[geosubmesh[i].mIndexCount];
+
+			memcpy(	geosubmesh[i].mIndex,
+					mInitialMesh->mSubMesh[i].mIndex,
+					mInitialMesh->mSubMesh[i].mIndexCount * sizeof(Index));
+
+			//	Copy the leaves submesh vertices.
+			geosubmesh[i].mVertexBuffer	=
+				mInitialMesh->mSubMesh[i].mVertexBuffer->Clone();
+
+			//	Next valid submesh.
+			submesh++;
+		}
+		else if (geoMesh->mSubMesh[i].mIndexCount > 0)
+		{
+			geosubmesh[submesh].mSharedVertexBuffer	=
+									geoMesh->mSubMesh[i].mSharedVertexBuffer;
+
+			geosubmesh[submesh].mVertexBuffer	=	geoMesh->mVertexBuffer;
+
+			geosubmesh[submesh].mType				=	geoMesh->mSubMesh[i].mType;
+
+			geosubmesh[submesh].mStripCount	=	geoMesh->mSubMesh[i].mStripCount;
+
+			geosubmesh[submesh].mIndexCount	=	geoMesh->mSubMesh[i].mIndexCount;
+
+			//	Reserve memory for indices.
+			geosubmesh[submesh].mIndex	=
+													new Index[geosubmesh[submesh].mIndexCount];
+
+			//	Copy indices.
+			memcpy(	geosubmesh[submesh].mIndex,
+					geoMesh->mSubMesh[i].mIndex,
+					geoMesh->mSubMesh[i].mIndexCount * sizeof(Index));
+
+			//	Reserve memory for array of strips.
+			geosubmesh[submesh].mStrip	=
+				new Index*[geoMesh->mSubMesh[submesh].mStripCount];
+
+			//	Copy strip list of the submesh.
+			for (size_t j = 0; j < geoMesh->mSubMesh[i].mStripCount; j++)
+			{
+				geosubmesh[submesh].mStrip[j]	=	geoMesh->mSubMesh[i].mStrip[j];
+			}
+
+			strcpy(	geosubmesh[submesh].mMaterialName,
+					geoMesh->mSubMesh[i].mMaterialName);
+
+			for (size_t	j	=	0;	j < geoMesh->mSubMesh[i].mBones.size();	j++)
+			{
+				geosubmesh[submesh].mBones
+					.push_back(geoMesh->mSubMesh[i].mBones[j]);
+			}
+
+			//	Next valid submesh.
+			submesh++;
+		}
+	}
+
+	//	Delete submeshes.
+	delete	[]geoMesh->mSubMesh;
+
+	geoMesh->mSubMesh	=	geosubmesh;
+}
+
+//-------------------------------------------------------------------------
 // Returns the simplified mesh.
 //-------------------------------------------------------------------------
 Mesh *	MeshSimplifier::GetMesh()
 {
+	//	Delete void submeshes.
+	eraseVoidSubMeshes(mGeoMesh);
+
 	return mGeoMesh;
 }
@@ -146,5 +282,5 @@
 // method to perform an image based simplification.
 //-------------------------------------------------------------------------
-void GeometryBasedSimplifier::Simplify(Real paramlod)
+int	GeometryBasedSimplifier::Simplify(Real paramlod)
 {
 	SimplificationMethod *m_qslim	=	new SimplificationMethod(mInitialMesh);
@@ -157,4 +293,6 @@
 
 	delete	m_qslim;
+
+	return	NO_GL_ERROR;
 }
 
@@ -165,5 +303,5 @@
 // perform an image based simplification.
 //-------------------------------------------------------------------------
-void GeometryBasedSimplifier::Simplify(uint32 numvertices)
+int	GeometryBasedSimplifier::Simplify(uint32 numvertices)
 {
 	SimplificationMethod *m_qslim	=	new SimplificationMethod(mInitialMesh);
@@ -176,4 +314,6 @@
 
 	delete m_qslim;
+
+	return	NO_GL_ERROR;
 }
 
@@ -235,4 +375,10 @@
 	//	Loads the vmi mesh structure for a geometry mesh given.
 	VMI::mesh = initMeshStructure(mGeoMesh);
+}
+
+//	Init VMI options.
+int	ViewPointDrivenSimplifier::init(void)
+{
+	int	error;
 
 	// RGB and Alpha.
@@ -245,5 +391,5 @@
 	glewInit();
 
-	VMI::init();
+	error	=	VMI::init();
 
 	if (VMI::bLoadCamerasFromFile == GL_FALSE)
@@ -256,4 +402,6 @@
 
 	VMI::initialIs	=	VMI::initIs(VMI::numCameras);
+
+	return	error;
 }
 
@@ -273,6 +421,10 @@
 ///	LOD factor in a range of [0,1]. Implements the
 ///	Simplifier::Simplify method to perform an image based simplification.
-void ViewPointDrivenSimplifier::Simplify(Real percent)
-{
+int ViewPointDrivenSimplifier::Simplify(Real percent)
+{
+	int error;
+
+	error	=	 init();
+
 	VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * percent);
 
@@ -284,20 +436,25 @@
 	}
 
-	VMI::display();
-
-	//	Cleans firts simplification.
-	VMI::freeMemory();
-
-	VMI::mesh =	initMeshStructure(mInitialMesh);
-
-	VMI::contractInitialMesh(VMI::mesh);
-
-	//	Load a geometry mesh for vmi mesh.
-	loadMesh();
-	
-	GetMeshSimpSequence();
-
-	//	Sort bones.
-	bonesReassignament();
+	if (error == NO_GL_ERROR)
+	{
+		VMI::display();
+
+		//	Cleans firts simplification.
+		VMI::freeMemory();
+
+		VMI::mesh =	initMeshStructure(mInitialMesh);
+
+		VMI::contractInitialMesh(VMI::mesh);
+
+		//	Load a geometry mesh for vmi mesh.
+		loadMesh();
+
+		GetMeshSimpSequence();
+
+		//	Sort bones.
+		bonesReassignament();
+	}
+
+	return	error;
 }
 
@@ -308,11 +465,16 @@
 ///	an image based simplification.
 //-------------------------------------------------------------------------
-void ViewPointDrivenSimplifier::Simplify(uint32 numVertices)
+int ViewPointDrivenSimplifier::Simplify(uint32 numVertices)
 {
 	float	percent;
+	int 	error;
+
+	error	=	 init();
 
 	percent	=	(numVertices	*	100.0) / VMI::mesh->numVertices;
 
-	VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * (percent / 100));
+	VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles
+																		*
+																		(percent / 100));
 
 	if ((VMI::numDemandedTriangles == 0)
@@ -323,20 +485,25 @@
 	}
 
-	VMI::display();
-
-	//	Cleans firts simplification.
-	VMI::freeMemory();
-
-	VMI::mesh =	initMeshStructure(mInitialMesh);
-
-	VMI::contractInitialMesh(VMI::mesh);
-	
-	//	Load a geometry mesh for vmi mesh.
-	loadMesh();
-	
-	GetMeshSimpSequence();
-
-	//	Sort bones.
-	bonesReassignament();
+	if (error == NO_GL_ERROR)
+	{
+		VMI::display();
+
+		//	Cleans firts simplification.
+		VMI::freeMemory();
+
+		VMI::mesh =	initMeshStructure(mInitialMesh);
+
+		VMI::contractInitialMesh(VMI::mesh);
+
+		//	Load a geometry mesh for vmi mesh.
+		loadMesh();
+
+		GetMeshSimpSequence();
+
+		//	Sort bones.
+		bonesReassignament();
+	}
+
+	return	error;
 }
 
