Skeleton.cpp 7.7 KB

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