Ignore:
Timestamp:
02/15/07 14:45:30 (17 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.cpp

    r2115 r2119  
    33#include "Triangle3.h" 
    44#include "IntersectableWrapper.h" 
     5#include "BvHierarchy.h" 
     6#include "Preprocessor.h" 
     7#include "OgreEntity.h" 
     8#include "PreprocessorFactory.h" 
     9#include "OgreLogManager.h" 
    510 
    611 
    7 ObjReader::ObjReader() 
     12 
     13ObjReader::ObjReader(Ogre::SceneManager *sceneManager): 
     14mSceneManager(sceneManager) 
    815{} 
    916 
     
    1320 
    1421 
    15 bool ObjReader::LoadFile(const string &filename, 
    16                                                  GtpVisibilityPreprocessor::ObjectContainer &objects) const 
     22bool ObjReader::LoadFile(const string &sceneName,  
     23                                                 const string &visibilitySolution, 
     24                                                 Ogre::SceneNode *root) 
    1725{ 
    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"); 
    2150                return false; 
    22          
    23         // read in triangle size 
    24         int numTriangles; 
     51        } 
    2552 
    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"; 
    3955 
    40                 // end of file reached 
    41                 if (samplesIn.eof()) 
    42                         break; 
     56        Ogre::LogManager::getSingleton().logMessage(d.str()); 
    4357 
    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); 
    4769        } 
    48          
    49         return true; 
     70 
     71        delete preprocessor; 
     72 
     73        return false; 
    5074} 
    5175 
    5276 
    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) 
     77Ogre::Entity *ObjReader::CreateEntity(const std::string &name, 
     78                                                                          GtpVisibilityPreprocessor::Intersectable *object) 
    6279{ 
    6380        using namespace Ogre; 
     81        //using namespace GtpVisibilityPreprocessor; 
     82 
    6483        Entity *entity; 
    6584 
    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"; 
    6889        std::string entityName = name + "/Entity"; 
    6990 
    7091        MeshPtr mesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup"); 
    7192        SubMesh* submesh = mesh->createSubMesh(); 
    72 int nbVertices = 99992; 
     93 
    7394        // We must create the vertex data, indicating how many vertices there will be 
    7495        submesh->useSharedVertices = false; 
    7596        submesh->vertexData = new VertexData(); 
    7697        submesh->vertexData->vertexStart = 0; 
    77         submesh->vertexData->vertexCount = nbVertices; 
     98        submesh->vertexData->vertexCount = (int)bvhObj->mObjects.size() * 3; 
    7899 
    79100        static const unsigned short source = 0; 
     
    82103        VertexDeclaration* declaration = HardwareBufferManager::getSingleton().createVertexDeclaration(); 
    83104 
    84         offset += declaration->addElement(source,offset,VET_FLOAT3,VES_POSITION).getSize(); 
     105        offset += declaration->addElement(source, offset, VET_FLOAT3,VES_POSITION).getSize(); 
    85106        //      offset += declaration->addElement(source,offset,VET_FLOAT3,VES_NORMAL).getSize(); 
    86107        //      offset += declaration->addElement(source,offset,VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize(); 
     
    89110        int numVertices = 0; 
    90111 
    91         HardwareVertexBufferSharedPtr vbuffer =  
    92                 HardwareBufferManager::getSingleton().createVertexBuffer(declaration->getVertexSize(source), // size of one whole vertex 
    93                 submesh->vertexData->vertexCount, // number of vertices 
    94                 HardwareBuffer::HBU_STATIC_WRITE_ONLY, // usage 
    95                 false); // no shadow buffer 
     112        HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton(). 
     113                createVertexBuffer(declaration->getVertexSize(source), 
     114                                                   submesh->vertexData->vertexCount, 
     115                                                   HardwareBuffer::HBU_STATIC_WRITE_ONLY, 
     116                                                   false); 
    96117 
    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. 
    99119        AxisAlignedBox aabox; 
    100120 
    101121        float* vdata = static_cast<float*>(vbuffer->lock(HardwareBuffer::HBL_DISCARD)); 
    102122 
    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) 
    104126        { 
    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(); 
    116131 
    117                 // Texture coordinate 
    118                 Vector2 tcoordinate;// = tcoordinates[i]; 
    119                 *vdata ++ = tcoordinate.x; 
     132                for (int i = 0; i < 3; ++ i) 
     133                { 
     134                        Vector3 vtx(tri.mVertices[i]); 
    120135 
    121                 *vdata ++ = tcoordinate.y; 
     136                        *vdata ++ = vtx.x; 
     137                        *vdata ++ = vtx.y; 
     138                        *vdata ++ = vtx.z; 
    122139 
     140                        aabox.merge(vtx); 
     141                } 
    123142        } 
    124143 
     
    126145 
    127146        // We must indicate the bounding box 
    128         //mesh->_setBounds(aabox);  
     147        mesh->_setBounds(aabox);  
    129148 
    130         //mesh->_setBoundingSphereRadius((aabox.getMaximum()-aabox.getMinimum()).length()/2.0);  
    131  
     149        mesh->_setBoundingSphereRadius((aabox.getMaximum()-aabox.getMinimum()).length()/2.0);  
    132150        mesh->load(); 
    133151         
    134152        // Create an entity with the mesh 
    135         //entity = mSceneManager->createEntity(meshName, entityName); 
     153        entity = mSceneManager->createEntity(meshName, entityName); 
    136154 
    137155        return entity; 
Note: See TracChangeset for help on using the changeset viewer.