Importer.cpp 42 KB

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