Importer.cpp 42 KB

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