SplitLargeMeshes.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. LIMIT = AI_SLM_DEFAULT_MAX_TRIANGLES;
  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. this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES);
  88. }
  89. // ------------------------------------------------------------------------------------------------
  90. // Update a node after some meshes have been split
  91. void SplitLargeMeshesProcess_Triangle::UpdateNode(aiNode* pcNode,
  92. const std::vector<std::pair<aiMesh*, unsigned int> >& avList)
  93. {
  94. // for every index in out list build a new entry
  95. std::vector<unsigned int> aiEntries;
  96. aiEntries.reserve(pcNode->mNumMeshes + 1);
  97. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i)
  98. {
  99. for (unsigned int a = 0; a < avList.size();++a)
  100. {
  101. if (avList[a].second == pcNode->mMeshes[i])
  102. {
  103. aiEntries.push_back(a);
  104. }
  105. }
  106. }
  107. // now build the new list
  108. delete pcNode->mMeshes;
  109. pcNode->mNumMeshes = (unsigned int)aiEntries.size();
  110. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  111. for (unsigned int b = 0; b < pcNode->mNumMeshes;++b)
  112. pcNode->mMeshes[b] = aiEntries[b];
  113. // recusively update all other nodes
  114. for (unsigned int i = 0; i < pcNode->mNumChildren;++i)
  115. {
  116. UpdateNode ( pcNode->mChildren[i], avList );
  117. }
  118. return;
  119. }
  120. // ------------------------------------------------------------------------------------------------
  121. // Executes the post processing step on the given imported data.
  122. void SplitLargeMeshesProcess_Triangle::SplitMesh(
  123. unsigned int a,
  124. aiMesh* pMesh,
  125. std::vector<std::pair<aiMesh*, unsigned int> >& avList)
  126. {
  127. if (pMesh->mNumFaces > SplitLargeMeshesProcess_Triangle::LIMIT)
  128. {
  129. DefaultLogger::get()->info("Mesh exceeds the triangle limit. It will be split ...");
  130. // we need to split this mesh into sub meshes
  131. // determine the size of a submesh
  132. const unsigned int iSubMeshes = (pMesh->mNumFaces / LIMIT) + 1;
  133. const unsigned int iOutFaceNum = pMesh->mNumFaces / iSubMeshes;
  134. const unsigned int iOutVertexNum = iOutFaceNum * 3;
  135. // now generate all submeshes
  136. for (unsigned int i = 0; i < iSubMeshes;++i)
  137. {
  138. aiMesh* pcMesh = new aiMesh;
  139. pcMesh->mNumFaces = iOutFaceNum;
  140. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  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 splitted 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. // and copy the contents of the old array, offset by current base
  240. for (unsigned int v = 0; v < iNumIndices;++v)
  241. {
  242. unsigned int iIndex = pi[v];
  243. unsigned int iIndexOut = iCurrent++;
  244. piOut[v] = iIndexOut;
  245. // copy positions
  246. if (pMesh->mVertices != NULL)
  247. pcMesh->mVertices[iIndexOut] = pMesh->mVertices[iIndex];
  248. // copy normals
  249. if (pMesh->HasNormals())
  250. pcMesh->mNormals[iIndexOut] = pMesh->mNormals[iIndex];
  251. // copy tangents/bitangents
  252. if (pMesh->HasTangentsAndBitangents())
  253. {
  254. pcMesh->mTangents[iIndexOut] = pMesh->mTangents[iIndex];
  255. pcMesh->mBitangents[iIndexOut] = pMesh->mBitangents[iIndex];
  256. }
  257. // texture coordinates
  258. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  259. {
  260. if (pMesh->HasTextureCoords( c))
  261. pcMesh->mTextureCoords[c][iIndexOut] = pMesh->mTextureCoords[c][iIndex];
  262. }
  263. // vertex colors
  264. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c)
  265. {
  266. if (pMesh->HasVertexColors( c))
  267. pcMesh->mColors[c][iIndexOut] = pMesh->mColors[c][iIndex];
  268. }
  269. }
  270. }
  271. // add the newly created mesh to the list
  272. avList.push_back(std::pair<aiMesh*, unsigned int>(pcMesh,a));
  273. }
  274. // now delete the old mesh data
  275. delete pMesh;
  276. }
  277. else avList.push_back(std::pair<aiMesh*, unsigned int>(pMesh,a));
  278. return;
  279. }
  280. // ------------------------------------------------------------------------------------------------
  281. SplitLargeMeshesProcess_Vertex::SplitLargeMeshesProcess_Vertex()
  282. {
  283. LIMIT = AI_SLM_DEFAULT_MAX_VERTICES;
  284. }
  285. // ------------------------------------------------------------------------------------------------
  286. SplitLargeMeshesProcess_Vertex::~SplitLargeMeshesProcess_Vertex()
  287. {
  288. // nothing to do here
  289. }
  290. // ------------------------------------------------------------------------------------------------
  291. // Returns whether the processing step is present in the given flag field.
  292. bool SplitLargeMeshesProcess_Vertex::IsActive( unsigned int pFlags) const
  293. {
  294. return (pFlags & aiProcess_SplitLargeMeshes) != 0;
  295. }
  296. // ------------------------------------------------------------------------------------------------
  297. // Executes the post processing step on the given imported data.
  298. void SplitLargeMeshesProcess_Vertex::Execute( aiScene* pScene)
  299. {
  300. std::vector<std::pair<aiMesh*, unsigned int> > avList;
  301. if (0xffffffff == this->LIMIT)return;
  302. DefaultLogger::get()->debug("SplitLargeMeshesProcess_Vertex begin");
  303. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  304. this->SplitMesh(a, pScene->mMeshes[a],avList);
  305. if (avList.size() != pScene->mNumMeshes)
  306. {
  307. // it seems something has been splitted. rebuild the mesh list
  308. delete[] pScene->mMeshes;
  309. pScene->mNumMeshes = (unsigned int)avList.size();
  310. pScene->mMeshes = new aiMesh*[avList.size()];
  311. for (unsigned int i = 0; i < avList.size();++i)
  312. pScene->mMeshes[i] = avList[i].first;
  313. // now we need to update all nodes
  314. SplitLargeMeshesProcess_Triangle::UpdateNode(pScene->mRootNode,avList);
  315. DefaultLogger::get()->info("SplitLargeMeshesProcess_Vertex finished. Meshes have been splitted");
  316. }
  317. else DefaultLogger::get()->debug("SplitLargeMeshesProcess_Vertex finished. There was nothing to do");
  318. return;
  319. }
  320. // ------------------------------------------------------------------------------------------------
  321. // Setup properties
  322. void SplitLargeMeshesProcess_Vertex::SetupProperties( const Importer* pImp)
  323. {
  324. this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
  325. }
  326. // ------------------------------------------------------------------------------------------------
  327. // Executes the post processing step on the given imported data.
  328. void SplitLargeMeshesProcess_Vertex::SplitMesh(
  329. unsigned int a,
  330. aiMesh* pMesh,
  331. std::vector<std::pair<aiMesh*, unsigned int> >& avList)
  332. {
  333. if (pMesh->mNumVertices > SplitLargeMeshesProcess_Vertex::LIMIT)
  334. {
  335. typedef std::vector< std::pair<unsigned int,float> > VertexWeightTable;
  336. VertexWeightTable* avPerVertexWeights = NULL;
  337. // build a per-vertex weight list if necessary
  338. if (pMesh->HasBones())
  339. {
  340. avPerVertexWeights = new VertexWeightTable[pMesh->mNumVertices];
  341. for (unsigned int i = 0; i < pMesh->mNumBones;++i)
  342. {
  343. aiBone* bone = pMesh->mBones[i];
  344. for (unsigned int a = 0; a < bone->mNumWeights;++a)
  345. {
  346. aiVertexWeight& weight = bone->mWeights[a];
  347. avPerVertexWeights[weight.mVertexId].push_back(
  348. std::pair<unsigned int,float>(a,weight.mWeight));
  349. }
  350. }
  351. }
  352. // we need to split this mesh into sub meshes
  353. // determine the estimated size of a submesh
  354. // (this could be too large. Max waste is a single digit percentage)
  355. const unsigned int iSubMeshes = (pMesh->mNumVertices / SplitLargeMeshesProcess_Vertex::LIMIT) + 1;
  356. //const unsigned int iOutVertexNum2 = pMesh->mNumVertices /iSubMeshes;
  357. // create a std::vector<unsigned int> to indicate which vertices
  358. // have already been copied
  359. std::vector<unsigned int> avWasCopied;
  360. avWasCopied.resize(pMesh->mNumVertices,0xFFFFFFFF);
  361. // try to find a good estimate for the number of output faces
  362. // per mesh. Add 12.5% as buffer
  363. unsigned int iEstimatedSize = pMesh->mNumFaces / iSubMeshes;
  364. iEstimatedSize += iEstimatedSize >> 3;
  365. // now generate all submeshes
  366. unsigned int iBase = 0;
  367. while (true)
  368. {
  369. const unsigned int iOutVertexNum = SplitLargeMeshesProcess_Vertex::LIMIT;
  370. aiMesh* pcMesh = new aiMesh;
  371. pcMesh->mNumVertices = 0;
  372. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  373. typedef std::vector<aiVertexWeight> BoneWeightList;
  374. if (pMesh->HasBones())
  375. {
  376. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  377. ::memset(pcMesh->mBones,0,sizeof(void*)*pMesh->mNumBones);
  378. }
  379. // clear the temporary helper array
  380. if (iBase)
  381. {
  382. // we can't use memset here we unsigned int needn' be 32 bits
  383. for (std::vector<unsigned int>::iterator
  384. iter = avWasCopied.begin(),end = avWasCopied.end();
  385. iter != end;++iter)
  386. {
  387. (*iter) = 0xffffffff;
  388. }
  389. }
  390. // output vectors
  391. std::vector<aiFace> vFaces;
  392. // reserve enough storage for most cases
  393. if (pMesh->HasPositions())
  394. {
  395. pcMesh->mVertices = new aiVector3D[iOutVertexNum];
  396. }
  397. if (pMesh->HasNormals())
  398. {
  399. pcMesh->mNormals = new aiVector3D[iOutVertexNum];
  400. }
  401. if (pMesh->HasTangentsAndBitangents())
  402. {
  403. pcMesh->mTangents = new aiVector3D[iOutVertexNum];
  404. pcMesh->mBitangents = new aiVector3D[iOutVertexNum];
  405. }
  406. for (unsigned int c = 0; pMesh->HasVertexColors(c);++c)
  407. {
  408. pcMesh->mColors[c] = new aiColor4D[iOutVertexNum];
  409. }
  410. for (unsigned int c = 0; pMesh->HasTextureCoords(c);++c)
  411. {
  412. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  413. pcMesh->mTextureCoords[c] = new aiVector3D[iOutVertexNum];
  414. }
  415. vFaces.reserve(iEstimatedSize);
  416. // (we will also need to copy the array of indices)
  417. while (iBase < pMesh->mNumFaces)
  418. {
  419. // allocate a new array
  420. const unsigned int iNumIndices = pMesh->mFaces[iBase].mNumIndices;
  421. // doesn't catch degenerates but is quite fast
  422. unsigned int iNeed = 0;
  423. for (unsigned int v = 0; v < iNumIndices;++v)
  424. {
  425. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  426. // check whether we do already have this vertex
  427. if (0xFFFFFFFF == avWasCopied[iIndex])
  428. {
  429. iNeed++;
  430. }
  431. }
  432. if (pcMesh->mNumVertices + iNeed > iOutVertexNum)
  433. {
  434. // don't use this face
  435. break;
  436. }
  437. vFaces.push_back(aiFace());
  438. aiFace& rFace = vFaces.back();
  439. // setup face type and number of indices
  440. rFace.mNumIndices = iNumIndices;
  441. rFace.mIndices = new unsigned int[iNumIndices];
  442. // and copy the contents of the old array, offset by current base
  443. for (unsigned int v = 0; v < iNumIndices;++v)
  444. {
  445. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  446. // check whether we do already have this vertex
  447. if (0xFFFFFFFF != avWasCopied[iIndex])
  448. {
  449. rFace.mIndices[v] = avWasCopied[iIndex];
  450. continue;
  451. }
  452. // copy positions
  453. pcMesh->mVertices[pcMesh->mNumVertices] = (pMesh->mVertices[iIndex]);
  454. // copy normals
  455. if (pMesh->HasNormals())
  456. {
  457. pcMesh->mNormals[pcMesh->mNumVertices] = (pMesh->mNormals[iIndex]);
  458. }
  459. // copy tangents/bitangents
  460. if (pMesh->HasTangentsAndBitangents())
  461. {
  462. pcMesh->mTangents[pcMesh->mNumVertices] = (pMesh->mTangents[iIndex]);
  463. pcMesh->mBitangents[pcMesh->mNumVertices] = (pMesh->mBitangents[iIndex]);
  464. }
  465. // texture coordinates
  466. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  467. {
  468. if (pMesh->HasTextureCoords( c))
  469. {
  470. pcMesh->mTextureCoords[c][pcMesh->mNumVertices] = pMesh->mTextureCoords[c][iIndex];
  471. }
  472. }
  473. // vertex colors
  474. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c)
  475. {
  476. if (pMesh->HasVertexColors( c))
  477. {
  478. pcMesh->mColors[c][pcMesh->mNumVertices] = pMesh->mColors[c][iIndex];
  479. }
  480. }
  481. // check whether we have bone weights assigned to this vertex
  482. rFace.mIndices[v] = pcMesh->mNumVertices;
  483. if (avPerVertexWeights)
  484. {
  485. VertexWeightTable& table = avPerVertexWeights[ pcMesh->mNumVertices ];
  486. if( !table.empty() )
  487. {
  488. for (VertexWeightTable::const_iterator
  489. iter = table.begin();
  490. iter != table.end();++iter)
  491. {
  492. // allocate the bone weight array if necessary
  493. BoneWeightList* pcWeightList = (BoneWeightList*)pcMesh->mBones[(*iter).first];
  494. if (!pcWeightList)
  495. {
  496. pcMesh->mBones[(*iter).first] = (aiBone*)(pcWeightList = new BoneWeightList());
  497. }
  498. pcWeightList->push_back(aiVertexWeight(pcMesh->mNumVertices,(*iter).second));
  499. }
  500. }
  501. }
  502. avWasCopied[iIndex] = pcMesh->mNumVertices;
  503. pcMesh->mNumVertices++;
  504. }
  505. iBase++;
  506. if(pcMesh->mNumVertices == iOutVertexNum)
  507. {
  508. // break here. The face is only added if it was complete
  509. break;
  510. }
  511. }
  512. // check which bones we'll need to create for this submesh
  513. if (pMesh->HasBones())
  514. {
  515. aiBone** ppCurrent = pcMesh->mBones;
  516. for (unsigned int k = 0; k < pMesh->mNumBones;++k)
  517. {
  518. // check whether the bone is existing
  519. BoneWeightList* pcWeightList;
  520. if ((pcWeightList = (BoneWeightList*)pcMesh->mBones[k]))
  521. {
  522. aiBone* pcOldBone = pMesh->mBones[k];
  523. aiBone* pcOut;
  524. *ppCurrent++ = pcOut = new aiBone();
  525. pcOut->mName = aiString(pcOldBone->mName);
  526. pcOut->mOffsetMatrix = pcOldBone->mOffsetMatrix;
  527. pcOut->mNumWeights = (unsigned int)pcWeightList->size();
  528. pcOut->mWeights = new aiVertexWeight[pcOut->mNumWeights];
  529. // copy the vertex weights
  530. ::memcpy(pcOut->mWeights,&pcWeightList->operator[](0),
  531. pcOut->mNumWeights * sizeof(aiVertexWeight));
  532. // delete the temporary bone weight list
  533. delete pcWeightList;
  534. pcMesh->mNumBones++;
  535. }
  536. }
  537. }
  538. // copy the face list to the mesh
  539. pcMesh->mFaces = new aiFace[vFaces.size()];
  540. pcMesh->mNumFaces = (unsigned int)vFaces.size();
  541. for (unsigned int p = 0; p < pcMesh->mNumFaces;++p)
  542. pcMesh->mFaces[p] = vFaces[p];
  543. // add the newly created mesh to the list
  544. avList.push_back(std::pair<aiMesh*, unsigned int>(pcMesh,a));
  545. if (iBase == pMesh->mNumFaces)
  546. {
  547. // have all faces ... finish the outer loop, too
  548. break;
  549. }
  550. }
  551. // delete the per-vertex weight list again
  552. delete[] avPerVertexWeights;
  553. // now delete the old mesh data
  554. delete pMesh;
  555. return;
  556. }
  557. avList.push_back(std::pair<aiMesh*, unsigned int>(pMesh,a));
  558. return;
  559. }