home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 22 Quaternions / QuatDemo / AnimationHelper.cpp next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  2.5 KB  |  83 lines

  1. //***************************************************************************************
  2. // AnimationHelper.cpp by Frank Luna (C) 2011 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. #include "AnimationHelper.h"
  6.  
  7. using namespace DirectX;
  8.  
  9. Keyframe::Keyframe()
  10.     : TimePos(0.0f),
  11.     Translation(0.0f, 0.0f, 0.0f),
  12.     Scale(1.0f, 1.0f, 1.0f),
  13.     RotationQuat(0.0f, 0.0f, 0.0f, 1.0f)
  14. {
  15. }
  16.  
  17. Keyframe::~Keyframe()
  18. {
  19. }
  20.  
  21. float BoneAnimation::GetStartTime()const
  22. {
  23.     // Keyframes are sorted by time, so first keyframe gives start time.
  24.     return Keyframes.front().TimePos;
  25. }
  26.  
  27. float BoneAnimation::GetEndTime()const
  28. {
  29.     // Keyframes are sorted by time, so last keyframe gives end time.
  30.     float f = Keyframes.back().TimePos;
  31.  
  32.     return f;
  33. }
  34.  
  35. void BoneAnimation::Interpolate(float t, XMFLOAT4X4& M)const
  36. {
  37.     if( t <= Keyframes.front().TimePos )
  38.     {
  39.         XMVECTOR S = XMLoadFloat3(&Keyframes.front().Scale);
  40.         XMVECTOR P = XMLoadFloat3(&Keyframes.front().Translation);
  41.         XMVECTOR Q = XMLoadFloat4(&Keyframes.front().RotationQuat);
  42.  
  43.         XMVECTOR zero = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
  44.         XMStoreFloat4x4(&M, XMMatrixAffineTransformation(S, zero, Q, P));
  45.     }
  46.     else if( t >= Keyframes.back().TimePos )
  47.     {
  48.         XMVECTOR S = XMLoadFloat3(&Keyframes.back().Scale);
  49.         XMVECTOR P = XMLoadFloat3(&Keyframes.back().Translation);
  50.         XMVECTOR Q = XMLoadFloat4(&Keyframes.back().RotationQuat);
  51.  
  52.         XMVECTOR zero = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
  53.         XMStoreFloat4x4(&M, XMMatrixAffineTransformation(S, zero, Q, P));
  54.     }
  55.     else
  56.     {
  57.         for(UINT i = 0; i < Keyframes.size()-1; ++i)
  58.         {
  59.             if( t >= Keyframes[i].TimePos && t <= Keyframes[i+1].TimePos )
  60.             {
  61.                 float lerpPercent = (t - Keyframes[i].TimePos) / (Keyframes[i+1].TimePos - Keyframes[i].TimePos);
  62.  
  63.                 XMVECTOR s0 = XMLoadFloat3(&Keyframes[i].Scale);
  64.                 XMVECTOR s1 = XMLoadFloat3(&Keyframes[i+1].Scale);
  65.  
  66.                 XMVECTOR p0 = XMLoadFloat3(&Keyframes[i].Translation);
  67.                 XMVECTOR p1 = XMLoadFloat3(&Keyframes[i+1].Translation);
  68.  
  69.                 XMVECTOR q0 = XMLoadFloat4(&Keyframes[i].RotationQuat);
  70.                 XMVECTOR q1 = XMLoadFloat4(&Keyframes[i+1].RotationQuat);
  71.  
  72.                 XMVECTOR S = XMVectorLerp(s0, s1, lerpPercent);
  73.                 XMVECTOR P = XMVectorLerp(p0, p1, lerpPercent);
  74.                 XMVECTOR Q = XMQuaternionSlerp(q0, q1, lerpPercent);
  75.  
  76.                 XMVECTOR zero = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
  77.                 XMStoreFloat4x4(&M, XMMatrixAffineTransformation(S, zero, Q, P));
  78.  
  79.                 break;
  80.             }
  81.         }
  82.     }
  83. }