PretransformVertices.cpp 29 KB

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