Importer.cpp 25 KB

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