Importer.cpp 28 KB

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