scene.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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. /**
  121. * @brief Searches for a node with a specific name, beginning at this
  122. * nodes. Normally you will call this method on the root node
  123. * of the scene.
  124. *
  125. * @param name Name to search for
  126. * @return nullptr or a valid Node if the search was successful.
  127. */
  128. inline const aiNode* FindNode(const aiString& name) const {
  129. return FindNode(name.data);
  130. }
  131. inline aiNode* FindNode(const aiString& name) {
  132. return FindNode(name.data);
  133. }
  134. /**
  135. * @brief Will search for a node described by its name.
  136. * @param[in] name The name for the node to look for.
  137. * @return Pointer showing to the node or nullptr if not found.
  138. */
  139. const aiNode* FindNode(const char* name) const;
  140. aiNode* FindNode(const char* name);
  141. // ------------------------------------------------------------------------------------------------
  142. // Helper to find the node associated with a bone in the scene
  143. const aiNode *findBoneNode(const aiBone *bone) const {
  144. if (bone == nullptr) {
  145. return nullptr;
  146. }
  147. if (mName == bone->mName) {
  148. return this;
  149. }
  150. for (unsigned int i = 0; i < mNumChildren; ++i) {
  151. aiNode *aChild = mChildren[i];
  152. if (aChild == nullptr) {
  153. continue;
  154. }
  155. const aiNode *foundFromChild = nullptr;
  156. foundFromChild = aChild->findBoneNode(bone);
  157. if (foundFromChild) {
  158. return foundFromChild;
  159. }
  160. }
  161. return nullptr;
  162. }
  163. /**
  164. * @brief Will add new children.
  165. * @param numChildren Number of children to add.
  166. * @param children The array with pointers showing to the children.
  167. */
  168. void addChildren(unsigned int numChildren, aiNode **children);
  169. #endif // __cplusplus
  170. };
  171. #ifdef __GNUC__
  172. #pragma GCC diagnostic pop
  173. #endif
  174. // -------------------------------------------------------------------------------
  175. /**
  176. * Specifies that the scene data structure that was imported is not complete.
  177. * This flag bypasses some internal validations and allows the import
  178. * of animation skeletons, material libraries or camera animation paths
  179. * using Assimp. Most applications won't support such data.
  180. */
  181. #define AI_SCENE_FLAGS_INCOMPLETE 0x1
  182. /**
  183. * This flag is set by the validation postprocess-step (aiPostProcess_ValidateDS)
  184. * if the validation is successful. In a validated scene you can be sure that
  185. * any cross references in the data structure (e.g. vertex indices) are valid.
  186. */
  187. #define AI_SCENE_FLAGS_VALIDATED 0x2
  188. /**
  189. * This flag is set by the validation postprocess-step (aiPostProcess_ValidateDS)
  190. * if the validation is successful but some issues have been found.
  191. * This can for example mean that a texture that does not exist is referenced
  192. * by a material or that the bone weights for a vertex don't sum to 1.0 ... .
  193. * In most cases you should still be able to use the import. This flag could
  194. * be useful for applications which don't capture Assimp's log output.
  195. */
  196. #define AI_SCENE_FLAGS_VALIDATION_WARNING 0x4
  197. /**
  198. * This flag is currently only set by the aiProcess_JoinIdenticalVertices step.
  199. * It indicates that the vertices of the output meshes aren't in the internal
  200. * verbose format anymore. In the verbose format all vertices are unique,
  201. * no vertex is ever referenced by more than one face.
  202. */
  203. #define AI_SCENE_FLAGS_NON_VERBOSE_FORMAT 0x8
  204. /**
  205. * Denotes pure height-map terrain data. Pure terrains usually consist of quads,
  206. * sometimes triangles, in a regular grid. The x,y coordinates of all vertex
  207. * positions refer to the x,y coordinates on the terrain height map, the z-axis
  208. * stores the elevation at a specific point.
  209. *
  210. * TER (Terragen) and HMP (3D Game Studio) are height map formats.
  211. * @note Assimp is probably not the best choice for loading *huge* terrains -
  212. * fully triangulated data takes extremely much free store and should be avoided
  213. * as long as possible (typically you'll do the triangulation when you actually
  214. * need to render it).
  215. */
  216. #define AI_SCENE_FLAGS_TERRAIN 0x10
  217. /**
  218. * Specifies that the scene data can be shared between structures. For example:
  219. * one vertex in few faces. \ref AI_SCENE_FLAGS_NON_VERBOSE_FORMAT can not be
  220. * used for this because \ref AI_SCENE_FLAGS_NON_VERBOSE_FORMAT has internal
  221. * meaning about postprocessing steps.
  222. */
  223. #define AI_SCENE_FLAGS_ALLOW_SHARED 0x20
  224. // -------------------------------------------------------------------------------
  225. /** The root structure of the imported data.
  226. *
  227. * Everything that was imported from the given file can be accessed from here.
  228. * Objects of this class are generally maintained and owned by Assimp, not
  229. * by the caller. You shouldn't want to instance it, nor should you ever try to
  230. * delete a given scene on your own.
  231. */
  232. // -------------------------------------------------------------------------------
  233. struct ASSIMP_API aiScene {
  234. /** Any combination of the AI_SCENE_FLAGS_XXX flags. By default
  235. * this value is 0, no flags are set. Most applications will
  236. * want to reject all scenes with the AI_SCENE_FLAGS_INCOMPLETE
  237. * bit set.
  238. */
  239. unsigned int mFlags;
  240. /** The root node of the hierarchy.
  241. *
  242. * There will always be at least the root node if the import
  243. * was successful (and no special flags have been set).
  244. * Presence of further nodes depends on the format and content
  245. * of the imported file.
  246. */
  247. C_STRUCT aiNode* mRootNode;
  248. /** The number of meshes in the scene. */
  249. unsigned int mNumMeshes;
  250. /** The array of meshes.
  251. *
  252. * Use the indices given in the aiNode structure to access
  253. * this array. The array is mNumMeshes in size. If the
  254. * AI_SCENE_FLAGS_INCOMPLETE flag is not set there will always
  255. * be at least ONE material.
  256. */
  257. C_STRUCT aiMesh** mMeshes;
  258. /** The number of materials in the scene. */
  259. unsigned int mNumMaterials;
  260. /** The array of materials.
  261. *
  262. * Use the index given in each aiMesh structure to access this
  263. * array. The array is mNumMaterials in size. If the
  264. * AI_SCENE_FLAGS_INCOMPLETE flag is not set there will always
  265. * be at least ONE material.
  266. */
  267. C_STRUCT aiMaterial** mMaterials;
  268. /** The number of animations in the scene. */
  269. unsigned int mNumAnimations;
  270. /** The array of animations.
  271. *
  272. * All animations imported from the given file are listed here.
  273. * The array is mNumAnimations in size.
  274. */
  275. C_STRUCT aiAnimation** mAnimations;
  276. /** The number of textures embedded into the file */
  277. unsigned int mNumTextures;
  278. /** The array of embedded textures.
  279. *
  280. * Not many file formats embed their textures into the file.
  281. * An example is Quake's MDL format (which is also used by
  282. * some GameStudio versions)
  283. */
  284. C_STRUCT aiTexture** mTextures;
  285. /** The number of light sources in the scene. Light sources
  286. * are fully optional, in most cases this attribute will be 0
  287. */
  288. unsigned int mNumLights;
  289. /** The array of light sources.
  290. *
  291. * All light sources imported from the given file are
  292. * listed here. The array is mNumLights in size.
  293. */
  294. C_STRUCT aiLight** mLights;
  295. /** The number of cameras in the scene. Cameras
  296. * are fully optional, in most cases this attribute will be 0
  297. */
  298. unsigned int mNumCameras;
  299. /** The array of cameras.
  300. *
  301. * All cameras imported from the given file are listed here.
  302. * The array is mNumCameras in size. The first camera in the
  303. * array (if existing) is the default camera view into
  304. * the scene.
  305. */
  306. C_STRUCT aiCamera** mCameras;
  307. /**
  308. * @brief The global metadata assigned to the scene itself.
  309. *
  310. * This data contains global metadata which belongs to the scene like
  311. * unit-conversions, versions, vendors or other model-specific data. This
  312. * can be used to store format-specific metadata as well.
  313. */
  314. C_STRUCT aiMetadata* mMetaData;
  315. /** The name of the scene itself.
  316. */
  317. C_STRUCT aiString mName;
  318. /**
  319. *
  320. */
  321. unsigned int mNumSkeletons;
  322. /**
  323. *
  324. */
  325. C_STRUCT aiSkeleton **mSkeletons;
  326. #ifdef __cplusplus
  327. //! Default constructor - set everything to 0/nullptr
  328. aiScene();
  329. //! Destructor
  330. ~aiScene();
  331. //! Check whether the scene contains meshes
  332. //! Unless no special scene flags are set this will always be true.
  333. inline bool HasMeshes() const {
  334. return mMeshes != nullptr && mNumMeshes > 0;
  335. }
  336. //! Check whether the scene contains materials
  337. //! Unless no special scene flags are set this will always be true.
  338. inline bool HasMaterials() const {
  339. return mMaterials != nullptr && mNumMaterials > 0;
  340. }
  341. //! Check whether the scene contains lights
  342. inline bool HasLights() const {
  343. return mLights != nullptr && mNumLights > 0;
  344. }
  345. //! Check whether the scene contains textures
  346. inline bool HasTextures() const {
  347. return mTextures != nullptr && mNumTextures > 0;
  348. }
  349. //! Check whether the scene contains cameras
  350. inline bool HasCameras() const {
  351. return mCameras != nullptr && mNumCameras > 0;
  352. }
  353. //! Check whether the scene contains animations
  354. inline bool HasAnimations() const {
  355. return mAnimations != nullptr && mNumAnimations > 0;
  356. }
  357. //! Check whether the scene contains skeletons
  358. inline bool HasSkeletons() const {
  359. return mSkeletons != nullptr && mNumSkeletons > 0;
  360. }
  361. //! Returns a short filename from a full path
  362. static const char* GetShortFilename(const char* filename) {
  363. const char* lastSlash = strrchr(filename, '/');
  364. const char* lastBackSlash = strrchr(filename, '\\');
  365. if (lastSlash < lastBackSlash) {
  366. lastSlash = lastBackSlash;
  367. }
  368. const char* shortFilename = lastSlash != nullptr ? lastSlash + 1 : filename;
  369. return shortFilename;
  370. }
  371. //! Returns an embedded texture
  372. const aiTexture* GetEmbeddedTexture(const char* filename) const {
  373. return GetEmbeddedTextureAndIndex(filename).first;
  374. }
  375. //! Returns an embedded texture and its index
  376. std::pair<const aiTexture*, int> GetEmbeddedTextureAndIndex(const char* filename) const {
  377. if (nullptr==filename) {
  378. return std::make_pair(nullptr, -1);
  379. }
  380. // lookup using texture ID (if referenced like: "*1", "*2", etc.)
  381. if ('*' == *filename) {
  382. int index = std::atoi(filename + 1);
  383. if (0 > index || mNumTextures <= static_cast<unsigned>(index)) {
  384. return std::make_pair(nullptr, -1);
  385. }
  386. return std::make_pair(mTextures[index], index);
  387. }
  388. // lookup using filename
  389. const char* shortFilename = GetShortFilename(filename);
  390. if (nullptr == shortFilename) {
  391. return std::make_pair(nullptr, -1);
  392. }
  393. for (unsigned int i = 0; i < mNumTextures; i++) {
  394. const char* shortTextureFilename = GetShortFilename(mTextures[i]->mFilename.C_Str());
  395. if (strcmp(shortTextureFilename, shortFilename) == 0) {
  396. return std::make_pair(mTextures[i], static_cast<int>(i));
  397. }
  398. }
  399. return std::make_pair(nullptr, -1);
  400. }
  401. /**
  402. * @brief Will try to locate a bone described by its name.
  403. *
  404. * @param name The name to look for.
  405. * @return The bone as a pointer.
  406. */
  407. inline aiBone *findBone(const aiString &name) const {
  408. for (size_t m = 0; m < mNumMeshes; m++) {
  409. aiMesh *mesh = mMeshes[m];
  410. if (mesh == nullptr) {
  411. continue;
  412. }
  413. for (size_t b = 0; b < mesh->mNumBones; b++) {
  414. aiBone *bone = mesh->mBones[b];
  415. if (bone == nullptr) {
  416. continue;
  417. }
  418. if (name == bone->mName) {
  419. return bone;
  420. }
  421. }
  422. }
  423. return nullptr;
  424. }
  425. #endif // __cplusplus
  426. /** Internal data, do not touch */
  427. #ifdef __cplusplus
  428. void* mPrivate;
  429. #else
  430. char* mPrivate;
  431. #endif
  432. };
  433. #ifdef __cplusplus
  434. }
  435. #endif //! extern "C"
  436. #endif // AI_SCENE_H_INC