Пример #1
0
// creates a node tree that matches the current scene and animation
AnimationNode* Animator::CreateNodeTree(aiNode* node, AnimationNode* parent)
{
    //create a node
    AnimationNode* internalNode = new AnimationNode(node->mName.data);
    internalNode->parent = parent;
    mapNodesByName[node] = internalNode;
    
    //copy its transformation
    internalNode->localTransform = node->mTransformation;
    CalculateGlobalTransform(internalNode);
    
    //find the index of the animation track affecting this node, if any
    if (currentAnimationIndex < mainScene->mNumAnimations)
    {
        internalNode->channelIndex = -1;
        
        for (unsigned int i = 0; i < currentAnimation->mNumChannels; i++)
        {
            if (currentAnimation->mChannels[i]->mNodeName.data == internalNode->name)
            {
                internalNode->channelIndex = i;
                break;
            }
        }
    }
    
    //continue for all child nodes and assign the created internal nodes as our children
    for (unsigned int i = 0; i < node->mNumChildren; i++)
    {
        AnimationNode* childNode = CreateNodeTree(node->mChildren[i], internalNode);
        internalNode->children.push_back(childNode);
    }
    
    return internalNode;
}
// ------------------------------------------------------------------------------------------------
// Recursively creates an internal node structure matching the current scene and animation.
SceneAnimNode* SceneAnimator::CreateNodeTree(aiNode* pNode, SceneAnimNode* pParent)
{
	// create a node
	SceneAnimNode* internalNode = new SceneAnimNode(pNode->mName.data);
	internalNode->mParent = pParent;
	mNodesByName[pNode] = internalNode;

	// 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++)
	{
		SceneAnimNode* childNode = CreateNodeTree(pNode->mChildren[a], internalNode);
		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)
	{
		ai_assert(pNode->mChannelIndex < pTransforms.size());
		pNode->mLocalTransform = pTransforms[pNode->mChannelIndex];
	}

	// update global transform as well
	CalculateGlobalTransform(pNode);

	// continue for all children
	for (std::vector<SceneAnimNode*>::iterator it = pNode->mChildren.begin(); it != pNode->mChildren.end(); ++it)
		UpdateTransforms(*it, pTransforms);
}