PretransformVertices.cpp 29 KB

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