SplitLargeMeshes.cpp 22 KB

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