2008-10-29 17:08:03 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2008-10-29 17:08:03 +00:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2015-06-28 19:48:25 +00:00
|
|
|
Copyright (c) 2006-2015, assimp team
|
2008-10-29 17:08:03 +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.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2008-10-29 17:08:03 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2008-10-29 17:08:03 +00:00
|
|
|
|
|
|
|
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.cpp
|
|
|
|
* @brief Implementation of the utility class SceneAnimator
|
|
|
|
*/
|
|
|
|
|
2008-10-29 17:08:03 +00:00
|
|
|
#include "assimp_view.h"
|
|
|
|
|
|
|
|
using namespace AssimpView;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor for a given scene.
|
|
|
|
SceneAnimator::SceneAnimator( const aiScene* pScene, size_t pAnimIndex)
|
|
|
|
{
|
|
|
|
mScene = pScene;
|
|
|
|
mCurrentAnimIndex = -1;
|
|
|
|
mAnimEvaluator = NULL;
|
|
|
|
mRootNode = NULL;
|
|
|
|
|
2009-02-11 20:56:05 +00:00
|
|
|
// build the nodes-for-bones table
|
|
|
|
for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
|
|
|
|
{
|
|
|
|
const aiMesh* mesh = pScene->mMeshes[i];
|
|
|
|
for (unsigned int n = 0; n < mesh->mNumBones;++n)
|
|
|
|
{
|
|
|
|
const aiBone* bone = mesh->mBones[n];
|
|
|
|
|
|
|
|
mBoneNodesByName[bone->mName.data] = pScene->mRootNode->FindNode(bone->mName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 17:08:03 +00:00
|
|
|
// changing the current animation also creates the node tree for this animation
|
|
|
|
SetAnimIndex( pAnimIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor
|
|
|
|
SceneAnimator::~SceneAnimator()
|
|
|
|
{
|
|
|
|
delete mRootNode;
|
|
|
|
delete mAnimEvaluator;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Sets the animation to use for playback.
|
|
|
|
void SceneAnimator::SetAnimIndex( size_t pAnimIndex)
|
|
|
|
{
|
|
|
|
// no change
|
|
|
|
if( pAnimIndex == mCurrentAnimIndex)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// kill data of the previous anim
|
|
|
|
delete mRootNode; mRootNode = NULL;
|
|
|
|
delete mAnimEvaluator; mAnimEvaluator = NULL;
|
|
|
|
mNodesByName.clear();
|
|
|
|
|
|
|
|
mCurrentAnimIndex = pAnimIndex;
|
|
|
|
|
|
|
|
// create the internal node tree. Do this even in case of invalid animation index
|
|
|
|
// so that the transformation matrices are properly set up to mimic the current scene
|
2008-11-01 14:18:13 +00:00
|
|
|
mRootNode = CreateNodeTree( mScene->mRootNode, NULL);
|
2008-10-29 17:08:03 +00:00
|
|
|
|
|
|
|
// invalid anim index
|
|
|
|
if( mCurrentAnimIndex >= mScene->mNumAnimations)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// create an evaluator for this animation
|
|
|
|
mAnimEvaluator = new AnimEvaluator( mScene->mAnimations[mCurrentAnimIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Calculates the node transformations for the scene.
|
|
|
|
void SceneAnimator::Calculate( double pTime)
|
|
|
|
{
|
|
|
|
// invalid anim
|
|
|
|
if( !mAnimEvaluator)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// calculate current local transformations
|
|
|
|
mAnimEvaluator->Evaluate( pTime);
|
|
|
|
|
|
|
|
// and update all node transformations with the results
|
|
|
|
UpdateTransforms( mRootNode, mAnimEvaluator->GetTransformations());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Retrieves the most recent local transformation matrix for the given node.
|
2009-02-11 20:56:05 +00:00
|
|
|
const aiMatrix4x4& SceneAnimator::GetLocalTransform( const aiNode* node) const
|
2008-10-29 17:08:03 +00:00
|
|
|
{
|
2009-02-11 20:56:05 +00:00
|
|
|
NodeMap::const_iterator it = mNodesByName.find( node);
|
2008-10-29 17:08:03 +00:00
|
|
|
if( it == mNodesByName.end())
|
|
|
|
return mIdentityMatrix;
|
|
|
|
|
|
|
|
return it->second->mLocalTransform;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Retrieves the most recent global transformation matrix for the given node.
|
2009-02-11 20:56:05 +00:00
|
|
|
const aiMatrix4x4& SceneAnimator::GetGlobalTransform( const aiNode* node) const
|
2008-10-29 17:08:03 +00:00
|
|
|
{
|
2009-02-11 20:56:05 +00:00
|
|
|
NodeMap::const_iterator it = mNodesByName.find( node);
|
2008-10-29 17:08:03 +00:00
|
|
|
if( it == mNodesByName.end())
|
|
|
|
return mIdentityMatrix;
|
|
|
|
|
2008-11-01 14:18:13 +00:00
|
|
|
return it->second->mGlobalTransform;
|
2008-10-29 17:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Calculates the bone matrices for the given mesh.
|
|
|
|
const std::vector<aiMatrix4x4>& SceneAnimator::GetBoneMatrices( const aiNode* pNode, size_t pMeshIndex /* = 0 */)
|
|
|
|
{
|
2008-10-30 19:51:01 +00:00
|
|
|
ai_assert( pMeshIndex < pNode->mNumMeshes);
|
2008-10-29 17:08:03 +00:00
|
|
|
size_t meshIndex = pNode->mMeshes[pMeshIndex];
|
2008-10-30 19:51:01 +00:00
|
|
|
ai_assert( meshIndex < mScene->mNumMeshes);
|
2008-10-29 17:08:03 +00:00
|
|
|
const aiMesh* mesh = mScene->mMeshes[meshIndex];
|
|
|
|
|
|
|
|
// resize array and initialise it with identity matrices
|
|
|
|
mTransforms.resize( mesh->mNumBones, aiMatrix4x4());
|
|
|
|
|
|
|
|
// calculate the mesh's inverse global transform
|
2009-02-11 20:56:05 +00:00
|
|
|
aiMatrix4x4 globalInverseMeshTransform = GetGlobalTransform( pNode);
|
2008-10-29 17:08:03 +00:00
|
|
|
globalInverseMeshTransform.Inverse();
|
|
|
|
|
|
|
|
// Bone matrices transform from mesh coordinates in bind pose to mesh coordinates in skinned pose
|
|
|
|
// Therefore the formula is offsetMatrix * currentGlobalTransform * inverseCurrentMeshTransform
|
|
|
|
for( size_t a = 0; a < mesh->mNumBones; ++a)
|
|
|
|
{
|
|
|
|
const aiBone* bone = mesh->mBones[a];
|
2009-02-11 20:56:05 +00:00
|
|
|
const aiMatrix4x4& currentGlobalTransform = GetGlobalTransform( mBoneNodesByName[ bone->mName.data ]);
|
2008-11-01 14:18:13 +00:00
|
|
|
mTransforms[a] = globalInverseMeshTransform * currentGlobalTransform * bone->mOffsetMatrix;
|
2008-10-29 17:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// and return the result
|
|
|
|
return mTransforms;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Recursively creates an internal node structure matching the current scene and animation.
|
2008-11-01 14:18:13 +00:00
|
|
|
SceneAnimNode* SceneAnimator::CreateNodeTree( aiNode* pNode, SceneAnimNode* pParent)
|
2008-10-29 17:08:03 +00:00
|
|
|
{
|
|
|
|
// create a node
|
|
|
|
SceneAnimNode* internalNode = new SceneAnimNode( pNode->mName.data);
|
2008-11-01 14:18:13 +00:00
|
|
|
internalNode->mParent = pParent;
|
2009-02-11 20:56:05 +00:00
|
|
|
mNodesByName[pNode] = internalNode;
|
2008-10-29 17:08:03 +00:00
|
|
|
|
|
|
|
// copy its transformation
|
|
|
|
internalNode->mLocalTransform = pNode->mTransformation;
|
|
|
|
CalculateGlobalTransform( internalNode);
|
|
|
|
|
|
|
|
// find the index of the animation track affecting this node, if any
|
|
|
|
if( mCurrentAnimIndex < mScene->mNumAnimations)
|
|
|
|
{
|
|
|
|
internalNode->mChannelIndex = -1;
|
|
|
|
const aiAnimation* currentAnim = mScene->mAnimations[mCurrentAnimIndex];
|
|
|
|
for( unsigned int a = 0; a < currentAnim->mNumChannels; a++)
|
|
|
|
{
|
|
|
|
if( currentAnim->mChannels[a]->mNodeName.data == internalNode->mName)
|
|
|
|
{
|
|
|
|
internalNode->mChannelIndex = a;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// continue for all child nodes and assign the created internal nodes as our children
|
|
|
|
for( unsigned int a = 0; a < pNode->mNumChildren; a++)
|
|
|
|
{
|
2008-11-01 14:18:13 +00:00
|
|
|
SceneAnimNode* childNode = CreateNodeTree( pNode->mChildren[a], internalNode);
|
2008-10-29 17:08:03 +00:00
|
|
|
internalNode->mChildren.push_back( childNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return internalNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Recursively updates the internal node transformations from the given matrix array
|
|
|
|
void SceneAnimator::UpdateTransforms( SceneAnimNode* pNode, const std::vector<aiMatrix4x4>& pTransforms)
|
|
|
|
{
|
|
|
|
// update node local transform
|
|
|
|
if( pNode->mChannelIndex != -1)
|
|
|
|
{
|
2008-10-30 19:51:01 +00:00
|
|
|
ai_assert( pNode->mChannelIndex < pTransforms.size());
|
2008-10-29 17:08:03 +00:00
|
|
|
pNode->mLocalTransform = pTransforms[pNode->mChannelIndex];
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
// update global transform as well
|
|
|
|
CalculateGlobalTransform( pNode);
|
|
|
|
|
2008-10-29 17:08:03 +00:00
|
|
|
// continue for all children
|
|
|
|
for( std::vector<SceneAnimNode*>::iterator it = pNode->mChildren.begin(); it != pNode->mChildren.end(); ++it)
|
|
|
|
UpdateTransforms( *it, pTransforms);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Calculates the global transformation matrix for the given internal node
|
|
|
|
void SceneAnimator::CalculateGlobalTransform( SceneAnimNode* pInternalNode)
|
|
|
|
{
|
|
|
|
// concatenate all parent transforms to get the global transform for this node
|
|
|
|
pInternalNode->mGlobalTransform = pInternalNode->mLocalTransform;
|
|
|
|
SceneAnimNode* node = pInternalNode->mParent;
|
|
|
|
while( node)
|
|
|
|
{
|
2008-11-01 14:18:13 +00:00
|
|
|
pInternalNode->mGlobalTransform = node->mLocalTransform * pInternalNode->mGlobalTransform;
|
2008-10-29 17:08:03 +00:00
|
|
|
node = node->mParent;
|
|
|
|
}
|
|
|
|
}
|