- Timestamp:
- 02/15/07 14:45:30 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.cpp
r2115 r2119 3 3 #include "Triangle3.h" 4 4 #include "IntersectableWrapper.h" 5 #include "BvHierarchy.h" 6 #include "Preprocessor.h" 7 #include "OgreEntity.h" 8 #include "PreprocessorFactory.h" 9 #include "OgreLogManager.h" 5 10 6 11 7 ObjReader::ObjReader() 12 13 ObjReader::ObjReader(Ogre::SceneManager *sceneManager): 14 mSceneManager(sceneManager) 8 15 {} 9 16 … … 13 20 14 21 15 bool ObjReader::LoadFile(const string &filename, 16 GtpVisibilityPreprocessor::ObjectContainer &objects) const 22 bool ObjReader::LoadFile(const string &sceneName, 23 const string &visibilitySolution, 24 Ogre::SceneNode *root) 17 25 { 18 igzstream samplesIn(filename.c_str()); 19 20 if (!samplesIn.is_open()) 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"); 21 50 return false; 22 23 // read in triangle size 24 int numTriangles; 51 } 25 52 26 samplesIn.read(reinterpret_cast<char *>(&numTriangles), sizeof(int)); 27 objects.reserve(numTriangles); 28 29 while (1) 30 { 31 GtpVisibilityPreprocessor::Triangle3 tri; 32 33 samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 0), 34 sizeof(GtpVisibilityPreprocessor::Vector3)); 35 samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 1), 36 sizeof(GtpVisibilityPreprocessor::Vector3)); 37 samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 2), 38 sizeof(GtpVisibilityPreprocessor::Vector3)); 53 std::stringstream d; 54 d << "successfully loaded " << pvsObjects.size() << " objects"; 39 55 40 // end of file reached 41 if (samplesIn.eof()) 42 break; 56 Ogre::LogManager::getSingleton().logMessage(d.str()); 43 57 44 GtpVisibilityPreprocessor::TriangleIntersectable *obj = 45 new GtpVisibilityPreprocessor::TriangleIntersectable(tri); 46 objects.push_back(obj); 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); 47 69 } 48 49 return true; 70 71 delete preprocessor; 72 73 return false; 50 74 } 51 75 52 76 53 const std::vector<Ogre::Entity *> &ObjReader::GetObjects() const 54 { 55 return mObjects; 56 } 57 58 59 Ogre::Entity *ObjReader::CreateEntity(const std::string &name) 60 // ObjMeshData *mData, 61 // Ogre::Vector3 *translation) 77 Ogre::Entity *ObjReader::CreateEntity(const std::string &name, 78 GtpVisibilityPreprocessor::Intersectable *object) 62 79 { 63 80 using namespace Ogre; 81 //using namespace GtpVisibilityPreprocessor; 82 64 83 Entity *entity; 65 84 66 67 std::string meshName = name.substr(name.find('/',0), name.length()) + "/Mesh"; 85 GtpVisibilityPreprocessor::BvhLeaf *bvhObj = 86 static_cast<GtpVisibilityPreprocessor::BvhLeaf *>(object); 87 88 std::string meshName = name + "/Mesh"; 68 89 std::string entityName = name + "/Entity"; 69 90 70 91 MeshPtr mesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup"); 71 92 SubMesh* submesh = mesh->createSubMesh(); 72 int nbVertices = 99992; 93 73 94 // We must create the vertex data, indicating how many vertices there will be 74 95 submesh->useSharedVertices = false; 75 96 submesh->vertexData = new VertexData(); 76 97 submesh->vertexData->vertexStart = 0; 77 submesh->vertexData->vertexCount = nbVertices;98 submesh->vertexData->vertexCount = (int)bvhObj->mObjects.size() * 3; 78 99 79 100 static const unsigned short source = 0; … … 82 103 VertexDeclaration* declaration = HardwareBufferManager::getSingleton().createVertexDeclaration(); 83 104 84 offset += declaration->addElement(source, offset,VET_FLOAT3,VES_POSITION).getSize();105 offset += declaration->addElement(source, offset, VET_FLOAT3,VES_POSITION).getSize(); 85 106 // offset += declaration->addElement(source,offset,VET_FLOAT3,VES_NORMAL).getSize(); 86 107 // offset += declaration->addElement(source,offset,VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize(); … … 89 110 int numVertices = 0; 90 111 91 HardwareVertexBufferSharedPtr vbuffer = 92 HardwareBufferManager::getSingleton().createVertexBuffer(declaration->getVertexSize(source), // size of one whole vertex93 submesh->vertexData->vertexCount, // number of vertices94 HardwareBuffer::HBU_STATIC_WRITE_ONLY, // usage95 false); // no shadow buffer112 HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton(). 113 createVertexBuffer(declaration->getVertexSize(source), 114 submesh->vertexData->vertexCount, 115 HardwareBuffer::HBU_STATIC_WRITE_ONLY, 116 false); 96 117 97 98 // No we get access to the buffer to fill it. During so we record the bounding box. 118 // No we get access to the buffer to fill it. During so we record the bounding box. 99 119 AxisAlignedBox aabox; 100 120 101 121 float* vdata = static_cast<float*>(vbuffer->lock(HardwareBuffer::HBL_DISCARD)); 102 122 103 for (size_t i = 0; i < nbVertices; ++ i) 123 GtpVisibilityPreprocessor::ObjectContainer::const_iterator oit, oit_end = bvhObj->mObjects.end(); 124 125 for (oit = bvhObj->mObjects.begin(); oit != oit_end; ++ oit) 104 126 { 105 // Position 106 Vector3 position;// = 20.0f*positions[i]; 107 *vdata++ = position.x; 108 *vdata++ = position.y; 109 *vdata++ = position.z; 110 aabox.merge(position); 111 // Normal 112 Vector3 normal = position.normalisedCopy(); 113 *vdata++ = normal.x; 114 *vdata++ = normal.y; 115 *vdata++ = normal.z; 127 GtpVisibilityPreprocessor::TriangleIntersectable *tObj = 128 static_cast<GtpVisibilityPreprocessor::TriangleIntersectable *>(*oit); 129 130 GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem(); 116 131 117 // Texture coordinate118 Vector2 tcoordinate;// = tcoordinates[i];119 *vdata ++ = tcoordinate.x;132 for (int i = 0; i < 3; ++ i) 133 { 134 Vector3 vtx(tri.mVertices[i]); 120 135 121 *vdata ++ = tcoordinate.y; 136 *vdata ++ = vtx.x; 137 *vdata ++ = vtx.y; 138 *vdata ++ = vtx.z; 122 139 140 aabox.merge(vtx); 141 } 123 142 } 124 143 … … 126 145 127 146 // We must indicate the bounding box 128 //mesh->_setBounds(aabox);147 mesh->_setBounds(aabox); 129 148 130 //mesh->_setBoundingSphereRadius((aabox.getMaximum()-aabox.getMinimum()).length()/2.0); 131 149 mesh->_setBoundingSphereRadius((aabox.getMaximum()-aabox.getMinimum()).length()/2.0); 132 150 mesh->load(); 133 151 134 152 // Create an entity with the mesh 135 //entity = mSceneManager->createEntity(meshName, entityName);153 entity = mSceneManager->createEntity(meshName, entityName); 136 154 137 155 return entity;
Note: See TracChangeset
for help on using the changeset viewer.