OgreStructs.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef AI_OGRESTRUCTS_H_INC
  34. #define AI_OGRESTRUCTS_H_INC
  35. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  36. #include <assimp/MemoryIOWrapper.h>
  37. #include <memory>
  38. #include <assimp/mesh.h>
  39. #include <map>
  40. #include <vector>
  41. #include <set>
  42. struct aiNodeAnim;
  43. struct aiAnimation;
  44. struct aiNode;
  45. struct aiMaterial;
  46. struct aiScene;
  47. /** @note Parts of this implementation, for example enums, deserialization constants and logic
  48. has been copied directly with minor modifications from the MIT licensed Ogre3D code base.
  49. See more from https://bitbucket.org/sinbad/ogre. */
  50. namespace Assimp
  51. {
  52. namespace Ogre
  53. {
  54. // Forward decl
  55. class Mesh;
  56. class MeshXml;
  57. class SubMesh;
  58. class SubMeshXml;
  59. class Skeleton;
  60. #define OGRE_SAFE_DELETE(p) delete p; p=0;
  61. // Typedefs
  62. typedef Assimp::MemoryIOStream MemoryStream;
  63. typedef std::shared_ptr<MemoryStream> MemoryStreamPtr;
  64. typedef std::map<uint16_t, MemoryStreamPtr> VertexBufferBindings;
  65. // Ogre Vertex Element
  66. class VertexElement
  67. {
  68. public:
  69. /// Vertex element semantics, used to identify the meaning of vertex buffer contents
  70. enum Semantic {
  71. /// Position, 3 reals per vertex
  72. VES_POSITION = 1,
  73. /// Blending weights
  74. VES_BLEND_WEIGHTS = 2,
  75. /// Blending indices
  76. VES_BLEND_INDICES = 3,
  77. /// Normal, 3 reals per vertex
  78. VES_NORMAL = 4,
  79. /// Diffuse colours
  80. VES_DIFFUSE = 5,
  81. /// Specular colours
  82. VES_SPECULAR = 6,
  83. /// Texture coordinates
  84. VES_TEXTURE_COORDINATES = 7,
  85. /// Binormal (Y axis if normal is Z)
  86. VES_BINORMAL = 8,
  87. /// Tangent (X axis if normal is Z)
  88. VES_TANGENT = 9,
  89. /// The number of VertexElementSemantic elements (note - the first value VES_POSITION is 1)
  90. VES_COUNT = 9
  91. };
  92. /// Vertex element type, used to identify the base types of the vertex contents
  93. enum Type
  94. {
  95. VET_FLOAT1 = 0,
  96. VET_FLOAT2 = 1,
  97. VET_FLOAT3 = 2,
  98. VET_FLOAT4 = 3,
  99. /// alias to more specific colour type - use the current rendersystem's colour packing
  100. VET_COLOUR = 4,
  101. VET_SHORT1 = 5,
  102. VET_SHORT2 = 6,
  103. VET_SHORT3 = 7,
  104. VET_SHORT4 = 8,
  105. VET_UBYTE4 = 9,
  106. /// D3D style compact colour
  107. VET_COLOUR_ARGB = 10,
  108. /// GL style compact colour
  109. VET_COLOUR_ABGR = 11,
  110. VET_DOUBLE1 = 12,
  111. VET_DOUBLE2 = 13,
  112. VET_DOUBLE3 = 14,
  113. VET_DOUBLE4 = 15,
  114. VET_USHORT1 = 16,
  115. VET_USHORT2 = 17,
  116. VET_USHORT3 = 18,
  117. VET_USHORT4 = 19,
  118. VET_INT1 = 20,
  119. VET_INT2 = 21,
  120. VET_INT3 = 22,
  121. VET_INT4 = 23,
  122. VET_UINT1 = 24,
  123. VET_UINT2 = 25,
  124. VET_UINT3 = 26,
  125. VET_UINT4 = 27
  126. };
  127. VertexElement();
  128. /// Size of the vertex element in bytes.
  129. size_t Size() const;
  130. /// Count of components in this element, eg. VET_FLOAT3 return 3.
  131. size_t ComponentCount() const;
  132. /// Type as string.
  133. std::string TypeToString();
  134. /// Semantic as string.
  135. std::string SemanticToString();
  136. static size_t TypeSize(Type type);
  137. static size_t ComponentCount(Type type);
  138. static std::string TypeToString(Type type);
  139. static std::string SemanticToString(Semantic semantic);
  140. uint16_t index;
  141. uint16_t source;
  142. uint16_t offset;
  143. Type type;
  144. Semantic semantic;
  145. };
  146. typedef std::vector<VertexElement> VertexElementList;
  147. /// Ogre Vertex Bone Assignment
  148. struct VertexBoneAssignment
  149. {
  150. uint32_t vertexIndex;
  151. uint16_t boneIndex;
  152. float weight;
  153. };
  154. typedef std::vector<VertexBoneAssignment> VertexBoneAssignmentList;
  155. typedef std::map<uint32_t, VertexBoneAssignmentList > VertexBoneAssignmentsMap;
  156. typedef std::map<uint16_t, std::vector<aiVertexWeight> > AssimpVertexBoneWeightList;
  157. // Ogre Vertex Data interface, inherited by the binary and XML implementations.
  158. class IVertexData
  159. {
  160. public:
  161. IVertexData();
  162. /// Returns if bone assignments are available.
  163. bool HasBoneAssignments() const;
  164. /// Add vertex mapping from old to new index.
  165. void AddVertexMapping(uint32_t oldIndex, uint32_t newIndex);
  166. /// Returns re-mapped bone assignments.
  167. /** @note Uses mappings added via AddVertexMapping. */
  168. AssimpVertexBoneWeightList AssimpBoneWeights(size_t vertices);
  169. /// Returns a set of bone indexes that are referenced by bone assignments (weights).
  170. std::set<uint16_t> ReferencedBonesByWeights() const;
  171. /// Vertex count.
  172. uint32_t count;
  173. /// Bone assignments.
  174. VertexBoneAssignmentList boneAssignments;
  175. private:
  176. void BoneAssignmentsForVertex(uint32_t currentIndex, uint32_t newIndex, VertexBoneAssignmentList &dest) const;
  177. std::map<uint32_t, std::vector<uint32_t> > vertexIndexMapping;
  178. VertexBoneAssignmentsMap boneAssignmentsMap;
  179. };
  180. // Ogre Vertex Data
  181. class VertexData : public IVertexData
  182. {
  183. public:
  184. VertexData();
  185. ~VertexData();
  186. /// Releases all memory that this data structure owns.
  187. void Reset();
  188. /// Get vertex size for @c source.
  189. uint32_t VertexSize(uint16_t source) const;
  190. /// Get vertex buffer for @c source.
  191. MemoryStream *VertexBuffer(uint16_t source);
  192. /// Get vertex element for @c semantic for @c index.
  193. VertexElement *GetVertexElement(VertexElement::Semantic semantic, uint16_t index = 0);
  194. /// Vertex elements.
  195. VertexElementList vertexElements;
  196. /// Vertex buffers mapped to bind index.
  197. VertexBufferBindings vertexBindings;
  198. };
  199. // Ogre Index Data
  200. class IndexData
  201. {
  202. public:
  203. IndexData();
  204. ~IndexData();
  205. /// Releases all memory that this data structure owns.
  206. void Reset();
  207. /// Index size in bytes.
  208. size_t IndexSize() const;
  209. /// Face size in bytes.
  210. size_t FaceSize() const;
  211. /// Index count.
  212. uint32_t count;
  213. /// Face count.
  214. uint32_t faceCount;
  215. /// If has 32-bit indexes.
  216. bool is32bit;
  217. /// Index buffer.
  218. MemoryStreamPtr buffer;
  219. };
  220. /// Ogre Pose
  221. class Pose
  222. {
  223. public:
  224. struct Vertex
  225. {
  226. uint32_t index;
  227. aiVector3D offset;
  228. aiVector3D normal;
  229. };
  230. typedef std::map<uint32_t, Vertex> PoseVertexMap;
  231. Pose() : target(0), hasNormals(false) {}
  232. /// Name.
  233. std::string name;
  234. /// Target.
  235. uint16_t target;
  236. /// Does vertices map have normals.
  237. bool hasNormals;
  238. /// Vertex offset and normals.
  239. PoseVertexMap vertices;
  240. };
  241. typedef std::vector<Pose*> PoseList;
  242. /// Ogre Pose Key Frame Ref
  243. struct PoseRef
  244. {
  245. uint16_t index;
  246. float influence;
  247. };
  248. typedef std::vector<PoseRef> PoseRefList;
  249. /// Ogre Pose Key Frame
  250. struct PoseKeyFrame
  251. {
  252. /// Time position in the animation.
  253. float timePos;
  254. PoseRefList references;
  255. };
  256. typedef std::vector<PoseKeyFrame> PoseKeyFrameList;
  257. /// Ogre Morph Key Frame
  258. struct MorphKeyFrame
  259. {
  260. /// Time position in the animation.
  261. float timePos;
  262. MemoryStreamPtr buffer;
  263. };
  264. typedef std::vector<MorphKeyFrame> MorphKeyFrameList;
  265. /// Ogre animation key frame
  266. struct TransformKeyFrame
  267. {
  268. TransformKeyFrame();
  269. aiMatrix4x4 Transform();
  270. float timePos;
  271. aiQuaternion rotation;
  272. aiVector3D position;
  273. aiVector3D scale;
  274. };
  275. typedef std::vector<TransformKeyFrame> TransformKeyFrameList;
  276. /// Ogre Animation Track
  277. struct VertexAnimationTrack
  278. {
  279. enum Type
  280. {
  281. /// No animation
  282. VAT_NONE = 0,
  283. /// Morph animation is made up of many interpolated snapshot keyframes
  284. VAT_MORPH = 1,
  285. /// Pose animation is made up of a single delta pose keyframe
  286. VAT_POSE = 2,
  287. /// Keyframe that has its on pos, rot and scale for a time position
  288. VAT_TRANSFORM = 3
  289. };
  290. VertexAnimationTrack();
  291. /// Convert to Assimp node animation.
  292. aiNodeAnim *ConvertToAssimpAnimationNode(Skeleton *skeleton);
  293. // Animation type.
  294. Type type;
  295. /// Vertex data target.
  296. /** 0 == shared geometry
  297. >0 == submesh index + 1 */
  298. uint16_t target;
  299. /// Only valid for VAT_TRANSFORM.
  300. std::string boneName;
  301. /// Only one of these will contain key frames, depending on the type enum.
  302. PoseKeyFrameList poseKeyFrames;
  303. MorphKeyFrameList morphKeyFrames;
  304. TransformKeyFrameList transformKeyFrames;
  305. };
  306. typedef std::vector<VertexAnimationTrack> VertexAnimationTrackList;
  307. /// Ogre Animation
  308. class Animation
  309. {
  310. public:
  311. explicit Animation(Skeleton *parent);
  312. explicit Animation(Mesh *parent);
  313. /// Returns the associated vertex data for a track in this animation.
  314. /** @note Only valid to call when parent Mesh is set. */
  315. VertexData *AssociatedVertexData(VertexAnimationTrack *track) const;
  316. /// Convert to Assimp animation.
  317. aiAnimation *ConvertToAssimpAnimation();
  318. /// Parent mesh.
  319. /** @note Set only when animation is read from a mesh. */
  320. Mesh *parentMesh;
  321. /// Parent skeleton.
  322. /** @note Set only when animation is read from a skeleton. */
  323. Skeleton *parentSkeleton;
  324. /// Animation name.
  325. std::string name;
  326. /// Base animation name.
  327. std::string baseName;
  328. /// Length in seconds.
  329. float length;
  330. /// Base animation key time.
  331. float baseTime;
  332. /// Animation tracks.
  333. VertexAnimationTrackList tracks;
  334. };
  335. typedef std::vector<Animation*> AnimationList;
  336. /// Ogre Bone
  337. class Bone
  338. {
  339. public:
  340. Bone();
  341. /// Returns if this bone is parented.
  342. bool IsParented() const;
  343. /// Parent index as uint16_t. Internally int32_t as -1 means unparented.
  344. uint16_t ParentId() const;
  345. /// Add child bone.
  346. void AddChild(Bone *bone);
  347. /// Calculates the world matrix for bone and its children.
  348. void CalculateWorldMatrixAndDefaultPose(Skeleton *skeleton);
  349. /// Convert to Assimp node (animation nodes).
  350. aiNode *ConvertToAssimpNode(Skeleton *parent, aiNode *parentNode = nullptr);
  351. /// Convert to Assimp bone (mesh bones).
  352. aiBone *ConvertToAssimpBone(Skeleton *parent, const std::vector<aiVertexWeight> &boneWeights);
  353. uint16_t id;
  354. std::string name;
  355. Bone *parent;
  356. int32_t parentId;
  357. std::vector<uint16_t> children;
  358. aiVector3D position;
  359. aiQuaternion rotation;
  360. aiVector3D scale;
  361. aiMatrix4x4 worldMatrix;
  362. aiMatrix4x4 defaultPose;
  363. };
  364. typedef std::vector<Bone*> BoneList;
  365. /// Ogre Skeleton
  366. class Skeleton
  367. {
  368. public:
  369. enum BlendMode
  370. {
  371. /// Animations are applied by calculating a weighted average of all animations
  372. ANIMBLEND_AVERAGE = 0,
  373. /// Animations are applied by calculating a weighted cumulative total
  374. ANIMBLEND_CUMULATIVE = 1
  375. };
  376. Skeleton();
  377. ~Skeleton();
  378. /// Releases all memory that this data structure owns.
  379. void Reset();
  380. /// Returns unparented root bones.
  381. BoneList RootBones() const;
  382. /// Returns number of unparented root bones.
  383. size_t NumRootBones() const;
  384. /// Get bone by name.
  385. Bone *BoneByName(const std::string &name) const;
  386. /// Get bone by id.
  387. Bone *BoneById(uint16_t id) const;
  388. BoneList bones;
  389. AnimationList animations;
  390. /// @todo Take blend mode into account, but where?
  391. BlendMode blendMode;
  392. };
  393. /// Ogre Sub Mesh interface, inherited by the binary and XML implementations.
  394. class ISubMesh
  395. {
  396. public:
  397. /// @note Full list of Ogre types, not all of them are supported and exposed to Assimp.
  398. enum OperationType
  399. {
  400. /// A list of points, 1 vertex per point
  401. OT_POINT_LIST = 1,
  402. /// A list of lines, 2 vertices per line
  403. OT_LINE_LIST = 2,
  404. /// A strip of connected lines, 1 vertex per line plus 1 start vertex
  405. OT_LINE_STRIP = 3,
  406. /// A list of triangles, 3 vertices per triangle
  407. OT_TRIANGLE_LIST = 4,
  408. /// A strip of triangles, 3 vertices for the first triangle, and 1 per triangle after that
  409. OT_TRIANGLE_STRIP = 5,
  410. /// A fan of triangles, 3 vertices for the first triangle, and 1 per triangle after that
  411. OT_TRIANGLE_FAN = 6
  412. };
  413. ISubMesh();
  414. /// SubMesh index.
  415. unsigned int index;
  416. /// SubMesh name.
  417. std::string name;
  418. /// Material used by this submesh.
  419. std::string materialRef;
  420. /// Texture alias information.
  421. std::string textureAliasName;
  422. std::string textureAliasRef;
  423. /// Assimp scene material index used by this submesh.
  424. /** -1 if no material or material could not be imported. */
  425. int materialIndex;
  426. /// If submesh uses shared geometry from parent mesh.
  427. bool usesSharedVertexData;
  428. /// Operation type.
  429. OperationType operationType;
  430. };
  431. /// Ogre SubMesh
  432. class SubMesh : public ISubMesh
  433. {
  434. public:
  435. SubMesh();
  436. ~SubMesh();
  437. /// Releases all memory that this data structure owns.
  438. /** @note Vertex and index data contains shared ptrs
  439. that are freed automatically. In practice the ref count
  440. should be 0 after this reset. */
  441. void Reset();
  442. /// Convert to Assimp mesh.
  443. aiMesh *ConvertToAssimpMesh(Mesh *parent);
  444. /// Vertex data.
  445. VertexData *vertexData;
  446. /// Index data.
  447. IndexData *indexData;
  448. };
  449. typedef std::vector<SubMesh*> SubMeshList;
  450. /// Ogre Mesh
  451. class Mesh
  452. {
  453. public:
  454. /// Constructor.
  455. Mesh();
  456. /// Destructor.
  457. ~Mesh();
  458. /// Releases all memory that this data structure owns.
  459. void Reset();
  460. /// Returns number of subMeshes.
  461. size_t NumSubMeshes() const;
  462. /// Returns submesh for @c index.
  463. SubMesh *GetSubMesh( size_t index) const;
  464. /// Convert mesh to Assimp scene.
  465. void ConvertToAssimpScene(aiScene* dest);
  466. /// Mesh has skeletal animations.
  467. bool hasSkeletalAnimations;
  468. /// Skeleton reference.
  469. std::string skeletonRef;
  470. /// Skeleton.
  471. Skeleton *skeleton;
  472. /// Vertex data
  473. VertexData *sharedVertexData;
  474. /// Sub meshes.
  475. SubMeshList subMeshes;
  476. /// Animations
  477. AnimationList animations;
  478. /// Poses
  479. PoseList poses;
  480. };
  481. /// Ogre XML Vertex Data
  482. class VertexDataXml : public IVertexData
  483. {
  484. public:
  485. VertexDataXml();
  486. bool HasPositions() const;
  487. bool HasNormals() const;
  488. bool HasTangents() const;
  489. bool HasUvs() const;
  490. size_t NumUvs() const;
  491. std::vector<aiVector3D> positions;
  492. std::vector<aiVector3D> normals;
  493. std::vector<aiVector3D> tangents;
  494. std::vector<std::vector<aiVector3D> > uvs;
  495. };
  496. /// Ogre XML Index Data
  497. class IndexDataXml
  498. {
  499. public:
  500. IndexDataXml() : faceCount(0) {}
  501. /// Face count.
  502. uint32_t faceCount;
  503. std::vector<aiFace> faces;
  504. };
  505. /// Ogre XML SubMesh
  506. class SubMeshXml : public ISubMesh
  507. {
  508. public:
  509. SubMeshXml();
  510. ~SubMeshXml();
  511. /// Releases all memory that this data structure owns.
  512. void Reset();
  513. aiMesh *ConvertToAssimpMesh(MeshXml *parent);
  514. IndexDataXml *indexData;
  515. VertexDataXml *vertexData;
  516. };
  517. typedef std::vector<SubMeshXml*> SubMeshXmlList;
  518. /// Ogre XML Mesh
  519. class MeshXml
  520. {
  521. public:
  522. MeshXml();
  523. ~MeshXml();
  524. /// Releases all memory that this data structure owns.
  525. void Reset();
  526. /// Returns number of subMeshes.
  527. size_t NumSubMeshes() const;
  528. /// Returns submesh for @c index.
  529. SubMeshXml *GetSubMesh(uint16_t index) const;
  530. /// Convert mesh to Assimp scene.
  531. void ConvertToAssimpScene(aiScene* dest);
  532. /// Skeleton reference.
  533. std::string skeletonRef;
  534. /// Skeleton.
  535. Skeleton *skeleton;
  536. /// Vertex data
  537. VertexDataXml *sharedVertexData;
  538. /// Sub meshes.
  539. SubMeshXmlList subMeshes;
  540. };
  541. } // Ogre
  542. } // Assimp
  543. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
  544. #endif // AI_OGRESTRUCTS_H_INC