SplitLargeMeshes.cpp 26 KB

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