PretransformVertices.cpp 25 KB

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