summaryrefslogtreecommitdiff
path: root/src/mesh/assimp-master/tools/assimp_view/SceneAnimator.h
blob: 454357f3073388324b535f83ccd3f74112b9a0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------

Copyright (c) 2006-2022, assimp team

All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/

/** @file SceneAnimator.h
 *  Manages animations for a given scene and calculates present
 *  transformations for all nodes
 */

#ifndef AV_SCENEANIMATOR_H_INCLUDED
#define AV_SCENEANIMATOR_H_INCLUDED

#include <map>

namespace AssimpView {

// ---------------------------------------------------------------------------------
/** A little tree structure to match the scene's node structure, but holding
 *  additional data. Needs to be public to allow using it in templates at
 *  certain compilers.
 */
struct SceneAnimNode {
    std::string mName;
    SceneAnimNode *mParent;
    std::vector<SceneAnimNode *> mChildren;

    //! most recently calculated local transform
    aiMatrix4x4 mLocalTransform;

    //! same, but in world space
    aiMatrix4x4 mGlobalTransform;

    //!  index in the current animation's channel array. -1 if not animated.
    int mChannelIndex;

    //! Default construction
    SceneAnimNode() :
            mName(), mParent(nullptr), mChildren(), mLocalTransform(), mGlobalTransform(), mChannelIndex(-1) {
        // empty
    }

    //! Construction from a given name
    SceneAnimNode(const std::string &pName) :
            mName(pName), mParent(nullptr), mChildren(), mLocalTransform(), mGlobalTransform(), mChannelIndex(-1) {
        // empty
    }

    //! Destruct all children recursively
    ~SceneAnimNode() {
        for (std::vector<SceneAnimNode *>::iterator it = mChildren.begin(); it != mChildren.end(); ++it) {
            delete *it;
        }
    }
};

// ---------------------------------------------------------------------------------
/** Calculates the animated node transformations for a given scene and timestamp.
 *
 *  Create an instance for a aiScene you want to animate and set the current animation
 *  to play. You can then have the instance calculate the current pose for all nodes
 *  by calling Calculate() for a given timestamp. After this you can retrieve the
 *  present transformation for a given node by calling GetLocalTransform() or
 *  GetGlobalTransform(). A full set of bone matrices can be retrieved by
 *  GetBoneMatrices() for a given mesh.
 */
class SceneAnimator {
public:
    // ----------------------------------------------------------------------------
    /** Constructor for a given scene.
     *
     * The object keeps a reference to the scene during its lifetime, but
     * ownership stays at the caller.
     * @param pScene The scene to animate.
     * @param pAnimIndex [optional] Index of the animation to play. Assumed to
     *  be 0 if not given.
     */
    SceneAnimator(const aiScene *pScene, size_t pAnimIndex = 0);

    /** Destructor */
    ~SceneAnimator();

    // ----------------------------------------------------------------------------
    /** Sets the animation to use for playback. This also recreates the internal
     * mapping structures, which might take a few cycles.
     * @param pAnimIndex Index of the animation in the scene's animation array
     */
    void SetAnimIndex(size_t pAnimIndex);

    // ----------------------------------------------------------------------------
    /** Calculates the node transformations for the scene. Call this to get
     * up-to-date results before calling one of the getters.
     * @param pTime Current time. Can be an arbitrary range.
     */
    void Calculate(double pTime);

    // ----------------------------------------------------------------------------
    /** Retrieves the most recent local transformation matrix for the given node.
     *
     * The returned matrix is in the node's parent's local space, just like the
     * original node's transformation matrix. If the node is not animated, the
     * node's original transformation is returned so that you can safely use or
     * assign it to the node itsself. If there is no node with the given name,
     * the identity matrix is returned. All transformations are updated whenever
     * Calculate() is called.
     * @param pNodeName Name of the node
     * @return A reference to the node's most recently calculated local
     *   transformation matrix.
     */
    const aiMatrix4x4 &GetLocalTransform(const aiNode *node) const;

    // ----------------------------------------------------------------------------
    /** Retrieves the most recent global transformation matrix for the given node.
     *
     * The returned matrix is in world space, which is the same coordinate space
     * as the transformation of the scene's root node. If the node is not animated,
     * the node's original transformation is returned so that you can safely use or
     * assign it to the node itsself. If there is no node with the given name, the
     * identity matrix is returned. All transformations are updated whenever
     * Calculate() is called.
     * @param pNodeName Name of the node
     * @return A reference to the node's most recently calculated global
     *   transformation matrix.
     */
    const aiMatrix4x4 &GetGlobalTransform(const aiNode *node) const;

    // ----------------------------------------------------------------------------
    /** Calculates the bone matrices for the given mesh.
     *
     * Each bone matrix transforms from mesh space in bind pose to mesh space in
     * skinned pose, it does not contain the mesh's world matrix. Thus the usual
     * matrix chain for using in the vertex shader is
     * @code
     * boneMatrix * worldMatrix * viewMatrix * projMatrix
     * @endcode
     * @param pNode The node carrying the mesh.
     * @param pMeshIndex Index of the mesh in the node's mesh array. The NODE's
     *   mesh array, not  the scene's mesh array! Leave out to use the first mesh
     *   of the node, which is usually also the only one.
     * @return A reference to a vector of bone matrices. Stays stable till the
     *   next call to GetBoneMatrices();
     */
    const std::vector<aiMatrix4x4> &GetBoneMatrices(const aiNode *pNode,
            size_t pMeshIndex = 0);

    // ----------------------------------------------------------------------------
    /** @brief Get the current animation index
     */
    size_t CurrentAnimIndex() const {
        return mCurrentAnimIndex;
    }

    // ----------------------------------------------------------------------------
    /** @brief Get the current animation or NULL
     */
    aiAnimation *CurrentAnim() const {
        return static_cast<unsigned int>(mCurrentAnimIndex) < mScene->mNumAnimations ? mScene->mAnimations[mCurrentAnimIndex] : NULL;
    }

protected:
    /** Recursively creates an internal node structure matching the
     *  current scene and animation.
     */
    SceneAnimNode *CreateNodeTree(aiNode *pNode, SceneAnimNode *pParent);

    /** Recursively updates the internal node transformations from the
     *  given matrix array
     */
    void UpdateTransforms(SceneAnimNode *pNode, const std::vector<aiMatrix4x4> &pTransforms);

    /** Calculates the global transformation matrix for the given internal node */
    void CalculateGlobalTransform(SceneAnimNode *pInternalNode);

protected:
    /** The scene we're operating on */
    const aiScene *mScene;

    /** Current animation index */
    int mCurrentAnimIndex;

    /** The AnimEvaluator we use to calculate the current pose for the current animation */
    AnimEvaluator *mAnimEvaluator;

    /** Root node of the internal scene structure */
    SceneAnimNode *mRootNode;

    /** Name to node map to quickly find nodes by their name */
    typedef std::map<const aiNode *, SceneAnimNode *> NodeMap;
    NodeMap mNodesByName;

    /** Name to node map to quickly find nodes for given bones by their name */
    typedef std::map<const char *, const aiNode *> BoneMap;
    BoneMap mBoneNodesByName;

    /** Array to return transformations results inside. */
    std::vector<aiMatrix4x4> mTransforms;
};

} // end of namespace AssimpView

#endif // AV_SCENEANIMATOR_H_INCLUDED