You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

171 lines
6.0 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #include "stdafx.h"
  35. #include "assimp_view.h"
  36. using namespace AssimpView;
  37. // ------------------------------------------------------------------------------------------------
  38. // Constructor on a given animation.
  39. AnimEvaluator::AnimEvaluator( const aiAnimation* pAnim)
  40. {
  41. mAnim = pAnim;
  42. mLastTime = 0.0;
  43. mLastPositions.resize( pAnim->mNumChannels, boost::make_tuple( 0, 0, 0));
  44. }
  45. // ------------------------------------------------------------------------------------------------
  46. // Evaluates the animation tracks for a given time stamp.
  47. void AnimEvaluator::Evaluate( double pTime)
  48. {
  49. // extract ticks per second. Assume default value if not given
  50. double ticksPerSecond = mAnim->mTicksPerSecond != 0.0 ? mAnim->mTicksPerSecond : 25.0;
  51. // every following time calculation happens in ticks
  52. pTime *= ticksPerSecond;
  53. // map into anim's duration
  54. double time = 0.0f;
  55. if( mAnim->mDuration > 0.0)
  56. time = fmod( pTime, mAnim->mDuration);
  57. if( mTransforms.size() != mAnim->mNumChannels)
  58. mTransforms.resize( mAnim->mNumChannels);
  59. // calculate the transformations for each animation channel
  60. for( unsigned int a = 0; a < mAnim->mNumChannels; a++)
  61. {
  62. const aiNodeAnim* channel = mAnim->mChannels[a];
  63. // ******** Position *****
  64. aiVector3D presentPosition( 0, 0, 0);
  65. if( channel->mNumPositionKeys > 0)
  66. {
  67. // Look for present frame number. Search from last position if time is after the last time, else from beginning
  68. // Should be much quicker than always looking from start for the average use case.
  69. unsigned int frame = (time >= mLastTime) ? mLastPositions[a].get<0>() : 0;
  70. while( frame < channel->mNumPositionKeys - 1)
  71. {
  72. if( time < channel->mPositionKeys[frame+1].mTime)
  73. break;
  74. frame++;
  75. }
  76. // interpolate between this frame's value and next frame's value
  77. unsigned int nextFrame = (frame + 1) % channel->mNumPositionKeys;
  78. const aiVectorKey& key = channel->mPositionKeys[frame];
  79. const aiVectorKey& nextKey = channel->mPositionKeys[nextFrame];
  80. double diffTime = nextKey.mTime - key.mTime;
  81. if( diffTime < 0.0)
  82. diffTime += mAnim->mDuration;
  83. if( diffTime > 0)
  84. {
  85. float factor = float( (time - key.mTime) / diffTime);
  86. presentPosition = key.mValue + (nextKey.mValue - key.mValue) * factor;
  87. } else
  88. {
  89. presentPosition = key.mValue;
  90. }
  91. mLastPositions[a].get<0>() = frame;
  92. }
  93. // ******** Rotation *********
  94. aiQuaternion presentRotation( 1, 0, 0, 0);
  95. if( channel->mNumRotationKeys > 0)
  96. {
  97. unsigned int frame = (time >= mLastTime) ? mLastPositions[a].get<1>() : 0;
  98. while( frame < channel->mNumRotationKeys - 1)
  99. {
  100. if( time < channel->mRotationKeys[frame+1].mTime)
  101. break;
  102. frame++;
  103. }
  104. // interpolate between this frame's value and next frame's value
  105. unsigned int nextFrame = (frame + 1) % channel->mNumRotationKeys;
  106. const aiQuatKey& key = channel->mRotationKeys[frame];
  107. const aiQuatKey& nextKey = channel->mRotationKeys[nextFrame];
  108. double diffTime = nextKey.mTime - key.mTime;
  109. if( diffTime < 0.0)
  110. diffTime += mAnim->mDuration;
  111. if( diffTime > 0)
  112. {
  113. float factor = float( (time - key.mTime) / diffTime);
  114. aiQuaternion::Interpolate( presentRotation, key.mValue, nextKey.mValue, factor);
  115. } else
  116. {
  117. presentRotation = key.mValue;
  118. }
  119. mLastPositions[a].get<1>() = frame;
  120. }
  121. // ******** Scaling **********
  122. aiVector3D presentScaling( 1, 1, 1);
  123. if( channel->mNumScalingKeys > 0)
  124. {
  125. unsigned int frame = (time >= mLastTime) ? mLastPositions[a].get<2>() : 0;
  126. while( frame < channel->mNumScalingKeys - 1)
  127. {
  128. if( time < channel->mScalingKeys[frame+1].mTime)
  129. break;
  130. frame++;
  131. }
  132. // TODO: (thom) interpolation maybe? This time maybe even logarithmic, not linear
  133. presentScaling = channel->mScalingKeys[frame].mValue;
  134. mLastPositions[a].get<2>() = frame;
  135. }
  136. // build a transformation matrix from it
  137. aiMatrix4x4& mat = mTransforms[a];
  138. mat = aiMatrix4x4( presentRotation.GetMatrix());
  139. mat.a1 *= presentScaling.x; mat.b1 *= presentScaling.x; mat.c1 *= presentScaling.x;
  140. mat.a2 *= presentScaling.y; mat.b2 *= presentScaling.y; mat.c2 *= presentScaling.y;
  141. mat.a3 *= presentScaling.z; mat.b3 *= presentScaling.z; mat.c3 *= presentScaling.z;
  142. mat.a4 = presentPosition.x; mat.b4 = presentPosition.y; mat.c4 = presentPosition.z;
  143. //mat.Transpose();
  144. }
  145. mLastTime = time;
  146. }