SkinNode.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #ifndef ANKI_SCENE_SKIN_NODE_H
  2. #define ANKI_SCENE_SKIN_NODE_H
  3. #include "anki/scene/SceneNode.h"
  4. #include "anki/scene/RenderComponent.h"
  5. #include "anki/scene/MoveComponent.h"
  6. #include "anki/scene/SpatialComponent.h"
  7. #include "anki/resource/Model.h"
  8. #include "anki/Math.h"
  9. namespace anki {
  10. class Skin;
  11. #if 0
  12. /// Skin specific mesh. It contains a number of VBOs for transform feedback
  13. class SkinMesh: public MeshBase
  14. {
  15. public:
  16. /// Create the @a tfVbos with empty data
  17. SkinMesh(const MeshBase* mesh);
  18. /// @name Accessors
  19. /// @{
  20. const Vbo& getXfbVbo() const
  21. {
  22. return vbo;
  23. }
  24. /// @}
  25. /// @name MeshBase implementers
  26. /// @{
  27. U32 getVerticesCount() const
  28. {
  29. return mesh->getVerticesCount();
  30. }
  31. U32 getIndicesCount() const
  32. {
  33. return mesh->getIndicesCount();
  34. }
  35. U32 getTextureChannelsCount() const
  36. {
  37. return mesh->getTextureChannelsCount();
  38. }
  39. Bool hasWeights() const
  40. {
  41. return false;
  42. }
  43. const Obb& getBoundingShape() const
  44. {
  45. return mesh->getBoundingShape();
  46. }
  47. void getVboInfo(
  48. const VertexAttribute attrib, const Vbo*& vbo,
  49. U32& size, GLenum& type, U32& stride, U32& offset) const;
  50. /// @}
  51. private:
  52. Vbo vbo; ///< Contains the transformed P,N,T
  53. const MeshBase* mesh; ///< The resource
  54. };
  55. /// Skin specific ModelPatch. It uses a SkinMesh to create the VAOs. It also
  56. /// creates a VAO for the transform feedback pass
  57. class SkinModelPatch: public ModelPatchBase
  58. {
  59. public:
  60. /// See TfHwSkinningGeneric.glsl for the locations
  61. enum XfbAttributeLocation
  62. {
  63. XFBAL_POSITION,
  64. XFBAL_NORMAL,
  65. XFBAL_TANGENT,
  66. XFBAL_BONES_COUNT,
  67. XFBAL_BONE_IDS,
  68. XFBAL_BONE_WEIGHTS
  69. };
  70. /// @name Constructors/Destructor
  71. /// @{
  72. SkinModelPatch(const ModelPatchBase* mpatch_,
  73. const SceneAllocator<U8>& alloc);
  74. ~SkinModelPatch();
  75. /// @}
  76. /// @name Accessors
  77. /// @{
  78. SkinMesh& getSkinMesh(const PassLodKey& key)
  79. {
  80. return *skinMeshes[key.level];
  81. }
  82. const SkinMesh& getSkinMesh(const PassLodKey& key) const
  83. {
  84. return *skinMeshes[key.level];
  85. }
  86. const Vao& getTransformFeedbackVao(const PassLodKey& key) const
  87. {
  88. return xfbVaos[key.level];
  89. }
  90. /// @}
  91. /// @name Implementations of ModelPatchBase virtuals
  92. /// @{
  93. const MeshBase& getMeshBase(const PassLodKey& key) const
  94. {
  95. return *skinMeshes[key.level];
  96. }
  97. U32 getMeshesCount() const
  98. {
  99. return skinMeshes.size();
  100. }
  101. const Material& getMaterial() const
  102. {
  103. return mpatch->getMaterial();
  104. }
  105. /// @}
  106. private:
  107. const ModelPatchBase* mpatch;
  108. SceneVector<SkinMesh*> skinMeshes;
  109. SceneVector<Vao> xfbVaos; ///< Used as a source VAO in XFB
  110. };
  111. /// A fragment of the SkinNode
  112. class SkinPatchNode: public SceneNode, public MoveComponent, public RenderComponent,
  113. public SpatialComponent
  114. {
  115. public:
  116. /// @name Constructors/Destructor
  117. /// @{
  118. SkinPatchNode(const ModelPatchBase* modelPatch_,
  119. const char* name, SceneGraph* scene, // Scene
  120. U32 movableFlags, MoveComponent* movParent, // MoveComponent
  121. CollisionShape* spatialCs); // SpatialComponent
  122. /// @}
  123. /// @name Accessors
  124. /// @{
  125. SkinModelPatch& getSkinModelPatch()
  126. {
  127. return *skinModelPatch;
  128. }
  129. const SkinModelPatch& getSkinModelPatch() const
  130. {
  131. return *skinModelPatch;
  132. }
  133. /// @}
  134. /// @name RenderComponent virtuals
  135. /// @{
  136. /// Implements RenderComponent::getModelPatchBase
  137. const ModelPatchBase& getRenderComponentModelPatchBase() const
  138. {
  139. return *skinModelPatch;
  140. }
  141. /// Implements RenderComponent::getMaterial
  142. const Material& getRenderComponentMaterial() const
  143. {
  144. return skinModelPatch->getMaterial();
  145. }
  146. /// Overrides RenderComponent::getRenderComponentWorldTransforms
  147. const Transform* getRenderComponentWorldTransforms() const
  148. {
  149. return &getWorldTransform();
  150. }
  151. /// @}
  152. private:
  153. std::unique_ptr<SkinModelPatch> skinModelPatch;
  154. };
  155. /// A skin scene node
  156. class SkinNode: public SceneNode, public MoveComponent
  157. {
  158. public:
  159. /// @name Constructors/Destructor
  160. /// @{
  161. SkinNode(const char* skinFname,
  162. const char* name, SceneGraph* scene, // SceneNode
  163. U32 movableFlags, MoveComponent* movParent); // MoveComponent
  164. ~SkinNode();
  165. /// @}
  166. /// @name SceneNode virtuals
  167. /// @{
  168. /// Update the animation stuff
  169. void frameUpdate(F32 prevUpdateTime, F32 crntTime, int frame);
  170. /// @}
  171. /// @name MoveComponent virtuals
  172. /// @{
  173. /// Update boundingShapeWSpace from bone tails (not heads as well
  174. /// cause its faster that way). The tails come from the previous frame
  175. void moveUpdate();
  176. /// @}
  177. /// @name Accessors
  178. /// @{
  179. const SceneVector<Vec3>& getHeads() const
  180. {
  181. return heads;
  182. }
  183. const SceneVector<Vec3>& getTails() const
  184. {
  185. return tails;
  186. }
  187. const SceneVector<Mat3>& getBoneRotations() const
  188. {
  189. return boneRotations;
  190. }
  191. const SceneVector<Vec3>& getBoneTranslations() const
  192. {
  193. return boneTranslations;
  194. }
  195. const SceneVector<SkinPatchNode*>& getPatchNodes() const
  196. {
  197. return patches;
  198. }
  199. const Skin& getSkin() const
  200. {
  201. return *skin;
  202. }
  203. F32 getStep() const
  204. {
  205. return step;
  206. }
  207. F32& getStep()
  208. {
  209. return step;
  210. }
  211. void setStep(const F32 x)
  212. {
  213. step = x;
  214. }
  215. F32 getFrame() const
  216. {
  217. return frame;
  218. }
  219. F32& getFrame()
  220. {
  221. return frame;
  222. }
  223. void setFrame(const F32 x)
  224. {
  225. frame = x;
  226. }
  227. void setAnimation(const SkelAnim& anim_)
  228. {
  229. anim = &anim_;
  230. }
  231. const SkelAnim* getAnimation() const
  232. {
  233. return anim;
  234. }
  235. /// @}
  236. private:
  237. SkinResourcePointer skin; ///< The resource
  238. SceneVector<SkinPatchNode*> patches;
  239. Obb visibilityShapeWSpace;
  240. /// @name Animation stuff
  241. /// @{
  242. F32 step;
  243. F32 frame;
  244. const SkelAnim* anim; ///< The active skeleton animation
  245. /// @}
  246. /// @name Bone data
  247. /// @{
  248. SceneVector<Vec3> heads;
  249. SceneVector<Vec3> tails;
  250. SceneVector<Mat3> boneRotations;
  251. SceneVector<Vec3> boneTranslations;
  252. /// @}
  253. /// Interpolate
  254. /// @param[in] animation Animation
  255. /// @param[in] frame Frame
  256. /// @param[out] translations Translations vector
  257. /// @param[out] rotations Rotations vector
  258. static void interpolate(const SkelAnim& animation, F32 frame,
  259. SceneVector<Vec3>& translations, SceneVector<Mat3>& rotations);
  260. /// Calculate the global pose
  261. static void updateBoneTransforms(const Skeleton& skel,
  262. SceneVector<Vec3>& translations, SceneVector<Mat3>& rotations);
  263. /// Deform the heads and tails
  264. static void deformHeadsTails(const Skeleton& skeleton,
  265. const SceneVector<Vec3>& boneTranslations,
  266. const SceneVector<Mat3>& boneRotations,
  267. SceneVector<Vec3>& heads, SceneVector<Vec3>& tails);
  268. };
  269. #endif
  270. } // end namespace
  271. #endif