Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.cpp	(revision 2122)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.cpp	(revision 2123)
@@ -8,4 +8,5 @@
 #include "PreprocessorFactory.h"
 #include "OgreLogManager.h"
+#include "ObjManualMeshLoader.h"
 
 
@@ -17,5 +18,7 @@
 
 ObjReader::~ObjReader()
-{}
+{
+	CLEAR_CONTAINER(mPvsObjects);
+}
 
 
@@ -31,7 +34,6 @@
 	// hack
 	preprocessor->mLoadMeshes = false;
-	GtpVisibilityPreprocessor::ObjectContainer pvsObjects;
 
-	Ogre::LogManager::getSingleton().logMessage("loading obj scene!!");
+	Ogre::LogManager::getSingleton().logMessage("loading obj scene");
 
 	if (preprocessor->LoadScene(sceneName))
@@ -39,5 +41,5 @@
 		Ogre::LogManager::getSingleton().logMessage("scene loaded, loading objects");
 		// form objects from the scene triangles
-		if (!preprocessor->LoadObjects(visibilitySolution, pvsObjects, preprocessor->mObjects))
+		if (!preprocessor->LoadObjects(visibilitySolution, mPvsObjects, preprocessor->mObjects))
 		{
 			Ogre::LogManager::getSingleton().logMessage("objects cannot be loaded");
@@ -52,17 +54,25 @@
 
 	std::stringstream d; 
-	d << "successfully loaded " << pvsObjects.size() << " objects from " << preprocessor->mObjects.size() << " preprocessor objects";
+	d << "successfully loaded " << mPvsObjects.size() << " objects from " << preprocessor->mObjects.size() << " preprocessor objects";
 
 	Ogre::LogManager::getSingleton().logMessage(d.str());
 
-	GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = pvsObjects.end();
+	GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = mPvsObjects.end();
 
 	int i = 0;
-	for (oit = pvsObjects.begin(); oit != oit_end; ++ oit, ++ i)
+	for (oit = mPvsObjects.begin(); oit != oit_end; ++ oit, ++ i)
 	{
-		Ogre::LogManager::getSingleton().logMessage("r");
+		if (i % 5000 == 4999)
+		{
+			d << i << " objects created" << endl;
+		}
+	
 		const Ogre::String entname = "Object" + Ogre::StringConverter::toString(i);
 
-		Ogre::ManualObject *ent = CreateEntity(entname, *oit);
+#if 0
+		Ogre::ManualObject *ent = CreateManualObject(entname, *oit);
+#else
+		Ogre::Entity *ent = CreateEntity(entname, *oit);
+#endif
 
 		const Ogre::String nodeName = entname + "Node";
@@ -70,6 +80,6 @@
 		node->attachObject(ent);
 
-		if (i > 100)
-			break;
+		// problems for too many objects (= vbo)?
+		//if (i > 30000)break;
 	}
 
@@ -89,14 +99,19 @@
 	std::string entityName = name + "Entity";
 
-GtpVisibilityPreprocessor::BvhLeaf *bvhObj = 
+	GtpVisibilityPreprocessor::BvhLeaf *bvhObj = 
 		static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
+
 	const int vertexCount = (int)bvhObj->mObjects.size() * 3;
 
 	ManualObject* manual = mSceneManager->createManualObject(entityName);
-	manual->begin("BaseWhiteNoLighting", RenderOperation::OT_LINE_STRIP);
+	manual->begin("BaseWhite", RenderOperation::OT_TRIANGLE_LIST);
 	
+	const float red = GtpVisibilityPreprocessor::Random(1.0f);
+	const float green = GtpVisibilityPreprocessor::Random(1.0f);
+	const float blue = GtpVisibilityPreprocessor::Random(1.0f);
+
 	// create vertices
-	
-	GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = bvhObj->mObjects.end();
+	GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, 
+		oit_end = bvhObj->mObjects.end();
 	
 	for (oit = bvhObj->mObjects.begin(); oit != oit_end; ++ oit)
@@ -110,13 +125,19 @@
 		{
 			const GtpVisibilityPreprocessor::Vector3 vtx = tri.mVertices[i];
+			const GtpVisibilityPreprocessor::Vector3 n = tri.GetNormal();
+
 			manual->position(vtx.x, vtx.y, vtx.z);
-			manual->colour(1.0f, 0.0f, 0.0f);
+			manual->normal(n.x, n.y, n.z);
+			//manual->colour(red, green, blue);
 		}
 	}
 
+	if (0)
 	for (int i = 0; i < vertexCount; ++ i)
 	{
 		manual->index(i);
 	}
+
+	manual->end();
 
 	return manual;
