scene.h 14 KB

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