Skeleton.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // Copyright (c) 2008-2017 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 "../Graphics/Skeleton.h"
  24. #include "../IO/Log.h"
  25. #include "../DebugNew.h"
  26. namespace Atomic
  27. {
  28. Skeleton::Skeleton() :
  29. rootBoneIndex_(M_MAX_UNSIGNED)
  30. {
  31. }
  32. Skeleton::~Skeleton()
  33. {
  34. }
  35. bool Skeleton::Load(Deserializer& source)
  36. {
  37. ClearBones();
  38. if (source.IsEof())
  39. return false;
  40. unsigned bones = source.ReadUInt();
  41. bones_.Reserve(bones);
  42. for (unsigned i = 0; i < bones; ++i)
  43. {
  44. Bone newBone;
  45. newBone.name_ = source.ReadString();
  46. newBone.nameHash_ = newBone.name_;
  47. newBone.parentIndex_ = source.ReadUInt();
  48. newBone.initialPosition_ = source.ReadVector3();
  49. newBone.initialRotation_ = source.ReadQuaternion();
  50. newBone.initialScale_ = source.ReadVector3();
  51. source.Read(&newBone.offsetMatrix_.m00_, sizeof(Matrix3x4));
  52. // Read bone collision data
  53. newBone.collisionMask_ = source.ReadUByte();
  54. if (newBone.collisionMask_ & BONECOLLISION_SPHERE)
  55. newBone.radius_ = source.ReadFloat();
  56. if (newBone.collisionMask_ & BONECOLLISION_BOX)
  57. newBone.boundingBox_ = source.ReadBoundingBox();
  58. if (newBone.parentIndex_ == i)
  59. rootBoneIndex_ = i;
  60. bones_.Push(newBone);
  61. }
  62. return true;
  63. }
  64. bool Skeleton::Save(Serializer& dest) const
  65. {
  66. if (!dest.WriteUInt(bones_.Size()))
  67. return false;
  68. for (unsigned i = 0; i < bones_.Size(); ++i)
  69. {
  70. const Bone& bone = bones_[i];
  71. dest.WriteString(bone.name_);
  72. dest.WriteUInt(bone.parentIndex_);
  73. dest.WriteVector3(bone.initialPosition_);
  74. dest.WriteQuaternion(bone.initialRotation_);
  75. dest.WriteVector3(bone.initialScale_);
  76. dest.Write(bone.offsetMatrix_.Data(), sizeof(Matrix3x4));
  77. // Collision info
  78. dest.WriteUByte(bone.collisionMask_);
  79. if (bone.collisionMask_ & BONECOLLISION_SPHERE)
  80. dest.WriteFloat(bone.radius_);
  81. if (bone.collisionMask_ & BONECOLLISION_BOX)
  82. dest.WriteBoundingBox(bone.boundingBox_);
  83. }
  84. return true;
  85. }
  86. void Skeleton::Define(const Skeleton& src)
  87. {
  88. ClearBones();
  89. bones_ = src.bones_;
  90. // Make sure we clear node references, if they exist
  91. // (AnimatedModel will create new nodes on its own)
  92. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  93. i->node_.Reset();
  94. rootBoneIndex_ = src.rootBoneIndex_;
  95. }
  96. void Skeleton::SetRootBoneIndex(unsigned index)
  97. {
  98. if (index < bones_.Size())
  99. rootBoneIndex_ = index;
  100. else
  101. ATOMIC_LOGERROR("Root bone index out of bounds");
  102. }
  103. void Skeleton::ClearBones()
  104. {
  105. bones_.Clear();
  106. rootBoneIndex_ = M_MAX_UNSIGNED;
  107. }
  108. void Skeleton::Reset()
  109. {
  110. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  111. {
  112. if (i->animated_ && i->node_)
  113. i->node_->SetTransform(i->initialPosition_, i->initialRotation_, i->initialScale_);
  114. }
  115. }
  116. void Skeleton::ResetSilent()
  117. {
  118. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  119. {
  120. if (i->animated_ && i->node_)
  121. i->node_->SetTransformSilent(i->initialPosition_, i->initialRotation_, i->initialScale_);
  122. }
  123. }
  124. Bone* Skeleton::GetRootBone()
  125. {
  126. return GetBone(rootBoneIndex_);
  127. }
  128. Bone* Skeleton::GetBone(unsigned index)
  129. {
  130. return index < bones_.Size() ? &bones_[index] : (Bone*)0;
  131. }
  132. Bone* Skeleton::GetBone(const String& name)
  133. {
  134. return GetBone(StringHash(name));
  135. }
  136. Bone* Skeleton::GetBone(const char* name)
  137. {
  138. return GetBone(StringHash(name));
  139. }
  140. Bone* Skeleton::GetBone(StringHash nameHash)
  141. {
  142. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  143. {
  144. if (i->nameHash_ == nameHash)
  145. return &(*i);
  146. }
  147. return 0;
  148. }
  149. }