Project

General

Profile

SO3Engine
SO3Skeleton.cpp
Go to the documentation of this file.
1
13
14namespace SO3
15{
16
17SSkeleton::SSkeleton(SScene* parent, const std::string& skeletonName, SEntity* parentEntity) : SNode(parent, skeletonName, SNode::SKELETON_TYPE_ID)
18{
19 assert(parentEntity != 0);
20 linkedEntity = parentEntity;
21
23
24 // debug skeletons
25 //parentEntity->getOgreEntityPointer()->setDebugDisplayEnabled(true);
26 //parentEntity->getOgreEntityPointer()->setDisplaySkeleton(true);
27}
28
29SSkeleton::SSkeleton() : SNode(0, "", SNode::SKELETON_TYPE_ID)
30{
31 // Forbiden (private)
32}
33
35{
36 // Create bones
37 Ogre::SkeletonInstance* O3SkeletonInstance = ogreLinkedEntity->getSkeleton();
38 if (!O3SkeletonInstance)
39 return;
40
41 unsigned short numBones = O3SkeletonInstance->getNumBones();
42 for (unsigned short i=0; i<numBones; i++)
43 {
44 Ogre::Bone* ogreBone = O3SkeletonInstance->getBone(i);
45
46 std::string tmpBoneName(linkedEntity->GetName() + "." + ogreBone->getName());
47 SBone* newBone = currentScene->CreateBone(tmpBoneName, this, i);
48 AddBone(newBone);
49 }
50
51 // Once all our bones are created, reconstruct the hierarchy.
52 rootBonesList.clear();
53 for (unsigned short i=0; i<numBones; i++)
54 {
55 SBone* newBone = GetBone(i);
56 Ogre::Bone* ogreBone = O3SkeletonInstance->getBone(i);
57
58 Ogre::Bone* parentBone = static_cast<Ogre::Bone*> (ogreBone->getParent());
59 if (parentBone == 0)
60 {
61 newBone->AttachToParent(this);
62 rootBonesList.insert(SBoneMap::value_type(newBone->GetName(), newBone));
63 }
64 else
65 newBone->AttachToParent(GetBone(parentBone->getHandle()));
66 }
67
68 // Create animations
69 unsigned short numAnimations = O3SkeletonInstance->getNumAnimations();
70 for (unsigned short i=0; i<numAnimations; i++)
71 {
72 Ogre::Animation* ogreAnimation = O3SkeletonInstance->getAnimation(i);
73 SAnim* anim = new SSkeletonAnimation(GetParentScene(), ogreAnimation->getName(), this, i);
74 AddAnimation(anim);
75
76 // TODO vertex track animations
77 /*Ogre::Animation::VertexTrackIterator it = ogreAnimation->getVertexTrackIterator();
78 while(it.hasMoreElements())
79 {
80 Ogre::VertexAnimationTrack* ogreVertexTrack = it.getNext();
81 SAnimTrack* animationTrack = new SAnimTrack("TODO JEFF", anim, myTrack);
82 anim->listOfAnimationTrack.insert(SAnimTrackMap::value_type("TODO JEFF", animationTrack));
83 int at = createAnimTrack(m,animationTrack, anim);
84 }*/
85 }
86
87 // Create animations
88 /*for(int i=0; i<O3SkeletonInstance->getNumAnimations(); i++)
89 {
90 // get all animations from list by entity and by type
91 SAnim* anim = NULL;
92
93 GetAnimByName(scene, &anim, node, entity->getOgreEntityPointer()->getName()+"."+oanim->getName());
94 if(anim == NULL)
95 {
96 anim = new SAnim(oanim->getName(), SAnim::SO3_VERTEX_ANIM); // TODO JEFF: bizarre, j'aurais mis SO3_SKELETON_ANIM ici.
97 anim->setOgreAnimationPointer(oanim);
98
99 anim->setAssociatedNodePointer(node);
100 anim->sceneName = scene->O3SceneManager->getName().c_str();
101
102 // Create AnimationTrack for each anim
103 scene->listOfAnim.insert(SAnimMap::value_type(, anim));
104 int a = createAnim(m,anim, entity);
105
106
107 Ogre::Animation::VertexTrackIterator it = anim->getOgreAnimationPointer()->getVertexTrackIterator();
108 while(it.hasMoreElements())
109 {
110 Ogre::VertexAnimationTrack* myTrack = static_cast <Ogre::VertexAnimationTrack*> (it.getNext());
111 SAnimTrack* animationTrack = new SAnimTrack("TODO JEFF", anim, myTrack);
112 anim->listOfAnimationTrack.insert(SAnimTrackMap::value_type("TODO JEFF", animationTrack));
113 int at = createAnimTrack(m,animationTrack, anim);
114 }
115 }
116 else
117 {
118 //TODO update tracks
119 anim->setOgreAnimationPointer(oanim);
120 }
121 }*/
122 ogreLinkedEntity->setUpdateBoundingBoxFromSkeleton((O3SkeletonInstance->getNumBones() > 1) ? true : false);
123 ogreLinkedEntity->getMesh()->_setBoneBoundingRadius(std::max(ogreLinkedEntity->getMesh()->getBoneBoundingRadius(), 0.1f));
124
126}
127
129{
130 // Animation deletion is done by SNode destructor,
131 // No need to be done there.
132
133 // Erase all bones
134 SBoneMap boneListCopy = boneList;
135 SBoneMap::const_iterator iBone = boneListCopy.begin();
136 while(iBone != boneListCopy.end())
137 {
138 RemoveBone(iBone->second);
139
140 //scol must do this
141 //currentScene->DeleteBone(iBone->second);
142 iBone++;
143 }
144
145 linkedEntity = 0;
146}
147
149{
150 Ogre::SkeletonInstance* O3SkeletonInstance = ogreLinkedEntity->getSkeleton();
151 O3SkeletonInstance->setBindingPose();
152}
153
155{
156 Ogre::SkeletonInstance* O3SkeletonInstance = ogreLinkedEntity->getSkeleton();
157 return static_cast <SkeletonAnimationMode> (O3SkeletonInstance->getBlendMode());
158}
159
161{
162 Ogre::SkeletonInstance* O3SkeletonInstance = ogreLinkedEntity->getSkeleton();
163 O3SkeletonInstance->setBlendMode(static_cast <Ogre::SkeletonAnimationBlendMode> (static_cast <int> (newBlendMode)));
164}
165
166void SSkeleton::AddBone(SBone* existingBone)
167{
168 string boneName = existingBone->GetName();
169 SBoneMap::iterator iBoneSearched = boneList.find(boneName);
170 if (iBoneSearched == boneList.end())
171 {
172 boneList.insert(SBoneMap::value_type(boneName, existingBone));
173 boneListByIndex.insert(SBoneIndexMap::value_type(existingBone->GetIndex(), existingBone));
174 }
175 else
176 {
177 // Bone already exist in the handled bones list.
178 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Can not add bone named \""+ boneName +"\", an element with the same name already exist!", "SSkeleton::AddBone");
179 }
180}
181
182void SSkeleton::RemoveBone(SBone* existingBone)
183{
184 RemoveBone(existingBone->GetName());
185}
186
187void SSkeleton::RemoveBone(const std::string& boneName)
188{
189 SBoneMap::iterator iBoneSearched = boneList.find(boneName);
190 if (iBoneSearched != boneList.end())
191 {
192 SBone* findedBone = iBoneSearched->second;
193 boneList.erase(iBoneSearched);
194
195 //reset skeleton
196 findedBone->UnsetSkeleton();
197
198 // Delete from index map too
199 SBoneIndexMap::iterator iBoneIndexSearched = boneListByIndex.find(findedBone->GetIndex());
200 boneListByIndex.erase(iBoneIndexSearched);
201
202 // Remove from the root bone list if in.
203 SBoneMap::iterator iBoneRootSearched = rootBonesList.find(boneName);
204 if(iBoneRootSearched != rootBonesList.end())
205 rootBonesList.erase(iBoneRootSearched);
206 }
207 else
208 {
209 // Bone not found in the handled bones list
210 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Can not remove Bone named \""+ boneName +"\", element not found!", "SSkeleton::RemoveBone");
211 }
212}
213
215{
216 assert(boneInstance != 0);
217 if(GetBone(boneInstance->GetName()) != boneInstance)
218 return;
219 //OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Cannot remove a bone that is not handled by this skeleton, or bone already removed", "SSkeleton::_OnBoneDeletion");
220
221 RemoveBone(boneInstance);
222}
223
225{
226 return boneList.size();
227}
228
229SBone* SSkeleton::GetBone(const std::string& boneName)
230{
231 SBoneMap::iterator iBoneSearched = boneList.find(boneName);
232 if (iBoneSearched != boneList.end())
233 return iBoneSearched->second;
234 else
235 return 0;
236}
237
238SBone* SSkeleton::GetBone(const unsigned short& boneIndex)
239{
240 SBoneIndexMap::iterator iBoneSearched = boneListByIndex.find(boneIndex);
241 if (iBoneSearched != boneListByIndex.end())
242 return iBoneSearched->second;
243 else
244 return 0;
245}
246
248{
249 return boneList;
250}
251
252Ogre::SkeletonInstance* SSkeleton::GetOgreSkeletonPointer()
253{
254 return ogreLinkedEntity->getSkeleton();;
255}
256
261
266
268{
269 return Ogre::Vector3::ZERO;
270}
271
273{
274 return Ogre::Quaternion::IDENTITY;
275}
276
278{
279 return Ogre::Vector3::UNIT_SCALE;
280}
281
282void SSkeleton::SetGlobalPosition(const Ogre::Vector3& pos, bool updateBody)
283{
284 //Invalid on skeleton
285}
286
287void SSkeleton::SetGlobalOrientation(const Ogre::Quaternion& quat, bool updateBody)
288{
289 //Invalid on skeleton
290}
291
292}
void UnsetSkeleton()
Definition SO3Bone.cpp:64
unsigned short GetIndex()
Definition SO3Bone.cpp:474
std::string GetName() const
void _SetSkeleton(SSkeleton *newSkeleton)
Ogre::Entity * getOgreEntityPointer()
void AddAnimation(SAnim *existingAnimation)
SScene * currentScene
Definition SO3NodeScol.h:68
SScene * GetParentScene()
void AttachToParent(SNode *newParentNode)
SBone * CreateBone(const std::string &newBoneName, SSkeleton *skeleton, const unsigned short &id)
Definition SO3Scene.cpp:946
SBoneMap rootBonesList
Definition SO3Skeleton.h:53
void RemoveBone(SBone *existingBone)
Ogre::SkeletonInstance * GetOgreSkeletonPointer()
SkeletonAnimationMode GetBlendMode()
void AddBone(SBone *existingBone)
SBoneMap GetBones() const
virtual void SetGlobalPosition(const Ogre::Vector3 &pos, bool updateBody=true)
Ogre::Entity * ogreLinkedEntity
Definition SO3Skeleton.h:51
SEntity * linkedEntity
Definition SO3Skeleton.h:50
SEntity * GetParentEntity()
SBone * GetBone(const std::string &boneName)
SBoneIndexMap boneListByIndex
Definition SO3Skeleton.h:54
virtual Ogre::Quaternion GetGlobalOrientation()
virtual void SetGlobalOrientation(const Ogre::Quaternion &quat, bool updateBody=true)
void SetBlendMode(const SkeletonAnimationMode &newBlendMode)
SBoneMap boneList
Definition SO3Skeleton.h:52
void _OnBoneDeletion(SBone *boneInstance)
virtual Ogre::Vector3 GetGlobalPosition()
virtual Ogre::Vector3 GetGlobalScale()
unsigned short GetNumBones()
SBoneMap GetRootBones()
std::unordered_map< std::string, SBone * > SBoneMap