Skeleton.cpp 4.6 KB

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