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

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
34        // HACK: get any preprocessor to load file
35        // note should be preserved to allow manual mesh reloading,
36        // but then geometry has to be stored two times
37        mPreprocessor =
38                GtpVisibilityPreprocessor::PreprocessorFactory::CreatePreprocessor("vss");
39
40        // hack
41        mPreprocessor->mLoadMeshes = false;
42
43        Ogre::LogManager::getSingleton().logMessage("loading obj scene");
44
45        if (mPreprocessor->LoadScene(sceneName))
46        {
47                Ogre::LogManager::getSingleton().logMessage("scene loaded, loading objects");
48                // form objects from the scene triangles
49                if (!mPreprocessor->LoadObjects(visibilitySolution, mPvsObjects, mPreprocessor->mObjects))
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        }
60
61        std::stringstream d;
62        d << "successfully loaded " << (int)mPvsObjects.size()
63          << " objects from " << (int)mPreprocessor->mObjects.size() << " preprocessor objects";
64
65        Ogre::LogManager::getSingleton().logMessage(d.str());
66
67        GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = mPvsObjects.end();
68
69        int i = 0;
70        for (oit = mPvsObjects.begin(); oit != oit_end; ++ oit, ++ i)
71        {
72                if (i % 5000 == 4999)
73                {
74                        d << i << " objects created";
75                        Ogre::LogManager::getSingleton().logMessage(d.str());
76                }
77       
78                const Ogre::String entname = "Object" + Ogre::StringConverter::toString(i);
79
80#if 0
81                Ogre::ManualObject *ent = CreateManualObject(entname, *oit);
82#else
83                Ogre::Entity *ent = CreateEntity(entname, *oit);
84#endif
85
86                const Ogre::String nodeName = entname + "Node";
87                Ogre::SceneNode* node = root->createChildSceneNode(nodeName);
88                node->attachObject(ent);
89
90                // problems for too many objects (= vbo)?
91                //if (i > 30000)break;
92        }
93
94        //delete preprocessor;
95
96        return true;
97}
98
99
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";
108
109        GtpVisibilityPreprocessor::BvhLeaf *bvhObj =
110                static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
111
112        const int vertexCount = (int)bvhObj->mObjects.size() * 3;
113
114        ManualObject* manual = mSceneManager->createManualObject(entityName);
115        manual->begin("BaseWhite", RenderOperation::OT_TRIANGLE_LIST);
116       
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
121        // create vertices
122        GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit,
123                oit_end = bvhObj->mObjects.end();
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];
135                        const GtpVisibilityPreprocessor::Vector3 n = tri.GetNormal();
136
137                        manual->position(vtx.x, vtx.y, vtx.z);
138                        manual->normal(n.x, n.y, n.z);
139                        //manual->colour(red, green, blue);
140                }
141        }
142
143        if (0)
144        for (int i = 0; i < vertexCount; ++ i)
145        {
146                manual->index(i);
147        }
148
149        manual->end();
150
151        return manual;
152}
153
154
155Ogre::Entity *ObjReader::CreateEntity(const std::string &name,
156                                                                          GtpVisibilityPreprocessor::Intersectable *object)
157{
158        using namespace Ogre;
159
160        Entity *entity;
161
162        GtpVisibilityPreprocessor::BvhLeaf *bvhObj =
163                static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
164
165        std::string meshName = name + ".mesh";
166        std::string entityName = name + "Entity";
167
168        ObjManualMeshLoader *loader = new ObjManualMeshLoader(bvhObj);
169
170        Mesh* pMesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup", loader).getPointer();
171        pMesh->load();
172
173        // Create an entity with the mesh
174        entity = mSceneManager->createEntity(entityName, meshName);
175
176        return entity;
177}
178
Note: See TracBrowser for help on using the repository browser.