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

Revision 2122, 7.4 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
11
12
13ObjReader::ObjReader(Ogre::SceneManager *sceneManager):
14mSceneManager(sceneManager)
15{}
16
17
18ObjReader::~ObjReader()
19{}
20
21
22bool ObjReader::LoadFile(const string &sceneName,
23                                                 const string &visibilitySolution,
24                                                 Ogre::SceneNode *root)
25{
26        GtpVisibilityPreprocessor::Debug.open("debug.log");
27        // HACK: get any preprocessor to load file
28        GtpVisibilityPreprocessor::Preprocessor *preprocessor =
29                GtpVisibilityPreprocessor::PreprocessorFactory::CreatePreprocessor("vss");
30
31        // hack
32        preprocessor->mLoadMeshes = false;
33        GtpVisibilityPreprocessor::ObjectContainer pvsObjects;
34
35        Ogre::LogManager::getSingleton().logMessage("loading obj scene!!");
36
37        if (preprocessor->LoadScene(sceneName))
38        {
39                Ogre::LogManager::getSingleton().logMessage("scene loaded, loading objects");
40                // form objects from the scene triangles
41                if (!preprocessor->LoadObjects(visibilitySolution, pvsObjects, preprocessor->mObjects))
42                {
43                        Ogre::LogManager::getSingleton().logMessage("objects cannot be loaded");
44                        return false;
45                }
46        }
47        else
48        {
49                Ogre::LogManager::getSingleton().logMessage("scene cannot be loaded");
50                return false;
51        }
52
53        std::stringstream d;
54        d << "successfully loaded " << pvsObjects.size() << " objects from " << preprocessor->mObjects.size() << " preprocessor objects";
55
56        Ogre::LogManager::getSingleton().logMessage(d.str());
57
58        GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = pvsObjects.end();
59
60        int i = 0;
61        for (oit = pvsObjects.begin(); oit != oit_end; ++ oit, ++ i)
62        {
63                Ogre::LogManager::getSingleton().logMessage("r");
64                const Ogre::String entname = "Object" + Ogre::StringConverter::toString(i);
65
66                Ogre::ManualObject *ent = CreateEntity(entname, *oit);
67
68                const Ogre::String nodeName = entname + "Node";
69                Ogre::SceneNode* node = root->createChildSceneNode(nodeName);
70                node->attachObject(ent);
71
72                if (i > 100)
73                        break;
74        }
75
76        delete preprocessor;
77
78        return true;
79}
80
81
82Ogre::ManualObject *ObjReader::CreateManualObject(const std::string &name,
83                                                                                                  GtpVisibilityPreprocessor::Intersectable *object)
84{
85        using namespace Ogre;
86        //using namespace GtpVisibilityPreprocessor;
87
88        std::string meshName = name + ".mesh";
89        std::string entityName = name + "Entity";
90
91GtpVisibilityPreprocessor::BvhLeaf *bvhObj =
92                static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
93        const int vertexCount = (int)bvhObj->mObjects.size() * 3;
94
95        ManualObject* manual = mSceneManager->createManualObject(entityName);
96        manual->begin("BaseWhiteNoLighting", RenderOperation::OT_LINE_STRIP);
97       
98        // create vertices
99       
100        GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = bvhObj->mObjects.end();
101       
102        for (oit = bvhObj->mObjects.begin(); oit != oit_end; ++ oit)
103        {
104                GtpVisibilityPreprocessor::TriangleIntersectable *tObj =
105                        static_cast<GtpVisibilityPreprocessor::TriangleIntersectable *>(*oit);
106               
107                GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem();
108
109                for (int i = 0; i < 3; ++ i)
110                {
111                        const GtpVisibilityPreprocessor::Vector3 vtx = tri.mVertices[i];
112                        manual->position(vtx.x, vtx.y, vtx.z);
113                        manual->colour(1.0f, 0.0f, 0.0f);
114                }
115        }
116
117        for (int i = 0; i < vertexCount; ++ i)
118        {
119                manual->index(i);
120        }
121
122        return manual;
123}
124
125
126Ogre::Entity *ObjReader::CreateEntity(const std::string &name,
127                                                                          GtpVisibilityPreprocessor::Intersectable *object)
128{
129        using namespace Ogre;
130
131        Entity *entity;
132
133        GtpVisibilityPreprocessor::BvhLeaf *bvhObj =
134                static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
135
136        std::string meshName = name + ".mesh";
137        std::string entityName = name + "Entity";
138
139        MeshPtr mesh = MeshManager::getSingleton().createManual(meshName, "Custom");
140        SubMesh* submesh = mesh->createSubMesh();
141
142        const int vertexCount = (int)bvhObj->mObjects.size() * 3;
143
144        // We must create the vertex data, indicating how many vertices there will be
145        submesh->useSharedVertices = false;
146        submesh->vertexData = new VertexData();
147        submesh->vertexData->vertexStart = 0;
148        submesh->vertexData->vertexCount = vertexCount;
149
150        std::stringstream d; d << "objects: " << bvhObj->mObjects.size() << " vtx: " << submesh->vertexData->vertexCount << endl;
151        Ogre::LogManager::getSingleton().logMessage(d.str());
152
153        static const unsigned short source = 0;
154        size_t offset = 0;
155
156        VertexDeclaration* declaration = HardwareBufferManager::getSingleton().createVertexDeclaration();
157
158        offset += declaration->addElement(source, offset, VET_FLOAT3, VES_POSITION).getSize();
159        //offset += declaration->addElement(source, offset, VET_FLOAT3,VES_NORMAL).getSize();
160        //offset += declaration->addElement(source, offset, VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize();
161
162
163        int numVertices = 0;
164
165        HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton().
166                createVertexBuffer(declaration->getVertexSize(source),
167                                                   submesh->vertexData->vertexCount,
168                                                   HardwareBuffer::HBU_STATIC_WRITE_ONLY);
169
170    // No we get access to the buffer to fill it.  During so we record the bounding box.
171        AxisAlignedBox aabox;
172
173        float* vdata = static_cast<float*>(vbuffer->lock(HardwareBuffer::HBL_DISCARD));
174
175        GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = bvhObj->mObjects.end();
176
177        for (oit = bvhObj->mObjects.begin(); oit != oit_end; ++ oit)
178        {
179                GtpVisibilityPreprocessor::TriangleIntersectable *tObj =
180                        static_cast<GtpVisibilityPreprocessor::TriangleIntersectable *>(*oit);
181               
182                GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem();
183
184                for (int i = 0; i < 3; ++ i)
185                {
186                        Vector3 vtx = Vector3(tri.mVertices[i]);
187
188                        *vdata ++ = vtx.x;
189                        *vdata ++ = vtx.y;
190                        *vdata ++ = vtx.z;
191
192                        //std::stringstream d; d << vtx;
193                        //Ogre::LogManager::getSingleton().logMessage(d.str());
194                        aabox.merge(vtx);
195                }
196        }
197        //Ogre::LogManager::getSingleton().logMessage("***");
198        vbuffer->unlock();
199       
200        /*Ogre::RenderOperation rop;
201        submesh->_getRenderOperation(rop);
202        rop.useIndexes = false;
203*/
204        //////
205        //-- Creates the index data
206
207    submesh->vertexData->vertexBufferBinding->setBinding(source, vbuffer);
208
209        submesh->indexData->indexStart = 0;
210        // we use an index for every vertex
211        submesh->indexData->indexCount = vertexCount;
212        submesh->indexData->indexBuffer =
213            HardwareBufferManager::getSingleton().
214                        createIndexBuffer(HardwareIndexBuffer::IT_16BIT,
215                                                          submesh->indexData->indexCount,
216                                                          HardwareBuffer::HBU_STATIC_WRITE_ONLY);
217        submesh->operationType = RenderOperation::OT_LINE_STRIP;
218        //uint16* idata = static_cast<uint16*>(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD));
219    unsigned short* idata = static_cast<unsigned short*>(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD));
220
221        for (int j = 0; j < vertexCount; ++ j)
222    {
223                *idata = j;
224                ++ idata;
225    }
226   
227        submesh->indexData->indexBuffer->unlock();
228
229        submesh->setMaterialName("BaseWhiteNoLighting");
230
231        // We must indicate the bounding box
232        mesh->_setBounds(aabox);
233        mesh->_setBoundingSphereRadius((aabox.getMaximum() - aabox.getMinimum()).length() / 2.0);
234        mesh->load();
235        mesh->touch();
236
237        // Create an entity with the mesh
238        entity = mSceneManager->createEntity(entityName, meshName);
239
240        //entity->setRenderQueueGroup(Ogre::RENDER_QUEUE_MAIN);
241
242        return entity;
243#endif
244}
Note: See TracBrowser for help on using the repository browser.