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 |
|
---|
9 | ObjManualMeshLoader::ObjManualMeshLoader(GtpVisibilityPreprocessor::BvhLeaf *obj):
|
---|
10 | mObject(obj)
|
---|
11 | {
|
---|
12 | }
|
---|
13 |
|
---|
14 |
|
---|
15 | ObjManualMeshLoader::~ObjManualMeshLoader()
|
---|
16 | {
|
---|
17 | }
|
---|
18 |
|
---|
19 |
|
---|
20 | void 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 |
|
---|
97 | //////
|
---|
98 | //-- Creates the index data
|
---|
99 |
|
---|
100 | const int indexCount = vertexCount;
|
---|
101 |
|
---|
102 | submesh->indexData->indexStart = 0;
|
---|
103 | submesh->operationType = RenderOperation::OT_TRIANGLE_LIST;
|
---|
104 |
|
---|
105 | // we use an index for every vertex
|
---|
106 | submesh->indexData->indexCount = indexCount;
|
---|
107 | submesh->indexData->indexBuffer =
|
---|
108 | HardwareBufferManager::getSingleton().
|
---|
109 | createIndexBuffer(HardwareIndexBuffer::IT_16BIT,
|
---|
110 | submesh->indexData->indexCount,
|
---|
111 | HardwareBuffer::HBU_STATIC_WRITE_ONLY);
|
---|
112 |
|
---|
113 | #if USE_HBL_DISCARD
|
---|
114 | uint16* idata = static_cast<uint16 *>(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD));
|
---|
115 | #else
|
---|
116 | uint16* idata = static_cast<uint16 *>(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_NORMAL));
|
---|
117 | #endif
|
---|
118 | for (int j = 0; j < indexCount; ++ j)
|
---|
119 | {
|
---|
120 | idata[j] = j;
|
---|
121 | }
|
---|
122 |
|
---|
123 | submesh->indexData->indexBuffer->unlock();
|
---|
124 | submesh->setMaterialName("BaseWhite");
|
---|
125 |
|
---|
126 | // indicate the bounding box
|
---|
127 | mesh->_setBounds(aabox);
|
---|
128 | mesh->_setBoundingSphereRadius((aabox.getMaximum() - aabox.getMinimum()).length() / 2.0);
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|