00001
00008 #include "SO3Skeleton.h"
00009 #include "SO3Entity.h"
00010 #include "SO3Scene.h"
00011 #include "SO3Bone.h"
00012 #include "../SO3Animation/SO3SkeletonAnimation.h"
00013
00014 namespace SO3
00015 {
00016
00017 SSkeleton::SSkeleton(SScene* parent, std::string skeletonName, SEntity* parentEntity) : SNode(parent, skeletonName, SNode::SKELETON_TYPE_ID)
00018 {
00019
00020 assert(parentEntity != 0);
00021 linkedEntity = parentEntity;
00022 rootBone = 0;
00023
00024 ogreLinkedEntity = linkedEntity->getOgreEntityPointer();
00025 O3SkeletonInstance = ogreLinkedEntity->getSkeleton();
00026 }
00027
00028 SSkeleton::SSkeleton() : SNode(0, "", SNode::SKELETON_TYPE_ID)
00029 {
00030
00031 }
00032
00033 void SSkeleton::Initialise()
00034 {
00035
00036 unsigned short numBones = O3SkeletonInstance->getNumBones();
00037 for (unsigned short i=0; i<numBones; i++)
00038 {
00039 Ogre::Bone* ogreBone = O3SkeletonInstance->getBone(i);
00040
00041 std::string tmpBoneName(linkedEntity->GetName()+"."+ogreBone->getName());
00042 SBone* newBone = currentScene->CreateBone(tmpBoneName, this, i);
00043 AddBone(newBone);
00044 }
00045
00046
00047 for (unsigned short i=0; i<numBones; i++)
00048 {
00049 SBone* newBone = GetBone(i);
00050 Ogre::Bone* ogreBone = O3SkeletonInstance->getBone(i);
00051
00052 Ogre::Bone* parentBone = static_cast<Ogre::Bone*> (ogreBone->getParent());
00053 if (parentBone == 0)
00054 {
00055 newBone->AttachToParent(this);
00056 rootBone = newBone;
00057 }
00058 else
00059 newBone->AttachToParent(GetBone(parentBone->getHandle()));
00060 }
00061
00062
00063 unsigned short numAnimations = O3SkeletonInstance->getNumAnimations();
00064 for (unsigned short i=0; i<numAnimations; i++)
00065 {
00066 Ogre::Animation* ogreAnimation = O3SkeletonInstance->getAnimation(i);
00067 SAnim* anim = new SSkeletonAnimation(GetParentScene(), ogreAnimation->getName(), this, i);
00068 AddAnimation(anim);
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 linkedEntity->_SetSkeleton(this);
00118 }
00119
00120 SSkeleton::~SSkeleton()
00121 {
00122
00123
00124
00125
00126 SBoneMap boneListCopy = boneList;
00127 SBoneMap::iterator iBone = boneListCopy.begin();
00128 while(iBone != boneListCopy.end())
00129 {
00130 RemoveBone(iBone->second);
00131 currentScene->DeleteBone(iBone->second);
00132 iBone++;
00133 }
00134
00135 O3SkeletonInstance = 0;
00136 linkedEntity = 0;
00137 }
00138
00139 void SSkeleton::SetBindingPose()
00140 {
00141 O3SkeletonInstance->setBindingPose();
00142 }
00143
00144 SSkeleton::SkeletonAnimationMode SSkeleton::GetBlendMode()
00145 {
00146 return static_cast <SkeletonAnimationMode> (O3SkeletonInstance->getBlendMode());
00147 }
00148
00149 void SSkeleton::SetBlendMode(SSkeleton::SkeletonAnimationMode newBlendMode)
00150 {
00151 O3SkeletonInstance->setBlendMode(static_cast <Ogre::SkeletonAnimationBlendMode> (static_cast <int> (newBlendMode)));
00152 }
00153
00154 void SSkeleton::AddBone(SBone* existingBone)
00155 {
00156 string boneName = existingBone->GetName();
00157 SBoneMap::iterator iBoneSearched = boneList.find(boneName);
00158 if (iBoneSearched == boneList.end())
00159 {
00160 boneList.insert(SBoneMap::value_type(boneName, existingBone));
00161 boneListByIndex.insert(SBoneIndexMap::value_type(existingBone->GetIndex(), existingBone));
00162 }
00163 else
00164 {
00165
00166 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Can not add bone named \""+ boneName +"\", an element with the same name already exist!", "SSkeleton::AddBone");
00167 }
00168 }
00169
00170 void SSkeleton::RemoveBone(SBone* existingBone)
00171 {
00172 RemoveBone(existingBone->GetName());
00173 }
00174
00175 void SSkeleton::RemoveBone(std::string boneName)
00176 {
00177 SBoneMap::iterator iBoneSearched = boneList.find(boneName);
00178 if (iBoneSearched != boneList.end())
00179 {
00180 SBone* findedBone = iBoneSearched->second;
00181 boneList.erase(iBoneSearched);
00182
00183
00184 SBoneIndexMap::iterator iBoneIndexSearched = boneListByIndex.find(findedBone->GetIndex());
00185 boneListByIndex.erase(iBoneIndexSearched);
00186 }
00187 else
00188 {
00189
00190 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Can not remove Bone named \""+ boneName +"\", element not found!", "SSkeleton::RemoveBone");
00191 }
00192 }
00193
00194 void SSkeleton::_OnBoneDeletion(SBone* boneInstance)
00195 {
00196 assert(boneInstance != 0);
00197 if(GetBone(boneInstance->GetName()) != boneInstance)
00198 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Cannot remove a bone that is not handled by this skeleton", "SSkeleton::_OnBoneDeletion");
00199
00200 RemoveBone(boneInstance);
00201 }
00202
00203 unsigned short SSkeleton::GetNumBones()
00204 {
00205 return boneList.size();
00206 }
00207
00208 SBone* SSkeleton::GetBone(std::string boneName)
00209 {
00210 SBoneMap::iterator iBoneSearched = boneList.find(boneName);
00211 if (iBoneSearched != boneList.end())
00212 return iBoneSearched->second;
00213 else
00214 return 0;
00215 }
00216
00217 SBone* SSkeleton::GetBone(unsigned short boneIndex)
00218 {
00219 SBoneIndexMap::iterator iBoneSearched = boneListByIndex.find(boneIndex);
00220 if (iBoneSearched != boneListByIndex.end())
00221 return iBoneSearched->second;
00222 else
00223 return 0;
00224 }
00225
00226 SBoneMap SSkeleton::GetBones() const
00227 {
00228 return boneList;
00229 }
00230
00231 Ogre::SkeletonInstance* SSkeleton::GetOgreSkeletonPointer()
00232 {
00233 return O3SkeletonInstance;
00234 }
00235
00236 SEntity* SSkeleton::GetParentEntity()
00237 {
00238 return linkedEntity;
00239 }
00240
00241 SBone* SSkeleton::GetRootBone()
00242 {
00243 return rootBone;
00244 }
00245
00246 Ogre::Vector3 SSkeleton::GetGlobalPosition()
00247 {
00248 return Ogre::Vector3::ZERO;
00249 }
00250
00251 Ogre::Quaternion SSkeleton::GetGlobalOrientation()
00252 {
00253 return Ogre::Quaternion::IDENTITY;
00254 }
00255
00256 Ogre::Vector3 SSkeleton::GetGlobalScale()
00257 {
00258 return Ogre::Vector3::UNIT_SCALE;
00259 }
00260
00261 void SSkeleton::SetGlobalPosition(Ogre::Vector3 pos)
00262 {
00263
00264 }
00265
00266 void SSkeleton::SetGlobalOrientation(Ogre::Quaternion quat)
00267 {
00268
00269 }
00270
00271 }