Importer.cpp 35 KB

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