aiMesh.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 aiMesh.h
  35. * @brief Declares the data structures in which the imported geometry is
  36. returned by ASSIMP: aiMesh, aiFace and aiBone data structures.
  37. */
  38. #ifndef INCLUDED_AI_MESH_H
  39. #define INCLUDED_AI_MESH_H
  40. #include "aiTypes.h"
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. // ---------------------------------------------------------------------------
  45. /** @brief A single face in a mesh, referring to multiple vertices.
  46. *
  47. * If mNumIndices is 3, we call the face 'triangle', for mNumIndices > 3
  48. * it's called 'polygon' (hey, that's just a definition!).
  49. * <br>
  50. * aiMesh::mPrimitiveTypes can be queried to quickly examine which types of
  51. * primitive are actually present in a mesh. The #aiProcess_SortByPType flag
  52. * executes a special post-processing algorithm which splits meshes with
  53. * *different* primitive types mixed up (e.g. lines and triangles) in several
  54. * 'clean' submeshes. Furthermore there is a configuration option (
  55. * #AI_CONFIG_PP_SBP_REMOVE) to force #aiProcess_SortByPType to remove
  56. * specific kinds of primitives from the imported scene, completely and forever.
  57. * In many cases you'll probably want to set this setting to
  58. * @code
  59. * aiPrimitiveType_LINE|aiPrimitiveType_POINT
  60. * @endcode
  61. * Together with the #aiProcess_Triangulate flag you can then be sure that
  62. * #aiFace::mNumIndices is always 3.
  63. * @note Take a look at the @link data Data Structures page @endlink for
  64. * more information on the layout and winding order of a face.
  65. */
  66. struct aiFace
  67. {
  68. //! Number of indices defining this face. 3 for a triangle, >3 for polygon
  69. unsigned int mNumIndices;
  70. //! Pointer to the indices array. Size of the array is given in numIndices.
  71. unsigned int* mIndices;
  72. #ifdef __cplusplus
  73. //! Default constructor
  74. aiFace()
  75. {
  76. mNumIndices = 0; mIndices = NULL;
  77. }
  78. //! Default destructor. Delete the index array
  79. ~aiFace()
  80. {
  81. delete [] mIndices;
  82. }
  83. //! Copy constructor. Copy the index array
  84. aiFace( const aiFace& o)
  85. {
  86. mIndices = NULL;
  87. *this = o;
  88. }
  89. //! Assignment operator. Copy the index array
  90. const aiFace& operator = ( const aiFace& o)
  91. {
  92. if (&o == this)
  93. return *this;
  94. delete[] mIndices;
  95. mNumIndices = o.mNumIndices;
  96. mIndices = new unsigned int[mNumIndices];
  97. ::memcpy( mIndices, o.mIndices, mNumIndices * sizeof( unsigned int));
  98. return *this;
  99. }
  100. //! Comparison operator. Checks whether the index array
  101. //! of two faces is identical
  102. bool operator== (const aiFace& o) const
  103. {
  104. if (mIndices == o.mIndices)return true;
  105. else if (mIndices && mNumIndices == o.mNumIndices)
  106. {
  107. for (unsigned int i = 0;i < this->mNumIndices;++i)
  108. if (mIndices[i] != o.mIndices[i])return false;
  109. return true;
  110. }
  111. return false;
  112. }
  113. //! Inverse comparison operator. Checks whether the index
  114. //! array of two faces is NOT identical
  115. bool operator != (const aiFace& o) const
  116. {
  117. return !(*this == o);
  118. }
  119. #endif // __cplusplus
  120. }; // struct aiFace
  121. // ---------------------------------------------------------------------------
  122. /** @brief A single influence of a bone on a vertex.
  123. */
  124. struct aiVertexWeight
  125. {
  126. //! Index of the vertex which is influenced by the bone.
  127. unsigned int mVertexId;
  128. //! The strength of the influence in the range (0...1).
  129. //! The influence from all bones at one vertex amounts to 1.
  130. float mWeight;
  131. #ifdef __cplusplus
  132. //! Default constructor
  133. aiVertexWeight() { }
  134. //! Initialisation from a given index and vertex weight factor
  135. //! \param pID ID
  136. //! \param pWeight Vertex weight factor
  137. aiVertexWeight( unsigned int pID, float pWeight)
  138. : mVertexId( pID), mWeight( pWeight)
  139. { /* nothing to do here */ }
  140. #endif // __cplusplus
  141. };
  142. // ---------------------------------------------------------------------------
  143. /** @brief A single bone of a mesh.
  144. *
  145. * A bone has a name by which it can be found in the frame hierarchy and by
  146. * which it can be addressed by animations. In addition it has a number of
  147. * influences on vertices.
  148. */
  149. struct aiBone
  150. {
  151. //! The name of the bone.
  152. C_STRUCT aiString mName;
  153. //! The number of vertices affected by this bone
  154. unsigned int mNumWeights;
  155. //! The vertices affected by this bone
  156. C_STRUCT aiVertexWeight* mWeights;
  157. //! Matrix that transforms from mesh space to bone space in bind pose
  158. C_STRUCT aiMatrix4x4 mOffsetMatrix;
  159. #ifdef __cplusplus
  160. //! Default constructor
  161. aiBone()
  162. {
  163. mNumWeights = 0; mWeights = NULL;
  164. }
  165. //! Copy constructor
  166. aiBone(const aiBone& other)
  167. {
  168. mNumWeights = other.mNumWeights;
  169. mOffsetMatrix = other.mOffsetMatrix;
  170. mName = other.mName;
  171. if (other.mWeights && other.mNumWeights)
  172. {
  173. mWeights = new aiVertexWeight[mNumWeights];
  174. ::memcpy(mWeights,other.mWeights,mNumWeights * sizeof(aiVertexWeight));
  175. }
  176. }
  177. //! Destructor - deletes the array of vertex weights
  178. ~aiBone()
  179. {
  180. delete [] mWeights;
  181. }
  182. #endif // __cplusplus
  183. };
  184. #ifndef AI_MAX_NUMBER_OF_COLOR_SETS
  185. // ---------------------------------------------------------------------------
  186. /** @def AI_MAX_NUMBER_OF_COLOR_SETS
  187. * Maximum number of vertex color sets per mesh.
  188. *
  189. * Normally: Diffuse, specular, ambient and emissive
  190. * However one could use the vertex color sets for any other purpose, too.
  191. *
  192. * @note Some internal structures expect (and assert) this value
  193. * to be at least 4. For the moment it is absolutely safe to assume that
  194. * this will never change.
  195. */
  196. # define AI_MAX_NUMBER_OF_COLOR_SETS 0x4
  197. #endif // !! AI_MAX_NUMBER_OF_COLOR_SETS
  198. #ifndef AI_MAX_NUMBER_OF_TEXTURECOORDS
  199. // ---------------------------------------------------------------------------
  200. /** @def AI_MAX_NUMBER_OF_TEXTURECOORDS
  201. * Maximum number of texture coord sets (UV(W) channels) per mesh
  202. *
  203. * The material system uses the AI_MATKEY_UVWSRC_XXX keys to specify
  204. * which UVW channel serves as data source for a texture.
  205. *
  206. * @note Some internal structures expect (and assert) this value
  207. * to be at least 4. For the moment it is absolutely safe to assume that
  208. * this will never change.
  209. */
  210. # define AI_MAX_NUMBER_OF_TEXTURECOORDS 0x4
  211. #endif // !! AI_MAX_NUMBER_OF_TEXTURECOORDS
  212. // ---------------------------------------------------------------------------
  213. /** @brief Enumerates the types of geometric primitives supported by Assimp.
  214. *
  215. * @see aiFace Face data structure
  216. * @see aiProcess_SortByPType Per-primitive sorting of meshes
  217. * @see aiProcess_Triangulate Automatic triangulation
  218. * @see AI_CONFIG_PP_SBP_REMOVE Removal of specific primitive types.
  219. */
  220. enum aiPrimitiveType
  221. {
  222. /** A point primitive.
  223. *
  224. * This is just a single vertex in the virtual world,
  225. * #aiFace contains just one index for such a primitive.
  226. */
  227. aiPrimitiveType_POINT = 0x1,
  228. /** A line primitive.
  229. *
  230. * This is a line defined through a start and an end position.
  231. * #aiFace contains exactly two indices for such a primitive.
  232. */
  233. aiPrimitiveType_LINE = 0x2,
  234. /** A triangular primitive.
  235. *
  236. * A triangle consists of three indices.
  237. */
  238. aiPrimitiveType_TRIANGLE = 0x4,
  239. /** A higher-level polygon with more than 3 edges.
  240. *
  241. * A triangle is a polygon, but polygon in this context means
  242. * "all polygons that are not triangles". The "Triangulate"-Step
  243. * is provided for your convenience, it splits all polygons in
  244. * triangles (which are much easier to handle).
  245. */
  246. aiPrimitiveType_POLYGON = 0x8,
  247. /** This value is not used. It is just here to force the
  248. * compiler to map this enum to a 32 Bit integer.
  249. */
  250. _aiPrimitiveType_Force32Bit = 0x9fffffff
  251. }; //! enum aiPrimitiveType
  252. // Get the #aiPrimitiveType flag for a specific number of face indices
  253. #define AI_PRIMITIVE_TYPE_FOR_N_INDICES(n) \
  254. ((n) > 3 ? aiPrimitiveType_POLYGON : (aiPrimitiveType)(1u << ((n)-1)))
  255. // ---------------------------------------------------------------------------
  256. /** @brief A mesh represents a geometry or model with a single material.
  257. *
  258. * It usually consists of a number of vertices and a series of primitives/faces
  259. * referencing the vertices. In addition there might be a series of bones, each
  260. * of them addressing a number of vertices with a certain weight. Vertex data
  261. * is presented in channels with each channel containing a single per-vertex
  262. * information such as a set of texture coords or a normal vector.
  263. * If a data pointer is non-null, the corresponding data stream is present.
  264. * From C++-programs you can also use the comfort functions Has*() to
  265. * test for the presence of various data streams.
  266. *
  267. * A Mesh uses only a single material which is referenced by a material ID.
  268. * @note The mPositions member is usually not optional. However, vertex positions
  269. * *could* be missing if the AI_SCENE_FLAGS_INCOMPLETE flag is set in
  270. * @code
  271. * aiScene::mFlags
  272. * @endcode
  273. */
  274. struct aiMesh
  275. {
  276. /** Bitwise combination of the members of the #aiPrimitiveType enum.
  277. * This specifies which types of primitives are present in the mesh.
  278. * The "SortByPrimitiveType"-Step can be used to make sure the
  279. * output meshes consist of one primitive type each.
  280. */
  281. unsigned int mPrimitiveTypes;
  282. /** The number of vertices in this mesh.
  283. * This is also the size of all of the per-vertex data arrays
  284. */
  285. unsigned int mNumVertices;
  286. /** The number of primitives (triangles, polygons, lines) in this mesh.
  287. * This is also the size of the mFaces array
  288. */
  289. unsigned int mNumFaces;
  290. /** Vertex positions.
  291. * This array is always present in a mesh. The array is
  292. * mNumVertices in size.
  293. */
  294. C_STRUCT aiVector3D* mVertices;
  295. /** Vertex normals.
  296. * The array contains normalized vectors, NULL if not present.
  297. * The array is mNumVertices in size. Normals are undefined for
  298. * point and line primitives. A mesh consisting of points and
  299. * lines only may not have normal vectors. Meshes with mixed
  300. * primitive types (i.e. lines and triangles) may have normals,
  301. * but the normals for vertices that are only referenced by
  302. * point or line primitives are undefined and set to QNaN (WARN:
  303. * qNaN compares to inequal to *everything*, even to qNaN itself.
  304. * Use code like this
  305. * @code
  306. * #define IS_QNAN(f) (f != f)
  307. * @endcode
  308. * to check whether a field is qnan).
  309. * @note Normal vectors computed by Assimp are always unit-length.
  310. * However, this needn't apply for normals that have been taken
  311. * directly from the model file.
  312. */
  313. C_STRUCT aiVector3D* mNormals;
  314. /** Vertex tangents.
  315. * The tangent of a vertex points in the direction of the positive
  316. * X texture axis. The array contains normalized vectors, NULL if
  317. * not present. The array is mNumVertices in size. A mesh consisting
  318. * of points and lines only may not have normal vectors. Meshes with
  319. * mixed primitive types (i.e. lines and triangles) may have
  320. * normals, but the normals for vertices that are only referenced by
  321. * point or line primitives are undefined and set to QNaN.
  322. * @note If the mesh contains tangents, it automatically also
  323. * contains bitangents (the bitangent is just the cross product of
  324. * tangent and normal vectors).
  325. */
  326. C_STRUCT aiVector3D* mTangents;
  327. /** Vertex bitangents.
  328. * The bitangent of a vertex points in the direction of the positive
  329. * Y texture axis. The array contains normalized vectors, NULL if not
  330. * present. The array is mNumVertices in size.
  331. * @note If the mesh contains tangents, it automatically also contains
  332. * bitangents.
  333. */
  334. C_STRUCT aiVector3D* mBitangents;
  335. /** Vertex color sets.
  336. * A mesh may contain 0 to #AI_MAX_NUMBER_OF_COLOR_SETS vertex
  337. * colors per vertex. NULL if not present. Each array is
  338. * mNumVertices in size if present.
  339. */
  340. C_STRUCT aiColor4D* mColors[AI_MAX_NUMBER_OF_COLOR_SETS];
  341. /** Vertex texture coords, also known as UV channels.
  342. * A mesh may contain 0 to AI_MAX_NUMBER_OF_TEXTURECOORDS per
  343. * vertex. NULL if not present. The array is mNumVertices in size.
  344. */
  345. C_STRUCT aiVector3D* mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  346. /** Specifies the number of components for a given UV channel.
  347. * Up to three channels are supported (UVW, for accessing volume
  348. * or cube maps). If the value is 2 for a given channel n, the
  349. * component p.z of mTextureCoords[n][p] is set to 0.0f.
  350. * If the value is 1 for a given channel, p.y is set to 0.0f, too.
  351. * @note 4D coords are not supported
  352. */
  353. unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  354. /** The faces the mesh is constructed from.
  355. * Each face refers to a number of vertices by their indices.
  356. * This array is always present in a mesh, its size is given
  357. * in mNumFaces. If the AI_SCENE_FLAGS_NON_VERBOSE_FORMAT
  358. * is NOT set each face references an unique set of vertices.
  359. */
  360. C_STRUCT aiFace* mFaces;
  361. /** The number of bones this mesh contains.
  362. * Can be 0, in which case the mBones array is NULL.
  363. */
  364. unsigned int mNumBones;
  365. /** The bones of this mesh.
  366. * A bone consists of a name by which it can be found in the
  367. * frame hierarchy and a set of vertex weights.
  368. */
  369. C_STRUCT aiBone** mBones;
  370. /** The material used by this mesh.
  371. * A mesh does use only a single material. If an imported model uses
  372. * multiple materials, the import splits up the mesh. Use this value
  373. * as index into the scene's material list.
  374. */
  375. unsigned int mMaterialIndex;
  376. #ifdef __cplusplus
  377. //! Default constructor. Initializes all members to 0
  378. aiMesh()
  379. {
  380. mNumVertices = 0;
  381. mNumFaces = 0;
  382. mPrimitiveTypes = 0;
  383. mVertices = NULL; mFaces = NULL;
  384. mNormals = NULL; mTangents = NULL;
  385. mBitangents = NULL;
  386. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++)
  387. {
  388. mNumUVComponents[a] = 0;
  389. mTextureCoords[a] = NULL;
  390. }
  391. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; a++)
  392. mColors[a] = NULL;
  393. mNumBones = 0; mBones = NULL;
  394. mMaterialIndex = 0;
  395. }
  396. //! Deletes all storage allocated for the mesh
  397. ~aiMesh()
  398. {
  399. delete [] mVertices;
  400. delete [] mNormals;
  401. delete [] mTangents;
  402. delete [] mBitangents;
  403. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++)
  404. delete [] mTextureCoords[a];
  405. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; a++)
  406. delete [] mColors[a];
  407. // DO NOT REMOVE THIS ADDITIONAL CHECK
  408. if (mNumBones && mBones)
  409. {
  410. for( unsigned int a = 0; a < mNumBones; a++)
  411. delete mBones[a];
  412. delete [] mBones;
  413. }
  414. delete [] mFaces;
  415. }
  416. //! Check whether the mesh contains positions. If no special scene flags
  417. //! (such as AI_SCENE_FLAGS_ANIM_SKELETON_ONLY) are set this will
  418. //! always return true
  419. bool HasPositions() const
  420. { return mVertices != NULL && mNumVertices > 0; }
  421. //! Check whether the mesh contains faces. If no special scene flags
  422. //! are set this should always return true
  423. bool HasFaces() const
  424. { return mFaces != NULL && mNumFaces > 0; }
  425. //! Check whether the mesh contains normal vectors
  426. bool HasNormals() const
  427. { return mNormals != NULL && mNumVertices > 0; }
  428. //! Check whether the mesh contains tangent and bitangent vectors
  429. //! It is not possible that it contains tangents and no bitangents
  430. //! (or the other way round). The existence of one of them
  431. //! implies that the second is there, too.
  432. bool HasTangentsAndBitangents() const
  433. { return mTangents != NULL && mBitangents != NULL && mNumVertices > 0; }
  434. //! Check whether the mesh contains a vertex color set
  435. //! \param pIndex Index of the vertex color set
  436. bool HasVertexColors( unsigned int pIndex) const
  437. {
  438. if( pIndex >= AI_MAX_NUMBER_OF_COLOR_SETS)
  439. return false;
  440. else
  441. return mColors[pIndex] != NULL && mNumVertices > 0;
  442. }
  443. //! Check whether the mesh contains a texture coordinate set
  444. //! \param pIndex Index of the texture coordinates set
  445. bool HasTextureCoords( unsigned int pIndex) const
  446. {
  447. if( pIndex >= AI_MAX_NUMBER_OF_TEXTURECOORDS)
  448. return false;
  449. else
  450. return mTextureCoords[pIndex] != NULL && mNumVertices > 0;
  451. }
  452. //! Get the number of UV channels the mesh contains
  453. unsigned int GetNumUVChannels() const
  454. {
  455. unsigned int n = 0;
  456. while (n < AI_MAX_NUMBER_OF_TEXTURECOORDS && mTextureCoords[n])++n;
  457. return n;
  458. }
  459. //! Get the number of vertex color channels the mesh contains
  460. unsigned int GetNumColorChannels() const
  461. {
  462. unsigned int n = 0;
  463. while (n < AI_MAX_NUMBER_OF_COLOR_SETS && mColors[n])++n;
  464. return n;
  465. }
  466. //! Check whether the mesh contains bones
  467. inline bool HasBones() const
  468. { return mBones != NULL && mNumBones > 0; }
  469. #endif // __cplusplus
  470. };
  471. #ifdef __cplusplus
  472. }
  473. #endif //! extern "C"
  474. #endif // __AI_MESH_H_INC