Animation.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Animation.h"
  25. #include "Deserializer.h"
  26. #include "Log.h"
  27. #include "Serializer.h"
  28. #include "DebugNew.h"
  29. void AnimationTrack::getKeyFrameIndex(float time, unsigned& index) const
  30. {
  31. if (time < 0.0f)
  32. time = 0.0f;
  33. if (index >= mKeyFrames.size())
  34. index = mKeyFrames.size() - 1;
  35. // Check for being too far ahead
  36. while ((index) && (time < mKeyFrames[index].mTime))
  37. index--;
  38. // Check for being too far behind
  39. while ((index < mKeyFrames.size() - 1) && (time >= mKeyFrames[index + 1].mTime))
  40. index++;
  41. }
  42. Animation::Animation(const std::string& name) :
  43. Resource(name)
  44. {
  45. }
  46. Animation::~Animation()
  47. {
  48. }
  49. void Animation::load(Deserializer& source, ResourceCache* cache)
  50. {
  51. unsigned memoryUse = 0;
  52. mAnimationName = source.readString();
  53. mAnimationNameHash = StringHash(mAnimationName);
  54. mLength = source.readFloat();
  55. mTracks.clear();
  56. unsigned tracks = source.readUInt();
  57. mTracks.resize(tracks);
  58. memoryUse += tracks * sizeof(AnimationTrack);
  59. // Read tracks
  60. for (unsigned i = 0; i < tracks; ++i)
  61. {
  62. AnimationTrack& newTrack = mTracks[i];
  63. newTrack.mName = source.readString();
  64. newTrack.mNameHash = StringHash(newTrack.mName);
  65. newTrack.mChannelMask = source.readUByte();
  66. unsigned keyFrames = source.readUInt();
  67. newTrack.mKeyFrames.resize(keyFrames);
  68. memoryUse += keyFrames * sizeof(AnimationKeyFrame);
  69. // Read keyframes of the track
  70. for (unsigned j = 0; j < keyFrames; ++j)
  71. {
  72. AnimationKeyFrame& newKeyFrame = newTrack.mKeyFrames[j];
  73. newKeyFrame.mTime = source.readFloat();
  74. if (newTrack.mChannelMask & CHANNEL_POSITION)
  75. newKeyFrame.mPosition = source.readVector3();
  76. if (newTrack.mChannelMask & CHANNEL_ROTATION)
  77. newKeyFrame.mRotation = source.readQuaternion();
  78. if (newTrack.mChannelMask & CHANNEL_SCALE)
  79. newKeyFrame.mScale = source.readVector3();
  80. }
  81. }
  82. setMemoryUse(memoryUse);
  83. }
  84. void Animation::save(Serializer& dest)
  85. {
  86. dest.writeString(mAnimationName);
  87. dest.writeFloat(mLength);
  88. // Write tracks
  89. dest.writeUInt(mTracks.size());
  90. for (unsigned i = 0; i < mTracks.size(); ++i)
  91. {
  92. const AnimationTrack& track = mTracks[i];
  93. dest.writeString(track.mName);
  94. dest.writeUByte(track.mChannelMask);
  95. dest.writeUInt(track.mKeyFrames.size());
  96. // Write keyframes of the track
  97. for (unsigned j = 0; j < track.mKeyFrames.size(); ++j)
  98. {
  99. const AnimationKeyFrame& keyFrame = track.mKeyFrames[j];
  100. dest.writeFloat(keyFrame.mTime);
  101. if (track.mChannelMask & CHANNEL_POSITION)
  102. dest.writeVector3(keyFrame.mPosition);
  103. if (track.mChannelMask & CHANNEL_ROTATION)
  104. dest.writeQuaternion(keyFrame.mRotation);
  105. if (track.mChannelMask & CHANNEL_SCALE)
  106. dest.writeVector3(keyFrame.mScale);
  107. }
  108. }
  109. }
  110. void Animation::setAnimationName(const std::string& name)
  111. {
  112. mAnimationName = name;
  113. mAnimationNameHash = StringHash(name);
  114. }
  115. void Animation::setLength(float length)
  116. {
  117. mLength = max(length, 0.0f);
  118. }
  119. void Animation::setTracks(const std::vector<AnimationTrack>& tracks)
  120. {
  121. mTracks = tracks;
  122. }
  123. unsigned Animation::getNumTracks() const
  124. {
  125. return mTracks.size();
  126. }
  127. const AnimationTrack* Animation::getTrack(unsigned index) const
  128. {
  129. return index < mTracks.size() ? &mTracks[index] : 0;
  130. }
  131. const AnimationTrack* Animation::getTrack(const std::string& name) const
  132. {
  133. for (std::vector<AnimationTrack>::const_iterator i = mTracks.begin(); i != mTracks.end(); ++i)
  134. {
  135. if ((*i).mName == name)
  136. return &(*i);
  137. }
  138. return 0;
  139. }
  140. const AnimationTrack* Animation::getTrack(StringHash nameHash) const
  141. {
  142. for (std::vector<AnimationTrack>::const_iterator i = mTracks.begin(); i != mTracks.end(); ++i)
  143. {
  144. if ((*i).mNameHash == nameHash)
  145. return &(*i);
  146. }
  147. return 0;
  148. }