[721] | 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]->enableVertexColours(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]->setVertexColour(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 | {
|
---|
[699] | 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;
|
---|
[721] | 78 | unsigned short dimTexCoord = 2;
|
---|
| 79 |
|
---|
| 80 | getMeshPositions(position,orient,scale,mergeSubMeshes);
|
---|
| 81 | getMeshNormals(orient,scale,mergeSubMeshes);
|
---|
| 82 | getMeshVertexColours(mergeSubMeshes);
|
---|
| 83 | getMeshTexCoords(mergeSubMeshes);
|
---|
[936] | 84 | //getMeshFacesVerticesID(mergeSubMeshes);
|
---|
[721] | 85 | }
|
---|
| 86 |
|
---|
| 87 | void Entity::sincronizeNumSubEntities()
|
---|
| 88 | {
|
---|
| 89 | if (this->getNumSubEntities() < mMesh->get()->getNumSubMeshes())
|
---|
[699] | 90 | {
|
---|
[721] | 91 | while (this->getNumSubEntities() < mMesh->get()->getNumSubMeshes())
|
---|
[699] | 92 | {
|
---|
| 93 | this->createSubEntity();
|
---|
| 94 | }
|
---|
[721] | 95 | }
|
---|
| 96 |
|
---|
| 97 | if (this->getNumSubEntities() > mMesh->get()->getNumSubMeshes())
|
---|
[699] | 98 | {
|
---|
[721] | 99 | while (this->getNumSubEntities() > mMesh->get()->getNumSubMeshes())
|
---|
[699] | 100 | {
|
---|
| 101 | this->removeSubEntity(this->getNumSubEntities()-1);
|
---|
| 102 | }
|
---|
[721] | 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void Entity::getMeshPositions(Ogre::Vector3 position, Ogre::Quaternion orient, Ogre::Vector3 scale, bool mergeSubMeshes)
|
---|
[699] | 107 | {
|
---|
| 108 | bool added_shared = false;
|
---|
| 109 | size_t current_offset = 0;
|
---|
| 110 | size_t shared_offset = 0;
|
---|
| 111 | size_t next_offset = 0;
|
---|
[936] | 112 | size_t index_offset = 0;
|
---|
| 113 | size_t vertex_count, index_count;
|
---|
[699] | 114 |
|
---|
[936] | 115 | vertex_count = index_count = 0;
|
---|
| 116 |
|
---|
[721] | 117 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
[699] | 118 | sincronizeNumSubEntities();
|
---|
| 119 |
|
---|
[936] | 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 |
|
---|
[699] | 152 | added_shared = false;
|
---|
[721] | 153 | for (unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
[699] | 154 | {
|
---|
[721] | 155 | Ogre::SubMesh* subMesh = mMesh->get()->getSubMesh(i);
|
---|
| 156 | Ogre::VertexData* vertexData = subMesh->useSharedVertices ? mMesh->get()->sharedVertexData : subMesh->vertexData;
|
---|
[699] | 157 |
|
---|
[936] | 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 |
|
---|
[699] | 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;
|
---|
[936] | 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 | }
|
---|
[699] | 257 | }
|
---|
[936] | 258 |
|
---|
| 259 | delete [] indices;
|
---|
[721] | 260 | }
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | void Entity::getMeshNormals(Ogre::Quaternion orient, Ogre::Vector3 scale, bool mergeSubMeshes)
|
---|
[699] | 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 |
|
---|
[721] | 270 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
[699] | 271 | sincronizeNumSubEntities();
|
---|
| 272 |
|
---|
| 273 | added_shared = false;
|
---|
[721] | 274 | for (unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
[699] | 275 | {
|
---|
[721] | 276 | Ogre::SubMesh* subMesh = mMesh->get()->getSubMesh(i);
|
---|
| 277 | Ogre::VertexData* vertexData = subMesh->useSharedVertices ? mMesh->get()->sharedVertexData : subMesh->vertexData;
|
---|
[699] | 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;
|
---|
[721] | 311 | }
|
---|
| 312 |
|
---|
| 313 | void Entity::getMeshVertexColours(bool mergeSubMeshes)
|
---|
[699] | 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 |
|
---|
[721] | 320 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
[699] | 321 | sincronizeNumSubEntities();
|
---|
| 322 |
|
---|
| 323 | added_shared = false;
|
---|
[721] | 324 | for (unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
[699] | 325 | {
|
---|
[721] | 326 | Ogre::SubMesh* subMesh = mMesh->get()->getSubMesh(i);
|
---|
| 327 | Ogre::VertexData* vertexData = subMesh->useSharedVertices ? mMesh->get()->sharedVertexData : subMesh->vertexData;
|
---|
[699] | 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));
|
---|
[936] | 343 | Ogre::ARGB *pSrcARGB;
|
---|
[699] | 344 |
|
---|
| 345 | for(size_t j = 0; j < vertexData->vertexCount; ++j, vertex += vbuf->getVertexSize())
|
---|
| 346 | {
|
---|
| 347 | if (colourElem)
|
---|
| 348 | {
|
---|
[936] | 349 | colourElem->baseVertexPointerToElement(vertex, &pSrcARGB);
|
---|
[699] | 350 |
|
---|
[936] | 351 | Ogre::ColourValue colour;
|
---|
[950] | 352 | //colour.setAsARGB((Ogre::ARGB)(*pSrcARGB) & 0xFF);
|
---|
| 353 | colour.setAsARGB((Ogre::ARGB)(pSrcARGB[0]));
|
---|
[699] | 354 |
|
---|
[936] | 355 | if (!mergeSubMeshes)
|
---|
| 356 | {
|
---|
| 357 | this->getSubEntity(i)->setVertexColour(j,colour.getAsARGB());
|
---|
| 358 | }
|
---|
| 359 | else
|
---|
| 360 | {
|
---|
| 361 | this->getSubEntity(0)->setVertexColour(j,colour.getAsARGB());
|
---|
| 362 | }
|
---|
[699] | 363 | }
|
---|
| 364 | }
|
---|
| 365 | vbuf->unlock();
|
---|
| 366 | next_offset += vertexData->vertexCount;
|
---|
| 367 |
|
---|
| 368 | if (!mergeSubMeshes)
|
---|
| 369 | {
|
---|
| 370 | this->getSubEntity(i)->enableVertexColours(true);
|
---|
| 371 | }
|
---|
| 372 | else
|
---|
| 373 | {
|
---|
| 374 | this->getSubEntity(0)->enableVertexColours(true);
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 | current_offset = next_offset;
|
---|
[721] | 380 | }
|
---|
| 381 |
|
---|
| 382 | void Entity::getMeshTexCoords(bool mergeSubMeshes)
|
---|
[699] | 383 | {
|
---|
| 384 | bool added_shared = false;
|
---|
| 385 | size_t current_offset = 0;
|
---|
| 386 | size_t shared_offset = 0;
|
---|
| 387 | size_t next_offset = 0;
|
---|
| 388 |
|
---|
[721] | 389 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
[699] | 390 | sincronizeNumSubEntities();
|
---|
| 391 |
|
---|
| 392 | added_shared = false;
|
---|
[721] | 393 | for (unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); i++)
|
---|
[699] | 394 | {
|
---|
[721] | 395 | Ogre::SubMesh* subMesh = mMesh->get()->getSubMesh(i);
|
---|
| 396 | Ogre::VertexData* vertexData = subMesh->useSharedVertices ? mMesh->get()->sharedVertexData : subMesh->vertexData;
|
---|
[699] | 397 |
|
---|
| 398 | if ((!subMesh->useSharedVertices) || (subMesh->useSharedVertices && !added_shared))
|
---|
| 399 | {
|
---|
| 400 | if (subMesh->useSharedVertices)
|
---|
| 401 | {
|
---|
| 402 | added_shared = true;
|
---|
| 403 | shared_offset = current_offset;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
[936] | 406 | const Ogre::VertexElement* texcoordElem = NULL;
|
---|
| 407 | texcoordElem = vertexData->vertexDeclaration->findElementBySemantic(Ogre::VES_TEXTURE_COORDINATES);
|
---|
| 408 | if (texcoordElem != NULL)
|
---|
[699] | 409 | {
|
---|
[936] | 410 | Ogre::HardwareVertexBufferSharedPtr vbuf = vertexData->vertexBufferBinding->getBuffer(texcoordElem->getSource());
|
---|
| 411 | unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
[699] | 412 |
|
---|
[936] | 413 | Ogre::VertexDeclaration* decl = vertexData->vertexDeclaration;
|
---|
| 414 | Ogre::VertexBufferBinding* bind = vertexData->vertexBufferBinding;
|
---|
| 415 | Ogre::VertexBufferBinding::VertexBufferBindingMap::const_iterator b, bend;
|
---|
| 416 | bend = bind->getBindings().end();
|
---|
| 417 | // Iterate over buffers
|
---|
| 418 | for (b = bind->getBindings().begin(); b != bend; ++b)
|
---|
[699] | 419 | {
|
---|
[936] | 420 | const Ogre::HardwareVertexBufferSharedPtr vbuf = b->second;
|
---|
| 421 | unsigned short bufferIdx = b->first;
|
---|
| 422 | // Get all the elements that relate to this buffer
|
---|
| 423 | Ogre::VertexDeclaration::VertexElementList elems = decl->findElementsBySource(bufferIdx);
|
---|
| 424 | Ogre::VertexDeclaration::VertexElementList::iterator it, itend;
|
---|
| 425 | itend = elems.end();
|
---|
| 426 |
|
---|
| 427 | // Skim over the elements to set up the general data
|
---|
| 428 | unsigned short numTextureCoords = 0;
|
---|
| 429 | std::vector<unsigned int> baseTexCoordSetIndex;
|
---|
| 430 | for (it = elems.begin(); it != itend; ++it)
|
---|
[699] | 431 | {
|
---|
[936] | 432 | Ogre::VertexElement& elem = *it;
|
---|
| 433 | if (elem.getSemantic() == Ogre::VES_TEXTURE_COORDINATES)
|
---|
[699] | 434 | {
|
---|
[936] | 435 | if (numTextureCoords == 0)
|
---|
| 436 | {
|
---|
| 437 | baseTexCoordSetIndex.push_back(0);
|
---|
| 438 | baseTexCoordSetIndex.push_back(Ogre::VertexElement::getTypeCount(elem.getType()));
|
---|
| 439 | }
|
---|
| 440 | ++numTextureCoords;
|
---|
[699] | 441 | }
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[936] | 444 | // There is _no_ baseVertexPointerToElement() which takes an Ogre::Real or a double
|
---|
| 445 | // as a second argument. So make it float, to avoid trouble when Ogre::Real will be
|
---|
| 446 | // compiled/typedefed as double:
|
---|
| 447 | // Ogre::Real* pReal;
|
---|
| 448 | float* pReal;
|
---|
[699] | 449 |
|
---|
[936] | 450 | unsigned int baseTexCoord = 0;
|
---|
| 451 | unsigned int texCoordSet = 0;
|
---|
| 452 | unsigned int dimBaseTexCoord = 0;
|
---|
| 453 | for (unsigned int texCoordSet = 0; texCoordSet < numTextureCoords; texCoordSet++)
|
---|
[699] | 454 | {
|
---|
[936] | 455 | for (unsigned int iTexCoordSet = 0; iTexCoordSet <= texCoordSet; iTexCoordSet++)
|
---|
[699] | 456 | {
|
---|
[936] | 457 | baseTexCoord = baseTexCoord + baseTexCoordSetIndex[iTexCoordSet];
|
---|
[699] | 458 | }
|
---|
[936] | 459 | unsigned short dimTexCoord = (unsigned short)baseTexCoordSetIndex[texCoordSet+1];
|
---|
| 460 | for (size_t j = 0; j < vertexData->vertexCount; j++, vertex += vbuf->getVertexSize())
|
---|
[699] | 461 | {
|
---|
[936] | 462 | texcoordElem->baseVertexPointerToElement(vertex, &pReal);
|
---|
| 463 | Ogre::Vector3 texcoord;
|
---|
| 464 | if (baseTexCoordSetIndex[texCoordSet+1] == 2)
|
---|
| 465 | {
|
---|
| 466 | texcoord = Ogre::Vector3(pReal[baseTexCoord + 0], pReal[baseTexCoord + 1], 0.0);
|
---|
| 467 | dimBaseTexCoord = 2;
|
---|
| 468 | }
|
---|
| 469 | else if (baseTexCoordSetIndex[texCoordSet + 1] == 3)
|
---|
| 470 | {
|
---|
| 471 | texcoord = Ogre::Vector3(pReal[baseTexCoord + 0], pReal[baseTexCoord + 1], pReal[baseTexCoord + 2]);
|
---|
| 472 | dimBaseTexCoord = 3;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | if (!mergeSubMeshes)
|
---|
| 476 | {
|
---|
| 477 | this->getSubEntity(i)->setTexCoord(j,texCoordSet,texcoord);
|
---|
| 478 | }
|
---|
| 479 | else
|
---|
| 480 | {
|
---|
| 481 | this->getSubEntity(0)->setTexCoord(j,texCoordSet,texcoord);
|
---|
| 482 | }
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[699] | 485 | if (!mergeSubMeshes)
|
---|
| 486 | {
|
---|
[936] | 487 | this->getSubEntity(i)->addTextureCoordSet(dimBaseTexCoord);
|
---|
[699] | 488 | }
|
---|
| 489 | else
|
---|
| 490 | {
|
---|
[936] | 491 | if (i == 0)
|
---|
| 492 | {
|
---|
| 493 | this->getSubEntity(0)->addTextureCoordSet(dimBaseTexCoord);
|
---|
| 494 | }
|
---|
[699] | 495 | }
|
---|
[936] | 496 | }
|
---|
| 497 | }
|
---|
| 498 | vbuf->unlock();
|
---|
| 499 | next_offset += vertexData->vertexCount;
|
---|
| 500 | }
|
---|
[699] | 501 | }
|
---|
| 502 | }
|
---|
| 503 | current_offset = next_offset;
|
---|
[721] | 504 | }
|
---|
| 505 |
|
---|
| 506 | void Entity::getMeshFacesVerticesID(bool mergeSubMeshes)
|
---|
| 507 | {
|
---|
[699] | 508 | bool added_shared = false;
|
---|
| 509 | size_t current_offset = 0;
|
---|
| 510 | size_t shared_offset = 0;
|
---|
| 511 | size_t next_offset = 0;
|
---|
| 512 | size_t index_offset = 0;
|
---|
| 513 |
|
---|
[721] | 514 | unsigned int numSubMeshes = mMesh->get()->getNumSubMeshes();
|
---|
[699] | 515 | sincronizeNumSubEntities();
|
---|
| 516 |
|
---|
| 517 | added_shared = false;
|
---|
| 518 |
|
---|
| 519 | // Run through the submeshes again, adding the data into the arrays
|
---|
[721] | 520 | for ( unsigned short i = 0; i < mMesh->get()->getNumSubMeshes(); ++i)
|
---|
[699] | 521 | {
|
---|
[721] | 522 | Ogre::SubMesh* submesh = mMesh->get()->getSubMesh(i);
|
---|
[699] | 523 |
|
---|
[721] | 524 | Ogre::VertexData* vertex_data = submesh->useSharedVertices ? mMesh->get()->sharedVertexData : submesh->vertexData;
|
---|
[699] | 525 |
|
---|
| 526 | if((!submesh->useSharedVertices)||(submesh->useSharedVertices && !added_shared))
|
---|
| 527 | {
|
---|
| 528 | if(submesh->useSharedVertices)
|
---|
| 529 | {
|
---|
| 530 | added_shared = true;
|
---|
| 531 | shared_offset = current_offset;
|
---|
| 532 | }
|
---|
| 533 | const Ogre::VertexElement* texcoordElem =
|
---|
| 534 | vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
|
---|
| 535 |
|
---|
| 536 | Ogre::HardwareVertexBufferSharedPtr vbuf =
|
---|
| 537 | vertex_data->vertexBufferBinding->getBuffer(texcoordElem->getSource());
|
---|
| 538 |
|
---|
| 539 | unsigned char* vertex =
|
---|
| 540 | static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
| 541 |
|
---|
| 542 | vbuf->unlock();
|
---|
| 543 | next_offset += vertex_data->vertexCount;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | Ogre::IndexData* index_data = submesh->indexData;
|
---|
| 547 | size_t numTris = index_data->indexCount / 3;
|
---|
| 548 | Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
|
---|
| 549 |
|
---|
| 550 | bool use32bitindexes = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
|
---|
| 551 |
|
---|
| 552 | unsigned long* pLong = static_cast<unsigned long*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
|
---|
| 553 | unsigned short* pShort = reinterpret_cast<unsigned short*>(pLong);
|
---|
| 554 |
|
---|
| 555 | size_t offset = (submesh->useSharedVertices)? shared_offset : current_offset;
|
---|
| 556 |
|
---|
| 557 |
|
---|
| 558 | if ( use32bitindexes )
|
---|
| 559 | {
|
---|
| 560 | for ( size_t k = 0; k < numTris*3; k=k+3)
|
---|
| 561 | {
|
---|
| 562 | if (!mergeSubMeshes)
|
---|
| 563 | {
|
---|
| 564 | Ogre::Vector3 indices(k,k+1,k+2);
|
---|
| 565 | this->getSubEntity(i)->addFaceVerticesIDs(indices);
|
---|
| 566 | }
|
---|
| 567 | else
|
---|
| 568 | {
|
---|
| 569 | 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));
|
---|
| 570 | this->getSubEntity(0)->addFaceVerticesIDs(indices);
|
---|
| 571 | }
|
---|
| 572 | index_offset++;
|
---|
| 573 | }
|
---|
| 574 | }
|
---|
| 575 | else
|
---|
| 576 | {
|
---|
| 577 | for ( size_t k = 0; k < numTris*3; k=k+3)
|
---|
| 578 | {
|
---|
| 579 | if (!mergeSubMeshes)
|
---|
| 580 | {
|
---|
| 581 | Ogre::Vector3 indices(k,k+1,k+2);
|
---|
| 582 | this->getSubEntity(i)->addFaceVerticesIDs(indices);
|
---|
| 583 | }
|
---|
| 584 | else
|
---|
| 585 | {
|
---|
| 586 | 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));
|
---|
| 587 | this->getSubEntity(0)->addFaceVerticesIDs(indices);
|
---|
| 588 | }
|
---|
| 589 | index_offset++;
|
---|
| 590 | }
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | ibuf->unlock();
|
---|
| 594 | current_offset = next_offset;
|
---|
[721] | 595 | }
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | unsigned int Entity::getNumSubEntities()
|
---|
| 599 | {
|
---|
| 600 | return mSubEntityList.size();
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | void Entity::mergeSubEntities()
|
---|
| 604 | {
|
---|
| 605 | if (mSubEntityList.size() > 1)
|
---|
| 606 | {
|
---|
| 607 | unsigned int currentMaxNumVertices = mSubEntityList[0]->getNumVertices();
|
---|
| 608 | unsigned int iIDVertex = currentMaxNumVertices;
|
---|
| 609 | for (unsigned int iSubEntity = 1; iSubEntity < mSubEntityList.size(); iSubEntity++)
|
---|
| 610 | {
|
---|
| 611 | if (mSubEntityList[iSubEntity]->getNumTexCoordSets() == mSubEntityList[0]->getNumTexCoordSets())
|
---|
| 612 | {
|
---|
| 613 | for (unsigned int iVertex = 0; iVertex < mSubEntityList[iSubEntity]->getNumVertices(); iVertex++)
|
---|
| 614 | {
|
---|
| 615 | mSubEntityList[0]->addUniqueVertex(mSubEntityList[iSubEntity]->getUniqueVertex(iVertex));
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | // Vertices IDs go from [0,NUMVERT-1] -> First new vertex should be NUMVERT...
|
---|
| 619 | for (unsigned int iFaceVerticesIDs = 0; iFaceVerticesIDs < mSubEntityList[iSubEntity]->getNumFaces(); iFaceVerticesIDs++)
|
---|
| 620 | {
|
---|
| 621 | mSubEntityList[0]->addFaceVerticesIDs(Ogre::Vector3(iIDVertex,iIDVertex+1,iIDVertex+2));
|
---|
| 622 | iIDVertex = iIDVertex + 3;
|
---|
| 623 | }
|
---|
| 624 | }
|
---|
| 625 | else
|
---|
| 626 | {
|
---|
| 627 | Ogre::LogManager::getSingleton().logMessage("Error: The number of texture coord. sets is not the same in all the subentities...");
|
---|
| 628 | }
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | for (unsigned int iSubEntity = 1; iSubEntity < mSubEntityList.size(); iSubEntity)
|
---|
| 632 | {
|
---|
| 633 | mSubEntityList.erase(mSubEntityList.begin()+iSubEntity);
|
---|
| 634 | }
|
---|
| 635 | }
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | }
|
---|