Coverage Report

Created: 2025-09-04 07:15

/src/ogre/OgreMain/include/OgreSkeleton.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
-----------------------------------------------------------------------------
3
This source file is part of OGRE
4
    (Object-oriented Graphics Rendering Engine)
5
For the latest info, see http://www.ogre3d.org/
6
7
Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
29
#ifndef __Skeleton_H__
30
#define __Skeleton_H__
31
32
#include "OgrePrerequisites.h"
33
#include "OgreResource.h"
34
#include "OgreStringVector.h"
35
#include "OgreAnimation.h"
36
#include "OgreHeaderPrefix.h"
37
#include "OgreSharedPtr.h"
38
39
namespace Ogre {
40
    /** \addtogroup Core
41
    *  @{
42
    */
43
    /** \addtogroup Animation
44
    *  @{
45
    */
46
47
    /**  */
48
    enum SkeletonAnimationBlendMode : uint8 {
49
        /// Animations are applied by calculating a weighted average of all animations
50
        ANIMBLEND_AVERAGE = 0,
51
        /// Animations are applied by calculating a weighted cumulative total
52
        ANIMBLEND_CUMULATIVE = 1
53
    };
54
    
55
    struct LinkedSkeletonAnimationSource;
56
57
    /** A collection of Bone objects used to animate a skinned mesh.
58
59
        Skeletal animation works by having a collection of 'bones' which are 
60
        actually just joints with a position and orientation, arranged in a tree structure.
61
        For example, the wrist joint is a child of the elbow joint, which in turn is a
62
        child of the shoulder joint. Rotating the shoulder automatically moves the elbow
63
        and wrist as well due to this hierarchy.
64
    @par
65
        So how does this animate a mesh? Well every vertex in a mesh is assigned to one or more
66
        bones which affects it's position when the bone is moved. If a vertex is assigned to 
67
        more than one bone, then weights must be assigned to determine how much each bone affects
68
        the vertex (actually a weight of 1.0 is used for single bone assignments). 
69
        Weighted vertex assignments are especially useful around the joints themselves
70
        to avoid 'pinching' of the mesh in this region. 
71
    @par
72
        Therefore by moving the skeleton using preset animations, we can animate the mesh. The
73
        advantage of using skeletal animation is that you store less animation data, especially
74
        as vertex counts increase. In addition, you are able to blend multiple animations together
75
        (e.g. walking and looking around, running and shooting) and provide smooth transitions
76
        between animations without incurring as much of an overhead as would be involved if you
77
        did this on the core vertex data.
78
    @par
79
        Skeleton definitions are loaded from datafiles, namely the .skeleton file format. They
80
        are loaded on demand, especially when referenced by a Mesh.
81
    */
82
    class _OgreExport Skeleton : public Resource, public AnimationContainer
83
    {
84
        friend class SkeletonInstance;
85
    private:
86
        /// Internal constructor for use by SkeletonInstance only
87
        Skeleton();
88
89
    public:
90
        /** Constructor, don't call directly, use SkeletonManager.
91
92
            On creation, a Skeleton has a no bones, you should create them and link
93
            them together appropriately. 
94
        */
95
        Skeleton(ResourceManager* creator, const String& name, ResourceHandle handle,
96
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
97
        virtual ~Skeleton();
98
99
100
        /** Creates a brand new Bone owned by this Skeleton. 
101
102
            This method creates an unattached new Bone for this skeleton.
103
            Unless this is to be a root bone (there may be more than one of 
104
            these), you must attach it to another Bone in the skeleton using addChild for it to be any use.
105
            For this reason you will likely be better off creating child bones using the
106
            Bone::createChild method instead, once you have created the root bone. 
107
        @par
108
            Note that this method automatically generates a handle for the bone, which you
109
            can retrieve using Bone::getHandle. If you wish the new Bone to have a specific
110
            handle, use the alternate form of this method which takes a handle as a parameter,
111
            although you should note the restrictions.
112
        */
113
        virtual Bone* createBone(void);
114
115
        /** Creates a brand new Bone owned by this Skeleton. 
116
117
            This method creates an unattached new Bone for this skeleton and assigns it a 
118
            specific handle. Unless this is to be a root bone (there may be more than one of 
119
            these), you must attach it to another Bone in the skeleton using addChild for it to be any use. 
120
            For this reason you will likely be better off creating child bones using the
121
            Bone::createChild method instead, once you have created a root bone. 
122
        @param handle The handle to give to this new bone - must be unique within this skeleton. 
123
            You should also ensure that all bone handles are eventually contiguous (this is to simplify
124
            their compilation into an indexed array of transformation matrices). For this reason
125
            it is advised that you use the simpler createBone method which automatically assigns a
126
            sequential handle starting from 0.
127
        */
128
        virtual Bone* createBone(unsigned short handle);
129
130
        /** Creates a brand new Bone owned by this Skeleton. 
131
132
            This method creates an unattached new Bone for this skeleton and assigns it a 
133
            specific name.Unless this is to be a root bone (there may be more than one of 
134
            these), you must attach it to another Bone in the skeleton using addChild for it to be any use.
135
            For this reason you will likely be better off creating child bones using the
136
            Bone::createChild method instead, once you have created the root bone. 
137
        @param name The name to give to this new bone - must be unique within this skeleton. 
138
            Note that the way OGRE looks up bones is via a numeric handle, so if you name a
139
            Bone this way it will be given an automatic sequential handle. The name is just
140
            for your convenience, although it is recommended that you only use the handle to 
141
            retrieve the bone in performance-critical code.
142
        */
143
        virtual Bone* createBone(const String& name);
144
145
        /** Creates a brand new Bone owned by this Skeleton. 
146
147
            This method creates an unattached new Bone for this skeleton and assigns it a 
148
            specific name and handle. Unless this is to be a root bone (there may be more than one of 
149
            these), you must attach it to another Bone in the skeleton using addChild for it to be any use.
150
            For this reason you will likely be better off creating child bones using the
151
            Bone::createChild method instead, once you have created the root bone. 
152
        @param name The name to give to this new bone - must be unique within this skeleton. 
153
        @param handle The handle to give to this new bone - must be unique within this skeleton. 
154
        */
155
        virtual Bone* createBone(const String& name, unsigned short handle);
156
157
        /** Returns the number of bones in this skeleton. */
158
0
        unsigned short getNumBones(void) const { return (unsigned short)mBoneList.size(); }
159
160
        typedef std::vector<Bone*> BoneList;
161
        typedef VectorIterator<BoneList> BoneIterator;
162
        /// Get an iterator over the root bones in the skeleton, ie those with no parents
163
        /// @deprecated use Skeleton::getRootBones
164
        OGRE_DEPRECATED virtual BoneIterator getRootBoneIterator(void);
165
166
        /** Get the root bones in the skeleton, ie those with no parents
167
168
            The system derives the root bone the first time you ask for it. The root bone is the
169
            only bone in the skeleton which has no parent. The system locates it by taking the
170
            first bone in the list and going up the bone tree until there are no more parents,
171
            and saves this top bone as the root. If you are building the skeleton manually using
172
            createBone then you must ensure there is only one bone which is not a child of
173
            another bone, otherwise your skeleton will not work properly. If you use createBone
174
            only once, and then use Bone::createChild from then on, then inherently the first
175
            bone you create will by default be the root.
176
        */
177
        const BoneList& getRootBones() const;
178
179
        /// Get an iterator over all the bones in the skeleton
180
        /// @deprecated use getBones()
181
        OGRE_DEPRECATED virtual BoneIterator getBoneIterator(void);
182
        /// Get all the bones in the skeleton
183
0
        const BoneList& getBones() const {
184
0
            return mBoneList;
185
0
        }
186
187
        /** Gets a bone by it's handle. */
188
        Bone* getBone(unsigned short handle) const
189
0
        {
190
0
            assert(handle < mBoneList.size() && "Index out of bounds");
191
0
            return mBoneList[handle];
192
0
        }
193
194
        /** Gets a bone by it's name. */
195
        Bone* getBone(const String& name) const;
196
197
        /** Returns whether this skeleton contains the named bone. */
198
        bool hasBone(const String& name) const;
199
200
        /** Sets the current position / orientation to be the 'binding pose' i.e. the layout in which 
201
            bones were originally bound to a mesh.
202
        */
203
        void setBindingPose(void);
204
205
        /** Resets the position and orientation of all bones in this skeleton to their original binding position.
206
207
            A skeleton is bound to a mesh in a binding pose. Bone positions are then modified from this
208
            position during animation. This method returns all the bones to their original position and
209
            orientation.
210
        @param resetManualBones If set to true, causes the state of manual bones to be reset
211
            too, which is normally not done to allow the manual state to persist even 
212
            when keyframe animation is applied.
213
        */
214
        void reset(bool resetManualBones = false);
215
216
        /** Creates a new Animation object for animating this skeleton. 
217
        @copydetails AnimationContainer::createAnimation
218
        */
219
        Animation* createAnimation(const String& name, Real length) override;
220
221
        /** Returns the named Animation object. 
222
223
            Will pick up animations in linked skeletons 
224
            (@see addLinkedSkeletonAnimationSource). 
225
        @param name The name of the animation
226
        @param linker Optional pointer to a pointer to the linked skeleton animation
227
            where this is coming from.
228
        */
229
        virtual Animation* getAnimation(const String& name, 
230
            const LinkedSkeletonAnimationSource** linker) const;
231
232
        /** Returns the named Animation object.
233
234
             Will pick up animations in linked skeletons 
235
             (@see addLinkedSkeletonAnimationSource). 
236
         @param name The name of the animation
237
         */
238
        Animation* getAnimation(const String& name) const override;
239
240
        /// Internal accessor for animations (returns null if animation does not exist)
241
        virtual Animation* _getAnimationImpl(const String& name, 
242
            const LinkedSkeletonAnimationSource** linker = 0) const;
243
244
245
        bool hasAnimation(const String& name) const override;
246
        void removeAnimation(const String& name) override;
247
248
        /** Changes the state of the skeleton to reflect the application of the passed in collection of animations.
249
250
            Animating a skeleton involves both interpolating between keyframes of a specific animation,
251
            and blending between the animations themselves. Calling this method sets the state of
252
            the skeleton so that it reflects the combination of all the passed in animations, at the
253
            time index specified for each, using the weights specified. Note that the weights between 
254
            animations do not have to sum to 1.0, because some animations may affect only subsets
255
            of the skeleton. If the weights exceed 1.0 for the same area of the skeleton, the 
256
            movement will just be exaggerated.
257
        */
258
        virtual void setAnimationState(const AnimationStateSet& animSet);
259
260
261
        /** Initialise an animation set suitable for use with this skeleton. 
262
263
            Only recommended for use inside the engine, not by applications.
264
        */
265
        virtual void _initAnimationState(AnimationStateSet* animSet);
266
267
        /** Refresh an animation set suitable for use with this skeleton. 
268
269
            Only recommended for use inside the engine, not by applications.
270
        */
271
        virtual void _refreshAnimationState(AnimationStateSet* animSet);
272
273
        /** Populates the passed in array with the bone matrices based on the current position.
274
275
            Internal use only. The array pointed to by the passed in pointer must
276
            be at least as large as the number of bones.
277
            Assumes animation has already been updated.
278
        */
279
        void _getBoneMatrices(Affine3* pMatrices);
280
281
        unsigned short getNumAnimations(void) const override;
282
283
        /** Gets a single animation by index. 
284
285
            Will NOT pick up animations in linked skeletons 
286
            (@see addLinkedSkeletonAnimationSource).
287
        */
288
        Animation* getAnimation(unsigned short index) const override;
289
290
291
        /** Gets the animation blending mode which this skeleton will use. */
292
0
        SkeletonAnimationBlendMode getBlendMode() const { return mBlendState; }
293
        /** Sets the animation blending mode this skeleton will use. */
294
0
        void setBlendMode(SkeletonAnimationBlendMode state) { mBlendState = state; }
295
296
        /// Updates all the derived transforms in the skeleton
297
        virtual void _updateTransforms(void);
298
299
        /** Optimise all of this skeleton's animations.
300
        @see Animation::optimise
301
        @param
302
            preservingIdentityNodeTracks If true, don't destroy identity node tracks.
303
        */
304
        virtual void optimiseAllAnimations(bool preservingIdentityNodeTracks = false);
305
306
        /** Allows you to use the animations from another Skeleton object to animate
307
            this skeleton.
308
309
            If you have skeletons of identical structure (that means identically
310
            named bones with identical handles, and with the same hierarchy), but
311
            slightly different proportions or binding poses, you can re-use animations
312
            from one in the other. Because animations are actually stored as
313
            changes to bones from their bind positions, it's possible to use the
314
            same animation data for different skeletons, provided the skeletal
315
            structure matches and the 'deltas' stored in the keyframes apply
316
            equally well to the other skeletons bind position (so they must be
317
            roughly similar, but don't have to be identical). You can use the 
318
            'scale' option to adjust the translation and scale keyframes where
319
            there are large differences in size between the skeletons.
320
        @note
321
            This method takes a skeleton name, rather than a more specific 
322
            animation name, for two reasons; firstly it allows some validation 
323
            of compatibility of skeletal structure, and secondly skeletons are
324
            the unit of loading. Linking a skeleton to another in this way means
325
            that the linkee will be prevented from being destroyed until the 
326
            linker is destroyed.
327
328
            You cannot set up cyclic relationships, e.g. SkeletonA uses SkeletonB's
329
            animations, and SkeletonB uses SkeletonA's animations. This is because
330
            it would set up a circular dependency which would prevent proper 
331
            unloading - make one of the skeletons the 'master' in this case.
332
        @param skelName Name of the skeleton to link animations from. This 
333
            skeleton will be loaded immediately if this skeleton is already 
334
            loaded, otherwise it will be loaded when this skeleton is.
335
        @param scale A scale factor to apply to translation and scaling elements
336
            of the keyframes in the other skeleton when applying the animations
337
            to this one. Compensates for skeleton size differences.
338
        */
339
        virtual void addLinkedSkeletonAnimationSource(const String& skelName, 
340
            Real scale = 1.0f);
341
        /// Remove all links to other skeletons for the purposes of sharing animation
342
        virtual void removeAllLinkedSkeletonAnimationSources(void);
343
        
344
        typedef std::vector<LinkedSkeletonAnimationSource> 
345
            LinkedSkeletonAnimSourceList;
346
        typedef ConstVectorIterator<LinkedSkeletonAnimSourceList> 
347
            LinkedSkeletonAnimSourceIterator;
348
        /// Get the linked skeletons used as animation sources
349
        virtual const LinkedSkeletonAnimSourceList& getLinkedSkeletonAnimationSources() const
350
0
        {
351
0
            return mLinkedSkeletonAnimSourceList;
352
0
        }
353
354
        /// @deprecated use getLinkedSkeletonAnimationSources
355
        OGRE_DEPRECATED virtual LinkedSkeletonAnimSourceIterator
356
            getLinkedSkeletonAnimationSourceIterator(void) const;
357
358
        /// Internal method for marking the manual bones as dirty
359
        virtual void _notifyManualBonesDirty(void);
360
        /// Internal method for notifying that a bone is manual
361
        virtual void _notifyManualBoneStateChange(Bone* bone);
362
363
        /// Have manual bones been modified since the skeleton was last updated?
364
0
        virtual bool getManualBonesDirty(void) const { return mManualBonesDirty; }
365
        /// Are there any manually controlled bones?
366
0
        virtual bool hasManualBones(void) const { return !mManualBones.empty(); }
367
368
        /// Map to translate bone handle from one skeleton to another skeleton.
369
        typedef std::vector<ushort> BoneHandleMap;
370
371
        /** Merge animations from another Skeleton object into this skeleton.
372
373
            This function allow merge two structures compatible skeletons. The
374
            'compatible' here means identically bones will have same hierarchy,
375
            but skeletons are not necessary to have same number of bones (if
376
            number bones of source skeleton's more than this skeleton, they will
377
            copied as is, except that duplicate names are disallowed; and in the
378
            case of bones missing in source skeleton, nothing happen for those
379
            bones).
380
        @par
381
            There are also unnecessary to have same binding poses, this function
382
            will adjust keyframes of the source skeleton to match this skeleton
383
            automatically.
384
        @par
385
            It's useful for exporting skeleton animations separately. i.e. export
386
            mesh and 'master' skeleton at the same time, and then other animations
387
            will export separately (even if used completely difference binding
388
            pose), finally, merge separately exported animations into 'master'
389
            skeleton.
390
        @param
391
            source Pointer to source skeleton. It'll keep unmodified.
392
        @param
393
            boneHandleMap A map to translate identically bone's handle from source
394
            skeleton to this skeleton. If mapped bone handle doesn't exists in this
395
            skeleton, it'll created. You can populate bone handle map manually, or
396
            use predefined functions build bone handle map for you. (@see 
397
            _buildMapBoneByHandle, _buildMapBoneByName)
398
        @param
399
            animations A list name of animations to merge, if empty, all animations
400
            of source skeleton are used to merge. Note that the animation names
401
            must not presented in this skeleton, and will NOT pick up animations
402
            in linked skeletons (@see addLinkedSkeletonAnimationSource).
403
        */
404
        virtual void _mergeSkeletonAnimations(const Skeleton* source,
405
            const BoneHandleMap& boneHandleMap,
406
            const StringVector& animations = StringVector());
407
408
        /** Build the bone handle map to use with Skeleton::_mergeSkeletonAnimations.
409
410
            Identically bones are determine by handle.
411
        */
412
        virtual void _buildMapBoneByHandle(const Skeleton* source,
413
            BoneHandleMap& boneHandleMap) const;
414
415
        /** Build the bone handle map to use with Skeleton::_mergeSkeletonAnimations.
416
417
            Identically bones are determine by name.
418
        */
419
        virtual void _buildMapBoneByName(const Skeleton* source,
420
            BoneHandleMap& boneHandleMap) const;
421
422
    protected:
423
        /// Storage of animations, lookup by name
424
        AnimationList mAnimationsList;
425
    private:
426
        /// Lookup by bone name
427
        typedef std::map<String, Bone*> BoneListByName;
428
        BoneListByName mBoneListByName;
429
430
        /// Pointer to root bones (can now have multiple roots)
431
        mutable BoneList mRootBones;
432
        typedef std::set<Bone*> BoneSet;
433
        /// Manual bones
434
        BoneSet mManualBones;
435
436
        /// List of references to other skeletons to use animations from 
437
        mutable LinkedSkeletonAnimSourceList mLinkedSkeletonAnimSourceList;
438
439
        SkeletonAnimationBlendMode mBlendState;
440
        /// Manual bones dirty?
441
        bool mManualBonesDirty;
442
        /// Storage of bones, indexed by bone handle
443
        BoneList mBoneList;
444
445
        /** Internal method which parses the bones to derive the root bone. 
446
447
            Must be const because called in getRootBone but mRootBone is mutable
448
            since lazy-updated.
449
        */
450
        void deriveRootBone(void) const;
451
452
        /// Debugging method
453
        _OgreExport friend std::ostream& operator<<(std::ostream& o, const Skeleton& s);
454
455
0
        void loadImpl() override {}
456
0
        void unloadImpl() override { unprepareImpl(); }
457
458
        void prepareImpl(void) override;
459
        void unprepareImpl(void) override;
460
        /// @copydoc Resource::calculateSize
461
        size_t calculateSize(void) const override;
462
463
    };
464
465
    /// Link to another skeleton to share animations
466
    struct LinkedSkeletonAnimationSource
467
    {
468
        String skeletonName;
469
        SkeletonPtr pSkeleton;
470
        Real scale;
471
        LinkedSkeletonAnimationSource(const String& skelName, Real scl)
472
0
            : skeletonName(skelName), scale(scl) {}
473
            LinkedSkeletonAnimationSource(const String& skelName, Real scl, 
474
                const SkeletonPtr& skelPtr)
475
0
                : skeletonName(skelName), pSkeleton(skelPtr), scale(scl) {}
476
    };
477
    /** @} */
478
    /** @} */
479
480
}
481
482
#include "OgreHeaderSuffix.h"
483
484
#endif
485