Importer.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, 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 Importer.cpp
  35. * @brief Implementation of the CPP-API class #Importer
  36. */
  37. #include "AssimpPCH.h"
  38. #include "../include/aiVersion.h"
  39. // ------------------------------------------------------------------------------------------------
  40. /* Uncomment this line to prevent Assimp from catching unknown exceptions.
  41. *
  42. * Note that any Exception except DeadlyImportError may lead to
  43. * undefined behaviour -> loaders could remain in an unusable state and
  44. * further imports with the same Importer instance could fail/crash/burn ...
  45. */
  46. // ------------------------------------------------------------------------------------------------
  47. #define ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  48. // ------------------------------------------------------------------------------------------------
  49. // Internal headers
  50. // ------------------------------------------------------------------------------------------------
  51. #include "BaseImporter.h"
  52. #include "BaseProcess.h"
  53. #include "DefaultIOStream.h"
  54. #include "DefaultIOSystem.h"
  55. #include "DefaultProgressHandler.h"
  56. #include "GenericProperty.h"
  57. #include "ProcessHelper.h"
  58. #include "ScenePreprocessor.h"
  59. #include "MemoryIOWrapper.h"
  60. #include "Profiler.h"
  61. #include "TinyFormatter.h"
  62. using namespace Assimp::Profiling;
  63. using namespace Assimp::Formatter;
  64. // ------------------------------------------------------------------------------------------------
  65. // Importers
  66. // (include_new_importers_here)
  67. // ------------------------------------------------------------------------------------------------
  68. #ifndef ASSIMP_BUILD_NO_X_IMPORTER
  69. # include "XFileImporter.h"
  70. #endif
  71. #ifndef ASSIMP_BUILD_NO_3DS_IMPORTER
  72. # include "3DSLoader.h"
  73. #endif
  74. #ifndef ASSIMP_BUILD_NO_MD3_IMPORTER
  75. # include "MD3Loader.h"
  76. #endif
  77. #ifndef ASSIMP_BUILD_NO_MDL_IMPORTER
  78. # include "MDLLoader.h"
  79. #endif
  80. #ifndef ASSIMP_BUILD_NO_MD2_IMPORTER
  81. # include "MD2Loader.h"
  82. #endif
  83. #ifndef ASSIMP_BUILD_NO_PLY_IMPORTER
  84. # include "PlyLoader.h"
  85. #endif
  86. #ifndef ASSIMP_BUILD_NO_ASE_IMPORTER
  87. # include "ASELoader.h"
  88. #endif
  89. #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
  90. # include "ObjFileImporter.h"
  91. #endif
  92. #ifndef ASSIMP_BUILD_NO_HMP_IMPORTER
  93. # include "HMPLoader.h"
  94. #endif
  95. #ifndef ASSIMP_BUILD_NO_SMD_IMPORTER
  96. # include "SMDLoader.h"
  97. #endif
  98. #ifndef ASSIMP_BUILD_NO_MDC_IMPORTER
  99. # include "MDCLoader.h"
  100. #endif
  101. #ifndef ASSIMP_BUILD_NO_MD5_IMPORTER
  102. # include "MD5Loader.h"
  103. #endif
  104. #ifndef ASSIMP_BUILD_NO_STL_IMPORTER
  105. # include "STLLoader.h"
  106. #endif
  107. #ifndef ASSIMP_BUILD_NO_LWO_IMPORTER
  108. # include "LWOLoader.h"
  109. #endif
  110. #ifndef ASSIMP_BUILD_NO_DXF_IMPORTER
  111. # include "DXFLoader.h"
  112. #endif
  113. #ifndef ASSIMP_BUILD_NO_NFF_IMPORTER
  114. # include "NFFLoader.h"
  115. #endif
  116. #ifndef ASSIMP_BUILD_NO_RAW_IMPORTER
  117. # include "RawLoader.h"
  118. #endif
  119. #ifndef ASSIMP_BUILD_NO_OFF_IMPORTER
  120. # include "OFFLoader.h"
  121. #endif
  122. #ifndef ASSIMP_BUILD_NO_AC_IMPORTER
  123. # include "ACLoader.h"
  124. #endif
  125. #ifndef ASSIMP_BUILD_NO_BVH_IMPORTER
  126. # include "BVHLoader.h"
  127. #endif
  128. #ifndef ASSIMP_BUILD_NO_IRRMESH_IMPORTER
  129. # include "IRRMeshLoader.h"
  130. #endif
  131. #ifndef ASSIMP_BUILD_NO_IRR_IMPORTER
  132. # include "IRRLoader.h"
  133. #endif
  134. #ifndef ASSIMP_BUILD_NO_Q3D_IMPORTER
  135. # include "Q3DLoader.h"
  136. #endif
  137. #ifndef ASSIMP_BUILD_NO_B3D_IMPORTER
  138. # include "B3DImporter.h"
  139. #endif
  140. #ifndef ASSIMP_BUILD_NO_COLLADA_IMPORTER
  141. # include "ColladaLoader.h"
  142. #endif
  143. #ifndef ASSIMP_BUILD_NO_TERRAGEN_IMPORTER
  144. # include "TerragenLoader.h"
  145. #endif
  146. #ifndef ASSIMP_BUILD_NO_CSM_IMPORTER
  147. # include "CSMLoader.h"
  148. #endif
  149. #ifndef ASSIMP_BUILD_NO_3D_IMPORTER
  150. # include "UnrealLoader.h"
  151. #endif
  152. #ifndef ASSIMP_BUILD_NO_LWS_IMPORTER
  153. # include "LWSLoader.h"
  154. #endif
  155. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  156. # include "OgreImporter.h"
  157. #endif
  158. #ifndef ASSIMP_BUILD_NO_MS3D_IMPORTER
  159. # include "MS3DLoader.h"
  160. #endif
  161. #ifndef ASSIMP_BUILD_NO_COB_IMPORTER
  162. # include "COBLoader.h"
  163. #endif
  164. #ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
  165. # include "BlenderLoader.h"
  166. #endif
  167. //#ifndef ASSIMP_BUILD_NO_SWORDOFMOONLIGHT_IMPORTER
  168. //# include "SomLoader.h"
  169. //#endif
  170. #ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
  171. # include "Q3BSPFileImporter.h"
  172. #endif
  173. #ifndef ASSIMP_BUILD_NO_NDO_IMPORTER
  174. # include "NDOLoader.h"
  175. #endif
  176. // ------------------------------------------------------------------------------------------------
  177. // Post processing-Steps
  178. // ------------------------------------------------------------------------------------------------
  179. #ifndef ASSIMP_BUILD_NO_CALCTANGENTS_PROCESS
  180. # include "CalcTangentsProcess.h"
  181. #endif
  182. #ifndef ASSIMP_BUILD_NO_JOINVERTICES_PROCESS
  183. # include "JoinVerticesProcess.h"
  184. #endif
  185. #if !(defined ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS && defined ASSIMP_BUILD_NO_FLIPUVS_PROCESS && defined ASSIMP_BUILD_NO_FLIPWINDINGORDER_PROCESS)
  186. # include "ConvertToLHProcess.h"
  187. #endif
  188. #ifndef ASSIMP_BUILD_NO_TRIANGULATE_PROCESS
  189. # include "TriangulateProcess.h"
  190. #endif
  191. #ifndef ASSIMP_BUILD_NO_GENFACENORMALS_PROCESS
  192. # include "GenFaceNormalsProcess.h"
  193. #endif
  194. #ifndef ASSIMP_BUILD_NO_GENVERTEXNORMALS_PROCESS
  195. # include "GenVertexNormalsProcess.h"
  196. #endif
  197. #ifndef ASSIMP_BUILD_NO_REMOVEVC_PROCESS
  198. # include "RemoveVCProcess.h"
  199. #endif
  200. #ifndef ASSIMP_BUILD_NO_SPLITLARGEMESHES_PROCESS
  201. # include "SplitLargeMeshes.h"
  202. #endif
  203. #ifndef ASSIMP_BUILD_NO_PRETRANSFORMVERTICES_PROCESS
  204. # include "PretransformVertices.h"
  205. #endif
  206. #ifndef ASSIMP_BUILD_NO_LIMITBONEWEIGHTS_PROCESS
  207. # include "LimitBoneWeightsProcess.h"
  208. #endif
  209. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  210. # include "ValidateDataStructure.h"
  211. #endif
  212. #ifndef ASSIMP_BUILD_NO_IMPROVECACHELOCALITY_PROCESS
  213. # include "ImproveCacheLocality.h"
  214. #endif
  215. #ifndef ASSIMP_BUILD_NO_FIXINFACINGNORMALS_PROCESS
  216. # include "FixNormalsStep.h"
  217. #endif
  218. #ifndef ASSIMP_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS
  219. # include "RemoveRedundantMaterials.h"
  220. #endif
  221. #ifndef ASSIMP_BUILD_NO_FINDINVALIDDATA_PROCESS
  222. # include "FindInvalidDataProcess.h"
  223. #endif
  224. #ifndef ASSIMP_BUILD_NO_FINDDEGENERATES_PROCESS
  225. # include "FindDegenerates.h"
  226. #endif
  227. #ifndef ASSIMP_BUILD_NO_SORTBYPTYPE_PROCESS
  228. # include "SortByPTypeProcess.h"
  229. #endif
  230. #ifndef ASSIMP_BUILD_NO_GENUVCOORDS_PROCESS
  231. # include "ComputeUVMappingProcess.h"
  232. #endif
  233. #ifndef ASSIMP_BUILD_NO_TRANSFORMTEXCOORDS_PROCESS
  234. # include "TextureTransform.h"
  235. #endif
  236. #ifndef ASSIMP_BUILD_NO_FINDINSTANCES_PROCESS
  237. # include "FindInstancesProcess.h"
  238. #endif
  239. #ifndef ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS
  240. # include "OptimizeMeshes.h"
  241. #endif
  242. #ifndef ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS
  243. # include "OptimizeGraph.h"
  244. #endif
  245. #ifndef ASSIMP_BUILD_NO_SPLITBYBONECOUNT_PROCESS
  246. # include "SplitByBoneCountProcess.h"
  247. #endif
  248. using namespace Assimp;
  249. using namespace Assimp::Intern;
  250. // ------------------------------------------------------------------------------------------------
  251. // Intern::AllocateFromAssimpHeap serves as abstract base class. It overrides
  252. // new and delete (and their array counterparts) of public API classes (e.g. Logger) to
  253. // utilize our DLL heap.
  254. // See http://www.gotw.ca/publications/mill15.htm
  255. // ------------------------------------------------------------------------------------------------
  256. void* AllocateFromAssimpHeap::operator new ( size_t num_bytes) {
  257. return ::operator new(num_bytes);
  258. }
  259. void* AllocateFromAssimpHeap::operator new ( size_t num_bytes, const std::nothrow_t& ) throw() {
  260. try {
  261. return AllocateFromAssimpHeap::operator new( num_bytes );
  262. }
  263. catch( ... ) {
  264. return NULL;
  265. }
  266. }
  267. void AllocateFromAssimpHeap::operator delete ( void* data) {
  268. return ::operator delete(data);
  269. }
  270. void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes) {
  271. return ::operator new[](num_bytes);
  272. }
  273. void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes, const std::nothrow_t& ) throw() {
  274. try {
  275. return AllocateFromAssimpHeap::operator new[]( num_bytes );
  276. }
  277. catch( ... ) {
  278. return NULL;
  279. }
  280. }
  281. void AllocateFromAssimpHeap::operator delete[] ( void* data) {
  282. return ::operator delete[](data);
  283. }
  284. // ------------------------------------------------------------------------------------------------
  285. // Importer constructor.
  286. Importer::Importer()
  287. {
  288. // allocate the pimpl first
  289. pimpl = new ImporterPimpl();
  290. pimpl->mScene = NULL;
  291. pimpl->mErrorString = "";
  292. // Allocate a default IO handler
  293. pimpl->mIOHandler = new DefaultIOSystem;
  294. pimpl->mIsDefaultHandler = true;
  295. pimpl->bExtraVerbose = false; // disable extra verbose mode by default
  296. pimpl->mProgressHandler = new DefaultProgressHandler();
  297. pimpl->mIsDefaultProgressHandler = true;
  298. // ----------------------------------------------------------------------------
  299. // Add an instance of each worker class here
  300. // (register_new_importers_here)
  301. // ----------------------------------------------------------------------------
  302. pimpl->mImporter.reserve(64);
  303. #if (!defined ASSIMP_BUILD_NO_X_IMPORTER)
  304. pimpl->mImporter.push_back( new XFileImporter());
  305. #endif
  306. #if (!defined ASSIMP_BUILD_NO_OBJ_IMPORTER)
  307. pimpl->mImporter.push_back( new ObjFileImporter());
  308. #endif
  309. #if (!defined ASSIMP_BUILD_NO_3DS_IMPORTER)
  310. pimpl->mImporter.push_back( new Discreet3DSImporter());
  311. #endif
  312. #if (!defined ASSIMP_BUILD_NO_MD3_IMPORTER)
  313. pimpl->mImporter.push_back( new MD3Importer());
  314. #endif
  315. #if (!defined ASSIMP_BUILD_NO_MD2_IMPORTER)
  316. pimpl->mImporter.push_back( new MD2Importer());
  317. #endif
  318. #if (!defined ASSIMP_BUILD_NO_PLY_IMPORTER)
  319. pimpl->mImporter.push_back( new PLYImporter());
  320. #endif
  321. #if (!defined ASSIMP_BUILD_NO_MDL_IMPORTER)
  322. pimpl->mImporter.push_back( new MDLImporter());
  323. #endif
  324. #if (!defined ASSIMP_BUILD_NO_ASE_IMPORTER)
  325. pimpl->mImporter.push_back( new ASEImporter());
  326. #endif
  327. #if (!defined ASSIMP_BUILD_NO_HMP_IMPORTER)
  328. pimpl->mImporter.push_back( new HMPImporter());
  329. #endif
  330. #if (!defined ASSIMP_BUILD_NO_SMD_IMPORTER)
  331. pimpl->mImporter.push_back( new SMDImporter());
  332. #endif
  333. #if (!defined ASSIMP_BUILD_NO_MDC_IMPORTER)
  334. pimpl->mImporter.push_back( new MDCImporter());
  335. #endif
  336. #if (!defined ASSIMP_BUILD_NO_MD5_IMPORTER)
  337. pimpl->mImporter.push_back( new MD5Importer());
  338. #endif
  339. #if (!defined ASSIMP_BUILD_NO_STL_IMPORTER)
  340. pimpl->mImporter.push_back( new STLImporter());
  341. #endif
  342. #if (!defined ASSIMP_BUILD_NO_LWO_IMPORTER)
  343. pimpl->mImporter.push_back( new LWOImporter());
  344. #endif
  345. #if (!defined ASSIMP_BUILD_NO_DXF_IMPORTER)
  346. pimpl->mImporter.push_back( new DXFImporter());
  347. #endif
  348. #if (!defined ASSIMP_BUILD_NO_NFF_IMPORTER)
  349. pimpl->mImporter.push_back( new NFFImporter());
  350. #endif
  351. #if (!defined ASSIMP_BUILD_NO_RAW_IMPORTER)
  352. pimpl->mImporter.push_back( new RAWImporter());
  353. #endif
  354. #if (!defined ASSIMP_BUILD_NO_OFF_IMPORTER)
  355. pimpl->mImporter.push_back( new OFFImporter());
  356. #endif
  357. #if (!defined ASSIMP_BUILD_NO_AC_IMPORTER)
  358. pimpl->mImporter.push_back( new AC3DImporter());
  359. #endif
  360. #if (!defined ASSIMP_BUILD_NO_BVH_IMPORTER)
  361. pimpl->mImporter.push_back( new BVHLoader());
  362. #endif
  363. #if (!defined ASSIMP_BUILD_NO_IRRMESH_IMPORTER)
  364. pimpl->mImporter.push_back( new IRRMeshImporter());
  365. #endif
  366. #if (!defined ASSIMP_BUILD_NO_IRR_IMPORTER)
  367. pimpl->mImporter.push_back( new IRRImporter());
  368. #endif
  369. #if (!defined ASSIMP_BUILD_NO_Q3D_IMPORTER)
  370. pimpl->mImporter.push_back( new Q3DImporter());
  371. #endif
  372. #if (!defined ASSIMP_BUILD_NO_B3D_IMPORTER)
  373. pimpl->mImporter.push_back( new B3DImporter());
  374. #endif
  375. #if (!defined ASSIMP_BUILD_NO_COLLADA_IMPORTER)
  376. pimpl->mImporter.push_back( new ColladaLoader());
  377. #endif
  378. #if (!defined ASSIMP_BUILD_NO_TERRAGEN_IMPORTER)
  379. pimpl->mImporter.push_back( new TerragenImporter());
  380. #endif
  381. #if (!defined ASSIMP_BUILD_NO_CSM_IMPORTER)
  382. pimpl->mImporter.push_back( new CSMImporter());
  383. #endif
  384. #if (!defined ASSIMP_BUILD_NO_3D_IMPORTER)
  385. pimpl->mImporter.push_back( new UnrealImporter());
  386. #endif
  387. #if (!defined ASSIMP_BUILD_NO_LWS_IMPORTER)
  388. pimpl->mImporter.push_back( new LWSImporter());
  389. #endif
  390. #if (!defined ASSIMP_BUILD_NO_OGRE_IMPORTER)
  391. pimpl->mImporter.push_back( new Ogre::OgreImporter());
  392. #endif
  393. #if (!defined ASSIMP_BUILD_NO_MS3D_IMPORTER)
  394. pimpl->mImporter.push_back( new MS3DImporter());
  395. #endif
  396. #if (!defined ASSIMP_BUILD_NO_COB_IMPORTER)
  397. pimpl->mImporter.push_back( new COBImporter());
  398. #endif
  399. #if (!defined ASSIMP_BUILD_NO_BLEND_IMPORTER)
  400. pimpl->mImporter.push_back( new BlenderImporter());
  401. #endif
  402. //#if (!defined ASSIMP_BUILD_NO_SWORDOFMOONLIGHT_IMPORTER)
  403. // pimpl->mImporter.push_back( new SomImporter());
  404. //#endif
  405. #if (!defined ASSIMP_BUILD_NO_Q3BSP_IMPORTER)
  406. pimpl->mImporter.push_back( new Q3BSPFileImporter() );
  407. #endif
  408. #if (!defined ASSIMP_BUILD_NO_NDO_IMPORTER)
  409. pimpl->mImporter.push_back( new NDOImporter() );
  410. #endif
  411. // ----------------------------------------------------------------------------
  412. // Add an instance of each post processing step here in the order
  413. // of sequence it is executed. Steps that are added here are not
  414. // validated - as RegisterPPStep() does - all dependencies must be given.
  415. // ----------------------------------------------------------------------------
  416. pimpl->mPostProcessingSteps.reserve(25);
  417. #if (!defined ASSIMP_BUILD_NO_REMOVEVC_PROCESS)
  418. pimpl->mPostProcessingSteps.push_back( new RemoveVCProcess());
  419. #endif
  420. #if (!defined ASSIMP_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS)
  421. pimpl->mPostProcessingSteps.push_back( new RemoveRedundantMatsProcess());
  422. #endif
  423. #if (!defined ASSIMP_BUILD_NO_FINDINSTANCES_PROCESS)
  424. pimpl->mPostProcessingSteps.push_back( new FindInstancesProcess());
  425. #endif
  426. #if (!defined ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS)
  427. pimpl->mPostProcessingSteps.push_back( new OptimizeGraphProcess());
  428. #endif
  429. #if (!defined ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS)
  430. pimpl->mPostProcessingSteps.push_back( new OptimizeMeshesProcess());
  431. #endif
  432. #if (!defined ASSIMP_BUILD_NO_FINDDEGENERATES_PROCESS)
  433. pimpl->mPostProcessingSteps.push_back( new FindDegeneratesProcess());
  434. #endif
  435. #ifndef ASSIMP_BUILD_NO_GENUVCOORDS_PROCESS
  436. pimpl->mPostProcessingSteps.push_back( new ComputeUVMappingProcess());
  437. #endif
  438. #ifndef ASSIMP_BUILD_NO_TRANSFORMTEXCOORDS_PROCESS
  439. pimpl->mPostProcessingSteps.push_back( new TextureTransformStep());
  440. #endif
  441. #if (!defined ASSIMP_BUILD_NO_PRETRANSFORMVERTICES_PROCESS)
  442. pimpl->mPostProcessingSteps.push_back( new PretransformVertices());
  443. #endif
  444. #if (!defined ASSIMP_BUILD_NO_TRIANGULATE_PROCESS)
  445. pimpl->mPostProcessingSteps.push_back( new TriangulateProcess());
  446. #endif
  447. #if (!defined ASSIMP_BUILD_NO_SORTBYPTYPE_PROCESS)
  448. pimpl->mPostProcessingSteps.push_back( new SortByPTypeProcess());
  449. #endif
  450. #if (!defined ASSIMP_BUILD_NO_FINDINVALIDDATA_PROCESS)
  451. pimpl->mPostProcessingSteps.push_back( new FindInvalidDataProcess());
  452. #endif
  453. #if (!defined ASSIMP_BUILD_NO_FIXINFACINGNORMALS_PROCESS)
  454. pimpl->mPostProcessingSteps.push_back( new FixInfacingNormalsProcess());
  455. #endif
  456. #if (!defined ASSIMP_BUILD_NO_SPLITBYBONECOUNT_PROCESS)
  457. pimpl->mPostProcessingSteps.push_back( new SplitByBoneCountProcess());
  458. #endif
  459. #if (!defined ASSIMP_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  460. pimpl->mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Triangle());
  461. #endif
  462. #if (!defined ASSIMP_BUILD_NO_GENFACENORMALS_PROCESS)
  463. pimpl->mPostProcessingSteps.push_back( new GenFaceNormalsProcess());
  464. #endif
  465. // .........................................................................
  466. // DON'T change the order of these five!
  467. pimpl->mPostProcessingSteps.push_back( new ComputeSpatialSortProcess());
  468. // .........................................................................
  469. #if (!defined ASSIMP_BUILD_NO_GENVERTEXNORMALS_PROCESS)
  470. pimpl->mPostProcessingSteps.push_back( new GenVertexNormalsProcess());
  471. #endif
  472. #if (!defined ASSIMP_BUILD_NO_CALCTANGENTS_PROCESS)
  473. pimpl->mPostProcessingSteps.push_back( new CalcTangentsProcess());
  474. #endif
  475. #if (!defined ASSIMP_BUILD_NO_JOINVERTICES_PROCESS)
  476. pimpl->mPostProcessingSteps.push_back( new JoinVerticesProcess());
  477. #endif
  478. // .........................................................................
  479. pimpl->mPostProcessingSteps.push_back( new DestroySpatialSortProcess());
  480. // .........................................................................
  481. #if (!defined ASSIMP_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  482. pimpl->mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Vertex());
  483. #endif
  484. #if (!defined ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS)
  485. pimpl->mPostProcessingSteps.push_back( new MakeLeftHandedProcess());
  486. #endif
  487. #if (!defined ASSIMP_BUILD_NO_FLIPUVS_PROCESS)
  488. pimpl->mPostProcessingSteps.push_back( new FlipUVsProcess());
  489. #endif
  490. #if (!defined ASSIMP_BUILD_NO_FLIPWINDINGORDER_PROCESS)
  491. pimpl->mPostProcessingSteps.push_back( new FlipWindingOrderProcess());
  492. #endif
  493. #if (!defined ASSIMP_BUILD_NO_LIMITBONEWEIGHTS_PROCESS)
  494. pimpl->mPostProcessingSteps.push_back( new LimitBoneWeightsProcess());
  495. #endif
  496. #if (!defined ASSIMP_BUILD_NO_IMPROVECACHELOCALITY_PROCESS)
  497. pimpl->mPostProcessingSteps.push_back( new ImproveCacheLocalityProcess());
  498. #endif
  499. // Allocate a SharedPostProcessInfo object and store pointers to it in all post-process steps in the list.
  500. pimpl->mPPShared = new SharedPostProcessInfo();
  501. for (std::vector<BaseProcess*>::iterator it = pimpl->mPostProcessingSteps.begin();
  502. it != pimpl->mPostProcessingSteps.end();
  503. ++it) {
  504. (*it)->SetSharedData(pimpl->mPPShared);
  505. }
  506. }
  507. // ------------------------------------------------------------------------------------------------
  508. // Destructor of Importer
  509. Importer::~Importer()
  510. {
  511. // Delete all import plugins
  512. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++)
  513. delete pimpl->mImporter[a];
  514. // Delete all post-processing plug-ins
  515. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++)
  516. delete pimpl->mPostProcessingSteps[a];
  517. // Delete the assigned IO and progress handler
  518. delete pimpl->mIOHandler;
  519. delete pimpl->mProgressHandler;
  520. // Kill imported scene. Destructors should do that recursivly
  521. delete pimpl->mScene;
  522. // Delete shared post-processing data
  523. delete pimpl->mPPShared;
  524. // and finally the pimpl itself
  525. delete pimpl;
  526. }
  527. // ------------------------------------------------------------------------------------------------
  528. // Copy constructor - copies the config of another Importer, not the scene
  529. Importer::Importer(const Importer &other)
  530. {
  531. new(this) Importer();
  532. pimpl->mIntProperties = other.pimpl->mIntProperties;
  533. pimpl->mFloatProperties = other.pimpl->mFloatProperties;
  534. pimpl->mStringProperties = other.pimpl->mStringProperties;
  535. }
  536. // ------------------------------------------------------------------------------------------------
  537. // Register a custom post-processing step
  538. aiReturn Importer::RegisterPPStep(BaseProcess* pImp)
  539. {
  540. ai_assert(NULL != pImp);
  541. ASSIMP_BEGIN_EXCEPTION_REGION();
  542. pimpl->mPostProcessingSteps.push_back(pImp);
  543. DefaultLogger::get()->info("Registering custom post-processing step");
  544. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  545. return AI_SUCCESS;
  546. }
  547. // ------------------------------------------------------------------------------------------------
  548. // Register a custom loader plugin
  549. aiReturn Importer::RegisterLoader(BaseImporter* pImp)
  550. {
  551. ai_assert(NULL != pImp);
  552. ASSIMP_BEGIN_EXCEPTION_REGION();
  553. // --------------------------------------------------------------------
  554. // Check whether we would have two loaders for the same file extension
  555. // This is absolutely OK, but we should warn the developer of the new
  556. // loader that his code will probably never be called if the first
  557. // loader is a bit too lazy in his file checking.
  558. // --------------------------------------------------------------------
  559. std::set<std::string> st;
  560. std::string baked;
  561. pImp->GetExtensionList(st);
  562. for(std::set<std::string>::const_iterator it = st.begin(); it != st.end(); ++it) {
  563. #ifdef _DEBUG
  564. if (IsExtensionSupported(*it)) {
  565. DefaultLogger::get()->warn("The file extension " + *it + " is already in use");
  566. }
  567. #endif
  568. baked += *it;
  569. }
  570. // add the loader
  571. pimpl->mImporter.push_back(pImp);
  572. DefaultLogger::get()->info("Registering custom importer for these file extensions: " + baked);
  573. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  574. return AI_SUCCESS;
  575. }
  576. // ------------------------------------------------------------------------------------------------
  577. // Unregister a custom loader plugin
  578. aiReturn Importer::UnregisterLoader(BaseImporter* pImp)
  579. {
  580. if(!pImp) {
  581. // unregistering a NULL importer is no problem for us ... really!
  582. return AI_SUCCESS;
  583. }
  584. ASSIMP_BEGIN_EXCEPTION_REGION();
  585. std::vector<BaseImporter*>::iterator it = std::find(pimpl->mImporter.begin(),
  586. pimpl->mImporter.end(),pImp);
  587. if (it != pimpl->mImporter.end()) {
  588. pimpl->mImporter.erase(it);
  589. std::set<std::string> st;
  590. pImp->GetExtensionList(st);
  591. DefaultLogger::get()->info("Unregistering custom importer: ");
  592. return AI_SUCCESS;
  593. }
  594. DefaultLogger::get()->warn("Unable to remove custom importer: I can't find you ...");
  595. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  596. return AI_FAILURE;
  597. }
  598. // ------------------------------------------------------------------------------------------------
  599. // Unregister a custom loader plugin
  600. aiReturn Importer::UnregisterPPStep(BaseProcess* pImp)
  601. {
  602. if(!pImp) {
  603. // unregistering a NULL ppstep is no problem for us ... really!
  604. return AI_SUCCESS;
  605. }
  606. ASSIMP_BEGIN_EXCEPTION_REGION();
  607. std::vector<BaseProcess*>::iterator it = std::find(pimpl->mPostProcessingSteps.begin(),
  608. pimpl->mPostProcessingSteps.end(),pImp);
  609. if (it != pimpl->mPostProcessingSteps.end()) {
  610. pimpl->mPostProcessingSteps.erase(it);
  611. DefaultLogger::get()->info("Unregistering custom post-processing step");
  612. return AI_SUCCESS;
  613. }
  614. DefaultLogger::get()->warn("Unable to remove custom post-processing step: I can't find you ..");
  615. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  616. return AI_FAILURE;
  617. }
  618. // ------------------------------------------------------------------------------------------------
  619. // Supplies a custom IO handler to the importer to open and access files.
  620. void Importer::SetIOHandler( IOSystem* pIOHandler)
  621. {
  622. ASSIMP_BEGIN_EXCEPTION_REGION();
  623. // If the new handler is zero, allocate a default IO implementation.
  624. if (!pIOHandler)
  625. {
  626. // Release pointer in the possession of the caller
  627. pimpl->mIOHandler = new DefaultIOSystem();
  628. pimpl->mIsDefaultHandler = true;
  629. }
  630. // Otherwise register the custom handler
  631. else if (pimpl->mIOHandler != pIOHandler)
  632. {
  633. delete pimpl->mIOHandler;
  634. pimpl->mIOHandler = pIOHandler;
  635. pimpl->mIsDefaultHandler = false;
  636. }
  637. ASSIMP_END_EXCEPTION_REGION(void);
  638. }
  639. // ------------------------------------------------------------------------------------------------
  640. // Get the currently set IO handler
  641. IOSystem* Importer::GetIOHandler() const
  642. {
  643. return pimpl->mIOHandler;
  644. }
  645. // ------------------------------------------------------------------------------------------------
  646. // Check whether a custom IO handler is currently set
  647. bool Importer::IsDefaultIOHandler() const
  648. {
  649. return pimpl->mIsDefaultHandler;
  650. }
  651. // ------------------------------------------------------------------------------------------------
  652. // Supplies a custom progress handler to get regular callbacks during importing
  653. void Importer::SetProgressHandler ( ProgressHandler* pHandler )
  654. {
  655. ASSIMP_BEGIN_EXCEPTION_REGION();
  656. // If the new handler is zero, allocate a default implementation.
  657. if (!pHandler)
  658. {
  659. // Release pointer in the possession of the caller
  660. pimpl->mProgressHandler = new DefaultProgressHandler();
  661. pimpl->mIsDefaultProgressHandler = true;
  662. }
  663. // Otherwise register the custom handler
  664. else if (pimpl->mProgressHandler != pHandler)
  665. {
  666. delete pimpl->mProgressHandler;
  667. pimpl->mProgressHandler = pHandler;
  668. pimpl->mIsDefaultProgressHandler = false;
  669. }
  670. ASSIMP_END_EXCEPTION_REGION(void);
  671. }
  672. // ------------------------------------------------------------------------------------------------
  673. // Get the currently set progress handler
  674. ProgressHandler* Importer::GetProgressHandler() const
  675. {
  676. return pimpl->mProgressHandler;
  677. }
  678. // ------------------------------------------------------------------------------------------------
  679. // Check whether a custom progress handler is currently set
  680. bool Importer::IsDefaultProgressHandler() const
  681. {
  682. return pimpl->mIsDefaultProgressHandler;
  683. }
  684. // ------------------------------------------------------------------------------------------------
  685. // Validate post process step flags
  686. bool _ValidateFlags(unsigned int pFlags)
  687. {
  688. if (pFlags & aiProcess_GenSmoothNormals && pFlags & aiProcess_GenNormals) {
  689. DefaultLogger::get()->error("#aiProcess_GenSmoothNormals and #aiProcess_GenNormals are incompatible");
  690. return false;
  691. }
  692. if (pFlags & aiProcess_OptimizeGraph && pFlags & aiProcess_PreTransformVertices) {
  693. DefaultLogger::get()->error("#aiProcess_OptimizeGraph and #aiProcess_PreTransformVertices are incompatible");
  694. return false;
  695. }
  696. return true;
  697. }
  698. // ------------------------------------------------------------------------------------------------
  699. // Free the current scene
  700. void Importer::FreeScene( )
  701. {
  702. ASSIMP_BEGIN_EXCEPTION_REGION();
  703. delete pimpl->mScene;
  704. pimpl->mScene = NULL;
  705. pimpl->mErrorString = "";
  706. ASSIMP_END_EXCEPTION_REGION(void);
  707. }
  708. // ------------------------------------------------------------------------------------------------
  709. // Get the current error string, if any
  710. const char* Importer::GetErrorString() const
  711. {
  712. /* Must remain valid as long as ReadFile() or FreeFile() are not called */
  713. return pimpl->mErrorString.c_str();
  714. }
  715. // ------------------------------------------------------------------------------------------------
  716. // Enable extra-verbose mode
  717. void Importer::SetExtraVerbose(bool bDo)
  718. {
  719. pimpl->bExtraVerbose = bDo;
  720. }
  721. // ------------------------------------------------------------------------------------------------
  722. // Get the current scene
  723. const aiScene* Importer::GetScene() const
  724. {
  725. return pimpl->mScene;
  726. }
  727. // ------------------------------------------------------------------------------------------------
  728. // Orphan the current scene and return it.
  729. aiScene* Importer::GetOrphanedScene()
  730. {
  731. aiScene* s = pimpl->mScene;
  732. ASSIMP_BEGIN_EXCEPTION_REGION();
  733. pimpl->mScene = NULL;
  734. pimpl->mErrorString = ""; /* reset error string */
  735. ASSIMP_END_EXCEPTION_REGION(aiScene*);
  736. return s;
  737. }
  738. // ------------------------------------------------------------------------------------------------
  739. // Validate post-processing flags
  740. bool Importer::ValidateFlags(unsigned int pFlags) const
  741. {
  742. ASSIMP_BEGIN_EXCEPTION_REGION();
  743. // run basic checks for mutually exclusive flags
  744. if(!_ValidateFlags(pFlags)) {
  745. return false;
  746. }
  747. // ValidateDS does not anymore occur in the pp list, it plays an awesome extra role ...
  748. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  749. if (pFlags & aiProcess_ValidateDataStructure) {
  750. return false;
  751. }
  752. #endif
  753. pFlags &= ~aiProcess_ValidateDataStructure;
  754. // Now iterate through all bits which are set in the flags and check whether we find at least
  755. // one pp plugin which handles it.
  756. for (unsigned int mask = 1; mask < (1u << (sizeof(unsigned int)*8-1));mask <<= 1) {
  757. if (pFlags & mask) {
  758. bool have = false;
  759. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  760. if (pimpl->mPostProcessingSteps[a]-> IsActive(mask) ) {
  761. have = true;
  762. break;
  763. }
  764. }
  765. if (!have) {
  766. return false;
  767. }
  768. }
  769. }
  770. ASSIMP_END_EXCEPTION_REGION(bool);
  771. return true;
  772. }
  773. // ------------------------------------------------------------------------------------------------
  774. const aiScene* Importer::ReadFileFromMemory( const void* pBuffer,
  775. size_t pLength,
  776. unsigned int pFlags,
  777. const char* pHint /*= ""*/)
  778. {
  779. ASSIMP_BEGIN_EXCEPTION_REGION();
  780. if (!pHint) {
  781. pHint = "";
  782. }
  783. if (!pBuffer || !pLength || strlen(pHint) > 100) {
  784. pimpl->mErrorString = "Invalid parameters passed to ReadFileFromMemory()";
  785. return NULL;
  786. }
  787. // prevent deletion of the previous IOHandler
  788. IOSystem* io = pimpl->mIOHandler;
  789. pimpl->mIOHandler = NULL;
  790. SetIOHandler(new MemoryIOSystem((const uint8_t*)pBuffer,pLength));
  791. // read the file and recover the previous IOSystem
  792. char fbuff[128];
  793. sprintf(fbuff,"%s.%s",AI_MEMORYIO_MAGIC_FILENAME,pHint);
  794. ReadFile(fbuff,pFlags);
  795. SetIOHandler(io);
  796. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  797. return pimpl->mScene;
  798. }
  799. // ------------------------------------------------------------------------------------------------
  800. void WriteLogOpening(const std::string& file)
  801. {
  802. Logger* l = DefaultLogger::get();
  803. if (!l) {
  804. return;
  805. }
  806. l->info("Load " + file);
  807. // print a full version dump. This is nice because we don't
  808. // need to ask the authors of incoming bug reports for
  809. // the library version they're using - a log dump is
  810. // sufficient.
  811. const unsigned int flags = aiGetCompileFlags();
  812. l->debug(format()
  813. << "Assimp "
  814. << aiGetVersionMajor()
  815. << "."
  816. << aiGetVersionMinor()
  817. << "."
  818. << aiGetVersionRevision()
  819. #if defined(ASSIMP_BUILD_X86_32BIT_ARCHITECTURE)
  820. << " x86"
  821. #elif defined(ASSIMP_BUILD_X86_64BIT_ARCHITECTURE)
  822. << " amd64"
  823. #elif defined(ASSIMP_BUILD_IA_64BIT_ARCHITECTURE)
  824. << " itanium"
  825. #elif defined(ASSIMP_BUILD_PPC_32BIT_ARCHITECTURE)
  826. << " ppc32"
  827. #elif defined(ASSIMP_BUILD_ARM_32BIT_ARCHITECTURE)
  828. << " arm"
  829. #else
  830. # error unknown architecture
  831. #endif
  832. #if defined(_MSC_VER)
  833. << " msvc"
  834. #elif defined(__GNUC__)
  835. << " gcc"
  836. #else
  837. # error unknown compiler
  838. #endif
  839. #ifndef NDEBUG
  840. << " debug"
  841. #endif
  842. << (flags & ASSIMP_CFLAGS_NOBOOST ? " noboost" : "")
  843. << (flags & ASSIMP_CFLAGS_SHARED ? " shared" : "")
  844. << (flags & ASSIMP_CFLAGS_SINGLETHREADED ? " singlethreaded" : "")
  845. );
  846. }
  847. // ------------------------------------------------------------------------------------------------
  848. // Reads the given file and returns its contents if successful.
  849. const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags)
  850. {
  851. ASSIMP_BEGIN_EXCEPTION_REGION();
  852. const std::string pFile(_pFile);
  853. // ----------------------------------------------------------------------
  854. // Put a large try block around everything to catch all std::exception's
  855. // that might be thrown by STL containers or by new().
  856. // ImportErrorException's are throw by ourselves and caught elsewhere.
  857. //-----------------------------------------------------------------------
  858. WriteLogOpening(pFile);
  859. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  860. try
  861. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  862. {
  863. // Check whether this Importer instance has already loaded
  864. // a scene. In this case we need to delete the old one
  865. if (pimpl->mScene) {
  866. DefaultLogger::get()->debug("(Deleting previous scene)");
  867. FreeScene();
  868. }
  869. // First check if the file is accessable at all
  870. if( !pimpl->mIOHandler->Exists( pFile)) {
  871. pimpl->mErrorString = "Unable to open file \"" + pFile + "\".";
  872. DefaultLogger::get()->error(pimpl->mErrorString);
  873. return NULL;
  874. }
  875. boost::scoped_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME,0)?new Profiler():NULL);
  876. if (profiler) {
  877. profiler->BeginRegion("total");
  878. }
  879. // Find an worker class which can handle the file
  880. BaseImporter* imp = NULL;
  881. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  882. if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, false)) {
  883. imp = pimpl->mImporter[a];
  884. break;
  885. }
  886. }
  887. if (!imp) {
  888. // not so bad yet ... try format auto detection.
  889. const std::string::size_type s = pFile.find_last_of('.');
  890. if (s != std::string::npos) {
  891. DefaultLogger::get()->info("File extension now known, trying signature-based detection");
  892. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  893. if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, true)) {
  894. imp = pimpl->mImporter[a];
  895. break;
  896. }
  897. }
  898. }
  899. // Put a proper error message if no suitable importer was found
  900. if( !imp) {
  901. pimpl->mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  902. DefaultLogger::get()->error(pimpl->mErrorString);
  903. return NULL;
  904. }
  905. }
  906. // Dispatch the reading to the worker class for this format
  907. DefaultLogger::get()->info("Found a matching importer for this file format");
  908. pimpl->mProgressHandler->Update();
  909. if (profiler) {
  910. profiler->BeginRegion("import");
  911. }
  912. pimpl->mScene = imp->ReadFile( this, pFile, pimpl->mIOHandler);
  913. pimpl->mProgressHandler->Update();
  914. if (profiler) {
  915. profiler->EndRegion("import");
  916. }
  917. // If successful, apply all active post processing steps to the imported data
  918. if( pimpl->mScene) {
  919. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  920. // The ValidateDS process is an exception. It is executed first, even before ScenePreprocessor is called.
  921. if (pFlags & aiProcess_ValidateDataStructure)
  922. {
  923. ValidateDSProcess ds;
  924. ds.ExecuteOnScene (this);
  925. if (!pimpl->mScene) {
  926. return NULL;
  927. }
  928. }
  929. #endif // no validation
  930. // Preprocess the scene and prepare it for post-processing
  931. if (profiler) {
  932. profiler->BeginRegion("preprocess");
  933. }
  934. ScenePreprocessor pre(pimpl->mScene);
  935. pre.ProcessScene();
  936. pimpl->mProgressHandler->Update();
  937. if (profiler) {
  938. profiler->EndRegion("preprocess");
  939. }
  940. // Ensure that the validation process won't be called twice
  941. ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure));
  942. }
  943. // if failed, extract the error string
  944. else if( !pimpl->mScene) {
  945. pimpl->mErrorString = imp->GetErrorText();
  946. }
  947. // clear any data allocated by post-process steps
  948. pimpl->mPPShared->Clean();
  949. if (profiler) {
  950. profiler->EndRegion("total");
  951. }
  952. }
  953. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  954. catch (std::exception &e)
  955. {
  956. #if (defined _MSC_VER) && (defined _CPPRTTI)
  957. // if we have RTTI get the full name of the exception that occured
  958. pimpl->mErrorString = std::string(typeid( e ).name()) + ": " + e.what();
  959. #else
  960. pimpl->mErrorString = std::string("std::exception: ") + e.what();
  961. #endif
  962. DefaultLogger::get()->error(pimpl->mErrorString);
  963. delete pimpl->mScene; pimpl->mScene = NULL;
  964. }
  965. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  966. // either successful or failure - the pointer expresses it anyways
  967. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  968. return pimpl->mScene;
  969. }
  970. // ------------------------------------------------------------------------------------------------
  971. // Apply post-processing to the currently bound scene
  972. const aiScene* Importer::ApplyPostProcessing(unsigned int pFlags)
  973. {
  974. ASSIMP_BEGIN_EXCEPTION_REGION();
  975. // Return immediately if no scene is active
  976. if (!pimpl->mScene) {
  977. return NULL;
  978. }
  979. // If no flags are given, return the current scene with no further action
  980. if (!pFlags) {
  981. return pimpl->mScene;
  982. }
  983. // In debug builds: run basic flag validation
  984. ai_assert(_ValidateFlags(pFlags));
  985. DefaultLogger::get()->info("Entering post processing pipeline");
  986. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  987. // The ValidateDS process plays an exceptional role. It isn't contained in the global
  988. // list of post-processing steps, so we need to call it manually.
  989. if (pFlags & aiProcess_ValidateDataStructure)
  990. {
  991. ValidateDSProcess ds;
  992. ds.ExecuteOnScene (this);
  993. if (!pimpl->mScene) {
  994. return NULL;
  995. }
  996. }
  997. #endif // no validation
  998. #ifdef _DEBUG
  999. if (pimpl->bExtraVerbose)
  1000. {
  1001. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  1002. DefaultLogger::get()->error("Verbose Import is not available due to build settings");
  1003. #endif // no validation
  1004. pFlags |= aiProcess_ValidateDataStructure;
  1005. }
  1006. #else
  1007. if (pimpl->bExtraVerbose) {
  1008. DefaultLogger::get()->warn("Not a debug build, ignoring extra verbose setting");
  1009. }
  1010. #endif // ! DEBUG
  1011. boost::scoped_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME,0)?new Profiler():NULL);
  1012. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  1013. BaseProcess* process = pimpl->mPostProcessingSteps[a];
  1014. if( process->IsActive( pFlags)) {
  1015. if (profiler) {
  1016. profiler->BeginRegion("postprocess");
  1017. }
  1018. process->ExecuteOnScene ( this );
  1019. pimpl->mProgressHandler->Update();
  1020. if (profiler) {
  1021. profiler->EndRegion("postprocess");
  1022. }
  1023. }
  1024. if( !pimpl->mScene) {
  1025. break;
  1026. }
  1027. #ifdef _DEBUG
  1028. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  1029. continue;
  1030. #endif // no validation
  1031. // If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
  1032. if (pimpl->bExtraVerbose) {
  1033. DefaultLogger::get()->debug("Verbose Import: revalidating data structures");
  1034. ValidateDSProcess ds;
  1035. ds.ExecuteOnScene (this);
  1036. if( !pimpl->mScene) {
  1037. DefaultLogger::get()->error("Verbose Import: failed to revalidate data structures");
  1038. break;
  1039. }
  1040. }
  1041. #endif // ! DEBUG
  1042. }
  1043. // clear any data allocated by post-process steps
  1044. pimpl->mPPShared->Clean();
  1045. DefaultLogger::get()->info("Leaving post processing pipeline");
  1046. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  1047. return pimpl->mScene;
  1048. }
  1049. // ------------------------------------------------------------------------------------------------
  1050. // Helper function to check whether an extension is supported by ASSIMP
  1051. bool Importer::IsExtensionSupported(const char* szExtension) const
  1052. {
  1053. return NULL != FindLoader(szExtension);
  1054. }
  1055. // ------------------------------------------------------------------------------------------------
  1056. // Find a loader plugin for a given file extension
  1057. BaseImporter* Importer::FindLoader (const char* szExtension) const
  1058. {
  1059. ai_assert(szExtension);
  1060. ASSIMP_BEGIN_EXCEPTION_REGION();
  1061. // skip over wildcard and dot characters at string head --
  1062. for(;*szExtension == '*' || *szExtension == '.'; ++szExtension);
  1063. std::string ext(szExtension);
  1064. if (ext.empty()) {
  1065. return NULL;
  1066. }
  1067. std::transform(ext.begin(),ext.end(), ext.begin(), tolower);
  1068. std::set<std::string> str;
  1069. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  1070. str.clear();
  1071. (*i)->GetExtensionList(str);
  1072. for (std::set<std::string>::const_iterator it = str.begin(); it != str.end(); ++it) {
  1073. if (ext == *it) {
  1074. return (*i);
  1075. }
  1076. }
  1077. }
  1078. ASSIMP_END_EXCEPTION_REGION(BaseImporter*);
  1079. return NULL;
  1080. }
  1081. // ------------------------------------------------------------------------------------------------
  1082. // Helper function to build a list of all file extensions supported by ASSIMP
  1083. void Importer::GetExtensionList(aiString& szOut) const
  1084. {
  1085. ASSIMP_BEGIN_EXCEPTION_REGION();
  1086. std::set<std::string> str;
  1087. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  1088. (*i)->GetExtensionList(str);
  1089. }
  1090. for (std::set<std::string>::const_iterator it = str.begin();; ) {
  1091. szOut.Append("*.");
  1092. szOut.Append((*it).c_str());
  1093. if (++it == str.end()) {
  1094. break;
  1095. }
  1096. szOut.Append(";");
  1097. }
  1098. ASSIMP_END_EXCEPTION_REGION(void);
  1099. }
  1100. // ------------------------------------------------------------------------------------------------
  1101. // Set a configuration property
  1102. void Importer::SetPropertyInteger(const char* szName, int iValue,
  1103. bool* bWasExisting /*= NULL*/)
  1104. {
  1105. ASSIMP_BEGIN_EXCEPTION_REGION();
  1106. SetGenericProperty<int>(pimpl->mIntProperties, szName,iValue,bWasExisting);
  1107. ASSIMP_END_EXCEPTION_REGION(void);
  1108. }
  1109. // ------------------------------------------------------------------------------------------------
  1110. // Set a configuration property
  1111. void Importer::SetPropertyFloat(const char* szName, float iValue,
  1112. bool* bWasExisting /*= NULL*/)
  1113. {
  1114. ASSIMP_BEGIN_EXCEPTION_REGION();
  1115. SetGenericProperty<float>(pimpl->mFloatProperties, szName,iValue,bWasExisting);
  1116. ASSIMP_END_EXCEPTION_REGION(void);
  1117. }
  1118. // ------------------------------------------------------------------------------------------------
  1119. // Set a configuration property
  1120. void Importer::SetPropertyString(const char* szName, const std::string& value,
  1121. bool* bWasExisting /*= NULL*/)
  1122. {
  1123. ASSIMP_BEGIN_EXCEPTION_REGION();
  1124. SetGenericProperty<std::string>(pimpl->mStringProperties, szName,value,bWasExisting);
  1125. ASSIMP_END_EXCEPTION_REGION(void);
  1126. }
  1127. // ------------------------------------------------------------------------------------------------
  1128. // Get a configuration property
  1129. int Importer::GetPropertyInteger(const char* szName,
  1130. int iErrorReturn /*= 0xffffffff*/) const
  1131. {
  1132. return GetGenericProperty<int>(pimpl->mIntProperties,szName,iErrorReturn);
  1133. }
  1134. // ------------------------------------------------------------------------------------------------
  1135. // Get a configuration property
  1136. float Importer::GetPropertyFloat(const char* szName,
  1137. float iErrorReturn /*= 10e10*/) const
  1138. {
  1139. return GetGenericProperty<float>(pimpl->mFloatProperties,szName,iErrorReturn);
  1140. }
  1141. // ------------------------------------------------------------------------------------------------
  1142. // Get a configuration property
  1143. const std::string& Importer::GetPropertyString(const char* szName,
  1144. const std::string& iErrorReturn /*= ""*/) const
  1145. {
  1146. return GetGenericProperty<std::string>(pimpl->mStringProperties,szName,iErrorReturn);
  1147. }
  1148. // ------------------------------------------------------------------------------------------------
  1149. // Get the memory requirements of a single node
  1150. inline void AddNodeWeight(unsigned int& iScene,const aiNode* pcNode)
  1151. {
  1152. iScene += sizeof(aiNode);
  1153. iScene += sizeof(unsigned int) * pcNode->mNumMeshes;
  1154. iScene += sizeof(void*) * pcNode->mNumChildren;
  1155. for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
  1156. AddNodeWeight(iScene,pcNode->mChildren[i]);
  1157. }
  1158. }
  1159. // ------------------------------------------------------------------------------------------------
  1160. // Get the memory requirements of the scene
  1161. void Importer::GetMemoryRequirements(aiMemoryInfo& in) const
  1162. {
  1163. in = aiMemoryInfo();
  1164. aiScene* mScene = pimpl->mScene;
  1165. // return if we have no scene loaded
  1166. if (!pimpl->mScene)
  1167. return;
  1168. in.total = sizeof(aiScene);
  1169. // add all meshes
  1170. for (unsigned int i = 0; i < mScene->mNumMeshes;++i)
  1171. {
  1172. in.meshes += sizeof(aiMesh);
  1173. if (mScene->mMeshes[i]->HasPositions()) {
  1174. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  1175. }
  1176. if (mScene->mMeshes[i]->HasNormals()) {
  1177. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  1178. }
  1179. if (mScene->mMeshes[i]->HasTangentsAndBitangents()) {
  1180. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices * 2;
  1181. }
  1182. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a) {
  1183. if (mScene->mMeshes[i]->HasVertexColors(a)) {
  1184. in.meshes += sizeof(aiColor4D) * mScene->mMeshes[i]->mNumVertices;
  1185. }
  1186. else break;
  1187. }
  1188. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a) {
  1189. if (mScene->mMeshes[i]->HasTextureCoords(a)) {
  1190. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  1191. }
  1192. else break;
  1193. }
  1194. if (mScene->mMeshes[i]->HasBones()) {
  1195. in.meshes += sizeof(void*) * mScene->mMeshes[i]->mNumBones;
  1196. for (unsigned int p = 0; p < mScene->mMeshes[i]->mNumBones;++p) {
  1197. in.meshes += sizeof(aiBone);
  1198. in.meshes += mScene->mMeshes[i]->mBones[p]->mNumWeights * sizeof(aiVertexWeight);
  1199. }
  1200. }
  1201. in.meshes += (sizeof(aiFace) + 3 * sizeof(unsigned int))*mScene->mMeshes[i]->mNumFaces;
  1202. }
  1203. in.total += in.meshes;
  1204. // add all embedded textures
  1205. for (unsigned int i = 0; i < mScene->mNumTextures;++i) {
  1206. const aiTexture* pc = mScene->mTextures[i];
  1207. in.textures += sizeof(aiTexture);
  1208. if (pc->mHeight) {
  1209. in.textures += 4 * pc->mHeight * pc->mWidth;
  1210. }
  1211. else in.textures += pc->mWidth;
  1212. }
  1213. in.total += in.textures;
  1214. // add all animations
  1215. for (unsigned int i = 0; i < mScene->mNumAnimations;++i) {
  1216. const aiAnimation* pc = mScene->mAnimations[i];
  1217. in.animations += sizeof(aiAnimation);
  1218. // add all bone anims
  1219. for (unsigned int a = 0; a < pc->mNumChannels; ++a) {
  1220. const aiNodeAnim* pc2 = pc->mChannels[i];
  1221. in.animations += sizeof(aiNodeAnim);
  1222. in.animations += pc2->mNumPositionKeys * sizeof(aiVectorKey);
  1223. in.animations += pc2->mNumScalingKeys * sizeof(aiVectorKey);
  1224. in.animations += pc2->mNumRotationKeys * sizeof(aiQuatKey);
  1225. }
  1226. }
  1227. in.total += in.animations;
  1228. // add all cameras and all lights
  1229. in.total += in.cameras = sizeof(aiCamera) * mScene->mNumCameras;
  1230. in.total += in.lights = sizeof(aiLight) * mScene->mNumLights;
  1231. // add all nodes
  1232. AddNodeWeight(in.nodes,mScene->mRootNode);
  1233. in.total += in.nodes;
  1234. // add all materials
  1235. for (unsigned int i = 0; i < mScene->mNumMaterials;++i) {
  1236. const aiMaterial* pc = mScene->mMaterials[i];
  1237. in.materials += sizeof(aiMaterial);
  1238. in.materials += pc->mNumAllocated * sizeof(void*);
  1239. for (unsigned int a = 0; a < pc->mNumProperties;++a) {
  1240. in.materials += pc->mProperties[a]->mDataLength;
  1241. }
  1242. }
  1243. in.total += in.materials;
  1244. }