PretransformVertices.cpp 24 KB

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