2015-06-30 02:54:59 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, assimp team
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
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>
|
|
|
|
|
2019-01-30 15:07:40 +00:00
|
|
|
namespace AssimpView {
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------
|
|
|
|
/** 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.
|
|
|
|
*/
|
2019-01-30 15:07:40 +00:00
|
|
|
struct SceneAnimNode {
|
2015-06-30 02:54:59 +00:00
|
|
|
std::string mName;
|
2020-05-07 12:00:40 +00:00
|
|
|
SceneAnimNode *mParent;
|
|
|
|
std::vector<SceneAnimNode *> mChildren;
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
//! 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.
|
2019-01-30 15:07:40 +00:00
|
|
|
int mChannelIndex;
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
//! Default construction
|
2020-05-07 12:00:40 +00:00
|
|
|
SceneAnimNode() :
|
|
|
|
mName(), mParent(nullptr), mChildren(), mLocalTransform(), mGlobalTransform(), mChannelIndex(-1) {
|
2017-06-01 14:21:23 +00:00
|
|
|
// empty
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Construction from a given name
|
2020-05-07 12:00:40 +00:00
|
|
|
SceneAnimNode(const std::string &pName) :
|
|
|
|
mName(pName), mParent(nullptr), mChildren(), mLocalTransform(), mGlobalTransform(), mChannelIndex(-1) {
|
2017-06-01 14:21:23 +00:00
|
|
|
// empty
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Destruct all children recursively
|
|
|
|
~SceneAnimNode() {
|
2020-05-07 12:00:40 +00:00
|
|
|
for (std::vector<SceneAnimNode *>::iterator it = mChildren.begin(); it != mChildren.end(); ++it) {
|
2015-06-30 02:54:59 +00:00
|
|
|
delete *it;
|
2017-06-01 14:21:23 +00:00
|
|
|
}
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------
|
|
|
|
/** 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.
|
|
|
|
*/
|
2019-01-30 15:07:40 +00:00
|
|
|
class SceneAnimator {
|
2015-06-30 02:54:59 +00:00
|
|
|
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.
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
SceneAnimator(const aiScene *pScene, size_t pAnimIndex = 0);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
/** 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
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
void SetAnimIndex(size_t pAnimIndex);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Calculates the node transformations for the scene. Call this to get
|
2021-10-05 08:59:43 +00:00
|
|
|
* up-to-date results before calling one of the getters.
|
2015-06-30 02:54:59 +00:00
|
|
|
* @param pTime Current time. Can be an arbitrary range.
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
void Calculate(double pTime);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** 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.
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
const aiMatrix4x4 &GetLocalTransform(const aiNode *node) const;
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** 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.
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
const aiMatrix4x4 &GetGlobalTransform(const aiNode *node) const;
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** 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();
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
const std::vector<aiMatrix4x4> &GetBoneMatrices(const aiNode *pNode,
|
|
|
|
size_t pMeshIndex = 0);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** @brief Get the current animation index
|
|
|
|
*/
|
|
|
|
size_t CurrentAnimIndex() const {
|
|
|
|
return mCurrentAnimIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** @brief Get the current animation or NULL
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
aiAnimation *CurrentAnim() const {
|
|
|
|
return static_cast<unsigned int>(mCurrentAnimIndex) < mScene->mNumAnimations ? mScene->mAnimations[mCurrentAnimIndex] : NULL;
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** Recursively creates an internal node structure matching the
|
|
|
|
* current scene and animation.
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
SceneAnimNode *CreateNodeTree(aiNode *pNode, SceneAnimNode *pParent);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
/** Recursively updates the internal node transformations from the
|
|
|
|
* given matrix array
|
|
|
|
*/
|
2020-05-07 12:00:40 +00:00
|
|
|
void UpdateTransforms(SceneAnimNode *pNode, const std::vector<aiMatrix4x4> &pTransforms);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
/** Calculates the global transformation matrix for the given internal node */
|
2020-05-07 12:00:40 +00:00
|
|
|
void CalculateGlobalTransform(SceneAnimNode *pInternalNode);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/** The scene we're operating on */
|
2020-05-07 12:00:40 +00:00
|
|
|
const aiScene *mScene;
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
/** Current animation index */
|
2019-01-30 15:07:40 +00:00
|
|
|
int mCurrentAnimIndex;
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
/** The AnimEvaluator we use to calculate the current pose for the current animation */
|
2020-05-07 12:00:40 +00:00
|
|
|
AnimEvaluator *mAnimEvaluator;
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
/** Root node of the internal scene structure */
|
2020-05-07 12:00:40 +00:00
|
|
|
SceneAnimNode *mRootNode;
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
/** Name to node map to quickly find nodes by their name */
|
2020-05-07 12:00:40 +00:00
|
|
|
typedef std::map<const aiNode *, SceneAnimNode *> NodeMap;
|
2015-06-30 02:54:59 +00:00
|
|
|
NodeMap mNodesByName;
|
|
|
|
|
|
|
|
/** Name to node map to quickly find nodes for given bones by their name */
|
2020-05-07 12:00:40 +00:00
|
|
|
typedef std::map<const char *, const aiNode *> BoneMap;
|
2015-06-30 02:54:59 +00:00
|
|
|
BoneMap mBoneNodesByName;
|
|
|
|
|
|
|
|
/** Array to return transformations results inside. */
|
|
|
|
std::vector<aiMatrix4x4> mTransforms;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end of namespace AssimpView
|
|
|
|
|
2020-03-21 06:34:12 +00:00
|
|
|
#endif // AV_SCENEANIMATOR_H_INCLUDED
|