source: GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.cpp @ 2280

Revision 2280, 4.8 KB checked in by mattausch, 17 years ago (diff)

removed dependency on ogre in gtpvisibility

RevLine 
[2110]1#include "ObjReader.h"
[2111]2#include "gzstream.h"
3#include "Triangle3.h"
[2112]4#include "IntersectableWrapper.h"
[2119]5#include "BvHierarchy.h"
6#include "Preprocessor.h"
7#include "OgreEntity.h"
8#include "PreprocessorFactory.h"
9#include "OgreLogManager.h"
[2123]10#include "ObjManualMeshLoader.h"
[2109]11
12
[2119]13
14ObjReader::ObjReader(Ogre::SceneManager *sceneManager):
[2124]15mSceneManager(sceneManager), mPreprocessor(NULL)
16{
17}
[2109]18
[2112]19
[2111]20ObjReader::~ObjReader()
[2123]21{
22        CLEAR_CONTAINER(mPvsObjects);
[2124]23       
24        DEL_PTR(mPreprocessor);
[2123]25}
[2109]26
27
[2119]28bool ObjReader::LoadFile(const string &sceneName,
29                                                 const string &visibilitySolution,
30                                                 Ogre::SceneNode *root)
[2109]31{
[2119]32        GtpVisibilityPreprocessor::Debug.open("debug.log");
[2280]33
[2119]34        // HACK: get any preprocessor to load file
[2124]35        // note should be preserved to allow manual mesh reloading,
36        // but then geometry has to be stored two times
37        mPreprocessor =
[2119]38                GtpVisibilityPreprocessor::PreprocessorFactory::CreatePreprocessor("vss");
[2109]39
[2119]40        // hack
[2124]41        mPreprocessor->mLoadMeshes = false;
[2119]42
[2123]43        Ogre::LogManager::getSingleton().logMessage("loading obj scene");
[2119]44
[2124]45        if (mPreprocessor->LoadScene(sceneName))
[2109]46        {
[2119]47                Ogre::LogManager::getSingleton().logMessage("scene loaded, loading objects");
48                // form objects from the scene triangles
[2124]49                if (!mPreprocessor->LoadObjects(visibilitySolution, mPvsObjects, mPreprocessor->mObjects))
[2119]50                {
51                        Ogre::LogManager::getSingleton().logMessage("objects cannot be loaded");
52                        return false;
53                }
54        }
55        else
56        {
57                Ogre::LogManager::getSingleton().logMessage("scene cannot be loaded");
58                return false;
59        }
[2109]60
[2119]61        std::stringstream d;
[2124]62        d << "successfully loaded " << (int)mPvsObjects.size()
63          << " objects from " << (int)mPreprocessor->mObjects.size() << " preprocessor objects";
[2109]64
[2119]65        Ogre::LogManager::getSingleton().logMessage(d.str());
66
[2123]67        GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = mPvsObjects.end();
[2119]68
69        int i = 0;
[2123]70        for (oit = mPvsObjects.begin(); oit != oit_end; ++ oit, ++ i)
[2122]71        {
[2123]72                if (i % 5000 == 4999)
73                {
[2124]74                        d << i << " objects created";
75                        Ogre::LogManager::getSingleton().logMessage(d.str());
[2123]76                }
77       
[2122]78                const Ogre::String entname = "Object" + Ogre::StringConverter::toString(i);
[2119]79
[2123]80#if 0
81                Ogre::ManualObject *ent = CreateManualObject(entname, *oit);
82#else
83                Ogre::Entity *ent = CreateEntity(entname, *oit);
84#endif
[2119]85
[2122]86                const Ogre::String nodeName = entname + "Node";
87                Ogre::SceneNode* node = root->createChildSceneNode(nodeName);
88                node->attachObject(ent);
89
[2123]90                // problems for too many objects (= vbo)?
91                //if (i > 30000)break;
[2109]92        }
93
[2124]94        //delete preprocessor;
[2109]95
[2122]96        return true;
[2109]97}
98
99
[2122]100Ogre::ManualObject *ObjReader::CreateManualObject(const std::string &name,
101                                                                                                  GtpVisibilityPreprocessor::Intersectable *object)
102{
103        using namespace Ogre;
104        //using namespace GtpVisibilityPreprocessor;
105
106        std::string meshName = name + ".mesh";
107        std::string entityName = name + "Entity";
[2123]108
109        GtpVisibilityPreprocessor::BvhLeaf *bvhObj =
[2122]110                static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
[2123]111
[2122]112        const int vertexCount = (int)bvhObj->mObjects.size() * 3;
113
114        ManualObject* manual = mSceneManager->createManualObject(entityName);
[2123]115        manual->begin("BaseWhite", RenderOperation::OT_TRIANGLE_LIST);
[2122]116       
[2123]117        const float red = GtpVisibilityPreprocessor::Random(1.0f);
118        const float green = GtpVisibilityPreprocessor::Random(1.0f);
119        const float blue = GtpVisibilityPreprocessor::Random(1.0f);
120
[2122]121        // create vertices
[2123]122        GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit,
123                oit_end = bvhObj->mObjects.end();
[2122]124       
125        for (oit = bvhObj->mObjects.begin(); oit != oit_end; ++ oit)
126        {
127                GtpVisibilityPreprocessor::TriangleIntersectable *tObj =
128                        static_cast<GtpVisibilityPreprocessor::TriangleIntersectable *>(*oit);
129               
130                GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem();
131
132                for (int i = 0; i < 3; ++ i)
133                {
134                        const GtpVisibilityPreprocessor::Vector3 vtx = tri.mVertices[i];
[2123]135                        const GtpVisibilityPreprocessor::Vector3 n = tri.GetNormal();
136
[2122]137                        manual->position(vtx.x, vtx.y, vtx.z);
[2123]138                        manual->normal(n.x, n.y, n.z);
139                        //manual->colour(red, green, blue);
[2122]140                }
141        }
142
[2123]143        if (0)
[2122]144        for (int i = 0; i < vertexCount; ++ i)
145        {
146                manual->index(i);
147        }
148
[2123]149        manual->end();
150
[2122]151        return manual;
152}
153
154
[2119]155Ogre::Entity *ObjReader::CreateEntity(const std::string &name,
156                                                                          GtpVisibilityPreprocessor::Intersectable *object)
[2109]157{
158        using namespace Ogre;
[2119]159
[2115]160        Entity *entity;
[2109]161
[2119]162        GtpVisibilityPreprocessor::BvhLeaf *bvhObj =
163                static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
164
[2122]165        std::string meshName = name + ".mesh";
166        std::string entityName = name + "Entity";
[2109]167
[2123]168        ObjManualMeshLoader *loader = new ObjManualMeshLoader(bvhObj);
[2119]169
[2123]170        Mesh* pMesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup", loader).getPointer();
171        pMesh->load();
[2122]172
[2115]173        // Create an entity with the mesh
[2122]174        entity = mSceneManager->createEntity(entityName, meshName);
[2109]175
[2115]176        return entity;
[2123]177}
178
Note: See TracBrowser for help on using the repository browser.