Animation.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "Context.h"
  26. #include "Deserializer.h"
  27. #include "Log.h"
  28. #include "Profiler.h"
  29. #include "Serializer.h"
  30. #include "DebugNew.h"
  31. void AnimationTrack::GetKeyFrameIndex(float time, unsigned& index) const
  32. {
  33. if (time < 0.0f)
  34. time = 0.0f;
  35. if (index >= keyFrames_.Size())
  36. index = keyFrames_.Size() - 1;
  37. // Check for being too far ahead
  38. while (index && time < keyFrames_[index].time_)
  39. index--;
  40. // Check for being too far behind
  41. while (index < keyFrames_.Size() - 1 && time >= keyFrames_[index + 1].time_)
  42. index++;
  43. }
  44. OBJECTTYPESTATIC(Animation);
  45. Animation::Animation(Context* context) :
  46. Resource(context)
  47. {
  48. }
  49. Animation::~Animation()
  50. {
  51. }
  52. void Animation::RegisterObject(Context* context)
  53. {
  54. context->RegisterFactory<Animation>();
  55. }
  56. bool Animation::Load(Deserializer& source)
  57. {
  58. PROFILE(LoadAnimation);
  59. unsigned memoryUse = sizeof(Animation);
  60. // Check ID
  61. if (source.ReadFileID() != "UANI")
  62. {
  63. LOGERROR(source.GetName() + " is not a valid animation file");
  64. return false;
  65. }
  66. // Read name and length
  67. animationName_ = source.ReadString();
  68. animationNameHash_ = StringHash(animationName_);
  69. length_ = source.ReadFloat();
  70. tracks_.Clear();
  71. unsigned tracks = source.ReadUInt();
  72. tracks_.Resize(tracks);
  73. memoryUse += tracks * sizeof(AnimationTrack);
  74. // Read tracks
  75. for (unsigned i = 0; i < tracks; ++i)
  76. {
  77. AnimationTrack& newTrack = tracks_[i];
  78. newTrack.name_ = source.ReadString();
  79. newTrack.nameHash_ = StringHash(newTrack.name_);
  80. newTrack.channelMask_ = source.ReadUByte();
  81. unsigned keyFrames = source.ReadUInt();
  82. newTrack.keyFrames_.Resize(keyFrames);
  83. memoryUse += keyFrames * sizeof(AnimationKeyFrame);
  84. // Read keyframes of the track
  85. for (unsigned j = 0; j < keyFrames; ++j)
  86. {
  87. AnimationKeyFrame& newKeyFrame = newTrack.keyFrames_[j];
  88. newKeyFrame.time_ = source.ReadFloat();
  89. if (newTrack.channelMask_ & CHANNEL_POSITION)
  90. newKeyFrame.position_ = source.ReadVector3();
  91. if (newTrack.channelMask_ & CHANNEL_ROTATION)
  92. newKeyFrame.rotation_ = source.ReadQuaternion();
  93. if (newTrack.channelMask_ & CHANNEL_SCALE)
  94. newKeyFrame.scale_ = source.ReadVector3();
  95. }
  96. }
  97. SetMemoryUse(memoryUse);
  98. return true;
  99. }
  100. bool Animation::Save(Serializer& dest)
  101. {
  102. // Write ID, name and length
  103. dest.WriteFileID("UANI");
  104. dest.WriteString(animationName_);
  105. dest.WriteFloat(length_);
  106. // Write tracks
  107. dest.WriteUInt(tracks_.Size());
  108. for (unsigned i = 0; i < tracks_.Size(); ++i)
  109. {
  110. const AnimationTrack& track = tracks_[i];
  111. dest.WriteString(track.name_);
  112. dest.WriteUByte(track.channelMask_);
  113. dest.WriteUInt(track.keyFrames_.Size());
  114. // Write keyframes of the track
  115. for (unsigned j = 0; j < track.keyFrames_.Size(); ++j)
  116. {
  117. const AnimationKeyFrame& keyFrame = track.keyFrames_[j];
  118. dest.WriteFloat(keyFrame.time_);
  119. if (track.channelMask_ & CHANNEL_POSITION)
  120. dest.WriteVector3(keyFrame.position_);
  121. if (track.channelMask_ & CHANNEL_ROTATION)
  122. dest.WriteQuaternion(keyFrame.rotation_);
  123. if (track.channelMask_ & CHANNEL_SCALE)
  124. dest.WriteVector3(keyFrame.scale_);
  125. }
  126. }
  127. return true;
  128. }
  129. void Animation::SetAnimationName(const String& name)
  130. {
  131. animationName_ = name;
  132. animationNameHash_ = StringHash(name);
  133. }
  134. void Animation::SetLength(float length)
  135. {
  136. length_ = Max(length, 0.0f);
  137. }
  138. void Animation::SetTracks(const Vector<AnimationTrack>& tracks)
  139. {
  140. tracks_ = tracks;
  141. }
  142. unsigned Animation::GetNumTracks() const
  143. {
  144. return tracks_.Size();
  145. }
  146. const AnimationTrack* Animation::GetTrack(unsigned index) const
  147. {
  148. return index < tracks_.Size() ? &tracks_[index] : 0;
  149. }
  150. const AnimationTrack* Animation::GetTrack(const String& name) const
  151. {
  152. for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  153. {
  154. if (i->name_ == name)
  155. return &(*i);
  156. }
  157. return 0;
  158. }
  159. const AnimationTrack* Animation::GetTrack(StringHash nameHash) const
  160. {
  161. for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  162. {
  163. if (i->nameHash_ == nameHash)
  164. return &(*i);
  165. }
  166. return 0;
  167. }