SplitLargeMeshes.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 "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 splitted. 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 splitted");
  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. if (i == iSubMeshes-1)
  140. {
  141. pcMesh->mNumFaces = iOutFaceNum + (
  142. pMesh->mNumFaces - iOutFaceNum * iSubMeshes);
  143. }
  144. // copy the list of faces
  145. pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  146. const unsigned int iBase = iOutFaceNum * i;
  147. // get the total number of indices
  148. unsigned int iCnt = 0;
  149. for (unsigned int p = iBase; p < pcMesh->mNumFaces + iBase;++p)
  150. {
  151. iCnt += pMesh->mFaces[p].mNumIndices;
  152. }
  153. pcMesh->mNumVertices = iCnt;
  154. // allocate storage
  155. if (pMesh->mVertices != NULL)
  156. pcMesh->mVertices = new aiVector3D[iCnt];
  157. if (pMesh->HasNormals())
  158. pcMesh->mNormals = new aiVector3D[iCnt];
  159. if (pMesh->HasTangentsAndBitangents())
  160. {
  161. pcMesh->mTangents = new aiVector3D[iCnt];
  162. pcMesh->mBitangents = new aiVector3D[iCnt];
  163. }
  164. // texture coordinates
  165. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  166. {
  167. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  168. if (pMesh->HasTextureCoords( c))
  169. {
  170. pcMesh->mTextureCoords[c] = new aiVector3D[iCnt];
  171. }
  172. }
  173. // vertex colors
  174. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c)
  175. {
  176. if (pMesh->HasVertexColors( c))
  177. {
  178. pcMesh->mColors[c] = new aiColor4D[iCnt];
  179. }
  180. }
  181. if (pMesh->HasBones())
  182. {
  183. // assume the number of bones won't change in most cases
  184. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  185. // iterate through all bones of the mesh and find those which
  186. // need to be copied to the splitted mesh
  187. std::vector<aiVertexWeight> avTempWeights;
  188. for (unsigned int p = 0; p < pcMesh->mNumBones;++p)
  189. {
  190. aiBone* const bone = pcMesh->mBones[p];
  191. avTempWeights.clear();
  192. avTempWeights.reserve(bone->mNumWeights / iSubMeshes);
  193. for (unsigned int q = 0; q < bone->mNumWeights;++q)
  194. {
  195. aiVertexWeight& weight = bone->mWeights[q];
  196. if(weight.mVertexId >= iBase && weight.mVertexId < iBase + iOutVertexNum)
  197. {
  198. avTempWeights.push_back(weight);
  199. weight = avTempWeights.back();
  200. weight.mVertexId -= iBase;
  201. }
  202. }
  203. if (!avTempWeights.empty())
  204. {
  205. // we'll need this bone. Copy it ...
  206. aiBone* pc = new aiBone();
  207. pcMesh->mBones[pcMesh->mNumBones++] = pc;
  208. pc->mName = aiString(bone->mName);
  209. pc->mNumWeights = (unsigned int)avTempWeights.size();
  210. pc->mOffsetMatrix = bone->mOffsetMatrix;
  211. // no need to reallocate the array for the last submesh.
  212. // Here we can reuse the (large) source array, although
  213. // we'll waste some memory
  214. if (iSubMeshes-1 == i)
  215. {
  216. pc->mWeights = bone->mWeights;
  217. bone->mWeights = NULL;
  218. }
  219. else pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  220. // copy the weights
  221. ::memcpy(pc->mWeights,&avTempWeights[0],sizeof(aiVertexWeight)*pc->mNumWeights);
  222. }
  223. }
  224. }
  225. // (we will also need to copy the array of indices)
  226. unsigned int iCurrent = 0;
  227. for (unsigned int p = 0; p < pcMesh->mNumFaces;++p)
  228. {
  229. pcMesh->mFaces[p].mNumIndices = 3;
  230. // allocate a new array
  231. const unsigned int iTemp = p + iBase;
  232. const unsigned int iNumIndices = pMesh->mFaces[iTemp].mNumIndices;
  233. // setup face type and number of indices
  234. pcMesh->mFaces[p].mNumIndices = iNumIndices;
  235. unsigned int* pi = pMesh->mFaces[iTemp].mIndices;
  236. unsigned int* piOut = pcMesh->mFaces[p].mIndices = new unsigned int[iNumIndices];
  237. // need to update the output primitive types
  238. switch (iNumIndices)
  239. {
  240. case 1:
  241. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  242. break;
  243. case 2:
  244. pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  245. break;
  246. case 3:
  247. pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  248. break;
  249. default:
  250. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  251. }
  252. // and copy the contents of the old array, offset by current base
  253. for (unsigned int v = 0; v < iNumIndices;++v)
  254. {
  255. unsigned int iIndex = pi[v];
  256. unsigned int iIndexOut = iCurrent++;
  257. piOut[v] = iIndexOut;
  258. // copy positions
  259. if (pMesh->mVertices != NULL)
  260. pcMesh->mVertices[iIndexOut] = pMesh->mVertices[iIndex];
  261. // copy normals
  262. if (pMesh->HasNormals())
  263. pcMesh->mNormals[iIndexOut] = pMesh->mNormals[iIndex];
  264. // copy tangents/bitangents
  265. if (pMesh->HasTangentsAndBitangents())
  266. {
  267. pcMesh->mTangents[iIndexOut] = pMesh->mTangents[iIndex];
  268. pcMesh->mBitangents[iIndexOut] = pMesh->mBitangents[iIndex];
  269. }
  270. // texture coordinates
  271. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  272. {
  273. if (pMesh->HasTextureCoords( c))
  274. pcMesh->mTextureCoords[c][iIndexOut] = pMesh->mTextureCoords[c][iIndex];
  275. }
  276. // vertex colors
  277. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c)
  278. {
  279. if (pMesh->HasVertexColors( c))
  280. pcMesh->mColors[c][iIndexOut] = pMesh->mColors[c][iIndex];
  281. }
  282. }
  283. }
  284. // add the newly created mesh to the list
  285. avList.push_back(std::pair<aiMesh*, unsigned int>(pcMesh,a));
  286. }
  287. // now delete the old mesh data
  288. delete pMesh;
  289. }
  290. else avList.push_back(std::pair<aiMesh*, unsigned int>(pMesh,a));
  291. return;
  292. }
  293. // ------------------------------------------------------------------------------------------------
  294. SplitLargeMeshesProcess_Vertex::SplitLargeMeshesProcess_Vertex()
  295. {
  296. LIMIT = AI_SLM_DEFAULT_MAX_VERTICES;
  297. }
  298. // ------------------------------------------------------------------------------------------------
  299. SplitLargeMeshesProcess_Vertex::~SplitLargeMeshesProcess_Vertex()
  300. {
  301. // nothing to do here
  302. }
  303. // ------------------------------------------------------------------------------------------------
  304. // Returns whether the processing step is present in the given flag field.
  305. bool SplitLargeMeshesProcess_Vertex::IsActive( unsigned int pFlags) const
  306. {
  307. return (pFlags & aiProcess_SplitLargeMeshes) != 0;
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. // Executes the post processing step on the given imported data.
  311. void SplitLargeMeshesProcess_Vertex::Execute( aiScene* pScene)
  312. {
  313. std::vector<std::pair<aiMesh*, unsigned int> > avList;
  314. if (0xffffffff == this->LIMIT)return;
  315. DefaultLogger::get()->debug("SplitLargeMeshesProcess_Vertex begin");
  316. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  317. this->SplitMesh(a, pScene->mMeshes[a],avList);
  318. if (avList.size() != pScene->mNumMeshes)
  319. {
  320. // it seems something has been splitted. rebuild the mesh list
  321. delete[] pScene->mMeshes;
  322. pScene->mNumMeshes = (unsigned int)avList.size();
  323. pScene->mMeshes = new aiMesh*[avList.size()];
  324. for (unsigned int i = 0; i < avList.size();++i)
  325. pScene->mMeshes[i] = avList[i].first;
  326. // now we need to update all nodes
  327. SplitLargeMeshesProcess_Triangle::UpdateNode(pScene->mRootNode,avList);
  328. DefaultLogger::get()->info("SplitLargeMeshesProcess_Vertex finished. Meshes have been splitted");
  329. }
  330. else DefaultLogger::get()->debug("SplitLargeMeshesProcess_Vertex finished. There was nothing to do");
  331. return;
  332. }
  333. // ------------------------------------------------------------------------------------------------
  334. // Setup properties
  335. void SplitLargeMeshesProcess_Vertex::SetupProperties( const Importer* pImp)
  336. {
  337. this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
  338. }
  339. // ------------------------------------------------------------------------------------------------
  340. // Executes the post processing step on the given imported data.
  341. void SplitLargeMeshesProcess_Vertex::SplitMesh(
  342. unsigned int a,
  343. aiMesh* pMesh,
  344. std::vector<std::pair<aiMesh*, unsigned int> >& avList)
  345. {
  346. if (pMesh->mNumVertices > SplitLargeMeshesProcess_Vertex::LIMIT)
  347. {
  348. typedef std::vector< std::pair<unsigned int,float> > VertexWeightTable;
  349. // build a per-vertex weight list if necessary
  350. VertexWeightTable* avPerVertexWeights = ComputeVertexBoneWeightTable(pMesh);
  351. // we need to split this mesh into sub meshes
  352. // determine the estimated size of a submesh
  353. // (this could be too large. Max waste is a single digit percentage)
  354. const unsigned int iSubMeshes = (pMesh->mNumVertices / SplitLargeMeshesProcess_Vertex::LIMIT) + 1;
  355. //const unsigned int iOutVertexNum2 = pMesh->mNumVertices /iSubMeshes;
  356. // create a std::vector<unsigned int> to indicate which vertices
  357. // have already been copied
  358. std::vector<unsigned int> avWasCopied;
  359. avWasCopied.resize(pMesh->mNumVertices,0xFFFFFFFF);
  360. // try to find a good estimate for the number of output faces
  361. // per mesh. Add 12.5% as buffer
  362. unsigned int iEstimatedSize = pMesh->mNumFaces / iSubMeshes;
  363. iEstimatedSize += iEstimatedSize >> 3;
  364. // now generate all submeshes
  365. unsigned int iBase = 0;
  366. while (true)
  367. {
  368. const unsigned int iOutVertexNum = SplitLargeMeshesProcess_Vertex::LIMIT;
  369. aiMesh* pcMesh = new aiMesh;
  370. pcMesh->mNumVertices = 0;
  371. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  372. typedef std::vector<aiVertexWeight> BoneWeightList;
  373. if (pMesh->HasBones())
  374. {
  375. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  376. ::memset(pcMesh->mBones,0,sizeof(void*)*pMesh->mNumBones);
  377. }
  378. // clear the temporary helper array
  379. if (iBase)
  380. {
  381. // we can't use memset here we unsigned int needn' be 32 bits
  382. for (std::vector<unsigned int>::iterator
  383. iter = avWasCopied.begin(),end = avWasCopied.end();
  384. iter != end;++iter)
  385. {
  386. (*iter) = 0xffffffff;
  387. }
  388. }
  389. // output vectors
  390. std::vector<aiFace> vFaces;
  391. // reserve enough storage for most cases
  392. if (pMesh->HasPositions())
  393. {
  394. pcMesh->mVertices = new aiVector3D[iOutVertexNum];
  395. }
  396. if (pMesh->HasNormals())
  397. {
  398. pcMesh->mNormals = new aiVector3D[iOutVertexNum];
  399. }
  400. if (pMesh->HasTangentsAndBitangents())
  401. {
  402. pcMesh->mTangents = new aiVector3D[iOutVertexNum];
  403. pcMesh->mBitangents = new aiVector3D[iOutVertexNum];
  404. }
  405. for (unsigned int c = 0; pMesh->HasVertexColors(c);++c)
  406. {
  407. pcMesh->mColors[c] = new aiColor4D[iOutVertexNum];
  408. }
  409. for (unsigned int c = 0; pMesh->HasTextureCoords(c);++c)
  410. {
  411. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  412. pcMesh->mTextureCoords[c] = new aiVector3D[iOutVertexNum];
  413. }
  414. vFaces.reserve(iEstimatedSize);
  415. // (we will also need to copy the array of indices)
  416. while (iBase < pMesh->mNumFaces)
  417. {
  418. // allocate a new array
  419. const unsigned int iNumIndices = pMesh->mFaces[iBase].mNumIndices;
  420. // doesn't catch degenerates but is quite fast
  421. unsigned int iNeed = 0;
  422. for (unsigned int v = 0; v < iNumIndices;++v)
  423. {
  424. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  425. // check whether we do already have this vertex
  426. if (0xFFFFFFFF == avWasCopied[iIndex])
  427. {
  428. iNeed++;
  429. }
  430. }
  431. if (pcMesh->mNumVertices + iNeed > iOutVertexNum)
  432. {
  433. // don't use this face
  434. break;
  435. }
  436. vFaces.push_back(aiFace());
  437. aiFace& rFace = vFaces.back();
  438. // setup face type and number of indices
  439. rFace.mNumIndices = iNumIndices;
  440. rFace.mIndices = new unsigned int[iNumIndices];
  441. // need to update the output primitive types
  442. switch (rFace.mNumIndices)
  443. {
  444. case 1:
  445. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  446. break;
  447. case 2:
  448. pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  449. break;
  450. case 3:
  451. pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  452. break;
  453. default:
  454. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  455. }
  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 = (unsigned int)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. }