Importer.cpp 22 KB

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