Skeleton.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "Deserializer.h"
  25. #include "Serializer.h"
  26. #include "Skeleton.h"
  27. #include "DebugNew.h"
  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. for (unsigned i = 0; i < bones; ++i)
  42. {
  43. Bone newBone;
  44. newBone.name_ = source.ReadString();
  45. newBone.nameHash_ = StringHash(newBone.name_);
  46. newBone.parentIndex_ = source.ReadUInt();
  47. newBone.initialPosition_ = source.ReadVector3();
  48. newBone.initialRotation_ = source.ReadQuaternion();
  49. newBone.initialScale_ = source.ReadVector3();
  50. source.Read(&newBone.offsetMatrix_.m00_, sizeof(Matrix4x3));
  51. // Read bone collision data
  52. newBone.collisionMask_ = source.ReadUByte();
  53. if (newBone.collisionMask_ & BONECOLLISION_SPHERE)
  54. newBone.radius_ = source.ReadFloat();
  55. if (newBone.collisionMask_ & BONECOLLISION_BOX)
  56. newBone.boundingBox_ = source.ReadBoundingBox();
  57. if (newBone.parentIndex_ == i)
  58. rootBoneIndex_ = i;
  59. bones_.Push(newBone);
  60. }
  61. return true;
  62. }
  63. bool Skeleton::Save(Serializer& dest)
  64. {
  65. if (!dest.WriteUInt(bones_.Size()))
  66. return false;
  67. for (unsigned i = 0; i < bones_.Size(); ++i)
  68. {
  69. const Bone& bone = bones_[i];
  70. dest.WriteString(bone.name_);
  71. dest.WriteUInt(bone.parentIndex_);
  72. dest.WriteVector3(bone.initialPosition_);
  73. dest.WriteQuaternion(bone.initialRotation_);
  74. dest.WriteVector3(bone.initialScale_);
  75. dest.Write(bone.offsetMatrix_.GetData(), sizeof(Matrix4x3));
  76. // Collision info
  77. dest.WriteUByte(bone.collisionMask_);
  78. if (bone.collisionMask_ & BONECOLLISION_SPHERE)
  79. dest.WriteFloat(bone.radius_);
  80. if (bone.collisionMask_ & BONECOLLISION_BOX)
  81. dest.WriteBoundingBox(bone.boundingBox_);
  82. }
  83. return true;
  84. }
  85. void Skeleton::Define(const Skeleton& src)
  86. {
  87. ClearBones();
  88. bones_ = src.GetBones();
  89. // Make sure we clear node references, if they exist
  90. // (AnimatedModel will create new nodes on its own)
  91. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  92. i->node_.Reset();
  93. rootBoneIndex_ = src.rootBoneIndex_;
  94. }
  95. void Skeleton::ClearBones()
  96. {
  97. bones_.Clear();
  98. rootBoneIndex_ = M_MAX_UNSIGNED;
  99. }
  100. void Skeleton::Reset()
  101. {
  102. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  103. {
  104. if ((i->animated_) && (i->node_))
  105. i->node_->SetTransform(i->initialPosition_, i->initialRotation_, i->initialScale_);
  106. }
  107. }
  108. Bone* Skeleton::GetRootBone()
  109. {
  110. return GetBone(rootBoneIndex_);
  111. }
  112. Bone* Skeleton::GetBone(unsigned index)
  113. {
  114. return index < bones_.Size() ? &bones_[index] : (Bone*)0;
  115. }
  116. Bone* Skeleton::GetBone(const String& name)
  117. {
  118. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  119. {
  120. if (i->name_ == name)
  121. return &(*i);
  122. }
  123. return 0;
  124. }
  125. Bone* Skeleton::GetBone(StringHash nameHash)
  126. {
  127. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  128. {
  129. if (i->nameHash_ == nameHash)
  130. return &(*i);
  131. }
  132. return 0;
  133. }