Skeleton.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Deserializer.h"
  24. #include "Log.h"
  25. #include "Serializer.h"
  26. #include "Skeleton.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. Skeleton::Skeleton() :
  31. rootBoneIndex_(M_MAX_UNSIGNED)
  32. {
  33. }
  34. Skeleton::~Skeleton()
  35. {
  36. }
  37. bool Skeleton::Load(Deserializer& source)
  38. {
  39. ClearBones();
  40. if (source.IsEof())
  41. return false;
  42. unsigned bones = source.ReadUInt();
  43. bones_.Reserve(bones);
  44. for (unsigned i = 0; i < bones; ++i)
  45. {
  46. Bone newBone;
  47. newBone.name_ = source.ReadString();
  48. newBone.nameHash_ = newBone.name_;
  49. newBone.parentIndex_ = source.ReadUInt();
  50. newBone.initialPosition_ = source.ReadVector3();
  51. newBone.initialRotation_ = source.ReadQuaternion();
  52. newBone.initialScale_ = source.ReadVector3();
  53. source.Read(&newBone.offsetMatrix_.m00_, sizeof(Matrix3x4));
  54. // Read bone collision data
  55. newBone.collisionMask_ = source.ReadUByte();
  56. if (newBone.collisionMask_ & BONECOLLISION_SPHERE)
  57. newBone.radius_ = source.ReadFloat();
  58. if (newBone.collisionMask_ & BONECOLLISION_BOX)
  59. newBone.boundingBox_ = source.ReadBoundingBox();
  60. if (newBone.parentIndex_ == i)
  61. rootBoneIndex_ = i;
  62. bones_.Push(newBone);
  63. }
  64. return true;
  65. }
  66. bool Skeleton::Save(Serializer& dest) const
  67. {
  68. if (!dest.WriteUInt(bones_.Size()))
  69. return false;
  70. for (unsigned i = 0; i < bones_.Size(); ++i)
  71. {
  72. const Bone& bone = bones_[i];
  73. dest.WriteString(bone.name_);
  74. dest.WriteUInt(bone.parentIndex_);
  75. dest.WriteVector3(bone.initialPosition_);
  76. dest.WriteQuaternion(bone.initialRotation_);
  77. dest.WriteVector3(bone.initialScale_);
  78. dest.Write(bone.offsetMatrix_.Data(), sizeof(Matrix3x4));
  79. // Collision info
  80. dest.WriteUByte(bone.collisionMask_);
  81. if (bone.collisionMask_ & BONECOLLISION_SPHERE)
  82. dest.WriteFloat(bone.radius_);
  83. if (bone.collisionMask_ & BONECOLLISION_BOX)
  84. dest.WriteBoundingBox(bone.boundingBox_);
  85. }
  86. return true;
  87. }
  88. void Skeleton::Define(const Skeleton& src)
  89. {
  90. ClearBones();
  91. bones_ = src.bones_;
  92. // Make sure we clear node references, if they exist
  93. // (AnimatedModel will create new nodes on its own)
  94. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  95. i->node_.Reset();
  96. rootBoneIndex_ = src.rootBoneIndex_;
  97. }
  98. void Skeleton::SetRootBoneIndex(unsigned index)
  99. {
  100. if (index < bones_.Size())
  101. rootBoneIndex_ = index;
  102. else
  103. LOGERROR("Root bone index out of bounds");
  104. }
  105. void Skeleton::ClearBones()
  106. {
  107. bones_.Clear();
  108. rootBoneIndex_ = M_MAX_UNSIGNED;
  109. }
  110. void Skeleton::Reset()
  111. {
  112. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  113. {
  114. if (i->animated_ && i->node_)
  115. i->node_->SetTransform(i->initialPosition_, i->initialRotation_, i->initialScale_);
  116. }
  117. }
  118. void Skeleton::ResetSilent()
  119. {
  120. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  121. {
  122. if (i->animated_ && i->node_)
  123. i->node_->SetTransformSilent(i->initialPosition_, i->initialRotation_, i->initialScale_);
  124. }
  125. }
  126. Bone* Skeleton::GetRootBone()
  127. {
  128. return GetBone(rootBoneIndex_);
  129. }
  130. Bone* Skeleton::GetBone(unsigned index)
  131. {
  132. return index < bones_.Size() ? &bones_[index] : (Bone*)0;
  133. }
  134. Bone* Skeleton::GetBone(const String& name)
  135. {
  136. return GetBone(StringHash(name));
  137. }
  138. Bone* Skeleton::GetBone(const char* name)
  139. {
  140. return GetBone(StringHash(name));
  141. }
  142. Bone* Skeleton::GetBone(StringHash nameHash)
  143. {
  144. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  145. {
  146. if (i->nameHash_ == nameHash)
  147. return &(*i);
  148. }
  149. return 0;
  150. }
  151. }