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

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