@@ -137,108 +158,13 @@
 	std::string entityName = name + "Entity";
 
-	MeshPtr mesh = MeshManager::getSingleton().createManual(meshName, "Custom");
-	SubMesh* submesh = mesh->createSubMesh();
+	ObjManualMeshLoader *loader = new ObjManualMeshLoader(bvhObj);
 
-	const int vertexCount = (int)bvhObj->mObjects.size() * 3;
-
-	// We must create the vertex data, indicating how many vertices there will be
-	submesh->useSharedVertices = false;
-	submesh->vertexData = new VertexData();
-	submesh->vertexData->vertexStart = 0;
-	submesh->vertexData->vertexCount = vertexCount;
-
-	std::stringstream d; d << "objects: " << bvhObj->mObjects.size() << " vtx: " << submesh->vertexData->vertexCount << endl;
-	Ogre::LogManager::getSingleton().logMessage(d.str());
-
-	static const unsigned short source = 0;
-	size_t offset = 0;
-
-	VertexDeclaration* declaration = HardwareBufferManager::getSingleton().createVertexDeclaration();
-
-	offset += declaration->addElement(source, offset, VET_FLOAT3, VES_POSITION).getSize();
-	//offset += declaration->addElement(source, offset, VET_FLOAT3,VES_NORMAL).getSize();
-	//offset += declaration->addElement(source, offset, VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize();
-
-
-	int numVertices = 0;
-
-	HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton().
-		createVertexBuffer(declaration->getVertexSize(source),
-						   submesh->vertexData->vertexCount,
-						   HardwareBuffer::HBU_STATIC_WRITE_ONLY);
-
-    // No we get access to the buffer to fill it.  During so we record the bounding box.
-	AxisAlignedBox aabox;
-
-	float* vdata = static_cast<float*>(vbuffer->lock(HardwareBuffer::HBL_DISCARD));
-
-	GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = bvhObj->mObjects.end();
-
-	for (oit = bvhObj->mObjects.begin(); oit != oit_end; ++ oit)
-	{
-		GtpVisibilityPreprocessor::TriangleIntersectable *tObj = 
-			static_cast<GtpVisibilityPreprocessor::TriangleIntersectable *>(*oit);
-		
-		GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem();
-
-		for (int i = 0; i < 3; ++ i)
-		{
-			Vector3 vtx = Vector3(tri.mVertices[i]);
-
-			*vdata ++ = vtx.x;
-			*vdata ++ = vtx.y;
-			*vdata ++ = vtx.z;
-
-			//std::stringstream d; d << vtx;
-			//Ogre::LogManager::getSingleton().logMessage(d.str());
-			aabox.merge(vtx);
-		}
-	}
-	//Ogre::LogManager::getSingleton().logMessage("***");
-	vbuffer->unlock();
-	
-	/*Ogre::RenderOperation rop;
-	submesh->_getRenderOperation(rop);
-	rop.useIndexes = false;
-*/
-	//////
-	//-- Creates the index data
-
-    submesh->vertexData->vertexBufferBinding->setBinding(source, vbuffer);
-
-	submesh->indexData->indexStart = 0;
-	// we use an index for every vertex
-	submesh->indexData->indexCount = vertexCount;
-	submesh->indexData->indexBuffer =
-	    HardwareBufferManager::getSingleton().
-			createIndexBuffer(HardwareIndexBuffer::IT_16BIT,
-							  submesh->indexData->indexCount,
-							  HardwareBuffer::HBU_STATIC_WRITE_ONLY);
-	submesh->operationType = RenderOperation::OT_LINE_STRIP;
-	//uint16* idata = static_cast<uint16*>(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD));
-    unsigned short* idata = static_cast<unsigned short*>(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD));
-
-	for (int j = 0; j < vertexCount; ++ j)
-    {
-		*idata = j;
-		++ idata;
-    }
-    
-	submesh->indexData->indexBuffer->unlock();
-
-	submesh->setMaterialName("BaseWhiteNoLighting"); 
-
-	// We must indicate the bounding box
-	mesh->_setBounds(aabox); 
-	mesh->_setBoundingSphereRadius((aabox.getMaximum() - aabox.getMinimum()).length() / 2.0); 
-	mesh->load();
-	mesh->touch();
+	Mesh* pMesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup", loader).getPointer(); 
+	pMesh->load(); 
 
 	// Create an entity with the mesh
 	entity = mSceneManager->createEntity(entityName, meshName);
 
-	//entity->setRenderQueueGroup(Ogre::RENDER_QUEUE_MAIN);
+	return entity;
+}
 
-	return entity;
-#endif
-}
