[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"
|
---|
[2109] | 10 |
|
---|
| 11 |
|
---|
[2119] | 12 |
|
---|
| 13 | ObjReader::ObjReader(Ogre::SceneManager *sceneManager):
|
---|
| 14 | mSceneManager(sceneManager)
|
---|
[2111] | 15 | {}
|
---|
[2109] | 16 |
|
---|
[2112] | 17 |
|
---|
[2111] | 18 | ObjReader::~ObjReader()
|
---|
| 19 | {}
|
---|
[2109] | 20 |
|
---|
| 21 |
|
---|
[2119] | 22 | bool ObjReader::LoadFile(const string &sceneName,
|
---|
| 23 | const string &visibilitySolution,
|
---|
| 24 | Ogre::SceneNode *root)
|
---|
[2109] | 25 | {
|
---|
[2119] | 26 | GtpVisibilityPreprocessor::Debug.open("debug.log");
|
---|
| 27 | // HACK: get any preprocessor to load file
|
---|
| 28 | GtpVisibilityPreprocessor::Preprocessor *preprocessor =
|
---|
| 29 | GtpVisibilityPreprocessor::PreprocessorFactory::CreatePreprocessor("vss");
|
---|
[2109] | 30 |
|
---|
[2119] | 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))
|
---|
[2109] | 38 | {
|
---|
[2119] | 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 | }
|
---|
[2109] | 52 |
|
---|
[2119] | 53 | std::stringstream d;
|
---|
| 54 | d << "successfully loaded " << pvsObjects.size() << " objects";
|
---|
[2109] | 55 |
|
---|
[2119] | 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);
|
---|
[2109] | 69 | }
|
---|
| 70 |
|
---|
[2119] | 71 | delete preprocessor;
|
---|
[2109] | 72 |
|
---|
[2119] | 73 | return false;
|
---|
[2109] | 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
[2119] | 77 | Ogre::Entity *ObjReader::CreateEntity(const std::string &name,
|
---|
| 78 | GtpVisibilityPreprocessor::Intersectable *object)
|
---|
[2109] | 79 | {
|
---|
| 80 | using namespace Ogre;
|
---|
[2119] | 81 | //using namespace GtpVisibilityPreprocessor;
|
---|
| 82 |
|
---|
[2115] | 83 | Entity *entity;
|
---|
[2109] | 84 |
|
---|
[2119] | 85 | GtpVisibilityPreprocessor::BvhLeaf *bvhObj =
|
---|
| 86 | static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object);
|
---|
| 87 |
|
---|
| 88 | std::string meshName = name + "/Mesh";
|
---|
[2109] | 89 | std::string entityName = name + "/Entity";
|
---|
| 90 |
|
---|
[2115] | 91 | MeshPtr mesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup");
|
---|
| 92 | SubMesh* submesh = mesh->createSubMesh();
|
---|
[2119] | 93 |
|
---|
[2115] | 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;
|
---|
[2119] | 98 | submesh->vertexData->vertexCount = (int)bvhObj->mObjects.size() * 3;
|
---|
[2109] | 99 |
|
---|
[2115] | 100 | static const unsigned short source = 0;
|
---|
| 101 | size_t offset = 0;
|
---|
[2109] | 102 |
|
---|
[2115] | 103 | VertexDeclaration* declaration = HardwareBufferManager::getSingleton().createVertexDeclaration();
|
---|
[2109] | 104 |
|
---|
[2119] | 105 | offset += declaration->addElement(source, offset, VET_FLOAT3,VES_POSITION).getSize();
|
---|
[2115] | 106 | // offset += declaration->addElement(source,offset,VET_FLOAT3,VES_NORMAL).getSize();
|
---|
| 107 | // offset += declaration->addElement(source,offset,VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize();
|
---|
[2109] | 108 |
|
---|
| 109 |
|
---|
[2115] | 110 | int numVertices = 0;
|
---|
[2109] | 111 |
|
---|
[2119] | 112 | HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton().
|
---|
| 113 | createVertexBuffer(declaration->getVertexSize(source),
|
---|
| 114 | submesh->vertexData->vertexCount,
|
---|
| 115 | HardwareBuffer::HBU_STATIC_WRITE_ONLY,
|
---|
| 116 | false);
|
---|
[2115] | 117 |
|
---|
[2119] | 118 | // No we get access to the buffer to fill it. During so we record the bounding box.
|
---|
[2115] | 119 | AxisAlignedBox aabox;
|
---|
| 120 |
|
---|
| 121 | float* vdata = static_cast<float*>(vbuffer->lock(HardwareBuffer::HBL_DISCARD));
|
---|
| 122 |
|
---|
[2119] | 123 | GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = bvhObj->mObjects.end();
|
---|
| 124 |
|
---|
| 125 | for (oit = bvhObj->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
[2109] | 126 | {
|
---|
[2119] | 127 | GtpVisibilityPreprocessor::TriangleIntersectable *tObj =
|
---|
| 128 | static_cast<GtpVisibilityPreprocessor::TriangleIntersectable *>(*oit);
|
---|
| 129 |
|
---|
| 130 | GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem();
|
---|
[2115] | 131 |
|
---|
[2119] | 132 | for (int i = 0; i < 3; ++ i)
|
---|
| 133 | {
|
---|
| 134 | Vector3 vtx(tri.mVertices[i]);
|
---|
[2115] | 135 |
|
---|
[2119] | 136 | *vdata ++ = vtx.x;
|
---|
| 137 | *vdata ++ = vtx.y;
|
---|
| 138 | *vdata ++ = vtx.z;
|
---|
[2115] | 139 |
|
---|
[2119] | 140 | aabox.merge(vtx);
|
---|
| 141 | }
|
---|
[2109] | 142 | }
|
---|
| 143 |
|
---|
[2115] | 144 | vbuffer->unlock();
|
---|
[2109] | 145 |
|
---|
[2115] | 146 | // We must indicate the bounding box
|
---|
[2119] | 147 | mesh->_setBounds(aabox);
|
---|
[2109] | 148 |
|
---|
[2119] | 149 | mesh->_setBoundingSphereRadius((aabox.getMaximum()-aabox.getMinimum()).length()/2.0);
|
---|
[2115] | 150 | mesh->load();
|
---|
| 151 |
|
---|
| 152 | // Create an entity with the mesh
|
---|
[2119] | 153 | entity = mSceneManager->createEntity(meshName, entityName);
|
---|
[2109] | 154 |
|
---|
[2115] | 155 | return entity;
|
---|
| 156 | } |
---|