PretransformVertices.cpp 25 KB

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