ISkinnedMesh.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "irrArray.h"
  6. #include "IAnimatedMesh.h"
  7. #include "SSkinMeshBuffer.h"
  8. #include "quaternion.h"
  9. #include <optional>
  10. #include <string>
  11. namespace irr
  12. {
  13. namespace scene
  14. {
  15. enum E_INTERPOLATION_MODE
  16. {
  17. // constant does use the current key-values without interpolation
  18. EIM_CONSTANT = 0,
  19. // linear interpolation
  20. EIM_LINEAR,
  21. //! count of all available interpolation modes
  22. EIM_COUNT
  23. };
  24. //! Interface for using some special functions of Skinned meshes
  25. class ISkinnedMesh : public IAnimatedMesh
  26. {
  27. public:
  28. //! Gets joint count.
  29. /** \return Amount of joints in the skeletal animated mesh. */
  30. virtual u32 getJointCount() const = 0;
  31. //! Gets the name of a joint.
  32. /** \param number: Zero based index of joint. The last joint
  33. has the number getJointCount()-1;
  34. \return Name of joint and null if an error happened. */
  35. virtual const std::optional<std::string> &getJointName(u32 number) const = 0;
  36. //! Gets a joint number from its name
  37. /** \param name: Name of the joint.
  38. \return Number of the joint or std::nullopt if not found. */
  39. virtual std::optional<u32> getJointNumber(const std::string &name) const = 0;
  40. //! Use animation from another mesh
  41. /** The animation is linked (not copied) based on joint names
  42. so make sure they are unique.
  43. \return True if all joints in this mesh were
  44. matched up (empty names will not be matched, and it's case
  45. sensitive). Unmatched joints will not be animated. */
  46. virtual bool useAnimationFrom(const ISkinnedMesh *mesh) = 0;
  47. //! Update Normals when Animating
  48. /** \param on If false don't animate, which is faster.
  49. Else update normals, which allows for proper lighting of
  50. animated meshes. */
  51. virtual void updateNormalsWhenAnimating(bool on) = 0;
  52. //! Sets Interpolation Mode
  53. virtual void setInterpolationMode(E_INTERPOLATION_MODE mode) = 0;
  54. //! Animates this mesh's joints based on frame input
  55. virtual void animateMesh(f32 frame, f32 blend) = 0;
  56. //! Preforms a software skin on this mesh based of joint positions
  57. virtual void skinMesh() = 0;
  58. //! converts the vertex type of all meshbuffers to tangents.
  59. /** E.g. used for bump mapping. */
  60. virtual void convertMeshToTangents() = 0;
  61. //! Allows to enable hardware skinning.
  62. /* This feature is not implemented in Irrlicht yet */
  63. virtual bool setHardwareSkinning(bool on) = 0;
  64. //! Refreshes vertex data cached in joints such as positions and normals
  65. virtual void refreshJointCache() = 0;
  66. //! Moves the mesh into static position.
  67. virtual void resetAnimation() = 0;
  68. //! A vertex weight
  69. struct SWeight
  70. {
  71. //! Index of the mesh buffer
  72. u16 buffer_id; // I doubt 32bits is needed
  73. //! Index of the vertex
  74. u32 vertex_id; // Store global ID here
  75. //! Weight Strength/Percentage (0-1)
  76. f32 strength;
  77. private:
  78. //! Internal members used by CSkinnedMesh
  79. friend class CSkinnedMesh;
  80. char *Moved;
  81. core::vector3df StaticPos;
  82. core::vector3df StaticNormal;
  83. };
  84. //! Animation keyframe which describes a new position
  85. struct SPositionKey
  86. {
  87. f32 frame;
  88. core::vector3df position;
  89. };
  90. //! Animation keyframe which describes a new scale
  91. struct SScaleKey
  92. {
  93. f32 frame;
  94. core::vector3df scale;
  95. };
  96. //! Animation keyframe which describes a new rotation
  97. struct SRotationKey
  98. {
  99. f32 frame;
  100. core::quaternion rotation;
  101. };
  102. //! Joints
  103. struct SJoint
  104. {
  105. SJoint() :
  106. UseAnimationFrom(0), GlobalSkinningSpace(false),
  107. positionHint(-1), scaleHint(-1), rotationHint(-1)
  108. {
  109. }
  110. //! The name of this joint
  111. std::optional<std::string> Name;
  112. //! Local matrix of this joint
  113. core::matrix4 LocalMatrix;
  114. //! List of child joints
  115. core::array<SJoint *> Children;
  116. //! List of attached meshes
  117. core::array<u32> AttachedMeshes;
  118. //! Animation keys causing translation change
  119. core::array<SPositionKey> PositionKeys;
  120. //! Animation keys causing scale change
  121. core::array<SScaleKey> ScaleKeys;
  122. //! Animation keys causing rotation change
  123. core::array<SRotationKey> RotationKeys;
  124. //! Skin weights
  125. core::array<SWeight> Weights;
  126. //! Unnecessary for loaders, will be overwritten on finalize
  127. core::matrix4 GlobalMatrix; // loaders may still choose to set this (temporarily) to calculate absolute vertex data.
  128. core::matrix4 GlobalAnimatedMatrix;
  129. core::matrix4 LocalAnimatedMatrix;
  130. //! These should be set by loaders.
  131. core::vector3df Animatedposition;
  132. core::vector3df Animatedscale;
  133. core::quaternion Animatedrotation;
  134. // The .x and .gltf formats pre-calculate this
  135. std::optional<core::matrix4> GlobalInversedMatrix;
  136. private:
  137. //! Internal members used by CSkinnedMesh
  138. friend class CSkinnedMesh;
  139. SJoint *UseAnimationFrom;
  140. bool GlobalSkinningSpace;
  141. s32 positionHint;
  142. s32 scaleHint;
  143. s32 rotationHint;
  144. };
  145. // Interface for the mesh loaders (finalize should lock these functions, and they should have some prefix like loader_
  146. // these functions will use the needed arrays, set values, etc to help the loaders
  147. //! exposed for loaders: to add mesh buffers
  148. virtual core::array<SSkinMeshBuffer *> &getMeshBuffers() = 0;
  149. //! exposed for loaders: joints list
  150. virtual core::array<SJoint *> &getAllJoints() = 0;
  151. //! exposed for loaders: joints list
  152. virtual const core::array<SJoint *> &getAllJoints() const = 0;
  153. //! loaders should call this after populating the mesh
  154. virtual void finalize() = 0;
  155. //! Adds a new meshbuffer to the mesh, access it as last one
  156. virtual SSkinMeshBuffer *addMeshBuffer() = 0;
  157. //! Adds a new meshbuffer to the mesh, access it as last one
  158. virtual void addMeshBuffer(SSkinMeshBuffer *meshbuf) = 0;
  159. //! Adds a new joint to the mesh, access it as last one
  160. virtual SJoint *addJoint(SJoint *parent = 0) = 0;
  161. //! Adds a new weight to the mesh, access it as last one
  162. virtual SWeight *addWeight(SJoint *joint) = 0;
  163. //! Adds a new position key to the mesh, access it as last one
  164. virtual SPositionKey *addPositionKey(SJoint *joint) = 0;
  165. //! Adds a new scale key to the mesh, access it as last one
  166. virtual SScaleKey *addScaleKey(SJoint *joint) = 0;
  167. //! Adds a new rotation key to the mesh, access it as last one
  168. virtual SRotationKey *addRotationKey(SJoint *joint) = 0;
  169. //! Check if the mesh is non-animated
  170. virtual bool isStatic() = 0;
  171. };
  172. } // end namespace scene
  173. } // end namespace irr