1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "engines/stark/model/animhandler.h"
#include "engines/stark/model/model.h"
#include "engines/stark/model/skeleton_anim.h"
namespace Stark {
AnimHandler::AnimHandler() :
_model(nullptr),
_anim(nullptr),
_animTime(-1),
_previousAnim(nullptr),
_previousAnimTime(-1),
_blendAnim(nullptr),
_blendAnimTime(-1),
_blendTimeRemaining(0)
{
}
AnimHandler::~AnimHandler() {
}
void AnimHandler::setAnim(SkeletonAnim *anim) {
if (_anim == anim) {
return;
}
if (_previousAnim) {
if (_previousAnim != anim) {
// Start blending the new animation up with the previous one
startBlending();
} else {
// The previous animation is the same as the one last used for rendering
// Stop blending in case it was started by another anim change since the last frame
stopBlending();
}
}
_anim = anim;
_animTime = 0;
}
void AnimHandler::setModel(Model *model) {
_model = model;
}
void AnimHandler::setNode(uint32 time, BoneNode *bone, const BoneNode *parent) {
const Common::Array<BoneNode *> &bones = _model->getBones();
if (_blendTimeRemaining <= 0) {
_anim->getCoordForBone(time, bone->_idx, bone->_animPos, bone->_animRot);
} else {
// Blend the coordinates of the previous and the current animation
Math::Vector3d previousAnimPos, animPos;
Math::Quaternion previousAnimRot, animRot;
_blendAnim->getCoordForBone(_blendAnimTime, bone->_idx, previousAnimPos, previousAnimRot);
_anim->getCoordForBone(time, bone->_idx, animPos, animRot);
float blendingRatio = 1.0 - _blendTimeRemaining / (float)_blendDuration;
bone->_animPos = previousAnimPos + (animPos - previousAnimPos) * blendingRatio;
bone->_animRot = previousAnimRot.slerpQuat(animRot, blendingRatio);
}
if (parent) {
parent->_animRot.transform(bone->_animPos);
bone->_animPos = parent->_animPos + bone->_animPos;
bone->_animRot = parent->_animRot * bone->_animRot;
}
for (uint i = 0; i < bone->_children.size(); ++i) {
setNode(time, bones[bone->_children[i]], bone);
}
}
void AnimHandler::animate(uint32 time) {
int32 deltaTime = time - _animTime;
if (deltaTime < 0 || time > _blendDuration / 2) {
deltaTime = 33;
}
// Store the last anim that was actually used for rendering
_previousAnim = _anim;
_previousAnimTime = time;
updateBlending(deltaTime);
// Start at root bone
// For each child
// - Set childs animation coordinate
// - Process that childs children
const Common::Array<BoneNode *> &bones = _model->getBones();
if (deltaTime >= 0) {
setNode(time, bones[0], nullptr);
_animTime = time;
}
}
void AnimHandler::startBlending() {
_blendTimeRemaining = _blendDuration;
_blendAnim = _previousAnim;
_blendAnimTime = _previousAnimTime;
}
void AnimHandler::updateBlending(int32 deltaTime) {
_blendTimeRemaining -= deltaTime;
if (_blendTimeRemaining > 0) {
// If we are blending, also update the previous animation's time
_blendAnimTime += deltaTime;
if (_blendAnimTime >= (int32) _blendAnim->getLength()) {
_blendAnimTime = _blendAnim->getLength() - 1;
}
} else {
// Otherwise make sure blending is not enabled
stopBlending();
}
}
void AnimHandler::stopBlending() {
_blendAnim = nullptr;
_blendAnimTime = -1;
_blendTimeRemaining = 0;
}
void AnimHandler::resetBlending() {
stopBlending();
_previousAnim = nullptr;
_previousAnimTime = -1;
}
} // End of namespace Stark
|