2011-12-12 04:42:39 +00:00
|
|
|
#include "animation.hpp"
|
|
|
|
|
2013-01-05 12:01:11 +00:00
|
|
|
#include <OgreSkeletonManager.h>
|
2012-07-03 13:32:38 +00:00
|
|
|
#include <OgreSkeletonInstance.h>
|
|
|
|
#include <OgreEntity.h>
|
|
|
|
#include <OgreBone.h>
|
|
|
|
#include <OgreSubMesh.h>
|
2012-07-18 07:17:39 +00:00
|
|
|
#include <OgreSceneManager.h>
|
2011-12-17 06:29:08 +00:00
|
|
|
|
2013-01-07 05:18:48 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2013-02-23 22:15:11 +00:00
|
|
|
#include "../mwbase/soundmanager.hpp"
|
2013-01-07 05:18:48 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
|
2013-01-16 21:09:21 +00:00
|
|
|
#include "../mwmechanics/character.hpp"
|
|
|
|
|
2012-07-13 10:51:58 +00:00
|
|
|
namespace MWRender
|
|
|
|
{
|
2012-07-13 03:12:18 +00:00
|
|
|
|
2013-01-07 01:05:48 +00:00
|
|
|
Animation::Animation(const MWWorld::Ptr &ptr)
|
|
|
|
: mPtr(ptr)
|
2013-01-16 21:09:21 +00:00
|
|
|
, mController(NULL)
|
2013-01-07 01:05:48 +00:00
|
|
|
, mInsert(NULL)
|
2013-01-07 05:18:48 +00:00
|
|
|
, mAccumRoot(NULL)
|
2013-01-07 01:31:53 +00:00
|
|
|
, mNonAccumRoot(NULL)
|
2013-01-18 22:25:32 +00:00
|
|
|
, mAccumulate(Ogre::Vector3::ZERO)
|
2013-01-07 05:18:48 +00:00
|
|
|
, mLastPosition(0.0f)
|
2013-01-17 21:18:40 +00:00
|
|
|
, mCurrentKeys(NULL)
|
2013-01-30 15:34:07 +00:00
|
|
|
, mCurrentAnim(NULL)
|
|
|
|
, mCurrentTime(0.0f)
|
2013-02-23 13:15:10 +00:00
|
|
|
, mStopTime(0.0f)
|
2013-01-29 08:43:42 +00:00
|
|
|
, mPlaying(false)
|
2013-01-30 15:34:07 +00:00
|
|
|
, mLooping(false)
|
2013-02-15 10:15:39 +00:00
|
|
|
, mAnimVelocity(0.0f)
|
2013-01-19 05:40:47 +00:00
|
|
|
, mAnimSpeedMult(1.0f)
|
2012-07-13 03:12:18 +00:00
|
|
|
{
|
2011-12-17 06:29:08 +00:00
|
|
|
}
|
2012-02-20 13:02:24 +00:00
|
|
|
|
2012-07-13 03:12:18 +00:00
|
|
|
Animation::~Animation()
|
|
|
|
{
|
2013-01-05 07:19:48 +00:00
|
|
|
if(mInsert)
|
|
|
|
{
|
|
|
|
Ogre::SceneManager *sceneMgr = mInsert->getCreator();
|
2013-04-07 08:52:35 +00:00
|
|
|
for(size_t i = 0;i < mObjectList.mParticles.size();i++)
|
|
|
|
sceneMgr->destroyParticleSystem(mObjectList.mParticles[i]);
|
|
|
|
for(size_t i = 0;i < mObjectList.mEntities.size();i++)
|
|
|
|
sceneMgr->destroyEntity(mObjectList.mEntities[i]);
|
2013-01-05 07:19:48 +00:00
|
|
|
}
|
2013-04-07 08:52:35 +00:00
|
|
|
mObjectList.mControllers.clear();
|
2013-04-07 09:28:15 +00:00
|
|
|
mObjectList.mCameras.clear();
|
2013-04-07 08:52:35 +00:00
|
|
|
mObjectList.mParticles.clear();
|
|
|
|
mObjectList.mEntities.clear();
|
|
|
|
mObjectList.mSkelBase = NULL;
|
2013-01-05 07:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-23 11:34:03 +00:00
|
|
|
void Animation::setAnimationSources(const std::vector<std::string> &names)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-07 08:52:35 +00:00
|
|
|
if(!mObjectList.mSkelBase)
|
2013-02-23 15:36:11 +00:00
|
|
|
return;
|
|
|
|
|
2013-02-23 11:34:03 +00:00
|
|
|
mCurrentAnim = NULL;
|
|
|
|
mCurrentKeys = NULL;
|
|
|
|
mAnimVelocity = 0.0f;
|
|
|
|
mAccumRoot = NULL;
|
|
|
|
mNonAccumRoot = NULL;
|
2013-04-07 18:09:55 +00:00
|
|
|
mTextKeys.clear();
|
2013-02-23 11:34:03 +00:00
|
|
|
mSkeletonSources.clear();
|
|
|
|
|
2013-02-27 20:33:36 +00:00
|
|
|
std::vector<std::string>::const_iterator nameiter;
|
2013-02-23 15:36:11 +00:00
|
|
|
for(nameiter = names.begin();nameiter != names.end();nameiter++)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-02-27 20:33:36 +00:00
|
|
|
Ogre::SkeletonPtr skel = NifOgre::Loader::getSkeleton(*nameiter);
|
2013-02-06 01:55:12 +00:00
|
|
|
if(skel.isNull())
|
|
|
|
{
|
2013-02-27 20:33:36 +00:00
|
|
|
std::cerr<< "Failed to get skeleton source "<<*nameiter <<std::endl;
|
|
|
|
continue;
|
2013-02-06 01:55:12 +00:00
|
|
|
}
|
2013-02-23 11:34:03 +00:00
|
|
|
skel->touch();
|
2013-02-06 00:29:51 +00:00
|
|
|
|
2013-02-23 11:34:03 +00:00
|
|
|
Ogre::Skeleton::BoneIterator boneiter = skel->getBoneIterator();
|
|
|
|
while(boneiter.hasMoreElements())
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-02-23 11:34:03 +00:00
|
|
|
Ogre::Bone *bone = boneiter.getNext();
|
|
|
|
Ogre::UserObjectBindings &bindings = bone->getUserObjectBindings();
|
|
|
|
const Ogre::Any &data = bindings.getUserAny(NifOgre::sTextKeyExtraDataID);
|
|
|
|
if(data.isEmpty() || !Ogre::any_cast<bool>(data))
|
|
|
|
continue;
|
|
|
|
|
2013-02-23 15:36:11 +00:00
|
|
|
if(!mNonAccumRoot)
|
2013-02-23 11:34:03 +00:00
|
|
|
{
|
|
|
|
mAccumRoot = mInsert;
|
2013-04-07 08:52:35 +00:00
|
|
|
mNonAccumRoot = mObjectList.mSkelBase->getSkeleton()->getBone(bone->getName());
|
2013-02-23 11:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mSkeletonSources.push_back(skel);
|
|
|
|
for(int i = 0;i < skel->getNumAnimations();i++)
|
|
|
|
{
|
|
|
|
Ogre::Animation *anim = skel->getAnimation(i);
|
|
|
|
const Ogre::Any &groupdata = bindings.getUserAny(std::string(NifOgre::sTextKeyExtraDataID)+
|
2013-04-07 18:09:55 +00:00
|
|
|
"@"+anim->getName());
|
2013-02-23 11:34:03 +00:00
|
|
|
if(!groupdata.isEmpty())
|
|
|
|
mTextKeys[anim->getName()] = Ogre::any_cast<NifOgre::TextKeyMap>(groupdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2013-02-06 00:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-07 08:52:35 +00:00
|
|
|
void Animation::createObjectList(Ogre::SceneNode *node, const std::string &model)
|
2013-01-05 07:19:48 +00:00
|
|
|
{
|
2013-02-02 13:43:37 +00:00
|
|
|
mInsert = node->createChildSceneNode();
|
2013-01-05 07:19:48 +00:00
|
|
|
assert(mInsert);
|
|
|
|
|
2013-04-07 08:52:35 +00:00
|
|
|
mObjectList = NifOgre::Loader::createObjects(mInsert, model);
|
|
|
|
if(mObjectList.mSkelBase)
|
2013-01-05 07:19:48 +00:00
|
|
|
{
|
2013-04-07 08:52:35 +00:00
|
|
|
Ogre::AnimationStateSet *aset = mObjectList.mSkelBase->getAllAnimationStates();
|
2013-01-09 11:30:55 +00:00
|
|
|
Ogre::AnimationStateIterator asiter = aset->getAnimationStateIterator();
|
|
|
|
while(asiter.hasMoreElements())
|
|
|
|
{
|
|
|
|
Ogre::AnimationState *state = asiter.getNext();
|
|
|
|
state->setEnabled(false);
|
|
|
|
state->setLoop(false);
|
|
|
|
}
|
|
|
|
|
2013-02-06 00:29:51 +00:00
|
|
|
// Set the bones as manually controlled since we're applying the
|
|
|
|
// transformations manually (needed if we want to apply an animation
|
|
|
|
// from one skeleton onto another).
|
2013-04-07 08:52:35 +00:00
|
|
|
Ogre::SkeletonInstance *skelinst = mObjectList.mSkelBase->getSkeleton();
|
2013-02-06 00:29:51 +00:00
|
|
|
Ogre::Skeleton::BoneIterator boneiter = skelinst->getBoneIterator();
|
2013-01-09 11:30:55 +00:00
|
|
|
while(boneiter.hasMoreElements())
|
2013-02-06 00:29:51 +00:00
|
|
|
boneiter.getNext()->setManuallyControlled(true);
|
2013-01-05 07:19:48 +00:00
|
|
|
}
|
2013-04-05 17:13:54 +00:00
|
|
|
|
|
|
|
Ogre::SharedPtr<Ogre::ControllerValue<Ogre::Real> > ctrlval(OGRE_NEW AnimationValue(this));
|
2013-04-07 08:52:35 +00:00
|
|
|
for(size_t i = 0;i < mObjectList.mControllers.size();i++)
|
2013-04-06 16:44:10 +00:00
|
|
|
{
|
2013-04-07 08:52:35 +00:00
|
|
|
if(mObjectList.mControllers[i].getSource().isNull())
|
|
|
|
mObjectList.mControllers[i].setSource(ctrlval);
|
2013-04-06 16:44:10 +00:00
|
|
|
}
|
2012-07-13 03:12:18 +00:00
|
|
|
}
|
2012-01-07 03:52:15 +00:00
|
|
|
|
2012-07-24 20:51:48 +00:00
|
|
|
|
2013-01-20 05:55:04 +00:00
|
|
|
bool Animation::hasAnimation(const std::string &anim)
|
2013-01-16 23:00:06 +00:00
|
|
|
{
|
2013-02-06 00:29:51 +00:00
|
|
|
for(std::vector<Ogre::SkeletonPtr>::const_iterator iter(mSkeletonSources.begin());iter != mSkeletonSources.end();iter++)
|
|
|
|
{
|
|
|
|
if((*iter)->hasAnimation(anim))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-01-16 23:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-16 19:01:08 +00:00
|
|
|
void Animation::setController(MWMechanics::CharacterController *controller)
|
|
|
|
{
|
|
|
|
mController = controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-18 22:25:32 +00:00
|
|
|
void Animation::setAccumulation(const Ogre::Vector3 &accum)
|
|
|
|
{
|
|
|
|
mAccumulate = accum;
|
|
|
|
}
|
|
|
|
|
2013-02-15 10:15:39 +00:00
|
|
|
void Animation::setSpeed(float speed)
|
|
|
|
{
|
|
|
|
mAnimSpeedMult = 1.0f;
|
|
|
|
if(mAnimVelocity > 1.0f && speed > 0.0f)
|
|
|
|
mAnimSpeedMult = speed / mAnimVelocity;
|
|
|
|
}
|
|
|
|
|
2013-02-19 12:01:33 +00:00
|
|
|
void Animation::setLooping(bool loop)
|
|
|
|
{
|
|
|
|
mLooping = loop;
|
|
|
|
}
|
|
|
|
|
2013-02-23 18:12:36 +00:00
|
|
|
void Animation::updatePtr(const MWWorld::Ptr &ptr)
|
|
|
|
{
|
|
|
|
mPtr = ptr;
|
|
|
|
}
|
|
|
|
|
2013-01-18 22:25:32 +00:00
|
|
|
|
2013-02-22 17:22:06 +00:00
|
|
|
void Animation::calcAnimVelocity()
|
|
|
|
{
|
|
|
|
const Ogre::NodeAnimationTrack *track = 0;
|
|
|
|
|
|
|
|
Ogre::Animation::NodeTrackIterator trackiter = mCurrentAnim->getNodeTrackIterator();
|
|
|
|
while(!track && trackiter.hasMoreElements())
|
|
|
|
{
|
|
|
|
const Ogre::NodeAnimationTrack *cur = trackiter.getNext();
|
|
|
|
if(cur->getAssociatedNode()->getName() == mNonAccumRoot->getName())
|
|
|
|
track = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(track && track->getNumKeyFrames() > 1)
|
|
|
|
{
|
|
|
|
float loopstarttime = 0.0f;
|
|
|
|
float loopstoptime = mCurrentAnim->getLength();
|
|
|
|
NifOgre::TextKeyMap::const_iterator keyiter = mCurrentKeys->begin();
|
|
|
|
while(keyiter != mCurrentKeys->end())
|
|
|
|
{
|
|
|
|
if(keyiter->second == "loop start")
|
|
|
|
loopstarttime = keyiter->first;
|
|
|
|
else if(keyiter->second == "loop stop")
|
|
|
|
{
|
|
|
|
loopstoptime = keyiter->first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
keyiter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(loopstoptime > loopstarttime)
|
|
|
|
{
|
|
|
|
Ogre::TransformKeyFrame startkf(0, loopstarttime);
|
|
|
|
Ogre::TransformKeyFrame endkf(0, loopstoptime);
|
|
|
|
|
|
|
|
track->getInterpolatedKeyFrame(mCurrentAnim->_getTimeIndex(loopstarttime), &startkf);
|
|
|
|
track->getInterpolatedKeyFrame(mCurrentAnim->_getTimeIndex(loopstoptime), &endkf);
|
|
|
|
|
|
|
|
mAnimVelocity = startkf.getTranslate().distance(endkf.getTranslate()) /
|
|
|
|
(loopstoptime-loopstarttime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:04:18 +00:00
|
|
|
void Animation::applyAnimation(const Ogre::Animation *anim, float time, Ogre::SkeletonInstance *skel)
|
|
|
|
{
|
|
|
|
Ogre::TimeIndex timeindex = anim->_getTimeIndex(time);
|
|
|
|
Ogre::Animation::NodeTrackIterator tracks = anim->getNodeTrackIterator();
|
|
|
|
while(tracks.hasMoreElements())
|
|
|
|
{
|
|
|
|
Ogre::NodeAnimationTrack *track = tracks.getNext();
|
|
|
|
const Ogre::String &targetname = track->getAssociatedNode()->getName();
|
|
|
|
if(!skel->hasBone(targetname))
|
|
|
|
continue;
|
|
|
|
Ogre::Bone *bone = skel->getBone(targetname);
|
|
|
|
bone->setOrientation(Ogre::Quaternion::IDENTITY);
|
|
|
|
bone->setPosition(Ogre::Vector3::ZERO);
|
|
|
|
bone->setScale(Ogre::Vector3::UNIT_SCALE);
|
|
|
|
track->applyToNode(bone, timeindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: Dirty the animation state set so that Ogre will apply the
|
|
|
|
// transformations to entities this skeleton instance is shared with.
|
2013-04-07 08:52:35 +00:00
|
|
|
mObjectList.mSkelBase->getAllAnimationStates()->_notifyDirty();
|
2013-01-30 15:04:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 10:32:48 +00:00
|
|
|
static void updateBoneTree(const Ogre::SkeletonInstance *skelsrc, Ogre::Bone *bone)
|
2013-01-30 17:29:16 +00:00
|
|
|
{
|
2013-02-20 10:32:48 +00:00
|
|
|
if(skelsrc->hasBone(bone->getName()))
|
2013-01-30 17:29:16 +00:00
|
|
|
{
|
|
|
|
Ogre::Bone *srcbone = skelsrc->getBone(bone->getName());
|
2013-02-20 10:32:48 +00:00
|
|
|
if(!srcbone->getParent() || !bone->getParent())
|
|
|
|
{
|
|
|
|
bone->setOrientation(srcbone->getOrientation());
|
|
|
|
bone->setPosition(srcbone->getPosition());
|
|
|
|
bone->setScale(srcbone->getScale());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bone->_setDerivedOrientation(srcbone->_getDerivedOrientation());
|
|
|
|
bone->_setDerivedPosition(srcbone->_getDerivedPosition());
|
|
|
|
bone->setScale(Ogre::Vector3::UNIT_SCALE);
|
|
|
|
}
|
2013-01-30 17:29:16 +00:00
|
|
|
}
|
2013-02-20 10:32:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// No matching bone in the source. Make sure it stays properly offset
|
|
|
|
// from its parent.
|
|
|
|
bone->resetToInitialState();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ogre::Node::ChildNodeIterator boneiter = bone->getChildIterator();
|
|
|
|
while(boneiter.hasMoreElements())
|
|
|
|
updateBoneTree(skelsrc, static_cast<Ogre::Bone*>(boneiter.getNext()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::updateSkeletonInstance(const Ogre::SkeletonInstance *skelsrc, Ogre::SkeletonInstance *skel)
|
|
|
|
{
|
|
|
|
Ogre::Skeleton::BoneIterator boneiter = skel->getRootBoneIterator();
|
|
|
|
while(boneiter.hasMoreElements())
|
|
|
|
updateBoneTree(skelsrc, boneiter.getNext());
|
2013-01-30 17:29:16 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 15:04:18 +00:00
|
|
|
|
2013-01-19 00:21:29 +00:00
|
|
|
Ogre::Vector3 Animation::updatePosition(float time)
|
2013-01-07 05:18:48 +00:00
|
|
|
{
|
2013-01-30 15:34:07 +00:00
|
|
|
if(mLooping)
|
|
|
|
mCurrentTime = std::fmod(std::max(time, 0.0f), mCurrentAnim->getLength());
|
|
|
|
else
|
|
|
|
mCurrentTime = std::min(mCurrentAnim->getLength(), std::max(time, 0.0f));
|
2013-04-07 08:52:35 +00:00
|
|
|
applyAnimation(mCurrentAnim, mCurrentTime, mObjectList.mSkelBase->getSkeleton());
|
2013-01-17 04:14:49 +00:00
|
|
|
|
2013-01-19 00:21:29 +00:00
|
|
|
Ogre::Vector3 posdiff = Ogre::Vector3::ZERO;
|
2013-01-07 05:18:48 +00:00
|
|
|
if(mNonAccumRoot)
|
|
|
|
{
|
2013-01-30 15:04:18 +00:00
|
|
|
/* Get the non-accumulation root's difference from the last update. */
|
2013-01-19 00:21:29 +00:00
|
|
|
posdiff = (mNonAccumRoot->getPosition() - mLastPosition) * mAccumulate;
|
2013-01-07 05:18:48 +00:00
|
|
|
|
|
|
|
/* Translate the accumulation root back to compensate for the move. */
|
|
|
|
mLastPosition += posdiff;
|
2013-02-19 14:04:25 +00:00
|
|
|
mAccumRoot->setPosition(-mLastPosition);
|
2013-01-07 05:18:48 +00:00
|
|
|
}
|
2013-01-19 00:21:29 +00:00
|
|
|
return posdiff;
|
2013-01-07 05:18:48 +00:00
|
|
|
}
|
|
|
|
|
2013-02-23 13:15:10 +00:00
|
|
|
void Animation::reset(const std::string &start, const std::string &stop)
|
2013-01-07 05:18:48 +00:00
|
|
|
{
|
2013-01-17 21:18:40 +00:00
|
|
|
mNextKey = mCurrentKeys->begin();
|
2013-01-16 19:57:08 +00:00
|
|
|
|
2013-02-23 13:15:10 +00:00
|
|
|
while(mNextKey != mCurrentKeys->end() && mNextKey->second != start)
|
|
|
|
mNextKey++;
|
2013-01-22 08:31:45 +00:00
|
|
|
if(mNextKey != mCurrentKeys->end())
|
2013-01-30 15:34:07 +00:00
|
|
|
mCurrentTime = mNextKey->first;
|
2013-01-22 08:31:45 +00:00
|
|
|
else
|
2013-01-09 11:30:55 +00:00
|
|
|
{
|
2013-01-17 21:18:40 +00:00
|
|
|
mNextKey = mCurrentKeys->begin();
|
2013-04-05 15:27:26 +00:00
|
|
|
while(mNextKey != mCurrentKeys->end() && mNextKey->second != "start")
|
|
|
|
mNextKey++;
|
|
|
|
if(mNextKey != mCurrentKeys->end())
|
|
|
|
mCurrentTime = mNextKey->first;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mNextKey = mCurrentKeys->begin();
|
|
|
|
mCurrentTime = 0.0f;
|
|
|
|
}
|
2013-01-09 11:30:55 +00:00
|
|
|
}
|
|
|
|
|
2013-02-23 13:15:10 +00:00
|
|
|
if(stop.length() > 0)
|
|
|
|
{
|
|
|
|
NifOgre::TextKeyMap::const_iterator stopKey = mNextKey;
|
|
|
|
while(stopKey != mCurrentKeys->end() && stopKey->second != stop)
|
|
|
|
stopKey++;
|
|
|
|
if(stopKey != mCurrentKeys->end())
|
|
|
|
mStopTime = stopKey->first;
|
|
|
|
else
|
|
|
|
mStopTime = mCurrentAnim->getLength();
|
|
|
|
}
|
|
|
|
|
2013-01-22 08:31:45 +00:00
|
|
|
if(mNonAccumRoot)
|
2012-07-24 20:51:48 +00:00
|
|
|
{
|
2013-02-19 14:04:25 +00:00
|
|
|
const Ogre::NodeAnimationTrack *track = 0;
|
|
|
|
Ogre::Animation::NodeTrackIterator trackiter = mCurrentAnim->getNodeTrackIterator();
|
|
|
|
while(!track && trackiter.hasMoreElements())
|
|
|
|
{
|
|
|
|
const Ogre::NodeAnimationTrack *cur = trackiter.getNext();
|
|
|
|
if(cur->getAssociatedNode()->getName() == mNonAccumRoot->getName())
|
|
|
|
track = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(track)
|
|
|
|
{
|
|
|
|
Ogre::TransformKeyFrame kf(0, mCurrentTime);
|
|
|
|
track->getInterpolatedKeyFrame(mCurrentAnim->_getTimeIndex(mCurrentTime), &kf);
|
|
|
|
mLastPosition = kf.getTranslate() * mAccumulate;
|
|
|
|
}
|
2012-07-24 20:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-23 22:39:01 +00:00
|
|
|
bool Animation::handleEvent(float time, const std::string &evt)
|
|
|
|
{
|
|
|
|
if(evt == "start" || evt == "loop start")
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(evt.compare(0, 7, "sound: ") == 0)
|
|
|
|
{
|
|
|
|
MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager();
|
|
|
|
sndMgr->playSound3D(mPtr, evt.substr(7), 1.0f, 1.0f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(evt.compare(0, 10, "soundgen: ") == 0)
|
|
|
|
{
|
|
|
|
// FIXME: Lookup the SoundGen (SNDG) for the specified sound that corresponds
|
|
|
|
// to this actor type
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(evt == "loop stop")
|
|
|
|
{
|
|
|
|
if(mLooping)
|
|
|
|
{
|
|
|
|
reset("loop start", "");
|
|
|
|
if(mCurrentTime >= time)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(evt == "stop")
|
|
|
|
{
|
|
|
|
if(mLooping)
|
|
|
|
{
|
|
|
|
reset("loop start", "");
|
|
|
|
if(mCurrentTime >= time)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// fall-through
|
|
|
|
}
|
|
|
|
if(mController)
|
|
|
|
mController->markerEvent(time, evt);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-23 13:15:10 +00:00
|
|
|
void Animation::play(const std::string &groupname, const std::string &start, const std::string &stop, bool loop)
|
2012-07-13 03:12:18 +00:00
|
|
|
{
|
2013-01-09 09:40:38 +00:00
|
|
|
try {
|
2013-02-06 00:29:51 +00:00
|
|
|
bool found = false;
|
|
|
|
/* Look in reverse; last-inserted source has priority. */
|
|
|
|
for(std::vector<Ogre::SkeletonPtr>::const_reverse_iterator iter(mSkeletonSources.rbegin());iter != mSkeletonSources.rend();iter++)
|
|
|
|
{
|
|
|
|
if((*iter)->hasAnimation(groupname))
|
|
|
|
{
|
|
|
|
mCurrentAnim = (*iter)->getAnimation(groupname);
|
|
|
|
mCurrentKeys = &mTextKeys[groupname];
|
2013-02-15 10:15:39 +00:00
|
|
|
mAnimVelocity = 0.0f;
|
|
|
|
|
|
|
|
if(mNonAccumRoot)
|
2013-02-22 17:22:06 +00:00
|
|
|
calcAnimVelocity();
|
2013-02-15 10:15:39 +00:00
|
|
|
|
2013-02-06 00:29:51 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found)
|
|
|
|
throw std::runtime_error("Failed to find animation "+groupname);
|
2013-01-30 15:34:07 +00:00
|
|
|
|
2013-02-23 13:15:10 +00:00
|
|
|
reset(start, stop);
|
|
|
|
setLooping(loop);
|
2013-01-29 08:43:42 +00:00
|
|
|
mPlaying = true;
|
2012-07-24 20:51:48 +00:00
|
|
|
}
|
2013-01-09 09:40:38 +00:00
|
|
|
catch(std::exception &e) {
|
|
|
|
std::cerr<< e.what() <<std::endl;
|
2013-01-07 13:56:03 +00:00
|
|
|
}
|
2012-07-24 20:51:48 +00:00
|
|
|
}
|
2011-12-15 05:33:10 +00:00
|
|
|
|
2013-01-19 00:21:29 +00:00
|
|
|
Ogre::Vector3 Animation::runAnimation(float timepassed)
|
2012-07-21 21:41:26 +00:00
|
|
|
{
|
2013-01-19 00:21:29 +00:00
|
|
|
Ogre::Vector3 movement = Ogre::Vector3::ZERO;
|
2013-01-30 15:04:18 +00:00
|
|
|
|
2013-01-19 05:40:47 +00:00
|
|
|
timepassed *= mAnimSpeedMult;
|
2013-02-03 09:38:42 +00:00
|
|
|
while(mCurrentAnim && mPlaying)
|
2012-07-21 21:41:26 +00:00
|
|
|
{
|
2013-02-23 13:15:10 +00:00
|
|
|
float targetTime = std::min(mStopTime, mCurrentTime+timepassed);
|
2013-01-17 22:49:42 +00:00
|
|
|
if(mNextKey == mCurrentKeys->end() || mNextKey->first > targetTime)
|
2012-07-24 20:51:48 +00:00
|
|
|
{
|
2013-01-19 00:21:29 +00:00
|
|
|
movement += updatePosition(targetTime);
|
2013-02-23 13:15:10 +00:00
|
|
|
mPlaying = (mLooping || mStopTime > targetTime);
|
2013-01-17 22:49:42 +00:00
|
|
|
break;
|
2013-01-16 19:57:08 +00:00
|
|
|
}
|
2012-07-21 21:41:26 +00:00
|
|
|
|
2013-01-18 22:50:55 +00:00
|
|
|
float time = mNextKey->first;
|
2013-01-17 22:49:42 +00:00
|
|
|
const std::string &evt = mNextKey->second;
|
|
|
|
mNextKey++;
|
|
|
|
|
2013-01-19 00:21:29 +00:00
|
|
|
movement += updatePosition(time);
|
2013-02-23 13:15:10 +00:00
|
|
|
mPlaying = (mLooping || mStopTime > time);
|
|
|
|
|
2013-01-18 22:50:55 +00:00
|
|
|
timepassed = targetTime - time;
|
|
|
|
|
2013-02-23 22:39:01 +00:00
|
|
|
if(!handleEvent(time, evt))
|
|
|
|
break;
|
2012-07-21 21:41:26 +00:00
|
|
|
}
|
2013-04-07 08:52:35 +00:00
|
|
|
for(size_t i = 0;i < mObjectList.mControllers.size();i++)
|
|
|
|
mObjectList.mControllers[i].update();
|
2013-01-30 15:04:18 +00:00
|
|
|
|
2013-01-19 00:21:29 +00:00
|
|
|
return movement;
|
2012-07-21 21:41:26 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 13:02:24 +00:00
|
|
|
}
|