1 |
|
---|
2 | #include "BBCEntity.h"
|
---|
3 |
|
---|
4 | namespace BBC {
|
---|
5 |
|
---|
6 | Entity::Entity(): references(0) // initialize references to 0
|
---|
7 | {
|
---|
8 | createSubEntity();
|
---|
9 | }
|
---|
10 |
|
---|
11 | Entity::~Entity()
|
---|
12 | {
|
---|
13 |
|
---|
14 | }
|
---|
15 |
|
---|
16 | void Entity::createSubEntity()
|
---|
17 | {
|
---|
18 | SubEntityPtr subEntityPtr((SubEntity*)new SubEntity());
|
---|
19 | mSubEntityList.push_back(subEntityPtr);
|
---|
20 | }
|
---|
21 |
|
---|
22 | void Entity::addSubEntity(SubEntityPtr value)
|
---|
23 | {
|
---|
24 | mSubEntityList.push_back(value);
|
---|
25 | }
|
---|
26 |
|
---|
27 | SubEntityPtr Entity::getSubEntity(unsigned int index)
|
---|
28 | {
|
---|
29 | return mSubEntityList[index];
|
---|
30 | }
|
---|
31 |
|
---|
32 | void Entity::removeSubEntity(unsigned int index)
|
---|
33 | {
|
---|
34 | mSubEntityList.erase(mSubEntityList.begin()+index);
|
---|
35 | }
|
---|
36 |
|
---|
37 | void Entity::setSubEntitiesDistinctVertexColours()
|
---|
38 | {
|
---|
39 | for (unsigned int iSubEntity = 0; iSubEntity < mSubEntityList.size(); iSubEntity++)
|
---|
40 | {
|
---|
41 | mSubEntityList[iSubEntity]->enableVertexColors(true);
|
---|
42 | float fRed = Ogre::Math::RangeRandom(0.0,1.0);
|
---|
43 | float fGreen = Ogre::Math::RangeRandom(0.0,1.0);
|
---|
44 | float fBlue = Ogre::Math::RangeRandom(0.0,1.0);
|
---|
45 | Ogre::ColourValue subEntityColour = Ogre::ColourValue(1.0,fRed,fGreen,fBlue);
|
---|
46 | for (unsigned int iVertex = 0; iVertex < mSubEntityList[iSubEntity]->getNumVertices(); iVertex++)
|
---|
47 | {
|
---|
48 | mSubEntityList[iSubEntity]->setVertexColor(iVertex, subEntityColour.getAsRGBA());
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | MeshPtr Entity::getMesh()
|
---|
54 | {
|
---|
55 | return mMesh;
|
---|
56 | }
|
---|
57 |
|
---|
58 | void Entity::setMesh(MeshPtr value)
|
---|
59 | {
|
---|
60 | mMesh = value;
|
---|
61 | }
|
---|
62 |
|
---|
63 | unsigned int Entity::getEntityHandle() {
|
---|
64 | return mEntityHandle;
|
---|
65 | }
|
---|
66 |
|
---|
67 | void Entity::setEntityHandle(unsigned int value)
|
---|
68 | {
|
---|
69 | mEntityHandle = value;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void Entity::loadMesh(bool mergeSubMeshes = false)
|
---|
73 | {
|
---|
74 | Ogre::Vector3 position = Ogre::Vector3::ZERO;
|
---|
75 | Ogre::Quaternion orient = Ogre::Quaternion(1.0,1.0,1.0,1.0);
|
---|
76 | Ogre::Vector3 scale = Ogre::Vector3::ZERO;
|
---|
77 | unsigned int coordset = 0;
|
---|
78 | unsigned short dimTexCoord = 2;
|
---|
79 |
|
---|
80 | getMeshPositions(position,orient,scale,mergeSubMeshes);
|
---|
81 | getMeshNormals(orient,scale,mergeSubMeshes);
|
---|
82 | getMeshVertexColours(mergeSubMeshes);
|
---|
83 | getMeshTexCoords(mergeSubMeshes);
|
---|
84 | //getMeshFacesVerticesID(mergeSubMeshes);
|
---|
85 | }
|
---|
86 |
|
---|
87 | void Entity::sincronizeNumSubEntities()
|
---|
88 | {
|
---|
89 | if (this->getNumSubEntities() < mMesh->get()->getNumSubMeshes())
|
---|
90 | {
|
---|
91 | while (this->getNumSubEntities() < mMesh->get()->getNumSubMeshes())
|
---|
92 | {
|
---|
93 | this->createSubEntity();
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (this->getNumSubEntities() > mMesh->get()->getNumSubMeshes())
|
---|
98 | {
|
---|
99 | while (this->getNumSubEntities() > mMesh->get()->getNumSubMeshes())
|
---|
100 | {
|
---|
101 | this->removeSubEntity(this->getNumSubEntities()-1);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | void Entity::getMeshPositions(Ogre::Vector3 position, Ogre::Quaternion orient, Ogre::Vector3 scale, bool mergeSubMeshes)
|
---|
107 | {
|
---|
108 | bool added_shared = false;
|
---|
109 | size_t current_offset = 0;
|
---|
110 | size_t shared_offset = 0;
|
---|
111 | size_t next_offset = 0;
|
---|
112 | size_t index_offset = 0;
|
---|
113 | size_t vertex_count, index_count;
|
---|
114 |
|
---|
115 | vertex_count = index_count = 0;
|
---|
116 |
|
---|
117 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
118 | sincronizeNumSubEntities();
|
---|
119 |
|
---|
120 | // Calculate how many vertices and indices we're going to need
|
---|
121 | for(int i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
122 | {
|
---|
123 | Ogre::SubMesh* submesh = mMesh->get()->getSubMesh(i);
|
---|
124 |
|
---|
125 | // We only need to add the shared vertices once
|
---|
126 | if(submesh->useSharedVertices)
|
---|
127 | {
|
---|
128 | if(!added_shared)
|
---|
129 | {
|
---|
130 | Ogre::VertexData* vertex_data = mMesh->get()->sharedVertexData;
|
---|
131 | vertex_count += vertex_data->vertexCount;
|
---|
132 | added_shared = true;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | Ogre::VertexData* vertex_data = submesh->vertexData;
|
---|
138 | vertex_count += vertex_data->vertexCount;
|
---|
139 | }
|
---|
140 |
|
---|
141 | // Add the indices
|
---|
142 | Ogre::IndexData* index_data = submesh->indexData;
|
---|
143 | index_count += index_data->indexCount;
|
---|
144 | }
|
---|
145 |
|
---|
146 | unsigned int* indices = NULL;
|
---|
147 | if (mergeSubMeshes)
|
---|
148 | {
|
---|
149 | indices = new unsigned[index_count];
|
---|
150 | }
|
---|
151 |
|
---|
152 | added_shared = false;
|
---|
153 | for (unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
154 | {
|
---|
155 | Ogre::SubMesh* subMesh = mMesh->get()->getSubMesh(i);
|
---|
156 | Ogre::VertexData* vertexData = subMesh->useSharedVertices ? mMesh->get()->sharedVertexData : subMesh->vertexData;
|
---|
157 |
|
---|
158 | if (!mergeSubMeshes)
|
---|
159 | {
|
---|
160 | // Add the indices
|
---|
161 | Ogre::IndexData* index_data = subMesh->indexData;
|
---|
162 | index_count = index_data->indexCount;
|
---|
163 |
|
---|
164 | if (indices != NULL)
|
---|
165 | {
|
---|
166 | delete [] indices;
|
---|
167 | }
|
---|
168 |
|
---|
169 | indices = new unsigned[index_count];
|
---|
170 | }
|
---|
171 |
|
---|
172 | if ((!subMesh->useSharedVertices) || (subMesh->useSharedVertices && !added_shared))
|
---|
173 | {
|
---|
174 | if (subMesh->useSharedVertices)
|
---|
175 | {
|
---|
176 | added_shared = true;
|
---|
177 | shared_offset = current_offset;
|
---|
178 | }
|
---|
179 |
|
---|
180 | const Ogre::VertexElement* positionElem = vertexData->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
|
---|
181 | Ogre::HardwareVertexBufferSharedPtr vbuf = vertexData->vertexBufferBinding->getBuffer(positionElem->getSource());
|
---|
182 | unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
183 | float* pReal;
|
---|
184 |
|
---|
185 | for(size_t j = 0; j < vertexData->vertexCount; ++j, vertex += vbuf->getVertexSize())
|
---|
186 | {
|
---|
187 | positionElem->baseVertexPointerToElement(vertex, &pReal);
|
---|
188 | Ogre::Vector3 position(pReal[0], pReal[1], pReal[2]);
|
---|
189 |
|
---|
190 | if (!mergeSubMeshes)
|
---|
191 | {
|
---|
192 | UniqueVertex uniqueVertex;
|
---|
193 | uniqueVertex.position = position;
|
---|
194 | this->getSubEntity(i)->addUniqueVertex(uniqueVertex);
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | UniqueVertex uniqueVertex;
|
---|
199 | uniqueVertex.position = position;
|
---|
200 |
|
---|
201 | this->getSubEntity(0)->addUniqueVertex(uniqueVertex);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | vbuf->unlock();
|
---|
205 | next_offset += vertexData->vertexCount;
|
---|
206 |
|
---|
207 |
|
---|
208 | Ogre::IndexData* index_data = subMesh->indexData;
|
---|
209 |
|
---|
210 | size_t numTris = index_data->indexCount / 3;
|
---|
211 | unsigned short* pShort;
|
---|
212 | unsigned int* pInt;
|
---|
213 | Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
|
---|
214 | bool use32bitindexes = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
|
---|
215 | if (use32bitindexes) pInt = static_cast<unsigned int*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
216 | else pShort = static_cast<unsigned short*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
217 |
|
---|
218 | index_offset = 0;
|
---|
219 | for(size_t k = 0; k < numTris; ++k)
|
---|
220 | {
|
---|
221 | size_t offset;
|
---|
222 | if (!mergeSubMeshes)
|
---|
223 | {
|
---|
224 | offset = (subMesh->useSharedVertices)?shared_offset:0;
|
---|
225 | }
|
---|
226 | else
|
---|
227 | {
|
---|
228 | offset = (subMesh->useSharedVertices)?shared_offset:current_offset;
|
---|
229 | }
|
---|
230 |
|
---|
231 | unsigned int vindex = use32bitindexes? *pInt++ : *pShort++;
|
---|
232 | indices[index_offset + 0] = vindex + offset;
|
---|
233 |
|
---|
234 | vindex = use32bitindexes? *pInt++ : *pShort++;
|
---|
235 | indices[index_offset + 1] = vindex + offset;
|
---|
236 |
|
---|
237 | vindex = use32bitindexes? *pInt++ : *pShort++;
|
---|
238 | indices[index_offset + 2] = vindex + offset;
|
---|
239 |
|
---|
240 | index_offset += 3;
|
---|
241 | }
|
---|
242 |
|
---|
243 | for(size_t k = 0; k < index_count; k = k + 3)
|
---|
244 | {
|
---|
245 | if (!mergeSubMeshes)
|
---|
246 | {
|
---|
247 | this->getSubEntity(i)->addFaceVerticesIDs(Ogre::Vector3(indices[k], indices[k+1], indices[k+2]));
|
---|
248 | }
|
---|
249 | else
|
---|
250 | {
|
---|
251 | this->getSubEntity(0)->addFaceVerticesIDs(Ogre::Vector3(indices[k], indices[k+1], indices[k+2]));
|
---|
252 | }
|
---|
253 | }
|
---|
254 | ibuf->unlock();
|
---|
255 | current_offset = next_offset;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | delete [] indices;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | void Entity::getMeshNormals(Ogre::Quaternion orient, Ogre::Vector3 scale, bool mergeSubMeshes)
|
---|
264 | {
|
---|
265 | bool added_shared = false;
|
---|
266 | size_t current_offset = 0;
|
---|
267 | size_t shared_offset = 0;
|
---|
268 | size_t next_offset = 0;
|
---|
269 |
|
---|
270 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
271 | sincronizeNumSubEntities();
|
---|
272 |
|
---|
273 | added_shared = false;
|
---|
274 | for (unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
275 | {
|
---|
276 | Ogre::SubMesh* subMesh = mMesh->get()->getSubMesh(i);
|
---|
277 | Ogre::VertexData* vertexData = subMesh->useSharedVertices ? mMesh->get()->sharedVertexData : subMesh->vertexData;
|
---|
278 |
|
---|
279 | if ((!subMesh->useSharedVertices) || (subMesh->useSharedVertices && !added_shared))
|
---|
280 | {
|
---|
281 | if (subMesh->useSharedVertices)
|
---|
282 | {
|
---|
283 | added_shared = true;
|
---|
284 | shared_offset = current_offset;
|
---|
285 | }
|
---|
286 |
|
---|
287 | const Ogre::VertexElement* normalElem = vertexData->vertexDeclaration->findElementBySemantic(Ogre::VES_NORMAL);
|
---|
288 | Ogre::HardwareVertexBufferSharedPtr vbuf = vertexData->vertexBufferBinding->getBuffer(normalElem->getSource());
|
---|
289 | unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
290 | float* pReal;
|
---|
291 |
|
---|
292 | for(size_t j = 0; j < vertexData->vertexCount; ++j, vertex += vbuf->getVertexSize())
|
---|
293 | {
|
---|
294 | normalElem->baseVertexPointerToElement(vertex, &pReal);
|
---|
295 | Ogre::Vector3 normal(pReal[0], pReal[1], pReal[2]);
|
---|
296 |
|
---|
297 | if (!mergeSubMeshes)
|
---|
298 | {
|
---|
299 | this->getSubEntity(i)->setNormal(j,normal);
|
---|
300 | }
|
---|
301 | else
|
---|
302 | {
|
---|
303 | this->getSubEntity(0)->setNormal(j,normal);
|
---|
304 | }
|
---|
305 | }
|
---|
306 | vbuf->unlock();
|
---|
307 | next_offset += vertexData->vertexCount;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | current_offset = next_offset;
|
---|
311 | }
|
---|
312 |
|
---|
313 | void Entity::getMeshVertexColours(bool mergeSubMeshes)
|
---|
314 | {
|
---|
315 | bool added_shared = false;
|
---|
316 | size_t current_offset = 0;
|
---|
317 | size_t shared_offset = 0;
|
---|
318 | size_t next_offset = 0;
|
---|
319 |
|
---|
320 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
321 | sincronizeNumSubEntities();
|
---|
322 |
|
---|
323 | added_shared = false;
|
---|
324 | for (unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
325 | {
|
---|
326 | Ogre::SubMesh* subMesh = mMesh->get()->getSubMesh(i);
|
---|
327 | Ogre::VertexData* vertexData = subMesh->useSharedVertices ? mMesh->get()->sharedVertexData : subMesh->vertexData;
|
---|
328 |
|
---|
329 | if ((!subMesh->useSharedVertices) || (subMesh->useSharedVertices && !added_shared))
|
---|
330 | {
|
---|
331 | if (subMesh->useSharedVertices)
|
---|
332 | {
|
---|
333 | added_shared = true;
|
---|
334 | shared_offset = current_offset;
|
---|
335 | }
|
---|
336 |
|
---|
337 | const Ogre::VertexElement* colourElem = vertexData->vertexDeclaration->findElementBySemantic(Ogre::VES_DIFFUSE);
|
---|
338 | if (colourElem)
|
---|
339 | {
|
---|
340 | //Ogre::LogManager::getSingleton().logMessage("Has vertex colours!");
|
---|
341 | Ogre::HardwareVertexBufferSharedPtr vbuf = vertexData->vertexBufferBinding->getBuffer(colourElem->getSource());
|
---|
342 | unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
343 | Ogre::ARGB *pSrcARGB;
|
---|
344 |
|
---|
345 | for(size_t j = 0; j < vertexData->vertexCount; ++j, vertex += vbuf->getVertexSize())
|
---|
346 | {
|
---|
347 | if (colourElem)
|
---|
348 | {
|
---|
349 | colourElem->baseVertexPointerToElement(vertex, &pSrcARGB);
|
---|
350 |
|
---|
351 | Ogre::ColourValue colour;
|
---|
352 | Ogre::ARGB rc = *pSrcARGB++;
|
---|
353 |
|
---|
354 | colour.b = (rc & 0xFF) / 255.0f; rc >>= 8;
|
---|
355 | colour.g = (rc & 0xFF) / 255.0f; rc >>= 8;
|
---|
356 | colour.r = (rc & 0xFF) / 255.0f; rc >>= 8;
|
---|
357 | colour.a = (rc & 0xFF) / 255.0f;
|
---|
358 |
|
---|
359 | //colour.setAsARGB(pSrcARGB[0]);
|
---|
360 |
|
---|
361 | if (!mergeSubMeshes)
|
---|
362 | {
|
---|
363 | this->getSubEntity(i)->setVertexColor(j,colour.getAsARGB());
|
---|
364 | }
|
---|
365 | else
|
---|
366 | {
|
---|
367 | this->getSubEntity(0)->setVertexColor(j,colour.getAsARGB());
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 | vbuf->unlock();
|
---|
372 | next_offset += vertexData->vertexCount;
|
---|
373 |
|
---|
374 | if (!mergeSubMeshes)
|
---|
375 | {
|
---|
376 | this->getSubEntity(i)->enableVertexColors(true);
|
---|
377 | }
|
---|
378 | else
|
---|
379 | {
|
---|
380 | this->getSubEntity(0)->enableVertexColors(true);
|
---|
381 | }
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|
385 | current_offset = next_offset;
|
---|
386 | }
|
---|
387 |
|
---|
388 | void Entity::getMeshTexCoords(bool mergeSubMeshes)
|
---|
389 | {
|
---|
390 | bool added_shared = false;
|
---|
391 | size_t current_offset = 0;
|
---|
392 | size_t shared_offset = 0;
|
---|
393 | size_t next_offset = 0;
|
---|
394 |
|
---|
395 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
396 | sincronizeNumSubEntities();
|
---|
397 |
|
---|
398 | added_shared = false;
|
---|
399 | for (unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
400 | {
|
---|
401 | Ogre::SubMesh* subMesh = mMesh->get()->getSubMesh(i);
|
---|
402 | Ogre::VertexData* vertexData = subMesh->useSharedVertices ? mMesh->get()->sharedVertexData : subMesh->vertexData;
|
---|
403 |
|
---|
404 | if ((!subMesh->useSharedVertices) || (subMesh->useSharedVertices && !added_shared))
|
---|
405 | {
|
---|
406 | if (subMesh->useSharedVertices)
|
---|
407 | {
|
---|
408 | added_shared = true;
|
---|
409 | shared_offset = current_offset;
|
---|
410 | }
|
---|
411 |
|
---|
412 | const Ogre::VertexElement* texcoordElem = NULL;
|
---|
413 | texcoordElem = vertexData->vertexDeclaration->findElementBySemantic(Ogre::VES_TEXTURE_COORDINATES);
|
---|
414 | if (texcoordElem != NULL)
|
---|
415 | {
|
---|
416 | Ogre::HardwareVertexBufferSharedPtr vbuf = vertexData->vertexBufferBinding->getBuffer(texcoordElem->getSource());
|
---|
417 | unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
418 |
|
---|
419 | Ogre::VertexDeclaration* decl = vertexData->vertexDeclaration;
|
---|
420 | Ogre::VertexBufferBinding* bind = vertexData->vertexBufferBinding;
|
---|
421 | Ogre::VertexBufferBinding::VertexBufferBindingMap::const_iterator b, bend;
|
---|
422 | bend = bind->getBindings().end();
|
---|
423 | // Iterate over buffers
|
---|
424 | for (b = bind->getBindings().begin(); b != bend; ++b)
|
---|
425 | {
|
---|
426 | const Ogre::HardwareVertexBufferSharedPtr vbuf = b->second;
|
---|
427 | unsigned short bufferIdx = b->first;
|
---|
428 | // Get all the elements that relate to this buffer
|
---|
429 | Ogre::VertexDeclaration::VertexElementList elems = decl->findElementsBySource(bufferIdx);
|
---|
430 | Ogre::VertexDeclaration::VertexElementList::iterator it, itend;
|
---|
431 | itend = elems.end();
|
---|
432 |
|
---|
433 | // Skim over the elements to set up the general data
|
---|
434 | unsigned short numTextureCoords = 0;
|
---|
435 | std::vector<unsigned int> baseTexCoordSetIndex;
|
---|
436 | for (it = elems.begin(); it != itend; ++it)
|
---|
437 | {
|
---|
438 | Ogre::VertexElement& elem = *it;
|
---|
439 | if (elem.getSemantic() == Ogre::VES_TEXTURE_COORDINATES)
|
---|
440 | {
|
---|
441 | if (numTextureCoords == 0)
|
---|
442 | {
|
---|
443 | baseTexCoordSetIndex.push_back(0);
|
---|
444 | baseTexCoordSetIndex.push_back(Ogre::VertexElement::getTypeCount(elem.getType()));
|
---|
445 | }
|
---|
446 | ++numTextureCoords;
|
---|
447 | }
|
---|
448 | }
|
---|
449 | elems.clear();
|
---|
450 |
|
---|
451 | // There is _no_ baseVertexPointerToElement() which takes an Ogre::Real or a double
|
---|
452 | // as a second argument. So make it float, to avoid trouble when Ogre::Real will be
|
---|
453 | // compiled/typedefed as double:
|
---|
454 | // Ogre::Real* pReal;
|
---|
455 | float* pReal;
|
---|
456 |
|
---|
457 | unsigned int baseTexCoord = 0;
|
---|
458 | unsigned int texCoordSet = 0;
|
---|
459 | unsigned int dimBaseTexCoord = 0;
|
---|
460 | for (unsigned int texCoordSet = 0; texCoordSet < numTextureCoords; texCoordSet++)
|
---|
461 | {
|
---|
462 | for (unsigned int iTexCoordSet = 0; iTexCoordSet <= texCoordSet; iTexCoordSet++)
|
---|
463 | {
|
---|
464 | baseTexCoord = baseTexCoord + baseTexCoordSetIndex[iTexCoordSet];
|
---|
465 | }
|
---|
466 | unsigned short dimTexCoord = (unsigned short)baseTexCoordSetIndex[texCoordSet+1];
|
---|
467 | for (size_t j = 0; j < vertexData->vertexCount; j++, vertex += vbuf->getVertexSize())
|
---|
468 | {
|
---|
469 | texcoordElem->baseVertexPointerToElement(vertex, &pReal);
|
---|
470 | Ogre::Vector3 texcoord;
|
---|
471 | if (baseTexCoordSetIndex[texCoordSet+1] == 2)
|
---|
472 | {
|
---|
473 | texcoord = Ogre::Vector3(pReal[baseTexCoord + 0], pReal[baseTexCoord + 1], 0.0);
|
---|
474 | dimBaseTexCoord = 2;
|
---|
475 | }
|
---|
476 | else if (baseTexCoordSetIndex[texCoordSet + 1] == 3)
|
---|
477 | {
|
---|
478 | texcoord = Ogre::Vector3(pReal[baseTexCoord + 0], pReal[baseTexCoord + 1], pReal[baseTexCoord + 2]);
|
---|
479 | dimBaseTexCoord = 3;
|
---|
480 | }
|
---|
481 |
|
---|
482 | if (!mergeSubMeshes)
|
---|
483 | {
|
---|
484 | this->getSubEntity(i)->setTexCoord(j,texCoordSet,texcoord);
|
---|
485 | }
|
---|
486 | else
|
---|
487 | {
|
---|
488 | this->getSubEntity(0)->setTexCoord(j,texCoordSet,texcoord);
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | if (!mergeSubMeshes)
|
---|
493 | {
|
---|
494 | this->getSubEntity(i)->addTextureCoordSet(dimBaseTexCoord);
|
---|
495 | }
|
---|
496 | else
|
---|
497 | {
|
---|
498 | if (i == 0)
|
---|
499 | {
|
---|
500 | this->getSubEntity(0)->addTextureCoordSet(dimBaseTexCoord);
|
---|
501 | }
|
---|
502 | }
|
---|
503 | }
|
---|
504 | }
|
---|
505 | vbuf->unlock();
|
---|
506 | next_offset += vertexData->vertexCount;
|
---|
507 | }
|
---|
508 | }
|
---|
509 | }
|
---|
510 | current_offset = next_offset;
|
---|
511 | }
|
---|
512 |
|
---|
513 | void Entity::getMeshFacesVerticesID(bool mergeSubMeshes)
|
---|
514 | {
|
---|
515 | bool added_shared = false;
|
---|
516 | size_t current_offset = 0;
|
---|
517 | size_t shared_offset = 0;
|
---|
518 | size_t next_offset = 0;
|
---|
519 | size_t index_offset = 0;
|
---|
520 |
|
---|
521 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
522 | sincronizeNumSubEntities();
|
---|
523 |
|
---|
524 | added_shared = false;
|
---|
525 |
|
---|
526 | // Run through the submeshes again, adding the data into the arrays
|
---|
527 | for ( unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); ++i)
|
---|
528 | {
|
---|
529 | Ogre::SubMesh* submesh = mMesh->get()->getSubMesh(i);
|
---|
530 |
|
---|
531 | Ogre::VertexData* vertex_data = submesh->useSharedVertices ? mMesh->get()->sharedVertexData : submesh->vertexData;
|
---|
532 |
|
---|
533 | if((!submesh->useSharedVertices)||(submesh->useSharedVertices && !added_shared))
|
---|
534 | {
|
---|
535 | if(submesh->useSharedVertices)
|
---|
536 | {
|
---|
537 | added_shared = true;
|
---|
538 | shared_offset = current_offset;
|
---|
539 | }
|
---|
540 | const Ogre::VertexElement* texcoordElem =
|
---|
541 | vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
|
---|
542 |
|
---|
543 | Ogre::HardwareVertexBufferSharedPtr vbuf =
|
---|
544 | vertex_data->vertexBufferBinding->getBuffer(texcoordElem->getSource());
|
---|
545 |
|
---|
546 | unsigned char* vertex =
|
---|
547 | static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
548 |
|
---|
549 | vbuf->unlock();
|
---|
550 | next_offset += vertex_data->vertexCount;
|
---|
551 | }
|
---|
552 |
|
---|
553 | Ogre::IndexData* index_data = submesh->indexData;
|
---|
554 | size_t numTris = index_data->indexCount / 3;
|
---|
555 | Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
|
---|
556 |
|
---|
557 | bool use32bitindexes = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
|
---|
558 |
|
---|
559 | unsigned long* pLong = static_cast<unsigned long*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
560 | unsigned short* pShort = reinterpret_cast<unsigned short*>(pLong);
|
---|
561 |
|
---|
562 | size_t offset = (submesh->useSharedVertices)? shared_offset : current_offset;
|
---|
563 |
|
---|
564 |
|
---|
565 | if ( use32bitindexes )
|
---|
566 | {
|
---|
567 | for ( size_t k = 0; k < numTris*3; k=k+3)
|
---|
568 | {
|
---|
569 | if (!mergeSubMeshes)
|
---|
570 | {
|
---|
571 | Ogre::Vector3 indices(k,k+1,k+2);
|
---|
572 | this->getSubEntity(i)->addFaceVerticesIDs(indices);
|
---|
573 | }
|
---|
574 | else
|
---|
575 | {
|
---|
576 | Ogre::Vector3 indices = Ogre::Vector3(pLong[k] + static_cast<unsigned long>(offset),pLong[k+1] + static_cast<unsigned long>(offset),pLong[k+2] + static_cast<unsigned long>(offset));
|
---|
577 | this->getSubEntity(0)->addFaceVerticesIDs(indices);
|
---|
578 | }
|
---|
579 | index_offset++;
|
---|
580 | }
|
---|
581 | }
|
---|
582 | else
|
---|
583 | {
|
---|
584 | for ( size_t k = 0; k < numTris*3; k=k+3)
|
---|
585 | {
|
---|
586 | if (!mergeSubMeshes)
|
---|
587 | {
|
---|
588 | Ogre::Vector3 indices(k,k+1,k+2);
|
---|
589 | this->getSubEntity(i)->addFaceVerticesIDs(indices);
|
---|
590 | }
|
---|
591 | else
|
---|
592 | {
|
---|
593 | Ogre::Vector3 indices = Ogre::Vector3(static_cast<unsigned long>(pShort[k]) + static_cast<unsigned long>(offset),static_cast<unsigned long>(pShort[k+1]) + static_cast<unsigned long>(offset),static_cast<unsigned long>(pShort[k+2]) + static_cast<unsigned long>(offset));
|
---|
594 | this->getSubEntity(0)->addFaceVerticesIDs(indices);
|
---|
595 | }
|
---|
596 | index_offset++;
|
---|
597 | }
|
---|
598 | }
|
---|
599 |
|
---|
600 | ibuf->unlock();
|
---|
601 | current_offset = next_offset;
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | unsigned int Entity::getNumSubEntities()
|
---|
606 | {
|
---|
607 | return mSubEntityList.size();
|
---|
608 | }
|
---|
609 |
|
---|
610 | void Entity::mergeSubEntities()
|
---|
611 | {
|
---|
612 | if (mSubEntityList.size() > 1)
|
---|
613 | {
|
---|
614 | unsigned int currentMaxNumVertices = mSubEntityList[0]->getNumVertices();
|
---|
615 | unsigned int iIDVertex = currentMaxNumVertices;
|
---|
616 | for (unsigned int iSubEntity = 1; iSubEntity < mSubEntityList.size(); iSubEntity++)
|
---|
617 | {
|
---|
618 | if (mSubEntityList[iSubEntity]->getNumTexCoordSets() == mSubEntityList[0]->getNumTexCoordSets())
|
---|
619 | {
|
---|
620 | for (unsigned int iVertex = 0; iVertex < mSubEntityList[iSubEntity]->getNumVertices(); iVertex++)
|
---|
621 | {
|
---|
622 | mSubEntityList[0]->addUniqueVertex(mSubEntityList[iSubEntity]->getUniqueVertex(iVertex));
|
---|
623 | }
|
---|
624 |
|
---|
625 | // Vertices IDs go from [0,NUMVERT-1] -> First new vertex should be NUMVERT...
|
---|
626 | for (unsigned int iFaceVerticesIDs = 0; iFaceVerticesIDs < mSubEntityList[iSubEntity]->getNumFaces(); iFaceVerticesIDs++)
|
---|
627 | {
|
---|
628 | mSubEntityList[0]->addFaceVerticesIDs(Ogre::Vector3(iIDVertex,iIDVertex+1,iIDVertex+2));
|
---|
629 | iIDVertex = iIDVertex + 3;
|
---|
630 | }
|
---|
631 | }
|
---|
632 | else
|
---|
633 | {
|
---|
634 | Ogre::LogManager::getSingleton().logMessage("Error: The number of texture coord. sets is not the same in all the subentities...");
|
---|
635 | }
|
---|
636 | }
|
---|
637 |
|
---|
638 | for (unsigned int iSubEntity = 1; iSubEntity < mSubEntityList.size(); iSubEntity)
|
---|
639 | {
|
---|
640 | mSubEntityList.erase(mSubEntityList.begin()+iSubEntity);
|
---|
641 | }
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | }
|
---|