Ignore:
Timestamp:
02/17/07 22:48:12 (17 years ago)
Author:
mattausch
Message:

worded on obj loading in Ogre

File:
1 edited

Legend:

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

    r2111 r2123  
    11#include "ObjManualMeshLoader.h" 
    2 #include "ObjReader.h" 
     2#include "Triangle3.h" 
     3#include "IntersectableWrapper.h" 
     4#include "BvHierarchy.h" 
     5#include "OgreLogManager.h" 
    36 
    4 ObjManualMeshLoader::ObjManualMeshLoader() 
    5 { 
    6         data = NULL; 
    7 } 
     7#define USE_HBL_DISCARD 1 
    88 
    9  
    10 ObjManualMeshLoader::ObjManualMeshLoader(ObjMeshData *mData): 
    11 data(mData) 
     9ObjManualMeshLoader::ObjManualMeshLoader(GtpVisibilityPreprocessor::BvhLeaf *obj): 
     10mObject(obj) 
    1211{ 
    1312} 
     
    1615ObjManualMeshLoader::~ObjManualMeshLoader() 
    1716{ 
    18         if (data != NULL) 
    19         { 
    20                 data->collapse(); 
    21                 delete data; 
    22         } 
    23 } 
    24  
    25  
    26 Ogre::AxisAlignedBox *ObjManualMeshLoader::getBoundingBox() 
    27 { 
    28         return data->boundingBox; 
    2917} 
    3018 
     
    3321{ 
    3422        using namespace Ogre; 
    35         Mesh *pMesh = (Mesh *) resource; 
    3623 
     24        Mesh *mesh = (Mesh *) resource; 
     25        SubMesh* submesh = mesh->createSubMesh(); 
    3726 
    38         Vector3 translation = data->boundingBox->getCenter(); 
     27        const int triCount = (int)mObject->mObjects.size(); 
     28        const int vertexCount = triCount * 3; 
    3929 
    40         bool nOk = ((data->normals != NULL) && (data->ncnt > 0)); 
    41         bool tOk = ((data->texCoords != NULL) && (data->tcnt > 0)); 
    42          
    43         pMesh->sharedVertexData = new VertexData();  
     30        // We must create the vertex data, indicating how many vertices there will be 
     31        submesh->useSharedVertices = false; 
     32        submesh->vertexData = new VertexData(); 
     33        submesh->vertexData->vertexStart = 0; 
     34        submesh->vertexData->vertexCount = vertexCount; 
    4435 
    45         VertexDeclaration* vertexDecl = pMesh->sharedVertexData->vertexDeclaration;  
     36        static const unsigned short source = 0; 
     37        size_t offset = 0; 
    4638 
    47         size_t currOffset = 0;  
    48         vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_POSITION);  
    49         currOffset += VertexElement::getTypeSize(VET_FLOAT3);  
    50         if (nOk) 
     39        // We must now declare what the vertex data contains 
     40        VertexDeclaration* declaration = submesh->vertexData->vertexDeclaration; 
     41 
     42        offset += declaration->addElement(source, offset, VET_FLOAT3, VES_POSITION).getSize(); 
     43        offset += declaration->addElement(source, offset, VET_FLOAT3, VES_NORMAL).getSize(); 
     44        //offset += declaration->addElement(source, offset, VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize(); 
     45 
     46        HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton(). 
     47                createVertexBuffer(declaration->getVertexSize(source), 
     48                                                   submesh->vertexData->vertexCount, 
     49                                                   HardwareBuffer::HBU_STATIC_WRITE_ONLY); 
     50 
     51    // we get access to the buffer to fill it.  During so we record the bounding box. 
     52        AxisAlignedBox aabox; 
     53 
     54        submesh->vertexData->vertexBufferBinding->setBinding(source, vbuffer); 
     55#if USE_HBL_DISCARD 
     56        float* vdata = static_cast<float*>(vbuffer->lock(HardwareBuffer::HBL_DISCARD)); 
     57#else 
     58        float* vdata = static_cast<float *>(vbuffer->lock(HardwareBuffer::HBL_NORMAL)); 
     59#endif 
     60 
     61        GtpVisibilityPreprocessor::ObjectContainer:: 
     62                const_iterator oit, oit_end = mObject->mObjects.end(); 
     63 
     64        for (oit = mObject->mObjects.begin(); oit != oit_end; ++ oit) 
    5165        { 
    52                 vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_NORMAL);  
    53                 currOffset += VertexElement::getTypeSize(VET_FLOAT3);  
    54         } 
    55     if (tOk) 
    56         { 
    57                 vertexDecl->addElement(0, currOffset, VET_FLOAT2, VES_TEXTURE_COORDINATES);  
    58                 currOffset += VertexElement::getTypeSize(VET_FLOAT2);  
    59         } 
     66                GtpVisibilityPreprocessor::TriangleIntersectable *tObj =  
     67                        static_cast<GtpVisibilityPreprocessor::TriangleIntersectable *>(*oit); 
     68                 
     69                const GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem(); 
     70                const GtpVisibilityPreprocessor::Vector3 n = tri.GetNormal(); 
    6071 
    61         pMesh->sharedVertexData->vertexCount = data->vcnt/3;  
    62         HardwareVertexBufferSharedPtr vbuf =  
    63                 HardwareBufferManager::getSingleton().  
    64                 createVertexBuffer(vertexDecl->getVertexSize(0), pMesh->sharedVertexData->vertexCount,  
    65                         HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);  
     72                for (int i = 0; i < 3; ++ i) 
     73                { 
     74                        Vector3 vtx(tri.mVertices[i].x, tri.mVertices[i].y, tri.mVertices[i].z); 
     75                 
     76                        *(vdata ++) = vtx.x; 
     77                        *(vdata ++) = vtx.y; 
     78                        *(vdata ++) = vtx.z; 
    6679 
    67         VertexBufferBinding* binding = pMesh->sharedVertexData->vertexBufferBinding;  
    68         binding->setBinding(0, vbuf);  
     80                        *(vdata ++) = n.x; 
     81                        *(vdata ++) = n.y; 
     82                        *(vdata ++) = n.z; 
    6983 
    70         Real* pVertices = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));  
    71  
    72         int offset = 0, size = 3; 
    73         if (nOk) size += 3; 
    74         if (tOk) size += 2; 
    75         for (int i=0; i<data->vcnt/3; i++)  
    76         { 
    77                 pVertices[i*size]   = data->vertices[i*3]   - translation.x; 
    78                 pVertices[i*size+1] = data->vertices[i*3+1] - translation.y; 
    79                 pVertices[i*size+2] = data->vertices[i*3+2] - translation.z; 
    80  
    81                 offset = 3; 
    82                 if (nOk) 
    83                 { 
    84                         pVertices[i*size+offset] = data->normals[i*3];   offset++; 
    85                         pVertices[i*size+offset] = data->normals[i*3+1]; offset++; 
    86                         pVertices[i*size+offset] = data->normals[i*3+2]; offset++; 
    87                 } 
    88                 if (tOk) 
    89                 { 
    90                         pVertices[i*size+offset] = data->texCoords[i*2];   offset++; 
    91                         pVertices[i*size+offset] = -data->texCoords[i*2+1]; offset++; 
     84                        aabox.merge(vtx); 
    9285                } 
    9386        } 
    9487 
    95         vbuf->unlock();  
     88        vbuffer->unlock(); 
     89 
     90#if 0 
     91        RenderOperation rop; 
     92        submesh->_getRenderOperation(rop); 
     93        rop.useIndexes = false; 
     94#endif 
     95        ////// 
     96        //-- Creates the index data 
     97     
     98        const int indexCount = vertexCount; 
     99        submesh->indexData->indexStart = 0; 
     100        submesh->operationType = RenderOperation::OT_TRIANGLE_LIST; 
     101 
     102        // we use an index for every vertex 
     103        submesh->indexData->indexCount = indexCount; 
     104        submesh->indexData->indexBuffer = 
     105            HardwareBufferManager::getSingleton(). 
     106                        createIndexBuffer(HardwareIndexBuffer::IT_16BIT, 
     107                                                          submesh->indexData->indexCount, 
     108                                                          HardwareBuffer::HBU_STATIC_WRITE_ONLY); 
     109 
     110#if USE_HBL_DISCARD 
     111        uint16* idata = static_cast<uint16 *>(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD)); 
     112#else 
     113        uint16* idata = static_cast<uint16 *>(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_NORMAL)); 
     114#endif 
     115        for (int j = 0; j < indexCount; ++ j) 
     116    { 
     117                idata[j] = j; 
     118    } 
     119     
     120        submesh->indexData->indexBuffer->unlock(); 
     121        submesh->setMaterialName("BaseWhite");  
     122 
     123        // indicate the bounding box 
     124        mesh->_setBounds(aabox);  
     125        mesh->_setBoundingSphereRadius((aabox.getMaximum() - aabox.getMinimum()).length() / 2.0);  
     126} 
    96127 
    97128 
    98         SubMesh *pSub = pMesh->createSubMesh();  
    99         pSub->operationType = RenderOperation::OT_TRIANGLE_LIST; //;data->roType; 
    100         pSub->useSharedVertices = true;  
    101129 
    102         pSub->indexData->indexCount = data->icnt;  
    103         pSub->indexData->indexBuffer = HardwareBufferManager::getSingleton().  
    104                 createIndexBuffer(HardwareIndexBuffer::IT_16BIT,  
    105                         pSub->indexData->indexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);  
    106  
    107         HardwareIndexBufferSharedPtr ibuf = pSub->indexData->indexBuffer;  
    108         unsigned short* pIndices = static_cast<unsigned short*>(ibuf->lock(HardwareBuffer::HBL_DISCARD));  
    109  
    110         for (i = 0; i < data->icnt; i++) pIndices[i] = data->indices[i]; 
    111          
    112         ibuf->unlock();  
    113                  
    114         AxisAlignedBox bb(data->boundingBox->getMinimum() - translation, data->boundingBox->getMaximum() - translation); 
    115         //AxisAlignedBox bb(data->boundingBox->getMinimum(), data->boundingBox->getMaximum()); 
    116         pMesh->_setBounds(bb);  
    117         Real radius = data->boundingBox->getMinimum().length(); 
    118         if (data->boundingBox->getMaximum().length() > radius) radius = data->boundingBox->getMaximum().length(); 
    119         pMesh->_setBoundingSphereRadius(radius);  
    120  
    121 } 
Note: See TracChangeset for help on using the changeset viewer.