scene.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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 scene.h
  35. * @brief Defines the data structures in which the imported scene is returned.
  36. */
  37. #pragma once
  38. #ifndef AI_SCENE_H_INC
  39. #define AI_SCENE_H_INC
  40. #ifdef __GNUC__
  41. # pragma GCC system_header
  42. #endif
  43. #include <assimp/types.h>
  44. #include <assimp/texture.h>
  45. #include <assimp/mesh.h>
  46. #include <assimp/light.h>
  47. #include <assimp/camera.h>
  48. #include <assimp/material.h>
  49. #include <assimp/anim.h>
  50. #include <assimp/metadata.h>
  51. #ifdef __cplusplus
  52. # include <cstdlib>
  53. extern "C" {
  54. #endif
  55. #ifdef __GNUC__
  56. #pragma GCC diagnostic push
  57. #pragma GCC diagnostic ignored "-Wattributes"
  58. #endif
  59. // -------------------------------------------------------------------------------
  60. /**
  61. * A node in the imported hierarchy.
  62. *
  63. * Each node has name, a parent node (except for the root node),
  64. * a transformation relative to its parent and possibly several child nodes.
  65. * Simple file formats don't support hierarchical structures - for these formats
  66. * the imported scene does consist of only a single root node without children.
  67. */
  68. // -------------------------------------------------------------------------------
  69. struct ASSIMP_API aiNode {
  70. /** The name of the node.
  71. *
  72. * The name might be empty (length of zero) but all nodes which
  73. * need to be referenced by either bones or animations are named.
  74. * Multiple nodes may have the same name, except for nodes which are referenced
  75. * by bones (see #aiBone and #aiMesh::mBones). Their names *must* be unique.
  76. *
  77. * Cameras and lights reference a specific node by name - if there
  78. * are multiple nodes with this name, they are assigned to each of them.
  79. * <br>
  80. * There are no limitations with regard to the characters contained in
  81. * the name string as it is usually taken directly from the source file.
  82. *
  83. * Implementations should be able to handle tokens such as whitespace, tabs,
  84. * line feeds, quotation marks, ampersands etc.
  85. *
  86. * Sometimes assimp introduces new nodes not present in the source file
  87. * into the hierarchy (usually out of necessity because sometimes the
  88. * source hierarchy format is simply not compatible). Their names are
  89. * surrounded by @verbatim <> @endverbatim e.g.
  90. * @verbatim<DummyRootNode> @endverbatim.
  91. */
  92. C_STRUCT aiString mName;
  93. /** The transformation relative to the node's parent. */
  94. C_STRUCT aiMatrix4x4 mTransformation;
  95. /** Parent node. nullptr if this node is the root node. */
  96. C_STRUCT aiNode* mParent;
  97. /** The number of child nodes of this node. */
  98. unsigned int mNumChildren;
  99. /** The child nodes of this node. nullptr if mNumChildren is 0. */
  100. C_STRUCT aiNode** mChildren;
  101. /** The number of meshes of this node. */
  102. unsigned int mNumMeshes;
  103. /** The meshes of this node. Each entry is an index into the
  104. * mesh list of the #aiScene.
  105. */
  106. unsigned int* mMeshes;
  107. /** Metadata associated with this node or nullptr if there is no metadata.
  108. * Whether any metadata is generated depends on the source file format. See the
  109. * @link importer_notes @endlink page for more information on every source file
  110. * format. Importers that don't document any metadata don't write any.
  111. */
  112. C_STRUCT aiMetadata* mMetaData;
  113. #ifdef __cplusplus
  114. /** Constructor */
  115. aiNode();
  116. /** Construction from a specific name */
  117. explicit aiNode(const std::string& name);
  118. /** Destructor */
  119. ~aiNode();
  120. /** Searches for a node with a specific name, beginning at this
  121. * nodes. Normally you will call this method on the root node
  122. * of the scene.
  123. *
  124. * @param name Name to search for
  125. * @return nullptr or a valid Node if the search was successful.
  126. */
  127. inline
  128. const aiNode* FindNode(const aiString& name) const {
  129. return FindNode(name.data);
  130. }
  131. inline
  132. aiNode* FindNode(const aiString& name) {
  133. return FindNode(name.data);
  134. }
  135. const aiNode* FindNode(const char* name) const;
  136. aiNode* FindNode(const char* name);
  137. /**
  138. * @brief Will add new children.
  139. * @param numChildren Number of children to add.
  140. * @param children The array with pointers showing to the children.
  141. */
  142. void addChildren(unsigned int numChildren, aiNode **children);
  143. #endif // __cplusplus
  144. };
  145. #ifdef __GNUC__
  146. #pragma GCC diagnostic pop
  147. #endif
  148. // -------------------------------------------------------------------------------
  149. /**
  150. * Specifies that the scene data structure that was imported is not complete.
  151. * This flag bypasses some internal validations and allows the import
  152. * of animation skeletons, material libraries or camera animation paths
  153. * using Assimp. Most applications won't support such data.
  154. */
  155. #define AI_SCENE_FLAGS_INCOMPLETE 0x1
  156. /**
  157. * This flag is set by the validation postprocess-step (aiPostProcess_ValidateDS)
  158. * if the validation is successful. In a validated scene you can be sure that
  159. * any cross references in the data structure (e.g. vertex indices) are valid.
  160. */
  161. #define AI_SCENE_FLAGS_VALIDATED 0x2
  162. /**
  163. * This flag is set by the validation postprocess-step (aiPostProcess_ValidateDS)
  164. * if the validation is successful but some issues have been found.
  165. * This can for example mean that a texture that does not exist is referenced
  166. * by a material or that the bone weights for a vertex don't sum to 1.0 ... .
  167. * In most cases you should still be able to use the import. This flag could
  168. * be useful for applications which don't capture Assimp's log output.
  169. */
  170. #define AI_SCENE_FLAGS_VALIDATION_WARNING 0x4
  171. /**
  172. * This flag is currently only set by the aiProcess_JoinIdenticalVertices step.
  173. * It indicates that the vertices of the output meshes aren't in the internal
  174. * verbose format anymore. In the verbose format all vertices are unique,
  175. * no vertex is ever referenced by more than one face.
  176. */
  177. #define AI_SCENE_FLAGS_NON_VERBOSE_FORMAT 0x8
  178. /**
  179. * Denotes pure height-map terrain data. Pure terrains usually consist of quads,
  180. * sometimes triangles, in a regular grid. The x,y coordinates of all vertex
  181. * positions refer to the x,y coordinates on the terrain height map, the z-axis
  182. * stores the elevation at a specific point.
  183. *
  184. * TER (Terragen) and HMP (3D Game Studio) are height map formats.
  185. * @note Assimp is probably not the best choice for loading *huge* terrains -
  186. * fully triangulated data takes extremely much free store and should be avoided
  187. * as long as possible (typically you'll do the triangulation when you actually
  188. * need to render it).
  189. */
  190. #define AI_SCENE_FLAGS_TERRAIN 0x10
  191. /**
  192. * Specifies that the scene data can be shared between structures. For example:
  193. * one vertex in few faces. \ref AI_SCENE_FLAGS_NON_VERBOSE_FORMAT can not be
  194. * used for this because \ref AI_SCENE_FLAGS_NON_VERBOSE_FORMAT has internal
  195. * meaning about postprocessing steps.
  196. */
  197. #define AI_SCENE_FLAGS_ALLOW_SHARED 0x20
  198. // -------------------------------------------------------------------------------
  199. /** The root structure of the imported data.
  200. *
  201. * Everything that was imported from the given file can be accessed from here.
  202. * Objects of this class are generally maintained and owned by Assimp, not
  203. * by the caller. You shouldn't want to instance it, nor should you ever try to
  204. * delete a given scene on your own.
  205. */
  206. // -------------------------------------------------------------------------------
  207. struct aiScene
  208. {
  209. /** Any combination of the AI_SCENE_FLAGS_XXX flags. By default
  210. * this value is 0, no flags are set. Most applications will
  211. * want to reject all scenes with the AI_SCENE_FLAGS_INCOMPLETE
  212. * bit set.
  213. */
  214. unsigned int mFlags;
  215. /** The root node of the hierarchy.
  216. *
  217. * There will always be at least the root node if the import
  218. * was successful (and no special flags have been set).
  219. * Presence of further nodes depends on the format and content
  220. * of the imported file.
  221. */
  222. C_STRUCT aiNode* mRootNode;
  223. /** The number of meshes in the scene. */
  224. unsigned int mNumMeshes;
  225. /** The array of meshes.
  226. *
  227. * Use the indices given in the aiNode structure to access
  228. * this array. The array is mNumMeshes in size. If the
  229. * AI_SCENE_FLAGS_INCOMPLETE flag is not set there will always
  230. * be at least ONE material.
  231. */
  232. C_STRUCT aiMesh** mMeshes;
  233. /** The number of materials in the scene. */
  234. unsigned int mNumMaterials;
  235. /** The array of materials.
  236. *
  237. * Use the index given in each aiMesh structure to access this
  238. * array. The array is mNumMaterials in size. If the
  239. * AI_SCENE_FLAGS_INCOMPLETE flag is not set there will always
  240. * be at least ONE material.
  241. */
  242. C_STRUCT aiMaterial** mMaterials;
  243. /** The number of animations in the scene. */
  244. unsigned int mNumAnimations;
  245. /** The array of animations.
  246. *
  247. * All animations imported from the given file are listed here.
  248. * The array is mNumAnimations in size.
  249. */
  250. C_STRUCT aiAnimation** mAnimations;
  251. /** The number of textures embedded into the file */
  252. unsigned int mNumTextures;
  253. /** The array of embedded textures.
  254. *
  255. * Not many file formats embed their textures into the file.
  256. * An example is Quake's MDL format (which is also used by
  257. * some GameStudio versions)
  258. */
  259. C_STRUCT aiTexture** mTextures;
  260. /** The number of light sources in the scene. Light sources
  261. * are fully optional, in most cases this attribute will be 0
  262. */
  263. unsigned int mNumLights;
  264. /** The array of light sources.
  265. *
  266. * All light sources imported from the given file are
  267. * listed here. The array is mNumLights in size.
  268. */
  269. C_STRUCT aiLight** mLights;
  270. /** The number of cameras in the scene. Cameras
  271. * are fully optional, in most cases this attribute will be 0
  272. */
  273. unsigned int mNumCameras;
  274. /** The array of cameras.
  275. *
  276. * All cameras imported from the given file are listed here.
  277. * The array is mNumCameras in size. The first camera in the
  278. * array (if existing) is the default camera view into
  279. * the scene.
  280. */
  281. C_STRUCT aiCamera** mCameras;
  282. /**
  283. * @brief The global metadata assigned to the scene itself.
  284. *
  285. * This data contains global metadata which belongs to the scene like
  286. * unit-conversions, versions, vendors or other model-specific data. This
  287. * can be used to store format-specific metadata as well.
  288. */
  289. C_STRUCT aiMetadata* mMetaData;
  290. /** The name of the scene itself.
  291. */
  292. C_STRUCT aiString mName;
  293. /**
  294. *
  295. */
  296. unsigned int mNumSkeletons;
  297. /**
  298. *
  299. */
  300. C_STRUCT aiSkeleton **mSkeletons;
  301. #ifdef __cplusplus
  302. //! Default constructor - set everything to 0/nullptr
  303. ASSIMP_API aiScene();
  304. //! Destructor
  305. ASSIMP_API ~aiScene();
  306. //! Check whether the scene contains meshes
  307. //! Unless no special scene flags are set this will always be true.
  308. inline bool HasMeshes() const {
  309. return mMeshes != nullptr && mNumMeshes > 0;
  310. }
  311. //! Check whether the scene contains materials
  312. //! Unless no special scene flags are set this will always be true.
  313. inline bool HasMaterials() const {
  314. return mMaterials != nullptr && mNumMaterials > 0;
  315. }
  316. //! Check whether the scene contains lights
  317. inline bool HasLights() const {
  318. return mLights != nullptr && mNumLights > 0;
  319. }
  320. //! Check whether the scene contains textures
  321. inline bool HasTextures() const {
  322. return mTextures != nullptr && mNumTextures > 0;
  323. }
  324. //! Check whether the scene contains cameras
  325. inline bool HasCameras() const {
  326. return mCameras != nullptr && mNumCameras > 0;
  327. }
  328. //! Check whether the scene contains animations
  329. inline bool HasAnimations() const {
  330. return mAnimations != nullptr && mNumAnimations > 0;
  331. }
  332. bool hasSkeletons() const {
  333. return mSkeletons != nullptr && mNumSkeletons > 0;
  334. }
  335. //! Returns a short filename from a full path
  336. static const char* GetShortFilename(const char* filename) {
  337. const char* lastSlash = strrchr(filename, '/');
  338. if (lastSlash == nullptr) {
  339. lastSlash = strrchr(filename, '\\');
  340. }
  341. const char* shortFilename = lastSlash != nullptr ? lastSlash + 1 : filename;
  342. return shortFilename;
  343. }
  344. //! Returns an embedded texture
  345. const aiTexture* GetEmbeddedTexture(const char* filename) const {
  346. return GetEmbeddedTextureAndIndex(filename).first;
  347. }
  348. //! Returns an embedded texture and its index
  349. std::pair<const aiTexture*, int> GetEmbeddedTextureAndIndex(const char* filename) const {
  350. if (nullptr==filename) {
  351. return std::make_pair(nullptr, -1);
  352. }
  353. // lookup using texture ID (if referenced like: "*1", "*2", etc.)
  354. if ('*' == *filename) {
  355. int index = std::atoi(filename + 1);
  356. if (0 > index || mNumTextures <= static_cast<unsigned>(index)) {
  357. return std::make_pair(nullptr, -1);
  358. }
  359. return std::make_pair(mTextures[index], index);
  360. }
  361. // lookup using filename
  362. const char* shortFilename = GetShortFilename(filename);
  363. if (nullptr == shortFilename) {
  364. return std::make_pair(nullptr, -1);
  365. }
  366. for (unsigned int i = 0; i < mNumTextures; i++) {
  367. const char* shortTextureFilename = GetShortFilename(mTextures[i]->mFilename.C_Str());
  368. if (strcmp(shortTextureFilename, shortFilename) == 0) {
  369. return std::make_pair(mTextures[i], i);
  370. }
  371. }
  372. return std::make_pair(nullptr, -1);
  373. }
  374. #endif // __cplusplus
  375. /** Internal data, do not touch */
  376. #ifdef __cplusplus
  377. void* mPrivate;
  378. #else
  379. char* mPrivate;
  380. #endif
  381. };
  382. #ifdef __cplusplus
  383. }
  384. #endif //! extern "C"
  385. #endif // AI_SCENE_H_INC