PretransformVertices.cpp 25 KB

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