No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

723 líneas
24 KiB

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