#include "ObjReader.h" #include "gzstream.h" #include "Triangle3.h" #include "IntersectableWrapper.h" #include "BvHierarchy.h" #include "Preprocessor.h" #include "OgreEntity.h" #include "PreprocessorFactory.h" #include "OgreLogManager.h" #include "ObjManualMeshLoader.h" ObjReader::ObjReader(Ogre::SceneManager *sceneManager): mSceneManager(sceneManager), mPreprocessor(NULL) { } ObjReader::~ObjReader() { CLEAR_CONTAINER(mPvsObjects); DEL_PTR(mPreprocessor); } bool ObjReader::LoadFile(const string &sceneName, const string &visibilitySolution, Ogre::SceneNode *root) { GtpVisibilityPreprocessor::Debug.open("debug.log"); // HACK: get any preprocessor to load file // note should be preserved to allow manual mesh reloading, // but then geometry has to be stored two times mPreprocessor = GtpVisibilityPreprocessor::PreprocessorFactory::CreatePreprocessor("vss"); // hack mPreprocessor->mLoadMeshes = false; Ogre::LogManager::getSingleton().logMessage("loading obj scene"); if (mPreprocessor->LoadScene(sceneName)) { Ogre::LogManager::getSingleton().logMessage("scene loaded, loading objects"); // form objects from the scene triangles if (!mPreprocessor->LoadObjects(visibilitySolution, mPvsObjects, mPreprocessor->mObjects)) { Ogre::LogManager::getSingleton().logMessage("objects cannot be loaded"); return false; } } else { Ogre::LogManager::getSingleton().logMessage("scene cannot be loaded"); return false; } std::stringstream d; d << "successfully loaded " << (int)mPvsObjects.size() << " objects from " << (int)mPreprocessor->mObjects.size() << " preprocessor objects"; Ogre::LogManager::getSingleton().logMessage(d.str()); GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = mPvsObjects.end(); int i = 0; for (oit = mPvsObjects.begin(); oit != oit_end; ++ oit, ++ i) { if (i % 5000 == 4999) { d << i << " objects created"; Ogre::LogManager::getSingleton().logMessage(d.str()); } const Ogre::String entname = "Object" + Ogre::StringConverter::toString(i); #if 0 Ogre::ManualObject *ent = CreateManualObject(entname, *oit); #else Ogre::Entity *ent = CreateEntity(entname, *oit); #endif const Ogre::String nodeName = entname + "Node"; Ogre::SceneNode* node = root->createChildSceneNode(nodeName); node->attachObject(ent); // problems for too many objects (= vbo)? //if (i > 30000)break; } //delete preprocessor; return true; } Ogre::ManualObject *ObjReader::CreateManualObject(const std::string &name, GtpVisibilityPreprocessor::Intersectable *object) { using namespace Ogre; //using namespace GtpVisibilityPreprocessor; std::string meshName = name + ".mesh"; std::string entityName = name + "Entity"; GtpVisibilityPreprocessor::BvhLeaf *bvhObj = static_cast(object); const int vertexCount = (int)bvhObj->mObjects.size() * 3; ManualObject* manual = mSceneManager->createManualObject(entityName); 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(); for (oit = bvhObj->mObjects.begin(); oit != oit_end; ++ oit) { GtpVisibilityPreprocessor::TriangleIntersectable *tObj = static_cast(*oit); GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem(); for (int i = 0; i < 3; ++ i) { const GtpVisibilityPreprocessor::Vector3 vtx = tri.mVertices[i]; const GtpVisibilityPreprocessor::Vector3 n = tri.GetNormal(); manual->position(vtx.x, vtx.y, vtx.z); 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; } Ogre::Entity *ObjReader::CreateEntity(const std::string &name, GtpVisibilityPreprocessor::Intersectable *object) { using namespace Ogre; Entity *entity; GtpVisibilityPreprocessor::BvhLeaf *bvhObj = static_cast(object); std::string meshName = name + ".mesh"; std::string entityName = name + "Entity"; ObjManualMeshLoader *loader = new ObjManualMeshLoader(bvhObj); Mesh* pMesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup", loader).getPointer(); pMesh->load(); // Create an entity with the mesh entity = mSceneManager->createEntity(entityName, meshName); return entity; }