Importer.cpp 33 KB

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