2015-06-30 02:54:59 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, 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.cpp
|
|
|
|
* @brief Implementation of the utility class SceneAnimator
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "assimp_view.h"
|
|
|
|
|
|
|
|
using namespace AssimpView;
|
|
|
|
|
2019-01-30 15:07:40 +00:00
|
|
|
const aiMatrix4x4 IdentityMatrix;
|
|
|
|
|
2015-06-30 02:54:59 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor for a given scene.
|
2020-05-02 13:14:38 +00:00
|
|
|
SceneAnimator::SceneAnimator(const aiScene *pScene, size_t pAnimIndex) :
|
|
|
|
mScene(pScene),
|
|
|
|
mCurrentAnimIndex(-1),
|
|
|
|
mAnimEvaluator(nullptr),
|
|
|
|
mRootNode(nullptr) {
|
2015-06-30 02:54:59 +00:00
|
|
|
// build the nodes-for-bones table
|
2020-05-02 13:14:38 +00:00
|
|
|
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];
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
mBoneNodesByName[bone->mName.data] = pScene->mRootNode->FindNode(bone->mName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// changing the current animation also creates the node tree for this animation
|
2020-05-02 13:14:38 +00:00
|
|
|
SetAnimIndex(pAnimIndex);
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor
|
2019-01-30 15:07:40 +00:00
|
|
|
SceneAnimator::~SceneAnimator() {
|
2015-06-30 02:54:59 +00:00
|
|
|
delete mRootNode;
|
|
|
|
delete mAnimEvaluator;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Sets the animation to use for playback.
|
2020-05-02 13:14:38 +00:00
|
|
|
void SceneAnimator::SetAnimIndex(size_t pAnimIndex) {
|
2015-06-30 02:54:59 +00:00
|
|
|
// no change
|
2020-05-02 13:14:38 +00:00
|
|
|
if (pAnimIndex == static_cast<unsigned int>(mCurrentAnimIndex)) {
|
2015-06-30 02:54:59 +00:00
|
|
|
return;
|
2019-01-30 15:07:40 +00:00
|
|
|
}
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// kill data of the previous anim
|
2020-05-02 13:14:38 +00:00
|
|
|
delete mRootNode;
|
|
|
|
mRootNode = nullptr;
|
|
|
|
delete mAnimEvaluator;
|
|
|
|
mAnimEvaluator = nullptr;
|
2015-06-30 02:54:59 +00:00
|
|
|
mNodesByName.clear();
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
mCurrentAnimIndex = static_cast<int>(pAnimIndex);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// 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
|
2020-05-02 13:14:38 +00:00
|
|
|
mRootNode = CreateNodeTree(mScene->mRootNode, nullptr);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// invalid anim index
|
2020-05-02 13:14:38 +00:00
|
|
|
if (static_cast<unsigned int>(mCurrentAnimIndex) >= mScene->mNumAnimations) {
|
2015-06-30 02:54:59 +00:00
|
|
|
return;
|
2019-01-30 15:07:40 +00:00
|
|
|
}
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// create an evaluator for this animation
|
2020-05-02 13:14:38 +00:00
|
|
|
mAnimEvaluator = new AnimEvaluator(mScene->mAnimations[mCurrentAnimIndex]);
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Calculates the node transformations for the scene.
|
2020-05-02 13:14:38 +00:00
|
|
|
void SceneAnimator::Calculate(double pTime) {
|
2015-06-30 02:54:59 +00:00
|
|
|
// invalid anim
|
2019-01-30 15:07:40 +00:00
|
|
|
if (!mAnimEvaluator) {
|
2015-06-30 02:54:59 +00:00
|
|
|
return;
|
2019-01-30 15:07:40 +00:00
|
|
|
}
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// calculate current local transformations
|
2020-05-02 13:14:38 +00:00
|
|
|
mAnimEvaluator->Evaluate(pTime);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// and update all node transformations with the results
|
2020-05-02 13:14:38 +00:00
|
|
|
UpdateTransforms(mRootNode, mAnimEvaluator->GetTransformations());
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Retrieves the most recent local transformation matrix for the given node.
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiMatrix4x4 &SceneAnimator::GetLocalTransform(const aiNode *node) const {
|
|
|
|
NodeMap::const_iterator it = mNodesByName.find(node);
|
2019-01-30 15:07:40 +00:00
|
|
|
if (it == mNodesByName.end()) {
|
|
|
|
return IdentityMatrix;
|
|
|
|
}
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
return it->second->mLocalTransform;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Retrieves the most recent global transformation matrix for the given node.
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiMatrix4x4 &SceneAnimator::GetGlobalTransform(const aiNode *node) const {
|
|
|
|
NodeMap::const_iterator it = mNodesByName.find(node);
|
2019-01-30 15:07:40 +00:00
|
|
|
if (it == mNodesByName.end()) {
|
|
|
|
return IdentityMatrix;
|
|
|
|
}
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
return it->second->mGlobalTransform;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Calculates the bone matrices for the given mesh.
|
2020-05-02 13:14:38 +00:00
|
|
|
const std::vector<aiMatrix4x4> &SceneAnimator::GetBoneMatrices(const aiNode *pNode, size_t pMeshIndex /* = 0 */) {
|
|
|
|
ai_assert(pMeshIndex < pNode->mNumMeshes);
|
2015-06-30 02:54:59 +00:00
|
|
|
size_t meshIndex = pNode->mMeshes[pMeshIndex];
|
2020-05-02 13:14:38 +00:00
|
|
|
ai_assert(meshIndex < mScene->mNumMeshes);
|
|
|
|
const aiMesh *mesh = mScene->mMeshes[meshIndex];
|
2015-06-30 02:54:59 +00:00
|
|
|
|
2019-01-30 15:07:40 +00:00
|
|
|
// resize array and initialize it with identity matrices
|
2020-05-02 13:14:38 +00:00
|
|
|
mTransforms.resize(mesh->mNumBones, aiMatrix4x4());
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// calculate the mesh's inverse global transform
|
2020-05-02 13:14:38 +00:00
|
|
|
aiMatrix4x4 globalInverseMeshTransform = GetGlobalTransform(pNode);
|
2015-06-30 02:54:59 +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
|
2020-05-02 13:14:38 +00:00
|
|
|
for (size_t a = 0; a < mesh->mNumBones; ++a) {
|
|
|
|
const aiBone *bone = mesh->mBones[a];
|
|
|
|
const aiMatrix4x4 ¤tGlobalTransform = GetGlobalTransform(mBoneNodesByName[bone->mName.data]);
|
2015-06-30 02:54:59 +00:00
|
|
|
mTransforms[a] = globalInverseMeshTransform * currentGlobalTransform * bone->mOffsetMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and return the result
|
|
|
|
return mTransforms;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Recursively creates an internal node structure matching the current scene and animation.
|
2020-05-02 13:14:38 +00:00
|
|
|
SceneAnimNode *SceneAnimator::CreateNodeTree(aiNode *pNode, SceneAnimNode *pParent) {
|
2015-06-30 02:54:59 +00:00
|
|
|
// create a node
|
2020-05-02 13:14:38 +00:00
|
|
|
SceneAnimNode *internalNode = new SceneAnimNode(pNode->mName.data);
|
2015-06-30 02:54:59 +00:00
|
|
|
internalNode->mParent = pParent;
|
|
|
|
mNodesByName[pNode] = internalNode;
|
|
|
|
|
|
|
|
// copy its transformation
|
|
|
|
internalNode->mLocalTransform = pNode->mTransformation;
|
2020-05-02 13:14:38 +00:00
|
|
|
CalculateGlobalTransform(internalNode);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// find the index of the animation track affecting this node, if any
|
2020-05-02 13:14:38 +00:00
|
|
|
if (static_cast<unsigned int>(mCurrentAnimIndex) < mScene->mNumAnimations) {
|
2015-06-30 02:54:59 +00:00
|
|
|
internalNode->mChannelIndex = -1;
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiAnimation *currentAnim = mScene->mAnimations[mCurrentAnimIndex];
|
|
|
|
for (unsigned int a = 0; a < currentAnim->mNumChannels; a++) {
|
|
|
|
if (currentAnim->mChannels[a]->mNodeName.data == internalNode->mName) {
|
2015-06-30 02:54:59 +00:00
|
|
|
internalNode->mChannelIndex = a;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// continue for all child nodes and assign the created internal nodes as our children
|
2020-05-02 13:14:38 +00:00
|
|
|
for (unsigned int a = 0; a < pNode->mNumChildren; ++a) {
|
|
|
|
SceneAnimNode *childNode = CreateNodeTree(pNode->mChildren[a], internalNode);
|
|
|
|
internalNode->mChildren.push_back(childNode);
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return internalNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Recursively updates the internal node transformations from the given matrix array
|
2020-05-02 13:14:38 +00:00
|
|
|
void SceneAnimator::UpdateTransforms(SceneAnimNode *pNode, const std::vector<aiMatrix4x4> &pTransforms) {
|
2015-06-30 02:54:59 +00:00
|
|
|
// update node local transform
|
2020-05-02 13:14:38 +00:00
|
|
|
if (pNode->mChannelIndex != -1) {
|
|
|
|
ai_assert(static_cast<unsigned int>(pNode->mChannelIndex) < pTransforms.size());
|
2015-06-30 02:54:59 +00:00
|
|
|
pNode->mLocalTransform = pTransforms[pNode->mChannelIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// update global transform as well
|
2020-05-02 13:14:38 +00:00
|
|
|
CalculateGlobalTransform(pNode);
|
2015-06-30 02:54:59 +00:00
|
|
|
|
|
|
|
// continue for all children
|
2020-05-02 13:14:38 +00:00
|
|
|
for (std::vector<SceneAnimNode *>::iterator it = pNode->mChildren.begin(); it != pNode->mChildren.end(); ++it) {
|
2019-01-30 15:07:40 +00:00
|
|
|
UpdateTransforms(*it, pTransforms);
|
|
|
|
}
|
2015-06-30 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Calculates the global transformation matrix for the given internal node
|
2020-05-02 13:14:38 +00:00
|
|
|
void SceneAnimator::CalculateGlobalTransform(SceneAnimNode *pInternalNode) {
|
2015-06-30 02:54:59 +00:00
|
|
|
// concatenate all parent transforms to get the global transform for this node
|
|
|
|
pInternalNode->mGlobalTransform = pInternalNode->mLocalTransform;
|
2020-05-02 13:14:38 +00:00
|
|
|
SceneAnimNode *node = pInternalNode->mParent;
|
|
|
|
while (node) {
|
2015-06-30 02:54:59 +00:00
|
|
|
pInternalNode->mGlobalTransform = node->mLocalTransform * pInternalNode->mGlobalTransform;
|
|
|
|
node = node->mParent;
|
|
|
|
}
|
|
|
|
}
|