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 |
|
---|
13 | ObjReader::ObjReader(Ogre::SceneManager *sceneManager):
|
---|
14 | mSceneManager(sceneManager)
|
---|
15 | {}
|
---|
16 |
|
---|
17 |
|
---|
18 | ObjReader::~ObjReader()
|
---|
19 | {}
|
---|
20 |
|
---|
21 |
|
---|
22 | bool 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";
|
---|
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 | {Ogre::LogManager::getSingleton().logMessage("r");
|
---|
63 | const Ogre::String entname = "Object" + i;
|
---|
64 |
|
---|
65 | Ogre::Entity *ent = CreateEntity(entname, *oit);
|
---|
66 |
|
---|
67 | const Ogre::String nodeName = entname + "/Node";
|
---|
68 | root->createChildSceneNode(nodeName);
|
---|
69 | }
|
---|
70 |
|
---|
71 | delete preprocessor;
|
---|
72 |
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | Ogre::Entity *ObjReader::CreateEntity(const std::string &name,
|
---|
78 | GtpVisibilityPreprocessor::Intersectable *object)
|
---|
79 | {
|
---|
80 | using namespace Ogre;
|
---|
81 | //using namespace GtpVisibilityPreprocessor;
|
---|
82 |
|
---|
83 | Entity *entity;
|
---|
84 |
|
---|
85 | GtpVisibilityPreprocessor::BvhLeaf *bvhObj =
|
---|
86 | static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
|
---|
87 |
|
---|
88 | std::string meshName = name + "/Mesh";
|
---|
89 | std::string entityName = name + "/Entity";
|
---|
90 |
|
---|
91 | MeshPtr mesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup");
|
---|
92 | SubMesh* submesh = mesh->createSubMesh();
|
---|
93 |
|
---|
94 | // We must create the vertex data, indicating how many vertices there will be
|
---|
95 | submesh->useSharedVertices = false;
|
---|
96 | submesh->vertexData = new VertexData();
|
---|
97 | submesh->vertexData->vertexStart = 0;
|
---|
98 | submesh->vertexData->vertexCount = (int)bvhObj->mObjects.size() * 3;
|
---|
99 |
|
---|
100 | static const unsigned short source = 0;
|
---|
101 | size_t offset = 0;
|
---|
102 |
|
---|
103 | VertexDeclaration* declaration = HardwareBufferManager::getSingleton().createVertexDeclaration();
|
---|
104 |
|
---|
105 | offset += declaration->addElement(source, offset, VET_FLOAT3,VES_POSITION).getSize();
|
---|
106 | // offset += declaration->addElement(source,offset,VET_FLOAT3,VES_NORMAL).getSize();
|
---|
107 | // offset += declaration->addElement(source,offset,VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize();
|
---|
108 |
|
---|
109 |
|
---|
110 | int numVertices = 0;
|
---|
111 |
|
---|
112 | HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton().
|
---|
113 | createVertexBuffer(declaration->getVertexSize(source),
|
---|
114 | submesh->vertexData->vertexCount,
|
---|
115 | HardwareBuffer::HBU_STATIC_WRITE_ONLY,
|
---|
116 | false);
|
---|
117 |
|
---|
118 | // No we get access to the buffer to fill it. During so we record the bounding box.
|
---|
119 | AxisAlignedBox aabox;
|
---|
120 |
|
---|
121 | float* vdata = static_cast<float*>(vbuffer->lock(HardwareBuffer::HBL_DISCARD));
|
---|
122 |
|
---|
123 | GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, 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 | Vector3 vtx(tri.mVertices[i]);
|
---|
135 |
|
---|
136 | *vdata ++ = vtx.x;
|
---|
137 | *vdata ++ = vtx.y;
|
---|
138 | *vdata ++ = vtx.z;
|
---|
139 |
|
---|
140 | aabox.merge(vtx);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | vbuffer->unlock();
|
---|
145 |
|
---|
146 | // We must indicate the bounding box
|
---|
147 | mesh->_setBounds(aabox);
|
---|
148 |
|
---|
149 | mesh->_setBoundingSphereRadius((aabox.getMaximum()-aabox.getMinimum()).length()/2.0);
|
---|
150 | mesh->load();
|
---|
151 |
|
---|
152 | // Create an entity with the mesh
|
---|
153 | entity = mSceneManager->createEntity(meshName, entityName);
|
---|
154 |
|
---|
155 | return entity;
|
---|
156 | } |
---|