CSkinnedMesh.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. //New skinned mesh
  5. #ifndef __C_SKINNED_MESH_H_INCLUDED__
  6. #define __C_SKINNED_MESH_H_INCLUDED__
  7. #include "ISkinnedMesh.h"
  8. #include "SMeshBuffer.h"
  9. #include "S3DVertex.h"
  10. #include "irrString.h"
  11. #include "matrix4.h"
  12. #include "quaternion.h"
  13. class JointInfluence
  14. {
  15. public:
  16. int joint_idx;
  17. float weight;
  18. };
  19. typedef irr::core::array<irr::core::array
  20. <irr::core::array<JointInfluence> > > WeightInfluence;
  21. namespace irr
  22. {
  23. namespace scene
  24. {
  25. class IAnimatedMeshSceneNode;
  26. class IBoneSceneNode;
  27. class CSkinnedMesh: public ISkinnedMesh
  28. {
  29. public:
  30. //! constructor
  31. CSkinnedMesh();
  32. //! destructor
  33. virtual ~CSkinnedMesh();
  34. //! returns the amount of frames. If the amount is 1, it is a static (=non animated) mesh.
  35. virtual u32 getFrameCount() const;
  36. //! Gets the default animation speed of the animated mesh.
  37. /** \return Amount of frames per second. If the amount is 0, it is a static, non animated mesh. */
  38. virtual f32 getAnimationSpeed() const;
  39. //! Gets the frame count of the animated mesh.
  40. /** \param fps Frames per second to play the animation with. If the amount is 0, it is not animated.
  41. The actual speed is set in the scene node the mesh is instantiated in.*/
  42. virtual void setAnimationSpeed(f32 fps);
  43. //! returns the animated mesh based on a detail level (which is ignored)
  44. virtual IMesh* getMesh(s32 frame, s32 detailLevel=255, s32 startFrameLoop=-1, s32 endFrameLoop=-1);
  45. //! Animates this mesh's joints based on frame input
  46. //! blend: {0-old position, 1-New position}
  47. virtual void animateMesh(f32 frame, f32 blend);
  48. //! Preforms a software skin on this mesh based of joint positions
  49. virtual void skinMesh(f32 strength=1.f, SkinningCallback sc = NULL, int offset = -1);
  50. //! returns amount of mesh buffers.
  51. virtual u32 getMeshBufferCount() const;
  52. //! returns pointer to a mesh buffer
  53. virtual IMeshBuffer* getMeshBuffer(u32 nr) const;
  54. //! Returns pointer to a mesh buffer which fits a material
  55. /** \param material: material to search for
  56. \return Returns the pointer to the mesh buffer or
  57. NULL if there is no such mesh buffer. */
  58. virtual IMeshBuffer* getMeshBuffer( const video::SMaterial &material) const;
  59. //! returns an axis aligned bounding box
  60. virtual const core::aabbox3d<f32>& getBoundingBox() const;
  61. //! set user axis aligned bounding box
  62. virtual void setBoundingBox( const core::aabbox3df& box);
  63. //! sets a flag of all contained materials to a new value
  64. virtual void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue);
  65. //! set the hardware mapping hint, for driver
  66. virtual void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX);
  67. //! flags the meshbuffer as changed, reloads hardware buffers
  68. virtual void setDirty(E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX);
  69. //! Returns the type of the animated mesh.
  70. virtual E_ANIMATED_MESH_TYPE getMeshType() const;
  71. //! Gets joint count.
  72. virtual u32 getJointCount() const;
  73. //! Gets the name of a joint.
  74. virtual const c8* getJointName(u32 number) const;
  75. //! Gets a joint number from its name
  76. virtual s32 getJointNumber(const c8* name) const;
  77. //! uses animation from another mesh
  78. virtual bool useAnimationFrom(const ISkinnedMesh *mesh);
  79. //! Update Normals when Animating
  80. //! False= Don't (default)
  81. //! True = Update normals, slower
  82. virtual void updateNormalsWhenAnimating(bool on);
  83. //! Sets Interpolation Mode
  84. virtual void setInterpolationMode(E_INTERPOLATION_MODE mode);
  85. //! Convertes the mesh to contain tangent information
  86. virtual void convertMeshToTangents(bool(*predicate)(IMeshBuffer*));
  87. //! Does the mesh have no animation
  88. virtual bool isStatic();
  89. //! (This feature is not implemented in irrlicht yet)
  90. virtual bool setHardwareSkinning(bool on);
  91. //Interface for the mesh loaders (finalize should lock these functions, and they should have some prefix like loader_
  92. //these functions will use the needed arrays, set values, etc to help the loaders
  93. //! exposed for loaders to add mesh buffers
  94. virtual core::array<SSkinMeshBuffer*> &getMeshBuffers();
  95. //! alternative method for adding joints
  96. virtual core::array<SJoint*> &getAllJoints();
  97. //! alternative method for adding joints
  98. virtual const core::array<SJoint*> &getAllJoints() const;
  99. //! loaders should call this after populating the mesh
  100. virtual void finalize();
  101. //! Adds a new meshbuffer to the mesh, access it as last one
  102. virtual SSkinMeshBuffer *addMeshBuffer();
  103. //! Adds a new joint to the mesh, access it as last one
  104. virtual SJoint *addJoint(SJoint *parent=0);
  105. //! Adds a new position key to the mesh, access it as last one
  106. virtual SPositionKey *addPositionKey(SJoint *joint);
  107. //! Adds a new rotation key to the mesh, access it as last one
  108. virtual SRotationKey *addRotationKey(SJoint *joint);
  109. //! Adds a new scale key to the mesh, access it as last one
  110. virtual SScaleKey *addScaleKey(SJoint *joint);
  111. //! Adds a new weight to the mesh, access it as last one
  112. virtual SWeight *addWeight(SJoint *joint);
  113. virtual void updateBoundingBox(void);
  114. //! Recovers the joints from the mesh
  115. void recoverJointsFromMesh(core::array<IBoneSceneNode*> &jointChildSceneNodes);
  116. //! Tranfers the joint data to the mesh
  117. void transferJointsToMesh(const core::array<IBoneSceneNode*> &jointChildSceneNodes);
  118. //! Tranfers the joint hints to the mesh
  119. void transferOnlyJointsHintsToMesh(const core::array<IBoneSceneNode*> &jointChildSceneNodes);
  120. //! Creates an array of joints from this mesh as children of node
  121. void addJoints(core::array<IBoneSceneNode*> &jointChildSceneNodes,
  122. IAnimatedMeshSceneNode* node,
  123. ISceneManager* smgr);
  124. void convertForSkinning();
  125. void computeWeightInfluence(SJoint *joint, size_t &index, WeightInfluence& wi);
  126. u32 getTotalJoints() const { return m_total_joints; }
  127. private:
  128. void toStaticPose();
  129. void checkForAnimation();
  130. void normalizeWeights();
  131. void buildAllLocalAnimatedMatrices();
  132. void buildAllGlobalAnimatedMatrices(SJoint *Joint=0, SJoint *ParentJoint=0);
  133. void getFrameData(f32 frame, SJoint *Node,
  134. core::vector3df &position, s32 &positionHint,
  135. core::vector3df &scale, s32 &scaleHint,
  136. core::quaternion &rotation, s32 &rotationHint);
  137. void calculateGlobalMatrices(SJoint *Joint,SJoint *ParentJoint);
  138. void skinJoint(SJoint *Joint, SJoint *ParentJoint, f32 strength=1.f,
  139. SkinningCallback sc = NULL, int offset = -1);
  140. void calculateTangents(core::vector3df& normal,
  141. core::vector3df& tangent, core::vector3df& binormal,
  142. core::vector3df& vt1, core::vector3df& vt2, core::vector3df& vt3,
  143. core::vector2df& tc1, core::vector2df& tc2, core::vector2df& tc3);
  144. static bool sortJointInfluenceFunc(const JointInfluence& a,
  145. const JointInfluence& b);
  146. core::array<SSkinMeshBuffer*> *SkinningBuffers; //Meshbuffer to skin, default is to skin localBuffers
  147. core::array<SSkinMeshBuffer*> LocalBuffers;
  148. core::array<SJoint*> AllJoints;
  149. core::array<SJoint*> RootJoints;
  150. core::array< core::array<bool> > Vertices_Moved;
  151. core::aabbox3d<f32> BoundingBox;
  152. f32 AnimationFrames;
  153. f32 FramesPerSecond;
  154. f32 LastAnimatedFrame;
  155. bool SkinnedLastFrame;
  156. E_INTERPOLATION_MODE InterpolationMode:8;
  157. bool HasAnimation;
  158. bool PreparedForSkinning;
  159. bool AnimateNormals;
  160. bool HardwareSkinning;
  161. u32 m_total_joints;
  162. u32 m_current_joint;
  163. };
  164. } // end namespace scene
  165. } // end namespace irr
  166. #endif