Importer.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2021, assimp 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 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 <assimp/version.h>
  38. #include <assimp/config.h>
  39. #include <assimp/importerdesc.h>
  40. // ------------------------------------------------------------------------------------------------
  41. /* Uncomment this line to prevent Assimp from catching unknown exceptions.
  42. *
  43. * Note that any Exception except DeadlyImportError may lead to
  44. * undefined behaviour -> loaders could remain in an unusable state and
  45. * further imports with the same Importer instance could fail/crash/burn ...
  46. */
  47. // ------------------------------------------------------------------------------------------------
  48. #ifndef ASSIMP_BUILD_DEBUG
  49. # define ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  50. #endif
  51. // ------------------------------------------------------------------------------------------------
  52. // Internal headers
  53. // ------------------------------------------------------------------------------------------------
  54. #include "Common/Importer.h"
  55. #include "Common/BaseProcess.h"
  56. #include "Common/DefaultProgressHandler.h"
  57. #include "PostProcessing/ProcessHelper.h"
  58. #include "Common/ScenePreprocessor.h"
  59. #include "Common/ScenePrivate.h"
  60. #include <assimp/BaseImporter.h>
  61. #include <assimp/GenericProperty.h>
  62. #include <assimp/MemoryIOWrapper.h>
  63. #include <assimp/Profiler.h>
  64. #include <assimp/TinyFormatter.h>
  65. #include <assimp/Exceptional.h>
  66. #include <assimp/Profiler.h>
  67. #include <assimp/commonMetaData.h>
  68. #include <exception>
  69. #include <set>
  70. #include <memory>
  71. #include <cctype>
  72. #include <assimp/DefaultIOStream.h>
  73. #include <assimp/DefaultIOSystem.h>
  74. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  75. # include "PostProcessing/ValidateDataStructure.h"
  76. #endif
  77. using namespace Assimp::Profiling;
  78. using namespace Assimp::Formatter;
  79. namespace Assimp {
  80. // ImporterRegistry.cpp
  81. void GetImporterInstanceList(std::vector< BaseImporter* >& out);
  82. void DeleteImporterInstanceList(std::vector< BaseImporter* >& out);
  83. // PostStepRegistry.cpp
  84. void GetPostProcessingStepInstanceList(std::vector< BaseProcess* >& out);
  85. }
  86. using namespace Assimp;
  87. using namespace Assimp::Intern;
  88. // ------------------------------------------------------------------------------------------------
  89. // Intern::AllocateFromAssimpHeap serves as abstract base class. It overrides
  90. // new and delete (and their array counterparts) of public API classes (e.g. Logger) to
  91. // utilize our DLL heap.
  92. // See http://www.gotw.ca/publications/mill15.htm
  93. // ------------------------------------------------------------------------------------------------
  94. void* AllocateFromAssimpHeap::operator new ( size_t num_bytes) {
  95. return ::operator new(num_bytes);
  96. }
  97. void* AllocateFromAssimpHeap::operator new ( size_t num_bytes, const std::nothrow_t& ) throw() {
  98. try {
  99. return AllocateFromAssimpHeap::operator new( num_bytes );
  100. }
  101. catch( ... ) {
  102. return nullptr;
  103. }
  104. }
  105. void AllocateFromAssimpHeap::operator delete ( void* data) {
  106. return ::operator delete(data);
  107. }
  108. void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes) {
  109. return ::operator new[](num_bytes);
  110. }
  111. void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes, const std::nothrow_t& ) throw() {
  112. try {
  113. return AllocateFromAssimpHeap::operator new[]( num_bytes );
  114. } catch( ... ) {
  115. return nullptr;
  116. }
  117. }
  118. void AllocateFromAssimpHeap::operator delete[] ( void* data) {
  119. return ::operator delete[](data);
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. // Importer constructor.
  123. Importer::Importer()
  124. : pimpl( new ImporterPimpl ) {
  125. pimpl->mScene = nullptr;
  126. pimpl->mErrorString = std::string();
  127. // Allocate a default IO handler
  128. pimpl->mIOHandler = new DefaultIOSystem;
  129. pimpl->mIsDefaultHandler = true;
  130. pimpl->bExtraVerbose = false; // disable extra verbose mode by default
  131. pimpl->mProgressHandler = new DefaultProgressHandler();
  132. pimpl->mIsDefaultProgressHandler = true;
  133. GetImporterInstanceList(pimpl->mImporter);
  134. GetPostProcessingStepInstanceList(pimpl->mPostProcessingSteps);
  135. // Allocate a SharedPostProcessInfo object and store pointers to it in all post-process steps in the list.
  136. pimpl->mPPShared = new SharedPostProcessInfo();
  137. for (std::vector<BaseProcess*>::iterator it = pimpl->mPostProcessingSteps.begin();
  138. it != pimpl->mPostProcessingSteps.end();
  139. ++it) {
  140. (*it)->SetSharedData(pimpl->mPPShared);
  141. }
  142. }
  143. // ------------------------------------------------------------------------------------------------
  144. // Destructor of Importer
  145. Importer::~Importer() {
  146. // Delete all import plugins
  147. DeleteImporterInstanceList(pimpl->mImporter);
  148. // Delete all post-processing plug-ins
  149. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); ++a ) {
  150. delete pimpl->mPostProcessingSteps[a];
  151. }
  152. // Delete the assigned IO and progress handler
  153. delete pimpl->mIOHandler;
  154. delete pimpl->mProgressHandler;
  155. // Kill imported scene. Destructor's should do that recursively
  156. delete pimpl->mScene;
  157. // Delete shared post-processing data
  158. delete pimpl->mPPShared;
  159. // and finally the pimpl itself
  160. delete pimpl;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. // Register a custom post-processing step
  164. aiReturn Importer::RegisterPPStep(BaseProcess* pImp) {
  165. ai_assert( nullptr != pImp );
  166. ASSIMP_BEGIN_EXCEPTION_REGION();
  167. pimpl->mPostProcessingSteps.push_back(pImp);
  168. ASSIMP_LOG_INFO("Registering custom post-processing step");
  169. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  170. return AI_SUCCESS;
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. // Register a custom loader plugin
  174. aiReturn Importer::RegisterLoader(BaseImporter* pImp) {
  175. ai_assert(nullptr != pImp);
  176. ASSIMP_BEGIN_EXCEPTION_REGION();
  177. // --------------------------------------------------------------------
  178. // Check whether we would have two loaders for the same file extension
  179. // This is absolutely OK, but we should warn the developer of the new
  180. // loader that his code will probably never be called if the first
  181. // loader is a bit too lazy in his file checking.
  182. // --------------------------------------------------------------------
  183. std::set<std::string> st;
  184. std::string baked;
  185. pImp->GetExtensionList(st);
  186. for(std::set<std::string>::const_iterator it = st.begin(); it != st.end(); ++it) {
  187. #ifdef ASSIMP_BUILD_DEBUG
  188. if (IsExtensionSupported(*it)) {
  189. ASSIMP_LOG_WARN("The file extension ", *it, " is already in use");
  190. }
  191. #endif
  192. baked += *it;
  193. }
  194. // add the loader
  195. pimpl->mImporter.push_back(pImp);
  196. ASSIMP_LOG_INFO("Registering custom importer for these file extensions: ", baked);
  197. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  198. return AI_SUCCESS;
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. // Unregister a custom loader plugin
  202. aiReturn Importer::UnregisterLoader(BaseImporter* pImp) {
  203. if(!pImp) {
  204. // unregistering a nullptr importer is no problem for us ... really!
  205. return AI_SUCCESS;
  206. }
  207. ASSIMP_BEGIN_EXCEPTION_REGION();
  208. std::vector<BaseImporter*>::iterator it = std::find(pimpl->mImporter.begin(),
  209. pimpl->mImporter.end(),pImp);
  210. if (it != pimpl->mImporter.end()) {
  211. pimpl->mImporter.erase(it);
  212. ASSIMP_LOG_INFO("Unregistering custom importer: ");
  213. return AI_SUCCESS;
  214. }
  215. ASSIMP_LOG_WARN("Unable to remove custom importer: I can't find you ...");
  216. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  217. return AI_FAILURE;
  218. }
  219. // ------------------------------------------------------------------------------------------------
  220. // Unregister a custom loader plugin
  221. aiReturn Importer::UnregisterPPStep(BaseProcess* pImp) {
  222. if(!pImp) {
  223. // unregistering a nullptr ppstep is no problem for us ... really!
  224. return AI_SUCCESS;
  225. }
  226. ASSIMP_BEGIN_EXCEPTION_REGION();
  227. std::vector<BaseProcess*>::iterator it = std::find(pimpl->mPostProcessingSteps.begin(),
  228. pimpl->mPostProcessingSteps.end(),pImp);
  229. if (it != pimpl->mPostProcessingSteps.end()) {
  230. pimpl->mPostProcessingSteps.erase(it);
  231. ASSIMP_LOG_INFO("Unregistering custom post-processing step");
  232. return AI_SUCCESS;
  233. }
  234. ASSIMP_LOG_WARN("Unable to remove custom post-processing step: I can't find you ..");
  235. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  236. return AI_FAILURE;
  237. }
  238. // ------------------------------------------------------------------------------------------------
  239. // Supplies a custom IO handler to the importer to open and access files.
  240. void Importer::SetIOHandler( IOSystem* pIOHandler) {
  241. ai_assert(nullptr != pimpl);
  242. ASSIMP_BEGIN_EXCEPTION_REGION();
  243. // If the new handler is zero, allocate a default IO implementation.
  244. if (!pIOHandler) {
  245. // Release pointer in the possession of the caller
  246. pimpl->mIOHandler = new DefaultIOSystem();
  247. pimpl->mIsDefaultHandler = true;
  248. } else if (pimpl->mIOHandler != pIOHandler) { // Otherwise register the custom handler
  249. delete pimpl->mIOHandler;
  250. pimpl->mIOHandler = pIOHandler;
  251. pimpl->mIsDefaultHandler = false;
  252. }
  253. ASSIMP_END_EXCEPTION_REGION(void);
  254. }
  255. // ------------------------------------------------------------------------------------------------
  256. // Get the currently set IO handler
  257. IOSystem* Importer::GetIOHandler() const {
  258. ai_assert(nullptr != pimpl);
  259. return pimpl->mIOHandler;
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. // Check whether a custom IO handler is currently set
  263. bool Importer::IsDefaultIOHandler() const {
  264. ai_assert(nullptr != pimpl);
  265. return pimpl->mIsDefaultHandler;
  266. }
  267. // ------------------------------------------------------------------------------------------------
  268. // Supplies a custom progress handler to get regular callbacks during importing
  269. void Importer::SetProgressHandler ( ProgressHandler* pHandler ) {
  270. ai_assert(nullptr != pimpl);
  271. ASSIMP_BEGIN_EXCEPTION_REGION();
  272. // If the new handler is zero, allocate a default implementation.
  273. if (!pHandler) {
  274. // Release pointer in the possession of the caller
  275. pimpl->mProgressHandler = new DefaultProgressHandler();
  276. pimpl->mIsDefaultProgressHandler = true;
  277. } else if (pimpl->mProgressHandler != pHandler) { // Otherwise register the custom handler
  278. delete pimpl->mProgressHandler;
  279. pimpl->mProgressHandler = pHandler;
  280. pimpl->mIsDefaultProgressHandler = false;
  281. }
  282. ASSIMP_END_EXCEPTION_REGION(void);
  283. }
  284. // ------------------------------------------------------------------------------------------------
  285. // Get the currently set progress handler
  286. ProgressHandler* Importer::GetProgressHandler() const {
  287. ai_assert(nullptr != pimpl);
  288. return pimpl->mProgressHandler;
  289. }
  290. // ------------------------------------------------------------------------------------------------
  291. // Check whether a custom progress handler is currently set
  292. bool Importer::IsDefaultProgressHandler() const {
  293. ai_assert(nullptr != pimpl);
  294. return pimpl->mIsDefaultProgressHandler;
  295. }
  296. // ------------------------------------------------------------------------------------------------
  297. // Validate post process step flags
  298. bool _ValidateFlags(unsigned int pFlags) {
  299. if (pFlags & aiProcess_GenSmoothNormals && pFlags & aiProcess_GenNormals) {
  300. ASSIMP_LOG_ERROR("#aiProcess_GenSmoothNormals and #aiProcess_GenNormals are incompatible");
  301. return false;
  302. }
  303. if (pFlags & aiProcess_OptimizeGraph && pFlags & aiProcess_PreTransformVertices) {
  304. ASSIMP_LOG_ERROR("#aiProcess_OptimizeGraph and #aiProcess_PreTransformVertices are incompatible");
  305. return false;
  306. }
  307. return true;
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. // Free the current scene
  311. void Importer::FreeScene( ) {
  312. ai_assert(nullptr != pimpl);
  313. ASSIMP_BEGIN_EXCEPTION_REGION();
  314. delete pimpl->mScene;
  315. pimpl->mScene = nullptr;
  316. pimpl->mErrorString = std::string();
  317. pimpl->mException = std::exception_ptr();
  318. ASSIMP_END_EXCEPTION_REGION(void);
  319. }
  320. // ------------------------------------------------------------------------------------------------
  321. // Get the current error string, if any
  322. const char* Importer::GetErrorString() const {
  323. ai_assert(nullptr != pimpl);
  324. // Must remain valid as long as ReadFile() or FreeFile() are not called
  325. return pimpl->mErrorString.c_str();
  326. }
  327. const std::exception_ptr& Importer::GetException() const {
  328. ai_assert(nullptr != pimpl);
  329. // Must remain valid as long as ReadFile() or FreeFile() are not called
  330. return pimpl->mException;
  331. }
  332. // ------------------------------------------------------------------------------------------------
  333. // Enable extra-verbose mode
  334. void Importer::SetExtraVerbose(bool bDo) {
  335. ai_assert(nullptr != pimpl);
  336. pimpl->bExtraVerbose = bDo;
  337. }
  338. // ------------------------------------------------------------------------------------------------
  339. // Get the current scene
  340. const aiScene* Importer::GetScene() const {
  341. ai_assert(nullptr != pimpl);
  342. return pimpl->mScene;
  343. }
  344. // ------------------------------------------------------------------------------------------------
  345. // Orphan the current scene and return it.
  346. aiScene* Importer::GetOrphanedScene() {
  347. ai_assert(nullptr != pimpl);
  348. aiScene* s = pimpl->mScene;
  349. ASSIMP_BEGIN_EXCEPTION_REGION();
  350. pimpl->mScene = nullptr;
  351. pimpl->mErrorString = std::string();
  352. pimpl->mException = std::exception_ptr();
  353. ASSIMP_END_EXCEPTION_REGION(aiScene*);
  354. return s;
  355. }
  356. // ------------------------------------------------------------------------------------------------
  357. // Validate post-processing flags
  358. bool Importer::ValidateFlags(unsigned int pFlags) const {
  359. ASSIMP_BEGIN_EXCEPTION_REGION();
  360. // run basic checks for mutually exclusive flags
  361. if(!_ValidateFlags(pFlags)) {
  362. return false;
  363. }
  364. // ValidateDS does not anymore occur in the pp list, it plays an awesome extra role ...
  365. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  366. if (pFlags & aiProcess_ValidateDataStructure) {
  367. return false;
  368. }
  369. #endif
  370. pFlags &= ~aiProcess_ValidateDataStructure;
  371. // Now iterate through all bits which are set in the flags and check whether we find at least
  372. // one pp plugin which handles it.
  373. for (unsigned int mask = 1; mask < (1u << (sizeof(unsigned int)*8-1));mask <<= 1) {
  374. if (pFlags & mask) {
  375. bool have = false;
  376. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  377. if (pimpl->mPostProcessingSteps[a]-> IsActive(mask) ) {
  378. have = true;
  379. break;
  380. }
  381. }
  382. if (!have) {
  383. return false;
  384. }
  385. }
  386. }
  387. ASSIMP_END_EXCEPTION_REGION(bool);
  388. return true;
  389. }
  390. // ------------------------------------------------------------------------------------------------
  391. const aiScene* Importer::ReadFileFromMemory( const void* pBuffer,
  392. size_t pLength,
  393. unsigned int pFlags,
  394. const char* pHint /*= ""*/) {
  395. ai_assert(nullptr != pimpl);
  396. ASSIMP_BEGIN_EXCEPTION_REGION();
  397. if (!pHint) {
  398. pHint = "";
  399. }
  400. if (!pBuffer || !pLength || strlen(pHint) > MaxLenHint ) {
  401. pimpl->mErrorString = "Invalid parameters passed to ReadFileFromMemory()";
  402. return nullptr;
  403. }
  404. // prevent deletion of the previous IOHandler
  405. IOSystem* io = pimpl->mIOHandler;
  406. pimpl->mIOHandler = nullptr;
  407. SetIOHandler(new MemoryIOSystem((const uint8_t*)pBuffer,pLength,io));
  408. // read the file and recover the previous IOSystem
  409. static const size_t BufSize(Importer::MaxLenHint + 28);
  410. char fbuff[BufSize];
  411. ai_snprintf(fbuff, BufSize, "%s.%s",AI_MEMORYIO_MAGIC_FILENAME,pHint);
  412. ReadFile(fbuff,pFlags);
  413. SetIOHandler(io);
  414. ASSIMP_END_EXCEPTION_REGION_WITH_ERROR_STRING(const aiScene*, pimpl->mErrorString, pimpl->mException);
  415. return pimpl->mScene;
  416. }
  417. // ------------------------------------------------------------------------------------------------
  418. void WriteLogOpening(const std::string& file) {
  419. ASSIMP_LOG_INFO("Load ", file);
  420. // print a full version dump. This is nice because we don't
  421. // need to ask the authors of incoming bug reports for
  422. // the library version they're using - a log dump is
  423. // sufficient.
  424. const unsigned int flags = aiGetCompileFlags();
  425. std::stringstream stream;
  426. stream << "Assimp " << aiGetVersionMajor() << "." << aiGetVersionMinor() << "." << aiGetVersionRevision() << " "
  427. #if defined(ASSIMP_BUILD_ARCHITECTURE)
  428. << ASSIMP_BUILD_ARCHITECTURE
  429. #elif defined(_M_IX86) || defined(__x86_32__) || defined(__i386__)
  430. << "x86"
  431. #elif defined(_M_X64) || defined(__x86_64__)
  432. << "amd64"
  433. #elif defined(_M_IA64) || defined(__ia64__)
  434. << "itanium"
  435. #elif defined(__ppc__) || defined(__powerpc__)
  436. << "ppc32"
  437. #elif defined(__powerpc64__)
  438. << "ppc64"
  439. #elif defined(__arm__)
  440. << "arm"
  441. #else
  442. << "<unknown architecture>"
  443. #endif
  444. << " "
  445. #if defined(ASSIMP_BUILD_COMPILER)
  446. << (ASSIMP_BUILD_COMPILER)
  447. #elif defined(_MSC_VER)
  448. << "msvc"
  449. #elif defined(__GNUC__)
  450. << "gcc"
  451. #elif defined(__clang__)
  452. << "clang"
  453. #elif defined(__EMSCRIPTEN__)
  454. << "emscripten"
  455. #elif defined(__MINGW32__)
  456. << "MinGW-w64 32bit"
  457. #elif defined(__MINGW64__)
  458. << "MinGW-w64 64bit"
  459. #else
  460. << "<unknown compiler>"
  461. #endif
  462. #ifdef ASSIMP_BUILD_DEBUG
  463. << " debug"
  464. #endif
  465. << (flags & ASSIMP_CFLAGS_NOBOOST ? " noboost" : "")
  466. << (flags & ASSIMP_CFLAGS_SHARED ? " shared" : "")
  467. << (flags & ASSIMP_CFLAGS_SINGLETHREADED ? " singlethreaded" : "")
  468. << (flags & ASSIMP_CFLAGS_DOUBLE_SUPPORT ? " double : " : "single : ");
  469. ASSIMP_LOG_DEBUG(stream.str());
  470. }
  471. // ------------------------------------------------------------------------------------------------
  472. // Reads the given file and returns its contents if successful.
  473. const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags) {
  474. ai_assert(nullptr != pimpl);
  475. ASSIMP_BEGIN_EXCEPTION_REGION();
  476. const std::string pFile(_pFile);
  477. // ----------------------------------------------------------------------
  478. // Put a large try block around everything to catch all std::exception's
  479. // that might be thrown by STL containers or by new().
  480. // ImportErrorException's are throw by ourselves and caught elsewhere.
  481. //-----------------------------------------------------------------------
  482. WriteLogOpening(pFile);
  483. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  484. try
  485. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  486. {
  487. // Check whether this Importer instance has already loaded
  488. // a scene. In this case we need to delete the old one
  489. if (pimpl->mScene) {
  490. ASSIMP_LOG_DEBUG("(Deleting previous scene)");
  491. FreeScene();
  492. }
  493. // First check if the file is accessible at all
  494. if( !pimpl->mIOHandler->Exists( pFile)) {
  495. pimpl->mErrorString = "Unable to open file \"" + pFile + "\".";
  496. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  497. return nullptr;
  498. }
  499. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  500. if (profiler) {
  501. profiler->BeginRegion("total");
  502. }
  503. // Find an worker class which can handle the file
  504. BaseImporter* imp = nullptr;
  505. SetPropertyInteger("importerIndex", -1);
  506. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  507. if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, false)) {
  508. imp = pimpl->mImporter[a];
  509. SetPropertyInteger("importerIndex", a);
  510. break;
  511. }
  512. }
  513. if (!imp) {
  514. // not so bad yet ... try format auto detection.
  515. const std::string::size_type s = pFile.find_last_of('.');
  516. if (s != std::string::npos) {
  517. ASSIMP_LOG_INFO("File extension not known, trying signature-based detection");
  518. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  519. if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, true)) {
  520. imp = pimpl->mImporter[a];
  521. SetPropertyInteger("importerIndex", a);
  522. break;
  523. }
  524. }
  525. }
  526. // Put a proper error message if no suitable importer was found
  527. if( !imp) {
  528. pimpl->mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  529. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  530. return nullptr;
  531. }
  532. }
  533. // Get file size for progress handler
  534. IOStream * fileIO = pimpl->mIOHandler->Open( pFile );
  535. uint32_t fileSize = 0;
  536. if (fileIO)
  537. {
  538. fileSize = static_cast<uint32_t>(fileIO->FileSize());
  539. pimpl->mIOHandler->Close( fileIO );
  540. }
  541. // Dispatch the reading to the worker class for this format
  542. const aiImporterDesc *desc( imp->GetInfo() );
  543. std::string ext( "unknown" );
  544. if ( nullptr != desc ) {
  545. ext = desc->mName;
  546. }
  547. ASSIMP_LOG_INFO("Found a matching importer for this file format: ", ext, "." );
  548. pimpl->mProgressHandler->UpdateFileRead( 0, fileSize );
  549. if (profiler) {
  550. profiler->BeginRegion("import");
  551. }
  552. pimpl->mScene = imp->ReadFile( this, pFile, pimpl->mIOHandler);
  553. pimpl->mProgressHandler->UpdateFileRead( fileSize, fileSize );
  554. if (profiler) {
  555. profiler->EndRegion("import");
  556. }
  557. SetPropertyString("sourceFilePath", pFile);
  558. // If successful, apply all active post processing steps to the imported data
  559. if( pimpl->mScene) {
  560. if (!pimpl->mScene->mMetaData || !pimpl->mScene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT)) {
  561. if (!pimpl->mScene->mMetaData) {
  562. pimpl->mScene->mMetaData = new aiMetadata;
  563. }
  564. pimpl->mScene->mMetaData->Add(AI_METADATA_SOURCE_FORMAT, aiString(ext));
  565. }
  566. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  567. // The ValidateDS process is an exception. It is executed first, even before ScenePreprocessor is called.
  568. if (pFlags & aiProcess_ValidateDataStructure) {
  569. ValidateDSProcess ds;
  570. ds.ExecuteOnScene (this);
  571. if (!pimpl->mScene) {
  572. return nullptr;
  573. }
  574. }
  575. #endif // no validation
  576. // Preprocess the scene and prepare it for post-processing
  577. if (profiler) {
  578. profiler->BeginRegion("preprocess");
  579. }
  580. ScenePreprocessor pre(pimpl->mScene);
  581. pre.ProcessScene();
  582. if (profiler) {
  583. profiler->EndRegion("preprocess");
  584. }
  585. // Ensure that the validation process won't be called twice
  586. ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure));
  587. }
  588. // if failed, extract the error string
  589. else if( !pimpl->mScene) {
  590. pimpl->mErrorString = imp->GetErrorText();
  591. pimpl->mException = imp->GetException();
  592. }
  593. // clear any data allocated by post-process steps
  594. pimpl->mPPShared->Clean();
  595. if (profiler) {
  596. profiler->EndRegion("total");
  597. }
  598. }
  599. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  600. catch (std::exception &e) {
  601. #if (defined _MSC_VER) && (defined _CPPRTTI)
  602. // if we have RTTI get the full name of the exception that occurred
  603. pimpl->mErrorString = std::string(typeid( e ).name()) + ": " + e.what();
  604. #else
  605. pimpl->mErrorString = std::string("std::exception: ") + e.what();
  606. #endif
  607. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  608. delete pimpl->mScene; pimpl->mScene = nullptr;
  609. }
  610. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  611. // either successful or failure - the pointer expresses it anyways
  612. ASSIMP_END_EXCEPTION_REGION_WITH_ERROR_STRING(const aiScene*, pimpl->mErrorString, pimpl->mException);
  613. return pimpl->mScene;
  614. }
  615. // ------------------------------------------------------------------------------------------------
  616. // Apply post-processing to the currently bound scene
  617. const aiScene* Importer::ApplyPostProcessing(unsigned int pFlags) {
  618. ai_assert(nullptr != pimpl);
  619. ASSIMP_BEGIN_EXCEPTION_REGION();
  620. // Return immediately if no scene is active
  621. if (!pimpl->mScene) {
  622. return nullptr;
  623. }
  624. // If no flags are given, return the current scene with no further action
  625. if (!pFlags) {
  626. return pimpl->mScene;
  627. }
  628. // In debug builds: run basic flag validation
  629. ai_assert(_ValidateFlags(pFlags));
  630. ASSIMP_LOG_INFO("Entering post processing pipeline");
  631. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  632. // The ValidateDS process plays an exceptional role. It isn't contained in the global
  633. // list of post-processing steps, so we need to call it manually.
  634. if (pFlags & aiProcess_ValidateDataStructure) {
  635. ValidateDSProcess ds;
  636. ds.ExecuteOnScene (this);
  637. if (!pimpl->mScene) {
  638. return nullptr;
  639. }
  640. }
  641. #endif // no validation
  642. #ifdef ASSIMP_BUILD_DEBUG
  643. if (pimpl->bExtraVerbose)
  644. {
  645. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  646. ASSIMP_LOG_ERROR("Verbose Import is not available due to build settings");
  647. #endif // no validation
  648. pFlags |= aiProcess_ValidateDataStructure;
  649. }
  650. #else
  651. if (pimpl->bExtraVerbose) {
  652. ASSIMP_LOG_WARN("Not a debug build, ignoring extra verbose setting");
  653. }
  654. #endif // ! DEBUG
  655. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  656. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  657. BaseProcess* process = pimpl->mPostProcessingSteps[a];
  658. pimpl->mProgressHandler->UpdatePostProcess(static_cast<int>(a), static_cast<int>(pimpl->mPostProcessingSteps.size()) );
  659. if( process->IsActive( pFlags)) {
  660. if (profiler) {
  661. profiler->BeginRegion("postprocess");
  662. }
  663. process->ExecuteOnScene ( this );
  664. if (profiler) {
  665. profiler->EndRegion("postprocess");
  666. }
  667. }
  668. if( !pimpl->mScene) {
  669. break;
  670. }
  671. #ifdef ASSIMP_BUILD_DEBUG
  672. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  673. continue;
  674. #endif // no validation
  675. // If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
  676. if (pimpl->bExtraVerbose) {
  677. ASSIMP_LOG_DEBUG("Verbose Import: re-validating data structures");
  678. ValidateDSProcess ds;
  679. ds.ExecuteOnScene (this);
  680. if( !pimpl->mScene) {
  681. ASSIMP_LOG_ERROR("Verbose Import: failed to re-validate data structures");
  682. break;
  683. }
  684. }
  685. #endif // ! DEBUG
  686. }
  687. pimpl->mProgressHandler->UpdatePostProcess( static_cast<int>(pimpl->mPostProcessingSteps.size()),
  688. static_cast<int>(pimpl->mPostProcessingSteps.size()) );
  689. // update private scene flags
  690. if( pimpl->mScene ) {
  691. ScenePriv(pimpl->mScene)->mPPStepsApplied |= pFlags;
  692. }
  693. // clear any data allocated by post-process steps
  694. pimpl->mPPShared->Clean();
  695. ASSIMP_LOG_INFO("Leaving post processing pipeline");
  696. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  697. return pimpl->mScene;
  698. }
  699. // ------------------------------------------------------------------------------------------------
  700. const aiScene* Importer::ApplyCustomizedPostProcessing( BaseProcess *rootProcess, bool requestValidation ) {
  701. ai_assert(nullptr != pimpl);
  702. ASSIMP_BEGIN_EXCEPTION_REGION();
  703. // Return immediately if no scene is active
  704. if ( nullptr == pimpl->mScene ) {
  705. return nullptr;
  706. }
  707. // If no flags are given, return the current scene with no further action
  708. if (nullptr == rootProcess) {
  709. return pimpl->mScene;
  710. }
  711. // In debug builds: run basic flag validation
  712. ASSIMP_LOG_INFO( "Entering customized post processing pipeline" );
  713. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  714. // The ValidateDS process plays an exceptional role. It isn't contained in the global
  715. // list of post-processing steps, so we need to call it manually.
  716. if ( requestValidation )
  717. {
  718. ValidateDSProcess ds;
  719. ds.ExecuteOnScene( this );
  720. if ( !pimpl->mScene ) {
  721. return nullptr;
  722. }
  723. }
  724. #endif // no validation
  725. #ifdef ASSIMP_BUILD_DEBUG
  726. if ( pimpl->bExtraVerbose )
  727. {
  728. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  729. ASSIMP_LOG_ERROR( "Verbose Import is not available due to build settings" );
  730. #endif // no validation
  731. }
  732. #else
  733. if ( pimpl->bExtraVerbose ) {
  734. ASSIMP_LOG_WARN( "Not a debug build, ignoring extra verbose setting" );
  735. }
  736. #endif // ! DEBUG
  737. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  738. if ( profiler ) {
  739. profiler->BeginRegion( "postprocess" );
  740. }
  741. rootProcess->ExecuteOnScene( this );
  742. if ( profiler ) {
  743. profiler->EndRegion( "postprocess" );
  744. }
  745. // If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
  746. if ( pimpl->bExtraVerbose || requestValidation ) {
  747. ASSIMP_LOG_DEBUG( "Verbose Import: revalidating data structures" );
  748. ValidateDSProcess ds;
  749. ds.ExecuteOnScene( this );
  750. if ( !pimpl->mScene ) {
  751. ASSIMP_LOG_ERROR( "Verbose Import: failed to revalidate data structures" );
  752. }
  753. }
  754. // clear any data allocated by post-process steps
  755. pimpl->mPPShared->Clean();
  756. ASSIMP_LOG_INFO( "Leaving customized post processing pipeline" );
  757. ASSIMP_END_EXCEPTION_REGION( const aiScene* );
  758. return pimpl->mScene;
  759. }
  760. // ------------------------------------------------------------------------------------------------
  761. // Helper function to check whether an extension is supported by ASSIMP
  762. bool Importer::IsExtensionSupported(const char* szExtension) const {
  763. return nullptr != GetImporter(szExtension);
  764. }
  765. // ------------------------------------------------------------------------------------------------
  766. size_t Importer::GetImporterCount() const {
  767. ai_assert(nullptr != pimpl);
  768. return pimpl->mImporter.size();
  769. }
  770. // ------------------------------------------------------------------------------------------------
  771. const aiImporterDesc* Importer::GetImporterInfo(size_t index) const {
  772. ai_assert(nullptr != pimpl);
  773. if (index >= pimpl->mImporter.size()) {
  774. return nullptr;
  775. }
  776. return pimpl->mImporter[index]->GetInfo();
  777. }
  778. // ------------------------------------------------------------------------------------------------
  779. BaseImporter* Importer::GetImporter (size_t index) const {
  780. ai_assert(nullptr != pimpl);
  781. if (index >= pimpl->mImporter.size()) {
  782. return nullptr;
  783. }
  784. return pimpl->mImporter[index];
  785. }
  786. // ------------------------------------------------------------------------------------------------
  787. // Find a loader plugin for a given file extension
  788. BaseImporter* Importer::GetImporter (const char* szExtension) const {
  789. ai_assert(nullptr != pimpl);
  790. return GetImporter(GetImporterIndex(szExtension));
  791. }
  792. // ------------------------------------------------------------------------------------------------
  793. // Find a loader plugin for a given file extension
  794. size_t Importer::GetImporterIndex (const char* szExtension) const {
  795. ai_assert(nullptr != pimpl);
  796. ai_assert(nullptr != szExtension);
  797. ASSIMP_BEGIN_EXCEPTION_REGION();
  798. // skip over wild-card and dot characters at string head --
  799. for ( ; *szExtension == '*' || *szExtension == '.'; ++szExtension );
  800. std::string ext(szExtension);
  801. if (ext.empty()) {
  802. return static_cast<size_t>(-1);
  803. }
  804. ext = ai_tolower(ext);
  805. std::set<std::string> str;
  806. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  807. str.clear();
  808. (*i)->GetExtensionList(str);
  809. for (std::set<std::string>::const_iterator it = str.begin(); it != str.end(); ++it) {
  810. if (ext == *it) {
  811. return std::distance(static_cast< std::vector<BaseImporter*>::const_iterator >(pimpl->mImporter.begin()), i);
  812. }
  813. }
  814. }
  815. ASSIMP_END_EXCEPTION_REGION(size_t);
  816. return static_cast<size_t>(-1);
  817. }
  818. // ------------------------------------------------------------------------------------------------
  819. // Helper function to build a list of all file extensions supported by ASSIMP
  820. void Importer::GetExtensionList(aiString& szOut) const {
  821. ai_assert(nullptr != pimpl);
  822. ASSIMP_BEGIN_EXCEPTION_REGION();
  823. std::set<std::string> str;
  824. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  825. (*i)->GetExtensionList(str);
  826. }
  827. // List can be empty
  828. if( !str.empty() ) {
  829. for (std::set<std::string>::const_iterator it = str.begin();; ) {
  830. szOut.Append("*.");
  831. szOut.Append((*it).c_str());
  832. if (++it == str.end()) {
  833. break;
  834. }
  835. szOut.Append(";");
  836. }
  837. }
  838. ASSIMP_END_EXCEPTION_REGION(void);
  839. }
  840. // ------------------------------------------------------------------------------------------------
  841. // Set a configuration property
  842. bool Importer::SetPropertyInteger(const char* szName, int iValue) {
  843. ai_assert(nullptr != pimpl);
  844. bool existing;
  845. ASSIMP_BEGIN_EXCEPTION_REGION();
  846. existing = SetGenericProperty<int>(pimpl->mIntProperties, szName,iValue);
  847. ASSIMP_END_EXCEPTION_REGION(bool);
  848. return existing;
  849. }
  850. // ------------------------------------------------------------------------------------------------
  851. // Set a configuration property
  852. bool Importer::SetPropertyFloat(const char* szName, ai_real iValue) {
  853. ai_assert(nullptr != pimpl);
  854. bool existing;
  855. ASSIMP_BEGIN_EXCEPTION_REGION();
  856. existing = SetGenericProperty<ai_real>(pimpl->mFloatProperties, szName,iValue);
  857. ASSIMP_END_EXCEPTION_REGION(bool);
  858. return existing;
  859. }
  860. // ------------------------------------------------------------------------------------------------
  861. // Set a configuration property
  862. bool Importer::SetPropertyString(const char* szName, const std::string& value) {
  863. ai_assert(nullptr != pimpl);
  864. bool existing;
  865. ASSIMP_BEGIN_EXCEPTION_REGION();
  866. existing = SetGenericProperty<std::string>(pimpl->mStringProperties, szName,value);
  867. ASSIMP_END_EXCEPTION_REGION(bool);
  868. return existing;
  869. }
  870. // ------------------------------------------------------------------------------------------------
  871. // Set a configuration property
  872. bool Importer::SetPropertyMatrix(const char* szName, const aiMatrix4x4& value) {
  873. ai_assert(nullptr != pimpl);
  874. bool existing;
  875. ASSIMP_BEGIN_EXCEPTION_REGION();
  876. existing = SetGenericProperty<aiMatrix4x4>(pimpl->mMatrixProperties, szName,value);
  877. ASSIMP_END_EXCEPTION_REGION(bool);
  878. return existing;
  879. }
  880. // ------------------------------------------------------------------------------------------------
  881. // Get a configuration property
  882. int Importer::GetPropertyInteger(const char* szName, int iErrorReturn /*= 0xffffffff*/) const {
  883. ai_assert(nullptr != pimpl);
  884. return GetGenericProperty<int>(pimpl->mIntProperties,szName,iErrorReturn);
  885. }
  886. // ------------------------------------------------------------------------------------------------
  887. // Get a configuration property
  888. ai_real Importer::GetPropertyFloat(const char* szName, ai_real iErrorReturn /*= 10e10*/) const {
  889. ai_assert(nullptr != pimpl);
  890. return GetGenericProperty<ai_real>(pimpl->mFloatProperties,szName,iErrorReturn);
  891. }
  892. // ------------------------------------------------------------------------------------------------
  893. // Get a configuration property
  894. std::string Importer::GetPropertyString(const char* szName, const std::string& iErrorReturn /*= ""*/) const {
  895. ai_assert(nullptr != pimpl);
  896. return GetGenericProperty<std::string>(pimpl->mStringProperties,szName,iErrorReturn);
  897. }
  898. // ------------------------------------------------------------------------------------------------
  899. // Get a configuration property
  900. aiMatrix4x4 Importer::GetPropertyMatrix(const char* szName, const aiMatrix4x4& iErrorReturn /*= aiMatrix4x4()*/) const {
  901. ai_assert(nullptr != pimpl);
  902. return GetGenericProperty<aiMatrix4x4>(pimpl->mMatrixProperties,szName,iErrorReturn);
  903. }
  904. // ------------------------------------------------------------------------------------------------
  905. // Get the memory requirements of a single node
  906. inline
  907. void AddNodeWeight(unsigned int& iScene,const aiNode* pcNode) {
  908. if ( nullptr == pcNode ) {
  909. return;
  910. }
  911. iScene += sizeof(aiNode);
  912. iScene += sizeof(unsigned int) * pcNode->mNumMeshes;
  913. iScene += sizeof(void*) * pcNode->mNumChildren;
  914. for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
  915. AddNodeWeight(iScene,pcNode->mChildren[i]);
  916. }
  917. }
  918. // ------------------------------------------------------------------------------------------------
  919. // Get the memory requirements of the scene
  920. void Importer::GetMemoryRequirements(aiMemoryInfo& in) const {
  921. ai_assert(nullptr != pimpl);
  922. in = aiMemoryInfo();
  923. aiScene* mScene = pimpl->mScene;
  924. // return if we have no scene loaded
  925. if (!mScene)
  926. return;
  927. in.total = sizeof(aiScene);
  928. // add all meshes
  929. for (unsigned int i = 0; i < mScene->mNumMeshes;++i) {
  930. in.meshes += sizeof(aiMesh);
  931. if (mScene->mMeshes[i]->HasPositions()) {
  932. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  933. }
  934. if (mScene->mMeshes[i]->HasNormals()) {
  935. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  936. }
  937. if (mScene->mMeshes[i]->HasTangentsAndBitangents()) {
  938. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices * 2;
  939. }
  940. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a) {
  941. if (mScene->mMeshes[i]->HasVertexColors(a)) {
  942. in.meshes += sizeof(aiColor4D) * mScene->mMeshes[i]->mNumVertices;
  943. } else {
  944. break;
  945. }
  946. }
  947. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a) {
  948. if (mScene->mMeshes[i]->HasTextureCoords(a)) {
  949. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  950. } else {
  951. break;
  952. }
  953. }
  954. if (mScene->mMeshes[i]->HasBones()) {
  955. in.meshes += sizeof(void*) * mScene->mMeshes[i]->mNumBones;
  956. for (unsigned int p = 0; p < mScene->mMeshes[i]->mNumBones;++p) {
  957. in.meshes += sizeof(aiBone);
  958. in.meshes += mScene->mMeshes[i]->mBones[p]->mNumWeights * sizeof(aiVertexWeight);
  959. }
  960. }
  961. in.meshes += (sizeof(aiFace) + 3 * sizeof(unsigned int))*mScene->mMeshes[i]->mNumFaces;
  962. }
  963. in.total += in.meshes;
  964. // add all embedded textures
  965. for (unsigned int i = 0; i < mScene->mNumTextures;++i) {
  966. const aiTexture* pc = mScene->mTextures[i];
  967. in.textures += sizeof(aiTexture);
  968. if (pc->mHeight) {
  969. in.textures += 4 * pc->mHeight * pc->mWidth;
  970. } else {
  971. in.textures += pc->mWidth;
  972. }
  973. }
  974. in.total += in.textures;
  975. // add all animations
  976. for (unsigned int i = 0; i < mScene->mNumAnimations;++i) {
  977. const aiAnimation* pc = mScene->mAnimations[i];
  978. in.animations += sizeof(aiAnimation);
  979. // add all bone anims
  980. for (unsigned int a = 0; a < pc->mNumChannels; ++a) {
  981. const aiNodeAnim* pc2 = pc->mChannels[a];
  982. in.animations += sizeof(aiNodeAnim);
  983. in.animations += pc2->mNumPositionKeys * sizeof(aiVectorKey);
  984. in.animations += pc2->mNumScalingKeys * sizeof(aiVectorKey);
  985. in.animations += pc2->mNumRotationKeys * sizeof(aiQuatKey);
  986. }
  987. }
  988. in.total += in.animations;
  989. // add all cameras and all lights
  990. in.total += in.cameras = sizeof(aiCamera) * mScene->mNumCameras;
  991. in.total += in.lights = sizeof(aiLight) * mScene->mNumLights;
  992. // add all nodes
  993. AddNodeWeight(in.nodes,mScene->mRootNode);
  994. in.total += in.nodes;
  995. // add all materials
  996. for (unsigned int i = 0; i < mScene->mNumMaterials;++i) {
  997. const aiMaterial* pc = mScene->mMaterials[i];
  998. in.materials += sizeof(aiMaterial);
  999. in.materials += pc->mNumAllocated * sizeof(void*);
  1000. for (unsigned int a = 0; a < pc->mNumProperties;++a) {
  1001. in.materials += pc->mProperties[a]->mDataLength;
  1002. }
  1003. }
  1004. in.total += in.materials;
  1005. }