00001
00007 #include "SO3Bone.h"
00008 #include "SO3Skeleton.h"
00009 #include "SO3Entity.h"
00010 #include "SO3Scene.h"
00011
00012 namespace SO3
00013 {
00014
00015 SBone::SBone(SScene* parent, std::string boneName, SSkeleton* skeleton, unsigned short id) : SNode(parent, boneName, SNode::BONE_TYPE_ID)
00016 {
00017 parentSkeleton = skeleton;
00018 ogreLinkedEntity = parentSkeleton->GetParentEntity()->getOgreEntityPointer();
00019 O3Bone = skeleton->GetOgreSkeletonPointer()->getBone(id);
00020
00021 assert(O3Bone != 0);
00022 boneIndex = id;
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 }
00048
00049 SBone::SBone() : SNode(0, "", SNode::BONE_TYPE_ID)
00050 {
00051
00052 }
00053
00054 SBone::~SBone()
00055 {
00056
00057 parentSkeleton->_OnBoneDeletion(this);
00058
00059 parentSkeleton = 0;
00060 ogreLinkedEntity = 0;
00061 O3Bone = 0;
00062 }
00063
00064 Ogre::Bone* SBone::GetOgreBonePointer()
00065 {
00066 return O3Bone;
00067 }
00068
00069 void SBone::Rotate(Ogre::Vector3 axis, float radianAngle, SNode::NodeTransformSpace relativeTo)
00070 {
00071 O3Bone->setManuallyControlled(true);
00072 O3Bone->rotate(axis, Ogre::Radian (radianAngle), static_cast <Ogre::Node::TransformSpace> (static_cast<int>(relativeTo)));
00073 O3Bone->setInitialState();
00074 O3Bone->setManuallyControlled(false);
00075
00076 }
00077
00078 void SBone::SetOrientation(Ogre::Quaternion quat)
00079 {
00080 quat.normalise();
00081
00082 O3Bone->setManuallyControlled(true);
00083 O3Bone->setOrientation(quat);
00084 O3Bone->setInitialState();
00085 O3Bone->setManuallyControlled(false);
00086 }
00087
00088 void SBone::AddOrientation(Ogre::Quaternion quat)
00089 {
00090 quat.normalise();
00091
00092 if (quat != Ogre::Quaternion::ZERO)
00093 {
00094 Ogre::Quaternion oldQuat = GetOrientation();
00095 Ogre::Quaternion resultQuat = oldQuat * quat;
00096
00097 SetOrientation(resultQuat);
00098 }
00099 }
00100
00101 void SBone::ResetToInitialOrientation()
00102 {
00103 O3Bone->setManuallyControlled(true);
00104 O3Bone->resetOrientation();
00105 O3Bone->setInitialState();
00106 O3Bone->setManuallyControlled(false);
00107 }
00108
00109 void SBone::SetPosition(Ogre::Vector3 pos)
00110 {
00111 O3Bone->setManuallyControlled(true);
00112 O3Bone->setPosition(pos);
00113 O3Bone->setInitialState();
00114 O3Bone->setManuallyControlled(false);
00115 }
00116
00117 void SBone::Translate(Ogre::Vector3 d, SNode::NodeTransformSpace relativeTo)
00118 {
00119 O3Bone->setManuallyControlled(true);
00120 O3Bone->translate(d, static_cast <Ogre::Node::TransformSpace> (static_cast<int>(relativeTo)));
00121 O3Bone->setInitialState();
00122 O3Bone->setManuallyControlled(false);
00123 }
00124
00125 void SBone::SetScale(Ogre::Vector3 scale)
00126 {
00127 O3Bone->setManuallyControlled(true);
00128 O3Bone->setScale(scale);
00129 O3Bone->setInitialState();
00130 O3Bone->setManuallyControlled(false);
00131 }
00132
00133 Ogre::Vector3 SBone::GetPosition()
00134 {
00135 return O3Bone->getPosition();
00136 }
00137
00138 Ogre::Quaternion SBone::GetOrientation()
00139 {
00140 return O3Bone->getOrientation();
00141 }
00142
00143 Ogre::Vector3 SBone::GetScale()
00144 {
00145 return O3Bone->getScale();
00146 }
00147
00148 Ogre::Vector3 SBone::GetInitialPosition()
00149 {
00150 return O3Bone->getInitialPosition();
00151 }
00152
00153 Ogre::Quaternion SBone::GetInitialOrientation()
00154 {
00155 return O3Bone->getInitialOrientation();
00156 }
00157
00158 Ogre::Vector3 SBone::GetInitialScale()
00159 {
00160 return O3Bone->getInitialScale();
00161 }
00162
00163
00164 Ogre::Vector3 SBone::GetGlobalPosition()
00165 {
00166 Ogre::SceneNode* parent = ogreLinkedEntity->getParentSceneNode();
00167 parent->_update(false, true);
00168 Ogre::Quaternion parentQuat = parent->_getDerivedOrientation();
00169 Ogre::Vector3 parentPos = parent->_getDerivedPosition();
00170 Ogre::Vector3 parentScale = parent->_getDerivedScale();
00171 O3Bone->_update(false, true);
00172 Ogre::Vector3 bonePos = O3Bone->_getDerivedPosition();
00173
00174 Ogre::Vector3 globalPosition = parentPos + (parentQuat * (bonePos * parentScale));
00175 return globalPosition;
00176 }
00177
00178 Ogre::Quaternion SBone::GetGlobalOrientation()
00179 {
00180 Ogre::SceneNode* parent = ogreLinkedEntity->getParentSceneNode();
00181 parent->_update(false, true);
00182 Ogre::Quaternion parentQuat = parent->_getDerivedOrientation();
00183 O3Bone->_update(false, true);
00184 Ogre::Quaternion boneQuat = O3Bone->_getDerivedOrientation();
00185
00186 Ogre::Quaternion globalOrientation = parentQuat * boneQuat;
00187
00188
00189
00190 return globalOrientation;
00191 }
00192
00193 Ogre::Vector3 SBone::GetGlobalScale()
00194 {
00195 Ogre::Matrix4 mat = ogreLinkedEntity->_getParentNodeFullTransform();
00196 Ogre::Vector3 col1(mat[0][0], mat[1][0], mat[2][0]);
00197 Ogre::Vector3 col2(mat[0][1], mat[1][1], mat[2][1]);
00198 Ogre::Vector3 col3(mat[0][2], mat[1][2], mat[2][2]);
00199
00200 Ogre::Vector3 sc;
00201 sc.x = col1.length();
00202 sc.y = col2.length();
00203 sc.z = col3.length();
00204
00205 return O3Bone->_getDerivedScale() * sc;
00206 }
00207
00208
00209 void SBone::SetGlobalPosition(Ogre::Vector3 pos)
00210 {
00211 Ogre::SceneNode* parent = ogreLinkedEntity->getParentSceneNode();
00212 parent->_update(false, true);
00213 Ogre::Quaternion parentQuat = parent->_getDerivedOrientation();
00214 Ogre::Vector3 parentPos = parent->_getDerivedPosition();
00215 Ogre::Vector3 parentScale = parent->_getDerivedScale();
00216
00217
00218 if (parentScale == Ogre::Vector3::ZERO)
00219 parentScale = Ogre::Vector3(FLT_MIN,FLT_MIN, FLT_MIN);
00220
00221 Ogre::Vector3 globalPosition = parentQuat.Inverse() * ((pos - parentPos) * (1.0 / parentScale));
00222
00223 O3Bone->setManuallyControlled(true);
00224
00225
00226 if (O3Bone->getParent() == 0)
00227 O3Bone->setPosition(globalPosition);
00228 else
00229 O3Bone->_setDerivedPosition(globalPosition);
00230
00231 O3Bone->setInitialState();
00232 O3Bone->setManuallyControlled(false);
00233 }
00234
00235 void SBone::SetGlobalOrientation(Ogre::Quaternion quat)
00236 {
00237 Ogre::SceneNode* parent = ogreLinkedEntity->getParentSceneNode();
00238 parent->_update(false, true);
00239 Ogre::Quaternion parentQuat = parent->_getDerivedOrientation();
00240
00241 Ogre::Quaternion globalOrientation = parentQuat.Inverse() * quat;
00242
00243 O3Bone->setManuallyControlled(true);
00244
00245
00246 if (O3Bone->getParent() == 0)
00247 O3Bone->setOrientation(globalOrientation);
00248 else
00249 O3Bone->_setDerivedOrientation(globalOrientation);
00250
00251 O3Bone->setInitialState();
00252 O3Bone->setManuallyControlled(false);
00253 }
00254
00255 void SBone::SetGlobalScale(Ogre::Vector3 scale)
00256 {
00257 Ogre::SceneNode* parent = ogreLinkedEntity->getParentSceneNode();
00258 parent->_update(false, true);
00259
00260 O3Bone->setManuallyControlled(true);
00261 if (O3Bone->getParent() == 0)
00262 O3Bone->setScale(scale / parent->_getDerivedScale());
00263 else
00264 O3Bone->setScale(scale / parent->_getDerivedScale() / O3Bone->getParent()->_getDerivedScale());
00265
00266 O3Bone->setInitialState();
00267 O3Bone->setManuallyControlled(false);
00268 }
00269
00270 Ogre::Matrix4 SBone::GetTransformationMatrix()
00271 {
00272 return O3Bone->_getFullTransform();
00273 }
00274
00275 void SBone::Pitch(float radianAngle, SNode::NodeTransformSpace relativeTo)
00276 {
00277 O3Bone->setManuallyControlled(true);
00278 O3Bone->pitch(Ogre::Radian(radianAngle), static_cast <Ogre::Node::TransformSpace> (static_cast<int>(relativeTo)));
00279 O3Bone->setInitialState();
00280 O3Bone->setManuallyControlled(false);
00281 }
00282
00283 void SBone::Yaw(float radianAngle, SNode::NodeTransformSpace relativeTo)
00284 {
00285 O3Bone->setManuallyControlled(true);
00286 O3Bone->yaw(Ogre::Radian(radianAngle), static_cast <Ogre::Node::TransformSpace> (static_cast<int>(relativeTo)));
00287 O3Bone->setInitialState();
00288 O3Bone->setManuallyControlled(false);
00289 }
00290
00291 void SBone::Roll(float radianAngle, SNode::NodeTransformSpace relativeTo)
00292 {
00293 O3Bone->setManuallyControlled(true);
00294 O3Bone->roll(Ogre::Radian(radianAngle), static_cast <Ogre::Node::TransformSpace> (static_cast<int>(relativeTo)));
00295 O3Bone->setInitialState();
00296 O3Bone->setManuallyControlled(false);
00297 }
00298
00299 int SBone::GetNumChildren()
00300 {
00301 return O3Bone->numChildren();
00302 }
00303
00304 void SBone::StoreInitialPRS()
00305 {
00306 O3Bone->setManuallyControlled(true);
00307 O3Bone->setInitialState();
00308 O3Bone->setManuallyControlled(false);
00309 }
00310
00311 void SBone::ResetToInitialPRS()
00312 {
00313 O3Bone->setManuallyControlled(true);
00314 O3Bone->resetToInitialState();
00315 O3Bone->setManuallyControlled(false);
00316 }
00317
00318 unsigned short SBone::GetIndex()
00319 {
00320 return boneIndex;
00321 }
00322
00323 void SBone::AttachToBone(SNode* newChild)
00324 {
00325 Ogre::MovableObject* ogreObject = newChild->GetMovableObjectPointer();
00326 if (ogreObject != 0)
00327 {
00328
00329
00330 #if OGRE_VERSION < ((1 << 16) | (7 << 8) | 0)
00331 ogreObject->detatchFromParent();
00332 #else
00333 ogreObject->detachFromParent();
00334 #endif
00335
00336
00337 ogreLinkedEntity->attachObjectToBone(O3Bone->getName(), ogreObject, O3SceneNode->getOrientation(), O3SceneNode->getPosition());
00338 }
00339 }
00340
00341 void SBone::DetachFromBone(SNode* existingChild)
00342 {
00343 Ogre::MovableObject* ogreObject = existingChild->GetMovableObjectPointer();
00344
00345 if (ogreObject != 0)
00346 {
00347 bool isBoneAttached = false;
00348
00349
00350 #if OGRE_VERSION < ((1 << 16) | (7 << 8) | 0)
00351 if(ogreObject->isAttached())
00352 {
00353 Ogre::Node* np = ogreObject->getParentNode();
00354 Ogre::SceneNode* snp = ogreObject->getParentSceneNode();
00355
00356 if(snp!=np)
00357 isBoneAttached = true;
00358 }
00359 #else
00360 isBoneAttached = ogreObject->isParentTagPoint();
00361 #endif
00362
00363
00364 if (isBoneAttached)
00365 {
00366
00367 ogreLinkedEntity->detachObjectFromBone(ogreObject);
00368
00369
00370 existingChild->GetOgreSceneNodePointer()->attachObject(ogreObject);
00371 }
00372 }
00373 }
00374
00375 Ogre::Quaternion SBone::ConvertWorldToLocalOrientation(Ogre::Quaternion quat)
00376 {
00377 return O3Bone->convertWorldToLocalOrientation(quat);
00378 }
00379
00380 void SBone::SetInheritOrientation(bool state)
00381 {
00382 O3Bone->setInheritOrientation(state);
00383 }
00384
00385 bool SBone::GetInheritOrientation()
00386 {
00387 return O3Bone->getInheritOrientation();
00388 }
00389
00390 void SBone::ResetOrientation()
00391 {
00392 O3Bone->resetOrientation();
00393 }
00394
00395 }