source: GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjManualMeshLoader.cpp @ 2123

Revision 2123, 3.8 KB checked in by mattausch, 17 years ago (diff)

worded on obj loading in Ogre

RevLine 
[2110]1#include "ObjManualMeshLoader.h"
[2123]2#include "Triangle3.h"
3#include "IntersectableWrapper.h"
4#include "BvHierarchy.h"
5#include "OgreLogManager.h"
[2109]6
[2123]7#define USE_HBL_DISCARD 1
[2109]8
[2123]9ObjManualMeshLoader::ObjManualMeshLoader(GtpVisibilityPreprocessor::BvhLeaf *obj):
10mObject(obj)
[2109]11{
12}
13
[2111]14
15ObjManualMeshLoader::~ObjManualMeshLoader()
[2109]16{
17}
18
19
[2111]20void ObjManualMeshLoader::loadResource(Ogre::Resource *resource)
[2109]21{
22        using namespace Ogre;
23
[2123]24        Mesh *mesh = (Mesh *) resource;
25        SubMesh* submesh = mesh->createSubMesh();
[2109]26
[2123]27        const int triCount = (int)mObject->mObjects.size();
28        const int vertexCount = triCount * 3;
[2109]29
[2123]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;
[2109]35
[2123]36        static const unsigned short source = 0;
37        size_t offset = 0;
[2109]38
[2123]39        // We must now declare what the vertex data contains
40        VertexDeclaration* declaration = submesh->vertexData->vertexDeclaration;
[2109]41
[2123]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();
[2109]45
[2123]46        HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton().
47                createVertexBuffer(declaration->getVertexSize(source),
48                                                   submesh->vertexData->vertexCount,
49                                                   HardwareBuffer::HBU_STATIC_WRITE_ONLY);
[2109]50
[2123]51    // we get access to the buffer to fill it.  During so we record the bounding box.
52        AxisAlignedBox aabox;
[2109]53
[2123]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)
[2109]65        {
[2123]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();
[2109]71
[2123]72                for (int i = 0; i < 3; ++ i)
[2109]73                {
[2123]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;
79
80                        *(vdata ++) = n.x;
81                        *(vdata ++) = n.y;
82                        *(vdata ++) = n.z;
83
84                        aabox.merge(vtx);
[2109]85                }
86        }
87
[2123]88        vbuffer->unlock();
[2109]89
[2123]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;
[2109]101
[2123]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);
[2109]109
[2123]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");
[2109]122
[2123]123        // indicate the bounding box
124        mesh->_setBounds(aabox);
125        mesh->_setBoundingSphereRadius((aabox.getMaximum() - aabox.getMinimum()).length() / 2.0);
126}
[2109]127
128
[2123]129
Note: See TracBrowser for help on using the repository browser.