2008-10-29 17:08:03 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development 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 Development 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.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2009-02-11 20:56:05 +00:00
|
|
|
/** @file SceneAnimator.h
|
|
|
|
* Manages animations for a given scene and calculates present
|
|
|
|
* transformations for all nodes
|
|
|
|
*/
|
|
|
|
|
2008-10-29 17:08:03 +00:00
|
|
|
#ifndef AV_SCENEANIMATOR_H_INCLUDED
|
|
|
|
#define AV_SCENEANIMATOR_H_INCLUDED
|
|
|
|
|
2009-04-24 20:53:24 +00:00
|
|
|
#include <map>
|
|
|
|
|
2009-02-11 20:56:05 +00:00
|
|
|
namespace AssimpView {
|
2008-10-29 17:08:03 +00:00
|
|
|
|
2009-02-11 20:56:05 +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.
|
2008-10-29 17:08:03 +00:00
|
|
|
*/
|
|
|
|
struct SceneAnimNode
|
|
|
|
{
|
|
|
|
std::string mName;
|
|
|
|
SceneAnimNode* mParent;
|
|
|
|
std::vector<SceneAnimNode*> mChildren;
|
|
|
|
|
2009-02-11 20:56:05 +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.
|
|
|
|
size_t mChannelIndex;
|
|
|
|
|
|
|
|
//! Default construction
|
|
|
|
SceneAnimNode() {
|
|
|
|
mChannelIndex = -1; mParent = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Construction from a given name
|
|
|
|
SceneAnimNode( const std::string& pName)
|
|
|
|
: mName( pName) {
|
|
|
|
mChannelIndex = -1; mParent = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Destruct all children recursively
|
|
|
|
~SceneAnimNode() {
|
|
|
|
for( std::vector<SceneAnimNode*>::iterator it = mChildren.begin(); it != mChildren.end(); ++it)
|
|
|
|
delete *it;
|
|
|
|
}
|
2008-10-29 17:08:03 +00:00
|
|
|
};
|
|
|
|
|
2009-02-11 20:56:05 +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.
|
2008-10-29 17:08:03 +00:00
|
|
|
*/
|
|
|
|
class SceneAnimator
|
|
|
|
{
|
|
|
|
public:
|
2009-02-11 20:56:05 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Constructor for a given scene.
|
|
|
|
*
|
|
|
|
* The object keeps a reference to the scene during its lifetime, but
|
|
|
|
* ownership stays at the caller.
|
2008-10-29 17:08:03 +00:00
|
|
|
* @param pScene The scene to animate.
|
2009-02-11 20:56:05 +00:00
|
|
|
* @param pAnimIndex [optional] Index of the animation to play. Assumed to
|
|
|
|
* be 0 if not given.
|
2008-10-29 17:08:03 +00:00
|
|
|
*/
|
|
|
|
SceneAnimator( const aiScene* pScene, size_t pAnimIndex = 0);
|
|
|
|
|
|
|
|
/** Destructor */
|
|
|
|
~SceneAnimator();
|
|
|
|
|
2009-02-11 20:56:05 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Sets the animation to use for playback. This also recreates the internal
|
|
|
|
* mapping structures, which might take a few cycles.
|
2008-10-29 17:08:03 +00:00
|
|
|
* @param pAnimIndex Index of the animation in the scene's animation array
|
|
|
|
*/
|
|
|
|
void SetAnimIndex( size_t pAnimIndex);
|
|
|
|
|
2009-02-11 20:56:05 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Calculates the node transformations for the scene. Call this to get
|
|
|
|
* uptodate results before calling one of the getters.
|
2008-10-29 17:08:03 +00:00
|
|
|
* @param pTime Current time. Can be an arbitrary range.
|
|
|
|
*/
|
|
|
|
void Calculate( double pTime);
|
|
|
|
|
2009-02-11 20:56:05 +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.
|
2008-10-29 17:08:03 +00:00
|
|
|
* @param pNodeName Name of the node
|
2009-02-11 20:56:05 +00:00
|
|
|
* @return A reference to the node's most recently calculated local
|
|
|
|
* transformation matrix.
|
2008-10-29 17:08:03 +00:00
|
|
|
*/
|
2009-02-11 20:56:05 +00:00
|
|
|
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
|
2008-10-29 17:08:03 +00:00
|
|
|
* Calculate() is called.
|
|
|
|
* @param pNodeName Name of the node
|
2009-02-11 20:56:05 +00:00
|
|
|
* @return A reference to the node's most recently calculated global
|
|
|
|
* transformation matrix.
|
2008-10-29 17:08:03 +00:00
|
|
|
*/
|
2009-02-11 20:56:05 +00:00
|
|
|
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
|
2008-10-29 17:08:03 +00:00
|
|
|
* @param pNode The node carrying the mesh.
|
2009-02-11 20:56:05 +00:00
|
|
|
* @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
|
2008-10-29 17:08:03 +00:00
|
|
|
*/
|
2009-02-11 20:56:05 +00:00
|
|
|
size_t CurrentAnimIndex() const {
|
|
|
|
return mCurrentAnimIndex;
|
|
|
|
}
|
2008-10-29 17:08:03 +00:00
|
|
|
|
2009-02-15 20:29:07 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** @brief Get the current animation or NULL
|
|
|
|
*/
|
|
|
|
aiAnimation* CurrentAnim() const {
|
|
|
|
return mCurrentAnimIndex < mScene->mNumAnimations ? mScene->mAnimations[ mCurrentAnimIndex ] : NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-29 17:08:03 +00:00
|
|
|
protected:
|
2009-02-11 20:56:05 +00:00
|
|
|
|
|
|
|
/** Recursively creates an internal node structure matching the
|
|
|
|
* current scene and animation.
|
|
|
|
*/
|
2008-11-01 14:18:13 +00:00
|
|
|
SceneAnimNode* CreateNodeTree( aiNode* pNode, SceneAnimNode* pParent);
|
2008-10-29 17:08:03 +00:00
|
|
|
|
2009-02-11 20:56:05 +00:00
|
|
|
/** Recursively updates the internal node transformations from the
|
|
|
|
* given matrix array
|
|
|
|
*/
|
2008-10-29 17:08:03 +00:00
|
|
|
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 */
|
|
|
|
size_t 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 */
|
2009-02-11 20:56:05 +00:00
|
|
|
typedef std::map<const aiNode*, SceneAnimNode*> NodeMap;
|
2008-10-29 17:08:03 +00:00
|
|
|
NodeMap mNodesByName;
|
|
|
|
|
2009-02-11 20:56:05 +00:00
|
|
|
/** Name to node map to quickly find nodes for given bones by their name */
|
|
|
|
typedef std::map<const char*, const aiNode*> BoneMap;
|
|
|
|
BoneMap mBoneNodesByName;
|
|
|
|
|
2008-10-29 17:08:03 +00:00
|
|
|
/** Array to return transformations results inside. */
|
|
|
|
std::vector<aiMatrix4x4> mTransforms;
|
|
|
|
|
|
|
|
/** Identity matrix to return a reference to in case of error */
|
|
|
|
aiMatrix4x4 mIdentityMatrix;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end of namespace AssimpView
|
|
|
|
|
|
|
|
#endif // AV_SCENEANIMATOR_H_INCLUDED
|