SplitLargeMeshes.cpp 22 KB

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