2
0

SkelAnimCtrl.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "SkelAnimCtrl.h"
  2. #include "SkelAnim.h"
  3. #include "SkelNode.h"
  4. #include "Skeleton.h"
  5. #include "Renderer.h"
  6. //=====================================================================================================================================
  7. // SkelAnimCtrl =
  8. //=====================================================================================================================================
  9. SkelAnimCtrl::SkelAnimCtrl( SkelNode* skelNode_ ):
  10. Controller(CT_SKEL_ANIM),
  11. skelNode( skelNode_ )
  12. {
  13. heads.resize( skelNode->skeleton->bones.size() );
  14. tails.resize( skelNode->skeleton->bones.size() );
  15. boneRotations.resize( skelNode->skeleton->bones.size() );
  16. boneTranslations.resize( skelNode->skeleton->bones.size() );
  17. }
  18. //=====================================================================================================================================
  19. // interpolate =
  20. //=====================================================================================================================================
  21. void SkelAnimCtrl::interpolate( SkelAnim* animation, float frame )
  22. {
  23. DEBUG_ERR( frame >= animation->framesNum );
  24. // calculate the t (used in slerp and lerp) using the keyframs and the frame and
  25. // calc the lPose and rPose witch indicate the pose ids in witch the frame lies between
  26. const Vec<uint>& keyframes = animation->keyframes;
  27. float t = 0.0;
  28. uint lPose = 0, rPose = 0;
  29. for( uint j=0; j<keyframes.size(); j++ )
  30. {
  31. if( (float)keyframes[j] == frame )
  32. {
  33. lPose = rPose = j;
  34. t = 0.0;
  35. break;
  36. }
  37. else if( (float)keyframes[j] > frame )
  38. {
  39. lPose = j-1;
  40. rPose = j;
  41. t = ( frame - (float)keyframes[lPose] ) / float( keyframes[rPose] - keyframes[lPose] );
  42. break;
  43. }
  44. }
  45. // now for all bones update bone's poses
  46. DEBUG_ERR( boneRotations.size()<1 );
  47. for( uint i=0; i<boneRotations.size(); i++ )
  48. {
  49. const SkelAnim::BoneAnim& banim = animation->bones[i];
  50. Mat3& localRot = boneRotations[i];
  51. Vec3& localTransl = boneTranslations[i];
  52. // if the bone has animations then slerp and lerp to find the rotation and translation
  53. if( banim.keyframes.size() != 0 )
  54. {
  55. const SkelAnim::BonePose& lBpose = banim.keyframes[lPose];
  56. const SkelAnim::BonePose& rBpose = banim.keyframes[rPose];
  57. // rotation
  58. const Quat& q0 = lBpose.rotation;
  59. const Quat& q1 = rBpose.rotation;
  60. localRot = Mat3( q0.slerp(q1, t) );
  61. // translation
  62. const Vec3& v0 = lBpose.translation;
  63. const Vec3& v1 = rBpose.translation;
  64. localTransl = v0.lerp( v1, t );
  65. }
  66. // else put the idents
  67. else
  68. {
  69. localRot = Mat3::getIdentity();
  70. localTransl = Vec3( 0.0, 0.0, 0.0 );
  71. }
  72. }
  73. }
  74. //=====================================================================================================================================
  75. // updateBoneTransforms =
  76. //=====================================================================================================================================
  77. void SkelAnimCtrl::updateBoneTransforms()
  78. {
  79. uint queue[ 128 ];
  80. uint head = 0, tail = 0;
  81. // put the roots
  82. for( uint i=0; i<skelNode->skeleton->bones.size(); i++ )
  83. if( skelNode->skeleton->bones[i].parent == NULL )
  84. queue[tail++] = i; // queue push
  85. // loop
  86. while( head != tail ) // while queue not empty
  87. {
  88. uint boneId = queue[head++]; // queue pop
  89. const Skeleton::Bone& boned = skelNode->skeleton->bones[boneId];
  90. // bone.final_transform = MA * ANIM * MAi
  91. // where MA is bone matrix at armature space and ANIM the interpolated transformation.
  92. combineTransformations( boneTranslations[boneId], boneRotations[boneId],
  93. boned.tslSkelSpaceInv, boned.rotSkelSpaceInv,
  94. boneTranslations[boneId], boneRotations[boneId] );
  95. combineTransformations( boned.tslSkelSpace, boned.rotSkelSpace,
  96. boneTranslations[boneId], boneRotations[boneId],
  97. boneTranslations[boneId], boneRotations[boneId] );
  98. // and finaly add the parent's transform
  99. if( boned.parent )
  100. {
  101. // bone.final_final_transform = parent.transf * bone.final_transform
  102. combineTransformations( boneTranslations[boned.parent->id], boneRotations[boned.parent->id],
  103. boneTranslations[boneId], boneRotations[boneId],
  104. boneTranslations[boneId], boneRotations[boneId] );
  105. }
  106. // now add the bone's childes
  107. for( uint i=0; i<boned.childsNum; i++ )
  108. queue[tail++] = boned.childs[i]->id;
  109. }
  110. }
  111. //=====================================================================================================================================
  112. // deform =
  113. //=====================================================================================================================================
  114. void SkelAnimCtrl::deform()
  115. {
  116. Skeleton* skeleton = skelNode->skeleton;
  117. for( uint i=0; i<skeleton->bones.size(); i++ )
  118. {
  119. const Mat3& rot = boneRotations[ i ];
  120. const Vec3& transl = boneTranslations[ i ];
  121. heads[i] = skeleton->bones[i].head.getTransformed( transl, rot );
  122. tails[i] = skeleton->bones[i].tail.getTransformed( transl, rot );
  123. }
  124. }
  125. //=====================================================================================================================================
  126. // update =
  127. //=====================================================================================================================================
  128. void SkelAnimCtrl::update( float )
  129. {
  130. frame += step;
  131. if( frame > skelAnim->framesNum ) // if the crnt is finished then play the next or loop the crnt
  132. {
  133. frame = 0.0;
  134. }
  135. interpolate( skelAnim, frame );
  136. updateBoneTransforms();
  137. if( R::Dbg::showSkeletons )
  138. {
  139. deform();
  140. }
  141. }