Importer.cpp 26 KB

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