assimp.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file assimp.hpp
  35. * @brief Defines the C++-API to the Open Asset Import Library.
  36. */
  37. #ifndef INCLUDED_AI_ASSIMP_HPP
  38. #define INCLUDED_AI_ASSIMP_HPP
  39. #ifndef __cplusplus
  40. # error This header requires C++ to be used. Use assimp.h for plain C.
  41. #endif
  42. // Public ASSIMP data structures
  43. #include "aiTypes.h"
  44. #include "aiConfig.h"
  45. #include "aiAssert.h"
  46. namespace Assimp {
  47. // =======================================================================
  48. // Public interface to Assimp
  49. class Importer;
  50. class IOStream;
  51. class IOSystem;
  52. // =======================================================================
  53. // Plugin development
  54. //
  55. // Include the following headers for the declarations:
  56. // BaseImporter.h
  57. // BaseProcess.h
  58. class BaseImporter;
  59. class BaseProcess;
  60. class SharedPostProcessInfo;
  61. class BatchLoader;
  62. // =======================================================================
  63. // Holy stuff, only for members of the high council of the Jedi.
  64. class ImporterPimpl;
  65. } //! namespace Assimp
  66. #define AI_PROPERTY_WAS_NOT_EXISTING 0xffffffff
  67. struct aiScene;
  68. struct aiFileIO;
  69. extern "C" ASSIMP_API const aiScene* aiImportFileEx( const char*, unsigned int, aiFileIO*);
  70. extern "C" ASSIMP_API const aiScene* aiImportFileFromMemory( const char*,
  71. unsigned int,unsigned int,const char*);
  72. namespace Assimp {
  73. // ----------------------------------------------------------------------------------
  74. /** The Importer class forms an C++ interface to the functionality of the
  75. * Open Asset Import Library.
  76. *
  77. * Create an object of this class and call ReadFile() to import a file.
  78. * If the import succeeds, the function returns a pointer to the imported data.
  79. * The data remains property of the object, it is intended to be accessed
  80. * read-only. The imported data will be destroyed along with the Importer
  81. * object. If the import fails, ReadFile() returns a NULL pointer. In this
  82. * case you can retrieve a human-readable error description be calling
  83. * GetErrorString(). You can call ReadFile() multiple times with a single Importer
  84. * instance. Actually, constructing Importer objects involves quite many
  85. * allocations and may take some time, so it's better to reuse them as often as
  86. * possible.
  87. *
  88. * If you need the Importer to do custom file handling to access the files,
  89. * implement IOSystem and IOStream and supply an instance of your custom
  90. * IOSystem implementation by calling SetIOHandler() before calling ReadFile().
  91. * If you do not assign a custion IO handler, a default handler using the
  92. * standard C++ IO logic will be used.
  93. *
  94. * @note One Importer instance is not thread-safe. If you use multiple
  95. * threads for loading, each thread should maintain its own Importer instance.
  96. */
  97. class ASSIMP_API Importer {
  98. // for internal use
  99. friend class BaseProcess;
  100. friend class BatchLoader;
  101. friend const aiScene* ::aiImportFileEx( const char*, unsigned int, aiFileIO*);
  102. friend const aiScene* ::aiImportFileFromMemory( const char*,
  103. unsigned int,unsigned int,const char*);
  104. public:
  105. // -------------------------------------------------------------------
  106. /** Constructor. Creates an empty importer object.
  107. *
  108. * Call ReadFile() to start the import process. The configuration
  109. * property table is initially empty.
  110. */
  111. Importer();
  112. // -------------------------------------------------------------------
  113. /** Copy constructor.
  114. *
  115. * This copies the configuration properties of another Importer.
  116. * If this Importer owns a scene it won't be copied.
  117. * Call ReadFile() to start the import process.
  118. */
  119. Importer(const Importer& other);
  120. // -------------------------------------------------------------------
  121. /** Destructor. The object kept ownership of the imported data,
  122. * which now will be destroyed along with the object.
  123. */
  124. ~Importer();
  125. // -------------------------------------------------------------------
  126. /** Registers a new loader.
  127. *
  128. * @param pImp Importer to be added. The Importer instance takes
  129. * ownership of the pointer, so it will be automatically deleted
  130. * with the Importer instance.
  131. * @return AI_SUCCESS if the loader has been added. The registration
  132. * fails if there is already a loader for a specific file extension.
  133. */
  134. aiReturn RegisterLoader(BaseImporter* pImp);
  135. // -------------------------------------------------------------------
  136. /** Unregisters a loader.
  137. *
  138. * @param pImp Importer to be unregistered.
  139. * @return AI_SUCCESS if the loader has been removed. The function
  140. * fails if the loader is currently in use (this could happen
  141. * if the #Importer instance is used by more than one thread) or
  142. * if it has not yet been registered.
  143. */
  144. aiReturn UnregisterLoader(BaseImporter* pImp);
  145. // -------------------------------------------------------------------
  146. /** Registers a new post-process step.
  147. *
  148. * At the moment, there's a small limitation: new post processing
  149. * steps are added to end of the list, or in other words, executed
  150. * last, after all built-in steps.
  151. * @param pImp Post-process step to be added. The Importer instance
  152. * takes ownership of the pointer, so it will be automatically
  153. * deleted with the Importer instance.
  154. * @return AI_SUCCESS if the step has been added correctly.
  155. */
  156. aiReturn RegisterPPStep(BaseProcess* pImp);
  157. // -------------------------------------------------------------------
  158. /** Unregisters a post-process step.
  159. *
  160. * @param pImp Step to be unregistered.
  161. * @return AI_SUCCESS if the step has been removed. The function
  162. * fails if the step is currently in use (this could happen
  163. * if the #Importer instance is used by more than one thread) or
  164. * if it has not yet been registered.
  165. */
  166. aiReturn UnregisterPPStep(BaseProcess* pImp);
  167. // -------------------------------------------------------------------
  168. /** Set an integer configuration property.
  169. * @param szName Name of the property. All supported properties
  170. * are defined in the aiConfig.g header (all constants share the
  171. * prefix AI_CONFIG_XXX).
  172. * @param iValue New value of the property
  173. * @param bWasExisting Optional pointer to receive true if the
  174. * property was set before. The new value replaced the old value
  175. * in this case.
  176. * @note Property of different types (float, int, string ..) are kept
  177. * on different stacks, so calling SetPropertyInteger() for a
  178. * floating-point property has no effect - the loader will call
  179. * GetPropertyFloat() to read the property, but it won't be there.
  180. */
  181. void SetPropertyInteger(const char* szName, int iValue,
  182. bool* bWasExisting = NULL);
  183. // -------------------------------------------------------------------
  184. /** Set a floating-point configuration property.
  185. * @see SetPropertyInteger()
  186. */
  187. void SetPropertyFloat(const char* szName, float fValue,
  188. bool* bWasExisting = NULL);
  189. // -------------------------------------------------------------------
  190. /** Set a string configuration property.
  191. * @see SetPropertyInteger()
  192. */
  193. void SetPropertyString(const char* szName, const std::string& sValue,
  194. bool* bWasExisting = NULL);
  195. // -------------------------------------------------------------------
  196. /** Get a configuration property.
  197. * @param szName Name of the property. All supported properties
  198. * are defined in the aiConfig.g header (all constants share the
  199. * prefix AI_CONFIG_XXX).
  200. * @param iErrorReturn Value that is returned if the property
  201. * is not found.
  202. * @return Current value of the property
  203. * @note Property of different types (float, int, string ..) are kept
  204. * on different lists, so calling SetPropertyInteger() for a
  205. * floating-point property has no effect - the loader will call
  206. * GetPropertyFloat() to read the property, but it won't be there.
  207. */
  208. int GetPropertyInteger(const char* szName,
  209. int iErrorReturn = 0xffffffff) const;
  210. // -------------------------------------------------------------------
  211. /** Get a floating-point configuration property
  212. * @see GetPropertyInteger()
  213. */
  214. float GetPropertyFloat(const char* szName,
  215. float fErrorReturn = 10e10f) const;
  216. // -------------------------------------------------------------------
  217. /** Get a string configuration property
  218. *
  219. * The return value remains valid until the property is modified.
  220. * @see GetPropertyInteger()
  221. */
  222. const std::string& GetPropertyString(const char* szName,
  223. const std::string& sErrorReturn = "") const;
  224. // -------------------------------------------------------------------
  225. /** Supplies a custom IO handler to the importer to use to open and
  226. * access files. If you need the importer to use custion IO logic to
  227. * access the files, you need to provide a custom implementation of
  228. * IOSystem and IOFile to the importer. Then create an instance of
  229. * your custion IOSystem implementation and supply it by this function.
  230. *
  231. * The Importer takes ownership of the object and will destroy it
  232. * afterwards. The previously assigned handler will be deleted.
  233. * Pass NULL to take again ownership of your IOSystem and reset Assimp
  234. * to use its default implementation.
  235. *
  236. * @param pIOHandler The IO handler to be used in all file accesses
  237. * of the Importer.
  238. */
  239. void SetIOHandler( IOSystem* pIOHandler);
  240. // -------------------------------------------------------------------
  241. /** Retrieves the IO handler that is currently set.
  242. * You can use IsDefaultIOHandler() to check whether the returned
  243. * interface is the default IO handler provided by ASSIMP. The default
  244. * handler is active as long the application doesn't supply its own
  245. * custom IO handler via SetIOHandler().
  246. * @return A valid IOSystem interface
  247. */
  248. IOSystem* GetIOHandler();
  249. // -------------------------------------------------------------------
  250. /** Checks whether a default IO handler is active
  251. * A default handler is active as long the application doesn't
  252. * supply its own custom IO handler via SetIOHandler().
  253. * @return true by default
  254. */
  255. bool IsDefaultIOHandler();
  256. // -------------------------------------------------------------------
  257. /** @brief Check whether a given set of postprocessing flags
  258. * is supported.
  259. *
  260. * Some flags are mutually exclusive, others are probably
  261. * not available because your excluded them from your
  262. * Assimp builds. Calling this function is recommended if
  263. * you're unsure.
  264. *
  265. * @param pFlags Bitwise combination of the aiPostProcess flags.
  266. * @return true if this flag combination is not supported.
  267. */
  268. bool ValidateFlags(unsigned int pFlags);
  269. // -------------------------------------------------------------------
  270. /** Reads the given file and returns its contents if successful.
  271. *
  272. * If the call succeeds, the contents of the file are returned as a
  273. * pointer to an aiScene object. The returned data is intended to be
  274. * read-only, the importer object keeps ownership of the data and will
  275. * destroy it upon destruction. If the import fails, NULL is returned.
  276. * A human-readable error description can be retrieved by calling
  277. * GetErrorString(). The previous scene will be deleted during this call.
  278. * @param pFile Path and filename to the file to be imported.
  279. * @param pFlags Optional post processing steps to be executed after
  280. * a successful import. Provide a bitwise combination of the
  281. * #aiPostProcessSteps flags. If you wish to inspect the imported
  282. * scene first in order to fine-tune your post-processing setup,
  283. * consider to use #ApplyPostProcessing().
  284. * @return A pointer to the imported data, NULL if the import failed.
  285. * The pointer to the scene remains in possession of the Importer
  286. * instance. Use GetOrphanedScene() to take ownership of it.
  287. *
  288. * @note Assimp is able to determine the file format of a file
  289. * automatically. However, to enable automatic detection of the file
  290. * format, the input path *must* not have an extension at all.
  291. */
  292. const aiScene* ReadFile( const char* pFile, unsigned int pFlags);
  293. // -------------------------------------------------------------------
  294. /** Reads the given file from a memory buffer and returns its
  295. * contents if successful.
  296. *
  297. * If the call succeeds, the contents of the file are returned as a
  298. * pointer to an aiScene object. The returned data is intended to be
  299. * read-only, the importer object keeps ownership of the data and will
  300. * destroy it upon destruction. If the import fails, NULL is returned.
  301. * A human-readable error description can be retrieved by calling
  302. * GetErrorString(). The previous scene will be deleted during this call.
  303. * Calling this method doesn't affect the active IOSystem.
  304. * @param pBuffer Pointer to the file data
  305. * @param pLength Length of pBuffer, in bytes
  306. * @param pFlags Optional post processing steps to be executed after
  307. * a successful import. Provide a bitwise combination of the
  308. * #aiPostProcessSteps flags. If you wish to inspect the imported
  309. * scene first in order to fine-tune your post-processing setup,
  310. * consider to use #ApplyPostProcessing().
  311. * @param pHint An additional hint to the library. If this is a non
  312. * empty string, the library looks for a loader to support
  313. * the file extension specified by pHint and passes the file to
  314. * the first matching loader. If this loader is unable to completely
  315. * the request, the library continues and tries to determine the
  316. * file format on its own, a task that may or may not be successful.
  317. * Check the return value, and you'll know ...
  318. * @return A pointer to the imported data, NULL if the import failed.
  319. * The pointer to the scene remains in possession of the Importer
  320. * instance. Use GetOrphanedScene() to take ownership of it.
  321. *
  322. * @note This is a straightforward way to decode models from memory
  323. * buffers, but it doesn't handle model formats spreading their
  324. * data across multiple files or even directories. Examples include
  325. * OBJ or MD3, which outsource parts of their material stuff into
  326. * external scripts. f you need the full functionality, provide
  327. * a custom IOSystem to make Assimp find these files.
  328. */
  329. const aiScene* ReadFileFromMemory( const void* pBuffer,
  330. size_t pLength,
  331. unsigned int pFlags,
  332. const char* pHint = "");
  333. // -------------------------------------------------------------------
  334. /** Apply post-processing to an already-imported scene.
  335. *
  336. * This is strictly equivalent to calling #ReadFile() with the same
  337. * flags. However, you can use this separate function to inspect
  338. * the imported scene first to fine-tune your post-processing setup.
  339. * @param pFlags Provide a bitwise combination of the
  340. * #aiPostProcessSteps flags.
  341. * @return A pointer to the post-processed data. This is still the
  342. * same as the pointer returned by #ReadFile(). However, if
  343. * post-processing fails, the scene could now be NULL.
  344. * That's quite a rare case, post processing steps are not really
  345. * designed to 'fail'. To be exact, the #aiProcess_ValidateDS
  346. * flag is currently the only post processing step which can actually
  347. * cause the scene to be reset to NULL.
  348. *
  349. * @note The method does nothing if no scene is currently bound
  350. * to the #Importer instance.
  351. */
  352. const aiScene* ApplyPostProcessing(unsigned int pFlags);
  353. // -------------------------------------------------------------------
  354. /** @brief Reads the given file and returns its contents if successful.
  355. *
  356. * This function is provided for backward compatibility.
  357. * See the const char* version for detailled docs.
  358. * @see ReadFile(const char*, pFlags)
  359. */
  360. const aiScene* ReadFile( const std::string& pFile, unsigned int pFlags);
  361. // -------------------------------------------------------------------
  362. /** Frees the current scene.
  363. *
  364. * The function does nothing if no scene has previously been
  365. * read via ReadFile(). FreeScene() is called automatically by the
  366. * destructor and ReadFile() itself.
  367. */
  368. void FreeScene( );
  369. // -------------------------------------------------------------------
  370. /** Returns an error description of an error that occurred in ReadFile().
  371. *
  372. * Returns an empty string if no error occurred.
  373. * @return A description of the last error, an empty string if no
  374. * error occurred. The string is never NULL.
  375. *
  376. * @note The returned function remains valid until one of the
  377. * following methods is called: #ReadFile(), #FreeScene().
  378. */
  379. const char* GetErrorString() const;
  380. // -------------------------------------------------------------------
  381. /** Returns whether a given file extension is supported by ASSIMP.
  382. *
  383. * @param szExtension Extension to be checked.
  384. * Must include a trailing dot '.'. Example: ".3ds", ".md3".
  385. * Cases-insensitive.
  386. * @return true if the extension is supported, false otherwise
  387. */
  388. bool IsExtensionSupported(const char* szExtension);
  389. // -------------------------------------------------------------------
  390. /** @brief Returns whether a given file extension is supported by ASSIMP.
  391. *
  392. * This function is provided for backward compatibility.
  393. * See the const char* version for detailled docs.
  394. * @see IsExtensionSupported(const char*)
  395. */
  396. inline bool IsExtensionSupported(const std::string& szExtension);
  397. // -------------------------------------------------------------------
  398. /** Get a full list of all file extensions supported by ASSIMP.
  399. *
  400. * If a file extension is contained in the list this does of course not
  401. * mean that ASSIMP is able to load all files with this extension.
  402. * @param szOut String to receive the extension list. It just means there
  403. * is a loader which handles such files.
  404. * Format of the list: "*.3ds;*.obj;*.dae".
  405. */
  406. void GetExtensionList(aiString& szOut);
  407. // -------------------------------------------------------------------
  408. /** @brief Get a full list of all file extensions supported by ASSIMP.
  409. *
  410. * This function is provided for backward compatibility.
  411. * See the aiString version for detailled docs.
  412. * @see GetExtensionList(aiString&)
  413. */
  414. inline void GetExtensionList(std::string& szOut);
  415. // -------------------------------------------------------------------
  416. /** Find the loader corresponding to a specific file extension.
  417. *
  418. * This is quite similar to IsExtensionSupported() except a
  419. * BaseImporter instance is returned.
  420. * @param szExtension Extension to be checked, cases insensitive,
  421. * must include a trailing dot.
  422. * @return NULL if there is no loader for the extension.
  423. */
  424. BaseImporter* FindLoader (const char* szExtension);
  425. // -------------------------------------------------------------------
  426. /** Returns the scene loaded by the last successful call to ReadFile()
  427. *
  428. * @return Current scene or NULL if there is currently no scene loaded
  429. */
  430. const aiScene* GetScene() const;
  431. // -------------------------------------------------------------------
  432. /** Returns the scene loaded by the last successful call to ReadFile()
  433. * and releases the scene from the ownership of the Importer
  434. * instance. The application is now responsible for deleting the
  435. * scene. Any further calls to GetScene() or GetOrphanedScene()
  436. * will return NULL - until a new scene has been loaded via ReadFile().
  437. *
  438. * @return Current scene or NULL if there is currently no scene loaded
  439. * @note Under windows, deleting the returned scene manually will
  440. * probably not work properly in applications using static runtime linkage.
  441. */
  442. aiScene* GetOrphanedScene();
  443. // -------------------------------------------------------------------
  444. /** Returns the storage allocated by ASSIMP to hold the scene data
  445. * in memory.
  446. *
  447. * This refers to the currently loaded file, see #ReadFile().
  448. * @param in Data structure to be filled.
  449. */
  450. void GetMemoryRequirements(aiMemoryInfo& in) const;
  451. // -------------------------------------------------------------------
  452. /** Enables "extra verbose" mode.
  453. *
  454. * In this mode the data structure is validated after every single
  455. * post processing step to make sure everyone modifies the data
  456. * structure in the defined manner. This is a debug feature and not
  457. * intended for public use.
  458. */
  459. void SetExtraVerbose(bool bDo);
  460. protected:
  461. // Just because we don't want you to know how we're hacking around.
  462. ImporterPimpl* pimpl;
  463. }; //! class Importer
  464. // ----------------------------------------------------------------------------
  465. // For compatibility, the interface of some functions taking a std::string was
  466. // changed to const char* to avoid crashes between binary incompatible STL
  467. // versions. This code her is inlined, so it shouldn't cause any problems.
  468. // ----------------------------------------------------------------------------
  469. // ----------------------------------------------------------------------------
  470. AI_FORCE_INLINE const aiScene* Importer::ReadFile( const std::string& pFile,
  471. unsigned int pFlags)
  472. {
  473. return ReadFile(pFile.c_str(),pFlags);
  474. }
  475. // ----------------------------------------------------------------------------
  476. AI_FORCE_INLINE void Importer::GetExtensionList(std::string& szOut)
  477. {
  478. aiString s;
  479. GetExtensionList(s);
  480. szOut = s.data;
  481. }
  482. // ----------------------------------------------------------------------------
  483. AI_FORCE_INLINE bool Importer::IsExtensionSupported(
  484. const std::string& szExtension)
  485. {
  486. return IsExtensionSupported(szExtension.c_str());
  487. }
  488. } // !namespace Assimp
  489. #endif // INCLUDED_AI_ASSIMP_HPP