You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

678 line
21 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Implementation of the SplitLargeMeshes postprocessing step
  34. */
  35. #include "AssimpPCH.h"
  36. // internal headers of the post-processing framework
  37. #include "SplitLargeMeshes.h"
  38. #include "ProcessHelper.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. SplitLargeMeshesProcess_Triangle::SplitLargeMeshesProcess_Triangle()
  42. {
  43. LIMIT = AI_SLM_DEFAULT_MAX_TRIANGLES;
  44. }
  45. // ------------------------------------------------------------------------------------------------
  46. SplitLargeMeshesProcess_Triangle::~SplitLargeMeshesProcess_Triangle()
  47. {
  48. // nothing to do here
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. // Returns whether the processing step is present in the given flag field.
  52. bool SplitLargeMeshesProcess_Triangle::IsActive( unsigned int pFlags) const
  53. {
  54. return (pFlags & aiProcess_SplitLargeMeshes) != 0;
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. // Executes the post processing step on the given imported data.
  58. void SplitLargeMeshesProcess_Triangle::Execute( aiScene* pScene)
  59. {
  60. if (0xffffffff == this->LIMIT)return;
  61. DefaultLogger::get()->debug("SplitLargeMeshesProcess_Triangle begin");
  62. std::vector<std::pair<aiMesh*, unsigned int> > avList;
  63. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  64. this->SplitMesh(a, pScene->mMeshes[a],avList);
  65. if (avList.size() != pScene->mNumMeshes)
  66. {
  67. // it seems something has been split. rebuild the mesh list
  68. delete[] pScene->mMeshes;
  69. pScene->mNumMeshes = (unsigned int)avList.size();
  70. pScene->mMeshes = new aiMesh*[avList.size()];
  71. for (unsigned int i = 0; i < avList.size();++i)
  72. pScene->mMeshes[i] = avList[i].first;
  73. // now we need to update all nodes
  74. this->UpdateNode(pScene->mRootNode,avList);
  75. DefaultLogger::get()->info("SplitLargeMeshesProcess_Triangle finished. Meshes have been split");
  76. }
  77. else DefaultLogger::get()->debug("SplitLargeMeshesProcess_Triangle finished. There was nothing to do");
  78. return;
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // Setup properties
  82. void SplitLargeMeshesProcess_Triangle::SetupProperties( const Importer* pImp)
  83. {
  84. // get the current value of the split property
  85. this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES);
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // Update a node after some meshes have been split
  89. void SplitLargeMeshesProcess_Triangle::UpdateNode(aiNode* pcNode,
  90. const std::vector<std::pair<aiMesh*, unsigned int> >& avList)
  91. {
  92. // for every index in out list build a new entry
  93. std::vector<unsigned int> aiEntries;
  94. aiEntries.reserve(pcNode->mNumMeshes + 1);
  95. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i)
  96. {
  97. for (unsigned int a = 0; a < avList.size();++a)
  98. {
  99. if (avList[a].second == pcNode->mMeshes[i])
  100. {
  101. aiEntries.push_back(a);
  102. }
  103. }
  104. }
  105. // now build the new list
  106. delete pcNode->mMeshes;
  107. pcNode->mNumMeshes = (unsigned int)aiEntries.size();
  108. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  109. for (unsigned int b = 0; b < pcNode->mNumMeshes;++b)
  110. pcNode->mMeshes[b] = aiEntries[b];
  111. // recusively update all other nodes
  112. for (unsigned int i = 0; i < pcNode->mNumChildren;++i)
  113. {
  114. UpdateNode ( pcNode->mChildren[i], avList );
  115. }
  116. return;
  117. }
  118. // ------------------------------------------------------------------------------------------------
  119. // Executes the post processing step on the given imported data.
  120. void SplitLargeMeshesProcess_Triangle::SplitMesh(
  121. unsigned int a,
  122. aiMesh* pMesh,
  123. std::vector<std::pair<aiMesh*, unsigned int> >& avList)
  124. {
  125. if (pMesh->mNumFaces > SplitLargeMeshesProcess_Triangle::LIMIT)
  126. {
  127. DefaultLogger::get()->info("Mesh exceeds the triangle limit. It will be split ...");
  128. // we need to split this mesh into sub meshes
  129. // determine the size of a submesh
  130. const unsigned int iSubMeshes = (pMesh->mNumFaces / LIMIT) + 1;
  131. const unsigned int iOutFaceNum = pMesh->mNumFaces / iSubMeshes;
  132. const unsigned int iOutVertexNum = iOutFaceNum * 3;
  133. // now generate all submeshes
  134. for (unsigned int i = 0; i < iSubMeshes;++i)
  135. {
  136. aiMesh* pcMesh = new aiMesh;
  137. pcMesh->mNumFaces = iOutFaceNum;
  138. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  139. // the name carries the adjacency information between the meshes
  140. pcMesh->mName = pMesh->mName;
  141. if (i == iSubMeshes-1)
  142. {
  143. pcMesh->mNumFaces = iOutFaceNum + (
  144. pMesh->mNumFaces - iOutFaceNum * iSubMeshes);
  145. }
  146. // copy the list of faces
  147. pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  148. const unsigned int iBase = iOutFaceNum * i;
  149. // get the total number of indices
  150. unsigned int iCnt = 0;
  151. for (unsigned int p = iBase; p < pcMesh->mNumFaces + iBase;++p)
  152. {
  153. iCnt += pMesh->mFaces[p].mNumIndices;
  154. }
  155. pcMesh->mNumVertices = iCnt;
  156. // allocate storage
  157. if (pMesh->mVertices != NULL)
  158. pcMesh->mVertices = new aiVector3D[iCnt];
  159. if (pMesh->HasNormals())
  160. pcMesh->mNormals = new aiVector3D[iCnt];
  161. if (pMesh->HasTangentsAndBitangents())
  162. {
  163. pcMesh->mTangents = new aiVector3D[iCnt];
  164. pcMesh->mBitangents = new aiVector3D[iCnt];
  165. }
  166. // texture coordinates
  167. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  168. {
  169. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  170. if (pMesh->HasTextureCoords( c))
  171. {
  172. pcMesh->mTextureCoords[c] = new aiVector3D[iCnt];
  173. }
  174. }
  175. // vertex colors
  176. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c)
  177. {
  178. if (pMesh->HasVertexColors( c))
  179. {
  180. pcMesh->mColors[c] = new aiColor4D[iCnt];
  181. }
  182. }
  183. if (pMesh->HasBones())
  184. {
  185. // assume the number of bones won't change in most cases
  186. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  187. // iterate through all bones of the mesh and find those which
  188. // need to be copied to the split mesh
  189. std::vector<aiVertexWeight> avTempWeights;
  190. for (unsigned int p = 0; p < pcMesh->mNumBones;++p)
  191. {
  192. aiBone* const bone = pcMesh->mBones[p];
  193. avTempWeights.clear();
  194. avTempWeights.reserve(bone->mNumWeights / iSubMeshes);
  195. for (unsigned int q = 0; q < bone->mNumWeights;++q)
  196. {
  197. aiVertexWeight& weight = bone->mWeights[q];
  198. if(weight.mVertexId >= iBase && weight.mVertexId < iBase + iOutVertexNum)
  199. {
  200. avTempWeights.push_back(weight);
  201. weight = avTempWeights.back();
  202. weight.mVertexId -= iBase;
  203. }
  204. }
  205. if (!avTempWeights.empty())
  206. {
  207. // we'll need this bone. Copy it ...
  208. aiBone* pc = new aiBone();
  209. pcMesh->mBones[pcMesh->mNumBones++] = pc;
  210. pc->mName = aiString(bone->mName);
  211. pc->mNumWeights = (unsigned int)avTempWeights.size();
  212. pc->mOffsetMatrix = bone->mOffsetMatrix;
  213. // no need to reallocate the array for the last submesh.
  214. // Here we can reuse the (large) source array, although
  215. // we'll waste some memory
  216. if (iSubMeshes-1 == i)
  217. {
  218. pc->mWeights = bone->mWeights;
  219. bone->mWeights = NULL;
  220. }
  221. else pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  222. // copy the weights
  223. ::memcpy(pc->mWeights,&avTempWeights[0],sizeof(aiVertexWeight)*pc->mNumWeights);
  224. }
  225. }
  226. }
  227. // (we will also need to copy the array of indices)
  228. unsigned int iCurrent = 0;
  229. for (unsigned int p = 0; p < pcMesh->mNumFaces;++p)
  230. {
  231. pcMesh->mFaces[p].mNumIndices = 3;
  232. // allocate a new array
  233. const unsigned int iTemp = p + iBase;
  234. const unsigned int iNumIndices = pMesh->mFaces[iTemp].mNumIndices;
  235. // setup face type and number of indices
  236. pcMesh->mFaces[p].mNumIndices = iNumIndices;
  237. unsigned int* pi = pMesh->mFaces[iTemp].mIndices;
  238. unsigned int* piOut = pcMesh->mFaces[p].mIndices = new unsigned int[iNumIndices];
  239. // need to update the output primitive types
  240. switch (iNumIndices)
  241. {
  242. case 1:
  243. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  244. break;
  245. case 2:
  246. pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  247. break;
  248. case 3:
  249. pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  250. break;
  251. default:
  252. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  253. }
  254. // and copy the contents of the old array, offset by current base
  255. for (unsigned int v = 0; v < iNumIndices;++v)
  256. {
  257. unsigned int iIndex = pi[v];
  258. unsigned int iIndexOut = iCurrent++;
  259. piOut[v] = iIndexOut;
  260. // copy positions
  261. if (pMesh->mVertices != NULL)
  262. pcMesh->mVertices[iIndexOut] = pMesh->mVertices[iIndex];
  263. // copy normals
  264. if (pMesh->HasNormals())
  265. pcMesh->mNormals[iIndexOut] = pMesh->mNormals[iIndex];
  266. // copy tangents/bitangents
  267. if (pMesh->HasTangentsAndBitangents())
  268. {
  269. pcMesh->mTangents[iIndexOut] = pMesh->mTangents[iIndex];
  270. pcMesh->mBitangents[iIndexOut] = pMesh->mBitangents[iIndex];
  271. }
  272. // texture coordinates
  273. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  274. {
  275. if (pMesh->HasTextureCoords( c))
  276. pcMesh->mTextureCoords[c][iIndexOut] = pMesh->mTextureCoords[c][iIndex];
  277. }
  278. // vertex colors
  279. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c)
  280. {
  281. if (pMesh->HasVertexColors( c))
  282. pcMesh->mColors[c][iIndexOut] = pMesh->mColors[c][iIndex];
  283. }
  284. }
  285. }
  286. // add the newly created mesh to the list
  287. avList.push_back(std::pair<aiMesh*, unsigned int>(pcMesh,a));
  288. }
  289. // now delete the old mesh data
  290. delete pMesh;
  291. }
  292. else avList.push_back(std::pair<aiMesh*, unsigned int>(pMesh,a));
  293. return;
  294. }
  295. // ------------------------------------------------------------------------------------------------
  296. SplitLargeMeshesProcess_Vertex::SplitLargeMeshesProcess_Vertex()
  297. {
  298. LIMIT = AI_SLM_DEFAULT_MAX_VERTICES;
  299. }
  300. // ------------------------------------------------------------------------------------------------
  301. SplitLargeMeshesProcess_Vertex::~SplitLargeMeshesProcess_Vertex()
  302. {
  303. // nothing to do here
  304. }
  305. // ------------------------------------------------------------------------------------------------
  306. // Returns whether the processing step is present in the given flag field.
  307. bool SplitLargeMeshesProcess_Vertex::IsActive( unsigned int pFlags) const
  308. {
  309. return (pFlags & aiProcess_SplitLargeMeshes) != 0;
  310. }
  311. // ------------------------------------------------------------------------------------------------
  312. // Executes the post processing step on the given imported data.
  313. void SplitLargeMeshesProcess_Vertex::Execute( aiScene* pScene)
  314. {
  315. std::vector<std::pair<aiMesh*, unsigned int> > avList;
  316. if (0xffffffff == this->LIMIT)return;
  317. DefaultLogger::get()->debug("SplitLargeMeshesProcess_Vertex begin");
  318. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  319. this->SplitMesh(a, pScene->mMeshes[a],avList);
  320. if (avList.size() != pScene->mNumMeshes)
  321. {
  322. // it seems something has been split. rebuild the mesh list
  323. delete[] pScene->mMeshes;
  324. pScene->mNumMeshes = (unsigned int)avList.size();
  325. pScene->mMeshes = new aiMesh*[avList.size()];
  326. for (unsigned int i = 0; i < avList.size();++i)
  327. pScene->mMeshes[i] = avList[i].first;
  328. // now we need to update all nodes
  329. SplitLargeMeshesProcess_Triangle::UpdateNode(pScene->mRootNode,avList);
  330. DefaultLogger::get()->info("SplitLargeMeshesProcess_Vertex finished. Meshes have been split");
  331. }
  332. else DefaultLogger::get()->debug("SplitLargeMeshesProcess_Vertex finished. There was nothing to do");
  333. return;
  334. }
  335. // ------------------------------------------------------------------------------------------------
  336. // Setup properties
  337. void SplitLargeMeshesProcess_Vertex::SetupProperties( const Importer* pImp)
  338. {
  339. this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
  340. }
  341. // ------------------------------------------------------------------------------------------------
  342. // Executes the post processing step on the given imported data.
  343. void SplitLargeMeshesProcess_Vertex::SplitMesh(
  344. unsigned int a,
  345. aiMesh* pMesh,
  346. std::vector<std::pair<aiMesh*, unsigned int> >& avList)
  347. {
  348. if (pMesh->mNumVertices > SplitLargeMeshesProcess_Vertex::LIMIT)
  349. {
  350. typedef std::vector< std::pair<unsigned int,float> > VertexWeightTable;
  351. // build a per-vertex weight list if necessary
  352. VertexWeightTable* avPerVertexWeights = ComputeVertexBoneWeightTable(pMesh);
  353. // we need to split this mesh into sub meshes
  354. // determine the estimated size of a submesh
  355. // (this could be too large. Max waste is a single digit percentage)
  356. const unsigned int iSubMeshes = (pMesh->mNumVertices / SplitLargeMeshesProcess_Vertex::LIMIT) + 1;
  357. //const unsigned int iOutVertexNum2 = pMesh->mNumVertices /iSubMeshes;
  358. // create a std::vector<unsigned int> to indicate which vertices
  359. // have already been copied
  360. std::vector<unsigned int> avWasCopied;
  361. avWasCopied.resize(pMesh->mNumVertices,0xFFFFFFFF);
  362. // try to find a good estimate for the number of output faces
  363. // per mesh. Add 12.5% as buffer
  364. unsigned int iEstimatedSize = pMesh->mNumFaces / iSubMeshes;
  365. iEstimatedSize += iEstimatedSize >> 3;
  366. // now generate all submeshes
  367. unsigned int iBase = 0;
  368. while (true)
  369. {
  370. const unsigned int iOutVertexNum = SplitLargeMeshesProcess_Vertex::LIMIT;
  371. aiMesh* pcMesh = new aiMesh;
  372. pcMesh->mNumVertices = 0;
  373. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  374. // the name carries the adjacency information between the meshes
  375. pcMesh->mName = pMesh->mName;
  376. typedef std::vector<aiVertexWeight> BoneWeightList;
  377. if (pMesh->HasBones())
  378. {
  379. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  380. ::memset(pcMesh->mBones,0,sizeof(void*)*pMesh->mNumBones);
  381. }
  382. // clear the temporary helper array
  383. if (iBase)
  384. {
  385. // we can't use memset here we unsigned int needn' be 32 bits
  386. for (std::vector<unsigned int>::iterator
  387. iter = avWasCopied.begin(),end = avWasCopied.end();
  388. iter != end;++iter)
  389. {
  390. (*iter) = 0xffffffff;
  391. }
  392. }
  393. // output vectors
  394. std::vector<aiFace> vFaces;
  395. // reserve enough storage for most cases
  396. if (pMesh->HasPositions())
  397. {
  398. pcMesh->mVertices = new aiVector3D[iOutVertexNum];
  399. }
  400. if (pMesh->HasNormals())
  401. {
  402. pcMesh->mNormals = new aiVector3D[iOutVertexNum];
  403. }
  404. if (pMesh->HasTangentsAndBitangents())
  405. {
  406. pcMesh->mTangents = new aiVector3D[iOutVertexNum];
  407. pcMesh->mBitangents = new aiVector3D[iOutVertexNum];
  408. }
  409. for (unsigned int c = 0; pMesh->HasVertexColors(c);++c)
  410. {
  411. pcMesh->mColors[c] = new aiColor4D[iOutVertexNum];
  412. }
  413. for (unsigned int c = 0; pMesh->HasTextureCoords(c);++c)
  414. {
  415. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  416. pcMesh->mTextureCoords[c] = new aiVector3D[iOutVertexNum];
  417. }
  418. vFaces.reserve(iEstimatedSize);
  419. // (we will also need to copy the array of indices)
  420. while (iBase < pMesh->mNumFaces)
  421. {
  422. // allocate a new array
  423. const unsigned int iNumIndices = pMesh->mFaces[iBase].mNumIndices;
  424. // doesn't catch degenerates but is quite fast
  425. unsigned int iNeed = 0;
  426. for (unsigned int v = 0; v < iNumIndices;++v)
  427. {
  428. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  429. // check whether we do already have this vertex
  430. if (0xFFFFFFFF == avWasCopied[iIndex])
  431. {
  432. iNeed++;
  433. }
  434. }
  435. if (pcMesh->mNumVertices + iNeed > iOutVertexNum)
  436. {
  437. // don't use this face
  438. break;
  439. }
  440. vFaces.push_back(aiFace());
  441. aiFace& rFace = vFaces.back();
  442. // setup face type and number of indices
  443. rFace.mNumIndices = iNumIndices;
  444. rFace.mIndices = new unsigned int[iNumIndices];
  445. // need to update the output primitive types
  446. switch (rFace.mNumIndices)
  447. {
  448. case 1:
  449. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  450. break;
  451. case 2:
  452. pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  453. break;
  454. case 3:
  455. pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  456. break;
  457. default:
  458. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  459. }
  460. // and copy the contents of the old array, offset by current base
  461. for (unsigned int v = 0; v < iNumIndices;++v)
  462. {
  463. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  464. // check whether we do already have this vertex
  465. if (0xFFFFFFFF != avWasCopied[iIndex])
  466. {
  467. rFace.mIndices[v] = avWasCopied[iIndex];
  468. continue;
  469. }
  470. // copy positions
  471. pcMesh->mVertices[pcMesh->mNumVertices] = (pMesh->mVertices[iIndex]);
  472. // copy normals
  473. if (pMesh->HasNormals())
  474. {
  475. pcMesh->mNormals[pcMesh->mNumVertices] = (pMesh->mNormals[iIndex]);
  476. }
  477. // copy tangents/bitangents
  478. if (pMesh->HasTangentsAndBitangents())
  479. {
  480. pcMesh->mTangents[pcMesh->mNumVertices] = (pMesh->mTangents[iIndex]);
  481. pcMesh->mBitangents[pcMesh->mNumVertices] = (pMesh->mBitangents[iIndex]);
  482. }
  483. // texture coordinates
  484. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  485. {
  486. if (pMesh->HasTextureCoords( c))
  487. {
  488. pcMesh->mTextureCoords[c][pcMesh->mNumVertices] = pMesh->mTextureCoords[c][iIndex];
  489. }
  490. }
  491. // vertex colors
  492. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c)
  493. {
  494. if (pMesh->HasVertexColors( c))
  495. {
  496. pcMesh->mColors[c][pcMesh->mNumVertices] = pMesh->mColors[c][iIndex];
  497. }
  498. }
  499. // check whether we have bone weights assigned to this vertex
  500. rFace.mIndices[v] = pcMesh->mNumVertices;
  501. if (avPerVertexWeights)
  502. {
  503. VertexWeightTable& table = avPerVertexWeights[ pcMesh->mNumVertices ];
  504. if( !table.empty() )
  505. {
  506. for (VertexWeightTable::const_iterator
  507. iter = table.begin();
  508. iter != table.end();++iter)
  509. {
  510. // allocate the bone weight array if necessary
  511. BoneWeightList* pcWeightList = (BoneWeightList*)pcMesh->mBones[(*iter).first];
  512. if (!pcWeightList)
  513. {
  514. pcMesh->mBones[(*iter).first] = (aiBone*)(pcWeightList = new BoneWeightList());
  515. }
  516. pcWeightList->push_back(aiVertexWeight(pcMesh->mNumVertices,(*iter).second));
  517. }
  518. }
  519. }
  520. avWasCopied[iIndex] = pcMesh->mNumVertices;
  521. pcMesh->mNumVertices++;
  522. }
  523. iBase++;
  524. if(pcMesh->mNumVertices == iOutVertexNum)
  525. {
  526. // break here. The face is only added if it was complete
  527. break;
  528. }
  529. }
  530. // check which bones we'll need to create for this submesh
  531. if (pMesh->HasBones())
  532. {
  533. aiBone** ppCurrent = pcMesh->mBones;
  534. for (unsigned int k = 0; k < pMesh->mNumBones;++k)
  535. {
  536. // check whether the bone is existing
  537. BoneWeightList* pcWeightList;
  538. if ((pcWeightList = (BoneWeightList*)pcMesh->mBones[k]))
  539. {
  540. aiBone* pcOldBone = pMesh->mBones[k];
  541. aiBone* pcOut;
  542. *ppCurrent++ = pcOut = new aiBone();
  543. pcOut->mName = aiString(pcOldBone->mName);
  544. pcOut->mOffsetMatrix = pcOldBone->mOffsetMatrix;
  545. pcOut->mNumWeights = (unsigned int)pcWeightList->size();
  546. pcOut->mWeights = new aiVertexWeight[pcOut->mNumWeights];
  547. // copy the vertex weights
  548. ::memcpy(pcOut->mWeights,&pcWeightList->operator[](0),
  549. pcOut->mNumWeights * sizeof(aiVertexWeight));
  550. // delete the temporary bone weight list
  551. delete pcWeightList;
  552. pcMesh->mNumBones++;
  553. }
  554. }
  555. }
  556. // copy the face list to the mesh
  557. pcMesh->mFaces = new aiFace[vFaces.size()];
  558. pcMesh->mNumFaces = (unsigned int)vFaces.size();
  559. for (unsigned int p = 0; p < pcMesh->mNumFaces;++p)
  560. pcMesh->mFaces[p] = vFaces[p];
  561. // add the newly created mesh to the list
  562. avList.push_back(std::pair<aiMesh*, unsigned int>(pcMesh,a));
  563. if (iBase == pMesh->mNumFaces)
  564. {
  565. // have all faces ... finish the outer loop, too
  566. break;
  567. }
  568. }
  569. // delete the per-vertex weight list again
  570. delete[] avPerVertexWeights;
  571. // now delete the old mesh data
  572. delete pMesh;
  573. return;
  574. }
  575. avList.push_back(std::pair<aiMesh*, unsigned int>(pMesh,a));
  576. return;
  577. }