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

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

worded on obj loading in Ogre

Line 
1#include "ObjManualMeshLoader.h"
2#include "Triangle3.h"
3#include "IntersectableWrapper.h"
4#include "BvHierarchy.h"
5#include "OgreLogManager.h"
6
7#define USE_HBL_DISCARD 1
8
9ObjManualMeshLoader::ObjManualMeshLoader(GtpVisibilityPreprocessor::BvhLeaf *obj):
10mObject(obj)
11{
12}
13
14
15ObjManualMeshLoader::~ObjManualMeshLoader()
16{
17}
18
19
20void ObjManualMeshLoader::loadResource(Ogre::Resource *resource)
21{
22        using namespace Ogre;
23
24        Mesh *mesh = (Mesh *) resource;
25        SubMesh* submesh = mesh->createSubMesh();
26
27        const int triCount = (int)mObject->mObjects.size();
28        const int vertexCount = triCount * 3;
29
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;
35
36        static const unsigned short source = 0;
37        size_t offset = 0;
38
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)
65        {
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();
71
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;
79
80                        *(vdata ++) = n.x;
81                        *(vdata ++) = n.y;
82                        *(vdata ++) = n.z;
83
84                        aabox.merge(vtx);
85                }
86        }
87
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}
127
128
129
Note: See TracBrowser for help on using the repository browser.