PretransformVertices.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file PretransformVertices.cpp
  35. * @brief Implementation of the "PretransformVertices" post processing step
  36. */
  37. #include "PretransformVertices.h"
  38. #include "ConvertToLHProcess.h"
  39. #include "ProcessHelper.h"
  40. #include <assimp/Exceptional.h>
  41. #include <assimp/SceneCombiner.h>
  42. using namespace Assimp;
  43. // some array offsets
  44. #define AI_PTVS_VERTEX 0x0
  45. #define AI_PTVS_FACE 0x1
  46. // ------------------------------------------------------------------------------------------------
  47. // Constructor to be privately used by Importer
  48. PretransformVertices::PretransformVertices() :
  49. configKeepHierarchy(false),
  50. configNormalize(false),
  51. configTransform(false),
  52. configTransformation(),
  53. mConfigPointCloud(false) {
  54. // empty
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. // Returns whether the processing step is present in the given flag field.
  58. bool PretransformVertices::IsActive(unsigned int pFlags) const {
  59. return (pFlags & aiProcess_PreTransformVertices) != 0;
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. // Setup import configuration
  63. void PretransformVertices::SetupProperties(const Importer *pImp) {
  64. // Get the current value of AI_CONFIG_PP_PTV_KEEP_HIERARCHY, AI_CONFIG_PP_PTV_NORMALIZE,
  65. // AI_CONFIG_PP_PTV_ADD_ROOT_TRANSFORMATION and AI_CONFIG_PP_PTV_ROOT_TRANSFORMATION
  66. configKeepHierarchy = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_KEEP_HIERARCHY, 0));
  67. configNormalize = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_NORMALIZE, 0));
  68. configTransform = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_ADD_ROOT_TRANSFORMATION, 0));
  69. configTransformation = pImp->GetPropertyMatrix(AI_CONFIG_PP_PTV_ROOT_TRANSFORMATION, aiMatrix4x4());
  70. mConfigPointCloud = pImp->GetPropertyBool(AI_CONFIG_EXPORT_POINT_CLOUDS);
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Count the number of nodes
  74. unsigned int PretransformVertices::CountNodes(const aiNode *pcNode) const {
  75. unsigned int iRet = 1;
  76. for (unsigned int i = 0; i < pcNode->mNumChildren; ++i) {
  77. iRet += CountNodes(pcNode->mChildren[i]);
  78. }
  79. return iRet;
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. // Get a bitwise combination identifying the vertex format of a mesh
  83. unsigned int PretransformVertices::GetMeshVFormat(aiMesh *pcMesh) const {
  84. // the vertex format is stored in aiMesh::mBones for later retrieval.
  85. // there isn't a good reason to compute it a few hundred times
  86. // from scratch. The pointer is unused as animations are lost
  87. // during PretransformVertices.
  88. if (pcMesh->mBones)
  89. return (unsigned int)(uint64_t)pcMesh->mBones;
  90. const unsigned int iRet = GetMeshVFormatUnique(pcMesh);
  91. // store the value for later use
  92. pcMesh->mBones = (aiBone **)(uint64_t)iRet;
  93. return iRet;
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. // Count the number of vertices in the whole scene and a given
  97. // material index
  98. void PretransformVertices::CountVerticesAndFaces(const aiScene *pcScene, const aiNode *pcNode, unsigned int iMat,
  99. unsigned int iVFormat, unsigned int *piFaces, unsigned int *piVertices) const {
  100. for (unsigned int i = 0; i < pcNode->mNumMeshes; ++i) {
  101. aiMesh *pcMesh = pcScene->mMeshes[pcNode->mMeshes[i]];
  102. if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh)) {
  103. *piVertices += pcMesh->mNumVertices;
  104. *piFaces += pcMesh->mNumFaces;
  105. }
  106. }
  107. for (unsigned int i = 0; i < pcNode->mNumChildren; ++i) {
  108. CountVerticesAndFaces(pcScene, pcNode->mChildren[i], iMat,
  109. iVFormat, piFaces, piVertices);
  110. }
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. // Collect vertex/face data
  114. void PretransformVertices::CollectData(const aiScene *pcScene, const aiNode *pcNode, unsigned int iMat,
  115. unsigned int iVFormat, aiMesh *pcMeshOut,
  116. unsigned int aiCurrent[2], unsigned int *num_refs) const {
  117. // No need to multiply if there's no transformation
  118. const bool identity = pcNode->mTransformation.IsIdentity();
  119. for (unsigned int i = 0; i < pcNode->mNumMeshes; ++i) {
  120. aiMesh *pcMesh = pcScene->mMeshes[pcNode->mMeshes[i]];
  121. if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh)) {
  122. // Decrement mesh reference counter
  123. unsigned int &num_ref = num_refs[pcNode->mMeshes[i]];
  124. ai_assert(0 != num_ref);
  125. --num_ref;
  126. // Save the name of the last mesh
  127. if (num_ref == 0) {
  128. pcMeshOut->mName = pcMesh->mName;
  129. }
  130. if (identity) {
  131. // copy positions without modifying them
  132. ::memcpy(pcMeshOut->mVertices + aiCurrent[AI_PTVS_VERTEX],
  133. pcMesh->mVertices,
  134. pcMesh->mNumVertices * sizeof(aiVector3D));
  135. if (iVFormat & 0x2) {
  136. // copy normals without modifying them
  137. ::memcpy(pcMeshOut->mNormals + aiCurrent[AI_PTVS_VERTEX],
  138. pcMesh->mNormals,
  139. pcMesh->mNumVertices * sizeof(aiVector3D));
  140. }
  141. if (iVFormat & 0x4) {
  142. // copy tangents without modifying them
  143. ::memcpy(pcMeshOut->mTangents + aiCurrent[AI_PTVS_VERTEX],
  144. pcMesh->mTangents,
  145. pcMesh->mNumVertices * sizeof(aiVector3D));
  146. // copy bitangents without modifying them
  147. ::memcpy(pcMeshOut->mBitangents + aiCurrent[AI_PTVS_VERTEX],
  148. pcMesh->mBitangents,
  149. pcMesh->mNumVertices * sizeof(aiVector3D));
  150. }
  151. } else {
  152. // copy positions, transform them to worldspace
  153. for (unsigned int n = 0; n < pcMesh->mNumVertices; ++n) {
  154. pcMeshOut->mVertices[aiCurrent[AI_PTVS_VERTEX] + n] = pcNode->mTransformation * pcMesh->mVertices[n];
  155. }
  156. aiMatrix4x4 mWorldIT = pcNode->mTransformation;
  157. mWorldIT.Inverse().Transpose();
  158. // TODO: implement Inverse() for aiMatrix3x3
  159. aiMatrix3x3 m = aiMatrix3x3(mWorldIT);
  160. if (iVFormat & 0x2) {
  161. // copy normals, transform them to worldspace
  162. for (unsigned int n = 0; n < pcMesh->mNumVertices; ++n) {
  163. pcMeshOut->mNormals[aiCurrent[AI_PTVS_VERTEX] + n] =
  164. (m * pcMesh->mNormals[n]).Normalize();
  165. }
  166. }
  167. if (iVFormat & 0x4) {
  168. // copy tangents and bitangents, transform them to worldspace
  169. for (unsigned int n = 0; n < pcMesh->mNumVertices; ++n) {
  170. pcMeshOut->mTangents[aiCurrent[AI_PTVS_VERTEX] + n] = (m * pcMesh->mTangents[n]).Normalize();
  171. pcMeshOut->mBitangents[aiCurrent[AI_PTVS_VERTEX] + n] = (m * pcMesh->mBitangents[n]).Normalize();
  172. }
  173. }
  174. }
  175. unsigned int p = 0;
  176. while (iVFormat & (0x100 << p)) {
  177. // copy texture coordinates
  178. memcpy(pcMeshOut->mTextureCoords[p] + aiCurrent[AI_PTVS_VERTEX],
  179. pcMesh->mTextureCoords[p],
  180. pcMesh->mNumVertices * sizeof(aiVector3D));
  181. ++p;
  182. }
  183. p = 0;
  184. while (iVFormat & (0x1000000 << p)) {
  185. // copy vertex colors
  186. memcpy(pcMeshOut->mColors[p] + aiCurrent[AI_PTVS_VERTEX],
  187. pcMesh->mColors[p],
  188. pcMesh->mNumVertices * sizeof(aiColor4D));
  189. ++p;
  190. }
  191. // now we need to copy all faces. since we will delete the source mesh afterwards,
  192. // we don't need to reallocate the array of indices except if this mesh is
  193. // referenced multiple times.
  194. for (unsigned int planck = 0; planck < pcMesh->mNumFaces; ++planck) {
  195. aiFace &f_src = pcMesh->mFaces[planck];
  196. aiFace &f_dst = pcMeshOut->mFaces[aiCurrent[AI_PTVS_FACE] + planck];
  197. const unsigned int num_idx = f_src.mNumIndices;
  198. f_dst.mNumIndices = num_idx;
  199. unsigned int *pi;
  200. if (!num_ref) { /* if last time the mesh is referenced -> no reallocation */
  201. pi = f_dst.mIndices = f_src.mIndices;
  202. // offset all vertex indices
  203. for (unsigned int hahn = 0; hahn < num_idx; ++hahn) {
  204. pi[hahn] += aiCurrent[AI_PTVS_VERTEX];
  205. }
  206. } else {
  207. pi = f_dst.mIndices = new unsigned int[num_idx];
  208. // copy and offset all vertex indices
  209. for (unsigned int hahn = 0; hahn < num_idx; ++hahn) {
  210. pi[hahn] = f_src.mIndices[hahn] + aiCurrent[AI_PTVS_VERTEX];
  211. }
  212. }
  213. // Update the mPrimitiveTypes member of the mesh
  214. switch (pcMesh->mFaces[planck].mNumIndices) {
  215. case 0x1:
  216. pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_POINT;
  217. break;
  218. case 0x2:
  219. pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_LINE;
  220. break;
  221. case 0x3:
  222. pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  223. break;
  224. default:
  225. pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  226. break;
  227. };
  228. }
  229. aiCurrent[AI_PTVS_VERTEX] += pcMesh->mNumVertices;
  230. aiCurrent[AI_PTVS_FACE] += pcMesh->mNumFaces;
  231. }
  232. }
  233. // append all children of us
  234. for (unsigned int i = 0; i < pcNode->mNumChildren; ++i) {
  235. CollectData(pcScene, pcNode->mChildren[i], iMat,
  236. iVFormat, pcMeshOut, aiCurrent, num_refs);
  237. }
  238. }
  239. // ------------------------------------------------------------------------------------------------
  240. // Get a list of all vertex formats that occur for a given material index
  241. // The output list contains duplicate elements
  242. void PretransformVertices::GetVFormatList(const aiScene *pcScene, unsigned int iMat,
  243. std::list<unsigned int> &aiOut) const {
  244. for (unsigned int i = 0; i < pcScene->mNumMeshes; ++i) {
  245. aiMesh *pcMesh = pcScene->mMeshes[i];
  246. if (iMat == pcMesh->mMaterialIndex) {
  247. aiOut.push_back(GetMeshVFormat(pcMesh));
  248. }
  249. }
  250. }
  251. // ------------------------------------------------------------------------------------------------
  252. // Compute the absolute transformation matrices of each node
  253. void PretransformVertices::ComputeAbsoluteTransform(aiNode *pcNode) {
  254. if (pcNode->mParent) {
  255. pcNode->mTransformation = pcNode->mParent->mTransformation * pcNode->mTransformation;
  256. }
  257. for (unsigned int i = 0; i < pcNode->mNumChildren; ++i) {
  258. ComputeAbsoluteTransform(pcNode->mChildren[i]);
  259. }
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. // Apply the node transformation to a mesh
  263. void PretransformVertices::ApplyTransform(aiMesh *mesh, const aiMatrix4x4 &mat) const {
  264. // Check whether we need to transform the coordinates at all
  265. if (!mat.IsIdentity()) {
  266. // Check for odd negative scale (mirror)
  267. if (mesh->HasFaces() && mat.Determinant() < 0) {
  268. // Reverse the mesh face winding order
  269. FlipWindingOrderProcess::ProcessMesh(mesh);
  270. }
  271. // Update positions
  272. if (mesh->HasPositions()) {
  273. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  274. mesh->mVertices[i] = mat * mesh->mVertices[i];
  275. }
  276. }
  277. // Update normals and tangents
  278. if (mesh->HasNormals() || mesh->HasTangentsAndBitangents()) {
  279. const aiMatrix3x3 m = aiMatrix3x3(mat).Inverse().Transpose();
  280. if (mesh->HasNormals()) {
  281. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  282. mesh->mNormals[i] = (m * mesh->mNormals[i]).Normalize();
  283. }
  284. }
  285. if (mesh->HasTangentsAndBitangents()) {
  286. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  287. mesh->mTangents[i] = (m * mesh->mTangents[i]).Normalize();
  288. mesh->mBitangents[i] = (m * mesh->mBitangents[i]).Normalize();
  289. }
  290. }
  291. }
  292. }
  293. }
  294. // ------------------------------------------------------------------------------------------------
  295. // Simple routine to build meshes in worldspace, no further optimization
  296. void PretransformVertices::BuildWCSMeshes(std::vector<aiMesh *> &out, aiMesh **in,
  297. unsigned int numIn, aiNode *node) const {
  298. // NOTE:
  299. // aiMesh::mNumBones store original source mesh, or UINT_MAX if not a copy
  300. // aiMesh::mBones store reference to abs. transform we multiplied with
  301. // process meshes
  302. for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
  303. aiMesh *mesh = in[node->mMeshes[i]];
  304. // check whether we can operate on this mesh
  305. if (!mesh->mBones || *reinterpret_cast<aiMatrix4x4 *>(mesh->mBones) == node->mTransformation) {
  306. // yes, we can.
  307. mesh->mBones = reinterpret_cast<aiBone **>(&node->mTransformation);
  308. mesh->mNumBones = UINT_MAX;
  309. } else {
  310. // try to find us in the list of newly created meshes
  311. for (unsigned int n = 0; n < out.size(); ++n) {
  312. aiMesh *ctz = out[n];
  313. if (ctz->mNumBones == node->mMeshes[i] && *reinterpret_cast<aiMatrix4x4 *>(ctz->mBones) == node->mTransformation) {
  314. // ok, use this one. Update node mesh index
  315. node->mMeshes[i] = numIn + n;
  316. }
  317. }
  318. if (node->mMeshes[i] < numIn) {
  319. // Worst case. Need to operate on a full copy of the mesh
  320. ASSIMP_LOG_INFO("PretransformVertices: Copying mesh due to mismatching transforms");
  321. aiMesh *ntz;
  322. const unsigned int tmp = mesh->mNumBones; //
  323. mesh->mNumBones = 0;
  324. SceneCombiner::Copy(&ntz, mesh);
  325. mesh->mNumBones = tmp;
  326. ntz->mNumBones = node->mMeshes[i];
  327. ntz->mBones = reinterpret_cast<aiBone **>(&node->mTransformation);
  328. out.push_back(ntz);
  329. node->mMeshes[i] = static_cast<unsigned int>(numIn + out.size() - 1);
  330. }
  331. }
  332. }
  333. // call children
  334. for (unsigned int i = 0; i < node->mNumChildren; ++i)
  335. BuildWCSMeshes(out, in, numIn, node->mChildren[i]);
  336. }
  337. // ------------------------------------------------------------------------------------------------
  338. // Reset transformation matrices to identity
  339. void PretransformVertices::MakeIdentityTransform(aiNode *nd) const {
  340. nd->mTransformation = aiMatrix4x4();
  341. // call children
  342. for (unsigned int i = 0; i < nd->mNumChildren; ++i)
  343. MakeIdentityTransform(nd->mChildren[i]);
  344. }
  345. // ------------------------------------------------------------------------------------------------
  346. // Build reference counters for all meshes
  347. void PretransformVertices::BuildMeshRefCountArray(const aiNode *nd, unsigned int *refs) const {
  348. for (unsigned int i = 0; i < nd->mNumMeshes; ++i)
  349. refs[nd->mMeshes[i]]++;
  350. // call children
  351. for (unsigned int i = 0; i < nd->mNumChildren; ++i)
  352. BuildMeshRefCountArray(nd->mChildren[i], refs);
  353. }
  354. // ------------------------------------------------------------------------------------------------
  355. // Executes the post processing step on the given imported data.
  356. void PretransformVertices::Execute(aiScene *pScene) {
  357. ASSIMP_LOG_DEBUG("PretransformVerticesProcess begin");
  358. // Return immediately if we have no meshes
  359. if (!pScene->mNumMeshes)
  360. return;
  361. const unsigned int iOldMeshes = pScene->mNumMeshes;
  362. const unsigned int iOldAnimationChannels = pScene->mNumAnimations;
  363. const unsigned int iOldNodes = CountNodes(pScene->mRootNode);
  364. if (configTransform) {
  365. pScene->mRootNode->mTransformation = configTransformation * pScene->mRootNode->mTransformation;
  366. }
  367. // first compute absolute transformation matrices for all nodes
  368. ComputeAbsoluteTransform(pScene->mRootNode);
  369. // Delete aiMesh::mBones for all meshes. The bones are
  370. // removed during this step and we need the pointer as
  371. // temporary storage
  372. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  373. aiMesh *mesh = pScene->mMeshes[i];
  374. for (unsigned int a = 0; a < mesh->mNumBones; ++a)
  375. delete mesh->mBones[a];
  376. delete[] mesh->mBones;
  377. mesh->mBones = nullptr;
  378. }
  379. // now build a list of output meshes
  380. std::vector<aiMesh *> apcOutMeshes;
  381. // Keep scene hierarchy? It's an easy job in this case ...
  382. // we go on and transform all meshes, if one is referenced by nodes
  383. // with different absolute transformations a depth copy of the mesh
  384. // is required.
  385. if (configKeepHierarchy) {
  386. // Hack: store the matrix we're transforming a mesh with in aiMesh::mBones
  387. BuildWCSMeshes(apcOutMeshes, pScene->mMeshes, pScene->mNumMeshes, pScene->mRootNode);
  388. // ... if new meshes have been generated, append them to the end of the scene
  389. if (apcOutMeshes.size() > 0) {
  390. aiMesh **npp = new aiMesh *[pScene->mNumMeshes + apcOutMeshes.size()];
  391. memcpy(npp, pScene->mMeshes, sizeof(aiMesh *) * pScene->mNumMeshes);
  392. memcpy(npp + pScene->mNumMeshes, &apcOutMeshes[0], sizeof(aiMesh *) * apcOutMeshes.size());
  393. pScene->mNumMeshes += static_cast<unsigned int>(apcOutMeshes.size());
  394. delete[] pScene->mMeshes;
  395. pScene->mMeshes = npp;
  396. }
  397. // now iterate through all meshes and transform them to world-space
  398. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  399. ApplyTransform(pScene->mMeshes[i], *reinterpret_cast<aiMatrix4x4 *>(pScene->mMeshes[i]->mBones));
  400. // prevent improper destruction
  401. pScene->mMeshes[i]->mBones = nullptr;
  402. pScene->mMeshes[i]->mNumBones = 0;
  403. }
  404. } else {
  405. apcOutMeshes.reserve(static_cast<size_t>(pScene->mNumMaterials) << 1u);
  406. std::list<unsigned int> aiVFormats;
  407. std::vector<unsigned int> s(pScene->mNumMeshes, 0);
  408. BuildMeshRefCountArray(pScene->mRootNode, &s[0]);
  409. for (unsigned int i = 0; i < pScene->mNumMaterials; ++i) {
  410. // get the list of all vertex formats for this material
  411. aiVFormats.clear();
  412. GetVFormatList(pScene, i, aiVFormats);
  413. aiVFormats.sort();
  414. aiVFormats.unique();
  415. for (std::list<unsigned int>::const_iterator j = aiVFormats.begin(); j != aiVFormats.end(); ++j) {
  416. unsigned int iVertices = 0;
  417. unsigned int iFaces = 0;
  418. CountVerticesAndFaces(pScene, pScene->mRootNode, i, *j, &iFaces, &iVertices);
  419. if (0 != iFaces && 0 != iVertices) {
  420. apcOutMeshes.push_back(new aiMesh());
  421. aiMesh *pcMesh = apcOutMeshes.back();
  422. pcMesh->mNumFaces = iFaces;
  423. pcMesh->mNumVertices = iVertices;
  424. pcMesh->mFaces = new aiFace[iFaces];
  425. pcMesh->mVertices = new aiVector3D[iVertices];
  426. pcMesh->mMaterialIndex = i;
  427. if ((*j) & 0x2) pcMesh->mNormals = new aiVector3D[iVertices];
  428. if ((*j) & 0x4) {
  429. pcMesh->mTangents = new aiVector3D[iVertices];
  430. pcMesh->mBitangents = new aiVector3D[iVertices];
  431. }
  432. iFaces = 0;
  433. while ((*j) & (0x100 << iFaces)) {
  434. pcMesh->mTextureCoords[iFaces] = new aiVector3D[iVertices];
  435. if ((*j) & (0x10000 << iFaces))
  436. pcMesh->mNumUVComponents[iFaces] = 3;
  437. else
  438. pcMesh->mNumUVComponents[iFaces] = 2;
  439. iFaces++;
  440. }
  441. iFaces = 0;
  442. while ((*j) & (0x1000000 << iFaces))
  443. pcMesh->mColors[iFaces++] = new aiColor4D[iVertices];
  444. // fill the mesh ...
  445. unsigned int aiTemp[2] = { 0, 0 };
  446. CollectData(pScene, pScene->mRootNode, i, *j, pcMesh, aiTemp, &s[0]);
  447. }
  448. }
  449. }
  450. // If no meshes are referenced in the node graph it is possible that we get no output meshes.
  451. if (apcOutMeshes.empty()) {
  452. throw DeadlyImportError("No output meshes: all meshes are orphaned and are not referenced by any nodes");
  453. } else {
  454. // now delete all meshes in the scene and build a new mesh list
  455. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  456. aiMesh *mesh = pScene->mMeshes[i];
  457. mesh->mNumBones = 0;
  458. mesh->mBones = nullptr;
  459. // we're reusing the face index arrays. avoid destruction
  460. for (unsigned int a = 0; a < mesh->mNumFaces; ++a) {
  461. mesh->mFaces[a].mNumIndices = 0;
  462. mesh->mFaces[a].mIndices = nullptr;
  463. }
  464. delete mesh;
  465. // Invalidate the contents of the old mesh array. We will most
  466. // likely have less output meshes now, so the last entries of
  467. // the mesh array are not overridden. We set them to nullptr to
  468. // make sure the developer gets notified when his application
  469. // attempts to access these fields ...
  470. mesh = nullptr;
  471. }
  472. // It is impossible that we have more output meshes than
  473. // input meshes, so we can easily reuse the old mesh array
  474. pScene->mNumMeshes = (unsigned int)apcOutMeshes.size();
  475. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  476. pScene->mMeshes[i] = apcOutMeshes[i];
  477. }
  478. }
  479. }
  480. // remove all animations from the scene
  481. for (unsigned int i = 0; i < pScene->mNumAnimations; ++i)
  482. delete pScene->mAnimations[i];
  483. delete[] pScene->mAnimations;
  484. pScene->mAnimations = nullptr;
  485. pScene->mNumAnimations = 0;
  486. // --- we need to keep all cameras and lights
  487. for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {
  488. aiCamera *cam = pScene->mCameras[i];
  489. const aiNode *nd = pScene->mRootNode->FindNode(cam->mName);
  490. ai_assert(nullptr != nd);
  491. // multiply all properties of the camera with the absolute
  492. // transformation of the corresponding node
  493. cam->mPosition = nd->mTransformation * cam->mPosition;
  494. cam->mLookAt = nd->mTransformation * cam->mLookAt;
  495. cam->mUp = aiMatrix3x3(nd->mTransformation) * cam->mUp;
  496. }
  497. for (unsigned int i = 0; i < pScene->mNumLights; ++i) {
  498. aiLight *l = pScene->mLights[i];
  499. const aiNode *nd = pScene->mRootNode->FindNode(l->mName);
  500. ai_assert(nullptr != nd);
  501. // multiply all properties of the camera with the absolute
  502. // transformation of the corresponding node
  503. l->mPosition = nd->mTransformation * l->mPosition;
  504. l->mDirection = aiMatrix3x3(nd->mTransformation) * l->mDirection;
  505. l->mUp = aiMatrix3x3(nd->mTransformation) * l->mUp;
  506. }
  507. if (!configKeepHierarchy) {
  508. // now delete all nodes in the scene and build a new
  509. // flat node graph with a root node and some level 1 children
  510. aiNode *newRoot = new aiNode();
  511. newRoot->mName = pScene->mRootNode->mName;
  512. delete pScene->mRootNode;
  513. pScene->mRootNode = newRoot;
  514. if (1 == pScene->mNumMeshes && !pScene->mNumLights && !pScene->mNumCameras) {
  515. pScene->mRootNode->mNumMeshes = 1;
  516. pScene->mRootNode->mMeshes = new unsigned int[1];
  517. pScene->mRootNode->mMeshes[0] = 0;
  518. } else {
  519. pScene->mRootNode->mNumChildren = pScene->mNumMeshes + pScene->mNumLights + pScene->mNumCameras;
  520. aiNode **nodes = pScene->mRootNode->mChildren = new aiNode *[pScene->mRootNode->mNumChildren];
  521. // generate mesh nodes
  522. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i, ++nodes) {
  523. aiNode *pcNode = new aiNode();
  524. *nodes = pcNode;
  525. pcNode->mParent = pScene->mRootNode;
  526. pcNode->mName = pScene->mMeshes[i]->mName;
  527. // setup mesh indices
  528. pcNode->mNumMeshes = 1;
  529. pcNode->mMeshes = new unsigned int[1];
  530. pcNode->mMeshes[0] = i;
  531. }
  532. // generate light nodes
  533. for (unsigned int i = 0; i < pScene->mNumLights; ++i, ++nodes) {
  534. aiNode *pcNode = new aiNode();
  535. *nodes = pcNode;
  536. pcNode->mParent = pScene->mRootNode;
  537. pcNode->mName.length = ai_snprintf(pcNode->mName.data, MAXLEN, "light_%u", i);
  538. pScene->mLights[i]->mName = pcNode->mName;
  539. }
  540. // generate camera nodes
  541. for (unsigned int i = 0; i < pScene->mNumCameras; ++i, ++nodes) {
  542. aiNode *pcNode = new aiNode();
  543. *nodes = pcNode;
  544. pcNode->mParent = pScene->mRootNode;
  545. pcNode->mName.length = ::ai_snprintf(pcNode->mName.data, MAXLEN, "cam_%u", i);
  546. pScene->mCameras[i]->mName = pcNode->mName;
  547. }
  548. }
  549. } else {
  550. // ... and finally set the transformation matrix of all nodes to identity
  551. MakeIdentityTransform(pScene->mRootNode);
  552. }
  553. if (configNormalize) {
  554. // compute the boundary of all meshes
  555. aiVector3D min, max;
  556. MinMaxChooser<aiVector3D>()(min, max);
  557. for (unsigned int a = 0; a < pScene->mNumMeshes; ++a) {
  558. aiMesh *m = pScene->mMeshes[a];
  559. for (unsigned int i = 0; i < m->mNumVertices; ++i) {
  560. min = std::min(m->mVertices[i], min);
  561. max = std::max(m->mVertices[i], max);
  562. }
  563. }
  564. // find the dominant axis
  565. aiVector3D d = max - min;
  566. const ai_real div = std::max(d.x, std::max(d.y, d.z)) * ai_real(0.5);
  567. d = min + d * (ai_real)0.5;
  568. for (unsigned int a = 0; a < pScene->mNumMeshes; ++a) {
  569. aiMesh *m = pScene->mMeshes[a];
  570. for (unsigned int i = 0; i < m->mNumVertices; ++i) {
  571. m->mVertices[i] = (m->mVertices[i] - d) / div;
  572. }
  573. }
  574. }
  575. // print statistics
  576. if (!DefaultLogger::isNullLogger()) {
  577. ASSIMP_LOG_DEBUG("PretransformVerticesProcess finished");
  578. ASSIMP_LOG_INFO("Removed ", iOldNodes, " nodes and ", iOldAnimationChannels, " animation channels (",
  579. CountNodes(pScene->mRootNode), " output nodes)");
  580. ASSIMP_LOG_INFO("Kept ", pScene->mNumLights, " lights and ", pScene->mNumCameras, " cameras.");
  581. ASSIMP_LOG_INFO("Moved ", iOldMeshes, " meshes to WCS (number of output meshes: ", pScene->mNumMeshes, ")");
  582. }
  583. }