Skeleton.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "Profiler.h"
  26. #include "Serializer.h"
  27. #include "Skeleton.h"
  28. #include "DebugNew.h"
  29. Skeleton::Skeleton() :
  30. mRootBone(0)
  31. {
  32. }
  33. Skeleton::~Skeleton()
  34. {
  35. clearBones();
  36. }
  37. void Skeleton::load(Deserializer& source)
  38. {
  39. PROFILE(Skeleton_Load);
  40. clearBones();
  41. unsigned bones = source.readUInt();
  42. std::vector<unsigned> boneParents;
  43. // First pass: read bone data, identify root bone
  44. for (unsigned i = 0; i < bones; ++i)
  45. {
  46. std::string name = source.readString();
  47. unsigned parentIndex = source.readUInt();
  48. Vector3 bindPosition = source.readVector3();
  49. Quaternion bindRotation = source.readQuaternion();
  50. Vector3 bindScale = source.readVector3();
  51. SharedPtr<Bone> newBone(new Bone(0, name));
  52. newBone->setBindTransform(bindPosition, bindRotation, bindScale);
  53. newBone->setInitialTransform(bindPosition, bindRotation, bindScale);
  54. newBone->reset();
  55. // Read bone collision data
  56. unsigned char collisionMask = source.readUByte();
  57. if (collisionMask & BONECOLLISION_SPHERE)
  58. newBone->setRadius(source.readFloat());
  59. if (collisionMask & BONECOLLISION_BOX)
  60. newBone->setBoundingBox(source.readBoundingBox());
  61. if (parentIndex == i)
  62. mRootBone = newBone;
  63. mBones.push_back(newBone);
  64. boneParents.push_back(parentIndex);
  65. }
  66. // Second pass: map parent bones & root bone
  67. for (unsigned i = 0; i < bones; ++i)
  68. {
  69. mBones[i]->setRootBone(mRootBone);
  70. unsigned parentBoneIndex = boneParents[i];
  71. if (parentBoneIndex != i)
  72. {
  73. if (parentBoneIndex >= mBones.size())
  74. EXCEPTION("Illegal parent bone assignment");
  75. mBones[parentBoneIndex]->addChild(mBones[i]);
  76. }
  77. }
  78. }
  79. void Skeleton::save(Serializer& dest)
  80. {
  81. dest.writeUInt(mBones.size());
  82. for (unsigned i = 0; i < mBones.size(); ++i)
  83. {
  84. Bone* bone = mBones[i];
  85. // Bone name
  86. dest.writeString(bone->getName());
  87. // Parent index, same as own if root bone
  88. unsigned parentIndex = getBoneIndex(dynamic_cast<Bone*>(bone->getParent()));
  89. if (parentIndex == M_MAX_UNSIGNED)
  90. parentIndex = i;
  91. dest.writeUInt(parentIndex);
  92. // Bind transform
  93. dest.writeVector3(bone->getBindPosition());
  94. dest.writeQuaternion(bone->getBindRotation());
  95. dest.writeVector3(bone->getBindScale());
  96. // Collision info
  97. unsigned char collisionMask = bone->getCollisionMask();
  98. dest.writeUByte(collisionMask);
  99. if (collisionMask & BONECOLLISION_SPHERE)
  100. dest.writeFloat(bone->getRadius());
  101. if (collisionMask & BONECOLLISION_BOX)
  102. dest.writeBoundingBox(bone->getBoundingBox());
  103. }
  104. }
  105. void Skeleton::define(const std::vector<SharedPtr<Bone > >& srcBones)
  106. {
  107. clearBones();
  108. if (!srcBones.size())
  109. return;
  110. std::map<Bone*, unsigned> srcBoneIndices;
  111. Bone* srcRootBone = 0;
  112. // First pass: copy the bones
  113. for (unsigned i = 0; i < srcBones.size(); ++i)
  114. {
  115. srcBoneIndices[srcBones[i]] = i;
  116. SharedPtr<Bone> newBone(new Bone(0, srcBones[i]->getName()));
  117. newBone->setBindTransform(srcBones[i]->getBindPosition(), srcBones[i]->getBindRotation(),
  118. srcBones[i]->getBindScale());
  119. newBone->setInitialTransform(srcBones[i]->getInitialPosition(), srcBones[i]->getInitialRotation(),
  120. srcBones[i]->getInitialScale());
  121. if (srcBones[i]->getCollisionMask() & BONECOLLISION_SPHERE)
  122. newBone->setRadius(srcBones[i]->getRadius());
  123. if (srcBones[i]->getCollisionMask() & BONECOLLISION_BOX)
  124. newBone->setBoundingBox(srcBones[i]->getBoundingBox());
  125. newBone->reset();
  126. Bone* srcParentBone = static_cast<Bone*>(srcBones[i]->getParent());
  127. // If parent bone is none of the listed bones, treat it as a root bone
  128. if ((!srcParentBone) || (srcBoneIndices.find(srcParentBone) == srcBoneIndices.end()))
  129. srcRootBone = srcBones[i];
  130. mBones.push_back(newBone);
  131. }
  132. // Second pass: copy the hierarchy
  133. unsigned rootBoneIndex = srcBoneIndices[srcRootBone];
  134. mRootBone = mBones[rootBoneIndex];
  135. for (unsigned i = 0; i < mBones.size(); ++i)
  136. {
  137. mBones[i]->setRootBone(mRootBone);
  138. Bone* srcParentBone = static_cast<Bone*>(srcBones[i]->getParent());
  139. if ((srcParentBone) && (srcBoneIndices.find(srcParentBone) != srcBoneIndices.end()))
  140. {
  141. unsigned parentBoneIndex = srcBoneIndices[srcParentBone];
  142. mBones[parentBoneIndex]->addChild(mBones[i]);
  143. }
  144. }
  145. }
  146. void Skeleton::reset(bool force)
  147. {
  148. // Start with resetting the root bone so that node dirtying is done most efficiently
  149. if (mRootBone)
  150. mRootBone->reset(force);
  151. // Then reset the rest of the bones
  152. for (std::vector<SharedPtr<Bone> >::iterator i = mBones.begin(); i != mBones.end(); ++i)
  153. {
  154. if ((*i) != mRootBone)
  155. (*i)->reset(force);
  156. }
  157. }
  158. Bone* Skeleton::getBone(unsigned index) const
  159. {
  160. if (index >= mBones.size())
  161. return 0;
  162. return mBones[index];
  163. }
  164. Bone* Skeleton::getBone(const std::string& name) const
  165. {
  166. for (std::vector<SharedPtr<Bone> >::const_iterator i = mBones.begin(); i != mBones.end(); ++i)
  167. {
  168. if ((*i)->getName() == name)
  169. return *i;
  170. }
  171. return 0;
  172. }
  173. Bone* Skeleton::getBone(StringHash nameHash) const
  174. {
  175. for (std::vector<SharedPtr<Bone> >::const_iterator i = mBones.begin(); i != mBones.end(); ++i)
  176. {
  177. if ((*i)->getNameHash() == nameHash)
  178. return *i;
  179. }
  180. return 0;
  181. }
  182. unsigned Skeleton::getBoneIndex(Bone* bone) const
  183. {
  184. for (unsigned i = 0; i < mBones.size(); ++i)
  185. {
  186. if (mBones[i] == bone)
  187. return i;
  188. }
  189. return M_MAX_UNSIGNED;
  190. }
  191. void Skeleton::clearBones()
  192. {
  193. if ((mRootBone) && (mRootBone->getParent()))
  194. mRootBone->getParent()->removeChild(mRootBone);
  195. mBones.clear();
  196. mRootBone.reset();
  197. }