SplitLargeMeshes.cpp 27 KB

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