Importer.cpp 29 KB

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