Importer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 Implementation of the CPP-API class #Importer */
  35. // STL/CSL heades
  36. #include <fstream>
  37. #include <string>
  38. // public Assimp API
  39. #include "../include/assimp.hpp"
  40. #include "../include/aiAssert.h"
  41. #include "../include/aiScene.h"
  42. #include "../include/aiPostProcess.h"
  43. #include "../include/DefaultLogger.h"
  44. // internal headers
  45. #include "BaseImporter.h"
  46. #include "BaseProcess.h"
  47. #include "DefaultIOStream.h"
  48. #include "DefaultIOSystem.h"
  49. // Importers
  50. #if (!defined AI_BUILD_NO_X_IMPORTER)
  51. # include "XFileImporter.h"
  52. #endif
  53. #if (!defined AI_BUILD_NO_3DS_IMPORTER)
  54. # include "3DSLoader.h"
  55. #endif
  56. #if (!defined AI_BUILD_NO_MD3_IMPORTER)
  57. # include "MD3Loader.h"
  58. #endif
  59. #if (!defined AI_BUILD_NO_MDL_IMPORTER)
  60. # include "MDLLoader.h"
  61. #endif
  62. #if (!defined AI_BUILD_NO_MD2_IMPORTER)
  63. # include "MD2Loader.h"
  64. #endif
  65. #if (!defined AI_BUILD_NO_PLY_IMPORTER)
  66. # include "PlyLoader.h"
  67. #endif
  68. #if (!defined AI_BUILD_NO_ASE_IMPORTER)
  69. # include "ASELoader.h"
  70. #endif
  71. #if (!defined AI_BUILD_NO_OBJ_IMPORTER)
  72. # include "ObjFileImporter.h"
  73. #endif
  74. #if (!defined AI_BUILD_NO_HMP_IMPORTER)
  75. # include "HMPLoader.h"
  76. #endif
  77. #if (!defined AI_BUILD_NO_SMD_IMPORTER)
  78. # include "SMDLoader.h"
  79. #endif
  80. #if 0
  81. #if (!defined AI_BUILD_NO_MDR_IMPORTER)
  82. # include "MDRLoader.h"
  83. #endif
  84. #endif
  85. #if (!defined AI_BUILD_NO_MDC_IMPORTER)
  86. # include "MDCLoader.h"
  87. #endif
  88. #if (!defined AI_BUILD_NO_MD5_IMPORTER)
  89. # include "MD5Loader.h"
  90. #endif
  91. #if (!defined AI_BUILD_NO_STL_IMPORTER)
  92. # include "STLLoader.h"
  93. #endif
  94. #if (!defined AI_BUILD_NO_LWO_IMPORTER)
  95. # include "LWOLoader.h"
  96. #endif
  97. // PostProcess-Steps
  98. #if (!defined AI_BUILD_NO_CALCTANGENTS_PROCESS)
  99. # include "CalcTangentsProcess.h"
  100. #endif
  101. #if (!defined AI_BUILD_NO_JOINVERTICES_PROCESS)
  102. # include "JoinVerticesProcess.h"
  103. #endif
  104. #if (!defined AI_BUILD_NO_CONVERTTOLH_PROCESS)
  105. # include "ConvertToLHProcess.h"
  106. #endif
  107. #if (!defined AI_BUILD_NO_TRIANGULATE_PROCESS)
  108. # include "TriangulateProcess.h"
  109. #endif
  110. #if (!defined AI_BUILD_NO_GENFACENORMALS_PROCESS)
  111. # include "GenFaceNormalsProcess.h"
  112. #endif
  113. #if (!defined AI_BUILD_NO_GENVERTEXNORMALS_PROCESS)
  114. # include "GenVertexNormalsProcess.h"
  115. #endif
  116. #if (!defined AI_BUILD_NO_KILLNORMALS_PROCESS)
  117. # include "KillNormalsProcess.h"
  118. #endif
  119. #if (!defined AI_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  120. # include "SplitLargeMeshes.h"
  121. #endif
  122. #if (!defined AI_BUILD_NO_PRETRANSFORMVERTICES_PROCESS)
  123. # include "PretransformVertices.h"
  124. #endif
  125. #if (!defined AI_BUILD_NO_LIMITBONEWEIGHTS_PROCESS)
  126. # include "LimitBoneWeightsProcess.h"
  127. #endif
  128. #if (!defined AI_BUILD_NO_VALIDATEDS_PROCESS)
  129. # include "ValidateDataStructure.h"
  130. #endif
  131. #if (!defined AI_BUILD_NO_IMPROVECACHELOCALITY_PROCESS)
  132. # include "ImproveCacheLocality.h"
  133. #endif
  134. #if (!defined AI_BUILD_NO_FIXINFACINGNORMALS_PROCESS)
  135. # include "FixNormalsStep.h"
  136. #endif
  137. #if (!defined AI_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS)
  138. # include "RemoveRedundantMaterials.h"
  139. #endif
  140. using namespace Assimp;
  141. // ------------------------------------------------------------------------------------------------
  142. // Constructor.
  143. Importer::Importer() :
  144. mIOHandler(NULL),
  145. mScene(NULL),
  146. mErrorString("")
  147. {
  148. // allocate a default IO handler
  149. mIOHandler = new DefaultIOSystem;
  150. mIsDefaultHandler = true;
  151. bExtraVerbose = false; // disable extra verbose mode by default
  152. // add an instance of each worker class here
  153. #if (!defined AI_BUILD_NO_X_IMPORTER)
  154. mImporter.push_back( new XFileImporter());
  155. #endif
  156. #if (!defined AI_BUILD_NO_OBJ_IMPORTER)
  157. mImporter.push_back( new ObjFileImporter());
  158. #endif
  159. #if (!defined AI_BUILD_NO_3DS_IMPORTER)
  160. mImporter.push_back( new Dot3DSImporter());
  161. #endif
  162. #if (!defined AI_BUILD_NO_MD3_IMPORTER)
  163. mImporter.push_back( new MD3Importer());
  164. #endif
  165. #if (!defined AI_BUILD_NO_MD2_IMPORTER)
  166. mImporter.push_back( new MD2Importer());
  167. #endif
  168. #if (!defined AI_BUILD_NO_PLY_IMPORTER)
  169. mImporter.push_back( new PLYImporter());
  170. #endif
  171. #if (!defined AI_BUILD_NO_MDL_IMPORTER)
  172. mImporter.push_back( new MDLImporter());
  173. #endif
  174. #if (!defined AI_BUILD_NO_ASE_IMPORTER)
  175. mImporter.push_back( new ASEImporter());
  176. #endif
  177. #if (!defined AI_BUILD_NO_HMP_IMPORTER)
  178. mImporter.push_back( new HMPImporter());
  179. #endif
  180. #if (!defined AI_BUILD_NO_SMD_IMPORTER)
  181. mImporter.push_back( new SMDImporter());
  182. #endif
  183. #if 0
  184. #if (!defined AI_BUILD_NO_MDR_IMPORTER)
  185. mImporter.push_back( new MDRImporter());
  186. #endif
  187. #endif
  188. #if (!defined AI_BUILD_NO_MDC_IMPORTER)
  189. mImporter.push_back( new MDCImporter());
  190. #endif
  191. #if (!defined AI_BUILD_NO_MD5_IMPORTER)
  192. mImporter.push_back( new MD5Importer());
  193. #endif
  194. #if (!defined AI_BUILD_NO_STL_IMPORTER)
  195. mImporter.push_back( new STLImporter());
  196. #endif
  197. #if (!defined AI_BUILD_NO_LWO_IMPORTER)
  198. mImporter.push_back( new LWOImporter());
  199. #endif
  200. // add an instance of each post processing step here in the order
  201. // of sequence it is executed
  202. #if (!defined AI_BUILD_NO_VALIDATEDS_PROCESS)
  203. mPostProcessingSteps.push_back( new ValidateDSProcess()); // must be first
  204. #endif
  205. #if (!defined AI_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS)
  206. mPostProcessingSteps.push_back( new RemoveRedundantMatsProcess());
  207. #endif
  208. #if (!defined AI_BUILD_NO_TRIANGULATE_PROCESS)
  209. mPostProcessingSteps.push_back( new TriangulateProcess());
  210. #endif
  211. #if (!defined AI_BUILD_NO_PRETRANSFORMVERTICES_PROCESS)
  212. mPostProcessingSteps.push_back( new PretransformVertices());
  213. #endif
  214. #if (!defined AI_BUILD_NO_FIXINFACINGNORMALS_PROCESS)
  215. mPostProcessingSteps.push_back( new FixInfacingNormalsProcess());
  216. #endif
  217. #if (!defined AI_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  218. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Triangle());
  219. #endif
  220. #if (!defined AI_BUILD_NO_KILLNORMALS_PROCESS)
  221. mPostProcessingSteps.push_back( new KillNormalsProcess());
  222. #endif
  223. #if (!defined AI_BUILD_NO_GENFACENORMALS_PROCESS)
  224. mPostProcessingSteps.push_back( new GenFaceNormalsProcess());
  225. #endif
  226. #if (!defined AI_BUILD_NO_GENVERTEXNORMALS_PROCESS)
  227. mPostProcessingSteps.push_back( new GenVertexNormalsProcess());
  228. #endif
  229. #if (!defined AI_BUILD_NO_CALCTANGENTS_PROCESS)
  230. mPostProcessingSteps.push_back( new CalcTangentsProcess());
  231. #endif
  232. #if (!defined AI_BUILD_NO_JOINVERTICES_PROCESS)
  233. mPostProcessingSteps.push_back( new JoinVerticesProcess());
  234. #endif
  235. #if (!defined AI_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  236. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Vertex());
  237. #endif
  238. #if (!defined AI_BUILD_NO_CONVERTTOLH_PROCESS)
  239. mPostProcessingSteps.push_back( new ConvertToLHProcess());
  240. #endif
  241. #if (!defined AI_BUILD_NO_LIMITBONEWEIGHTS_PROCESS)
  242. mPostProcessingSteps.push_back( new LimitBoneWeightsProcess());
  243. #endif
  244. #if (!defined AI_BUILD_NO_IMPROVECACHELOCALITY_PROCESS)
  245. mPostProcessingSteps.push_back( new ImproveCacheLocalityProcess());
  246. #endif
  247. }
  248. // ------------------------------------------------------------------------------------------------
  249. // Destructor.
  250. Importer::~Importer()
  251. {
  252. for( unsigned int a = 0; a < mImporter.size(); a++)
  253. delete mImporter[a];
  254. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  255. delete mPostProcessingSteps[a];
  256. // delete the assigned IO handler
  257. delete mIOHandler;
  258. // kill imported scene. Destructors should do that recursivly
  259. delete mScene;
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. // Empty and private copy constructor
  263. Importer::Importer(const Importer &other)
  264. {
  265. // empty
  266. }
  267. // ------------------------------------------------------------------------------------------------
  268. aiReturn Importer::RegisterLoader(BaseImporter* pImp)
  269. {
  270. ai_assert(NULL != pImp);
  271. // check whether we would have two loaders for the same file extension now
  272. std::string st;
  273. pImp->GetExtensionList(st);
  274. #ifdef _DEBUG
  275. const char* sz = ::strtok(st.c_str(),";");
  276. while (sz)
  277. {
  278. if (IsExtensionSupported(std::string(sz)))
  279. {
  280. DefaultLogger::get()->error(std::string( "The file extension " ) + sz + " is already in use");
  281. return AI_FAILURE;
  282. }
  283. sz = ::strtok(NULL,";");
  284. }
  285. #endif
  286. // add the loader
  287. this->mImporter.push_back(pImp);
  288. DefaultLogger::get()->info("Registering custom importer: " + st);
  289. return AI_SUCCESS;
  290. }
  291. // ------------------------------------------------------------------------------------------------
  292. aiReturn Importer::UnregisterLoader(BaseImporter* pImp)
  293. {
  294. ai_assert(NULL != pImp);
  295. for (std::vector<BaseImporter*>::iterator
  296. it = mImporter.begin(),end = mImporter.end();
  297. it != end;++it)
  298. {
  299. if (pImp == (*it))
  300. {
  301. mImporter.erase(it);
  302. std::string st;
  303. pImp->GetExtensionList(st);
  304. DefaultLogger::get()->info("Unregistering custom importer: " + st);
  305. return AI_SUCCESS;
  306. }
  307. }
  308. DefaultLogger::get()->warn("Unable to remove importer: importer not found");
  309. return AI_FAILURE;
  310. }
  311. // ------------------------------------------------------------------------------------------------
  312. // Supplies a custom IO handler to the importer to open and access files.
  313. void Importer::SetIOHandler( IOSystem* pIOHandler)
  314. {
  315. if (!pIOHandler)
  316. {
  317. delete mIOHandler;
  318. mIOHandler = new DefaultIOSystem();
  319. mIsDefaultHandler = true;
  320. }
  321. else if (mIOHandler != pIOHandler)
  322. {
  323. delete mIOHandler;
  324. mIOHandler = pIOHandler;
  325. mIsDefaultHandler = false;
  326. }
  327. return;
  328. }
  329. // ------------------------------------------------------------------------------------------------
  330. IOSystem* Importer::GetIOHandler()
  331. {
  332. return mIOHandler;
  333. }
  334. // ------------------------------------------------------------------------------------------------
  335. bool Importer::IsDefaultIOHandler()
  336. {
  337. return mIsDefaultHandler;
  338. }
  339. #ifdef _DEBUG
  340. // ------------------------------------------------------------------------------------------------
  341. // Validate post process step flags
  342. bool ValidateFlags(unsigned int pFlags)
  343. {
  344. if (pFlags & aiProcess_GenSmoothNormals &&
  345. pFlags & aiProcess_GenNormals)
  346. {
  347. DefaultLogger::get()->error("aiProcess_GenSmoothNormals and aiProcess_GenNormals "
  348. "may not be specified together");
  349. return false;
  350. }
  351. return true;
  352. }
  353. #endif // ! DEBUG
  354. // ------------------------------------------------------------------------------------------------
  355. // Reads the given file and returns its contents if successful.
  356. const aiScene* Importer::ReadFile( const std::string& pFile, unsigned int pFlags)
  357. {
  358. // validate the flags
  359. ai_assert(ValidateFlags(pFlags));
  360. // check whether this Importer instance has already loaded
  361. // a scene. In this case we need to delete the old one
  362. if (this->mScene)
  363. {
  364. delete mScene;
  365. this->mScene = NULL;
  366. }
  367. // first check if the file is accessable at all
  368. if( !mIOHandler->Exists( pFile))
  369. {
  370. mErrorString = "Unable to open file \"" + pFile + "\".";
  371. DefaultLogger::get()->error(mErrorString);
  372. return NULL;
  373. }
  374. // find an worker class which can handle the file
  375. BaseImporter* imp = NULL;
  376. for( unsigned int a = 0; a < mImporter.size(); a++)
  377. {
  378. if( mImporter[a]->CanRead( pFile, mIOHandler))
  379. {
  380. imp = mImporter[a];
  381. break;
  382. }
  383. }
  384. // put a proper error message if no suitable importer was found
  385. if( !imp)
  386. {
  387. mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  388. DefaultLogger::get()->error(mErrorString);
  389. return NULL;
  390. }
  391. // dispatch the reading to the worker class for this format
  392. imp->SetupProperties( this );
  393. mScene = imp->ReadFile( pFile, mIOHandler);
  394. // if successful, apply all active post processing steps to the imported data
  395. if( mScene)
  396. {
  397. #ifdef _DEBUG
  398. if (bExtraVerbose)
  399. {
  400. pFlags |= aiProcess_ValidateDataStructure;
  401. // use the MSB to tell the ValidateDS-Step that e're in extra verbose mode
  402. // TODO: temporary solution, clean up later
  403. mScene->mFlags |= 0x80000000;
  404. }
  405. #else
  406. if (bExtraVerbose)DefaultLogger::get()->warn("Not a debug build, ignoring extra verbose setting");
  407. #endif // ! DEBUG
  408. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  409. {
  410. BaseProcess* process = mPostProcessingSteps[a];
  411. if( process->IsActive( pFlags))
  412. {
  413. process->SetupProperties( this );
  414. process->ExecuteOnScene ( this );
  415. }
  416. if( !mScene)break;
  417. #ifdef _DEBUG
  418. // if the extra verbose mode is active execute the
  419. // VaidateDataStructureStep again after each step
  420. if (bExtraVerbose && a)
  421. {
  422. DefaultLogger::get()->debug("Extra verbose: revalidating data structures");
  423. ((ValidateDSProcess*)mPostProcessingSteps[0])->ExecuteOnScene (this);
  424. if( !mScene)
  425. {
  426. DefaultLogger::get()->error("Extra verbose: failed to revalidate data structures");
  427. break;
  428. }
  429. }
  430. #endif // ! DEBUG
  431. }
  432. #ifdef _DEBUG
  433. if (bExtraVerbose)mScene->mFlags &= ~0x80000000;
  434. #endif // ! DEBUG
  435. }
  436. // if failed, extract the error string
  437. else if( !mScene)mErrorString = imp->GetErrorText();
  438. // either successful or failure - the pointer expresses it anyways
  439. return mScene;
  440. }
  441. // ------------------------------------------------------------------------------------------------
  442. // Helper function to check whether an extension is supported by ASSIMP
  443. bool Importer::IsExtensionSupported(const std::string& szExtension)
  444. {
  445. for (std::vector<BaseImporter*>::const_iterator
  446. i = this->mImporter.begin();
  447. i != this->mImporter.end();++i)
  448. {
  449. // pass the file extension to the CanRead(..,NULL)-method
  450. if ((*i)->CanRead(szExtension,NULL))return true;
  451. }
  452. return false;
  453. }
  454. // ------------------------------------------------------------------------------------------------
  455. // Helper function to build a list of all file extensions supported by ASSIMP
  456. void Importer::GetExtensionList(std::string& szOut)
  457. {
  458. unsigned int iNum = 0;
  459. for (std::vector<BaseImporter*>::const_iterator
  460. i = this->mImporter.begin();
  461. i != this->mImporter.end();++i,++iNum)
  462. {
  463. // insert a comma as delimiter character
  464. if (0 != iNum)
  465. szOut.append(";");
  466. (*i)->GetExtensionList(szOut);
  467. }
  468. return;
  469. }
  470. // ------------------------------------------------------------------------------------------------
  471. // Set a configuration property
  472. int Importer::SetProperty(const char* szName, int iValue)
  473. {
  474. ai_assert(NULL != szName);
  475. // search in the list ...
  476. for (std::vector<IntPropertyInfo>::iterator
  477. i = this->mIntProperties.begin();
  478. i != this->mIntProperties.end();++i)
  479. {
  480. if (0 == ::strcmp( (*i).name.c_str(), szName ))
  481. {
  482. int iOld = (*i).value;
  483. (*i).value = iValue;
  484. return iOld;
  485. }
  486. }
  487. // the property is not yet in the list ...
  488. this->mIntProperties.push_back( IntPropertyInfo() );
  489. IntPropertyInfo& me = this->mIntProperties.back();
  490. me.name = std::string(szName);
  491. me.value = iValue;
  492. return AI_PROPERTY_WAS_NOT_EXISTING;
  493. }
  494. // ------------------------------------------------------------------------------------------------
  495. // Get a configuration property
  496. int Importer::GetProperty(const char* szName,
  497. int iErrorReturn /*= 0xffffffff*/) const
  498. {
  499. ai_assert(NULL != szName);
  500. // search in the list ...
  501. for (std::vector<IntPropertyInfo>::const_iterator
  502. i = this->mIntProperties.begin();
  503. i != this->mIntProperties.end();++i)
  504. {
  505. if (0 == ::strcmp( (*i).name.c_str(), szName ))
  506. {
  507. return (*i).value;
  508. }
  509. }
  510. return iErrorReturn;
  511. }
  512. // ------------------------------------------------------------------------------------------------
  513. void AddNodeWeight(unsigned int& iScene,const aiNode* pcNode)
  514. {
  515. iScene += sizeof(aiNode);
  516. iScene += sizeof(unsigned int) * pcNode->mNumMeshes;
  517. iScene += sizeof(void*) * pcNode->mNumChildren;
  518. for (unsigned int i = 0; i < pcNode->mNumChildren;++i)
  519. AddNodeWeight(iScene,pcNode->mChildren[i]);
  520. }
  521. // ------------------------------------------------------------------------------------------------
  522. // Get the memory requirements of the scene
  523. void Importer::GetMemoryRequirements(aiMemoryInfo& in) const
  524. {
  525. in.aiMemoryInfo::aiMemoryInfo();
  526. if (!this->mScene)return;
  527. in.total = sizeof(aiScene);
  528. // add all meshes
  529. for (unsigned int i = 0; i < mScene->mNumMeshes;++i)
  530. {
  531. in.meshes += sizeof(aiMesh);
  532. if (mScene->mMeshes[i]->HasPositions())
  533. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  534. if (mScene->mMeshes[i]->HasNormals())
  535. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  536. if (mScene->mMeshes[i]->HasTangentsAndBitangents())
  537. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices * 2;
  538. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a)
  539. {
  540. if (mScene->mMeshes[i]->HasVertexColors(a))
  541. in.meshes += sizeof(aiColor4D) * mScene->mMeshes[i]->mNumVertices;
  542. else break;
  543. }
  544. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a)
  545. {
  546. if (mScene->mMeshes[i]->HasTextureCoords(a))
  547. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  548. else break;
  549. }
  550. if (mScene->mMeshes[i]->HasBones())
  551. {
  552. in.meshes += sizeof(void*) * mScene->mMeshes[i]->mNumBones;
  553. for (unsigned int p = 0; p < mScene->mMeshes[i]->mNumBones;++p)
  554. {
  555. in.meshes += sizeof(aiBone);
  556. in.meshes += mScene->mMeshes[i]->mBones[p]->mNumWeights * sizeof(aiVertexWeight);
  557. }
  558. }
  559. in.meshes += (sizeof(aiFace) + 3 * sizeof(unsigned int))*mScene->mMeshes[i]->mNumFaces;
  560. }
  561. in.total += in.meshes;
  562. // add all embedded textures
  563. for (unsigned int i = 0; i < mScene->mNumTextures;++i)
  564. {
  565. const aiTexture* pc = mScene->mTextures[i];
  566. in.textures += sizeof(aiTexture);
  567. if (pc->mHeight)
  568. {
  569. in.textures += 4 * pc->mHeight * pc->mWidth;
  570. }
  571. else in.textures += pc->mWidth;
  572. }
  573. in.total += in.textures;
  574. // add all animations
  575. for (unsigned int i = 0; i < mScene->mNumAnimations;++i)
  576. {
  577. const aiAnimation* pc = mScene->mAnimations[i];
  578. in.animations += sizeof(aiAnimation);
  579. // add all bone anims
  580. for (unsigned int a = 0; a < pc->mNumBones;++a)
  581. {
  582. const aiBoneAnim* pc2 = pc->mBones[i];
  583. in.animations += sizeof(aiBoneAnim);
  584. in.animations += pc2->mNumPositionKeys * sizeof(aiVectorKey);
  585. in.animations += pc2->mNumScalingKeys * sizeof(aiVectorKey);
  586. in.animations += pc2->mNumRotationKeys * sizeof(aiQuatKey);
  587. }
  588. }
  589. in.total += in.animations;
  590. // add all nodes
  591. AddNodeWeight(in.nodes,mScene->mRootNode);
  592. in.total += in.nodes;
  593. // add all materials
  594. for (unsigned int i = 0; i < mScene->mNumMaterials;++i)
  595. {
  596. const aiMaterial* pc = mScene->mMaterials[i];
  597. in.materials += sizeof(aiMaterial);
  598. in.materials += pc->mNumAllocated * sizeof(void*);
  599. for (unsigned int a = 0; a < pc->mNumProperties;++a)
  600. {
  601. in.materials += pc->mProperties[a]->mDataLength;
  602. }
  603. }
  604. in.total += in.materials;
  605. return;
  606. }