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>
|
2013-04-19 09:01:50 +00:00
|
|
|
#include <OgreSubEntity.h>
|
|
|
|
#include <OgreParticleSystem.h>
|
2012-07-03 13:32:38 +00:00
|
|
|
#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-04-07 19:41:27 +00:00
|
|
|
void Animation::destroyObjectList(Ogre::SceneManager *sceneMgr, NifOgre::ObjectList &objects)
|
|
|
|
{
|
|
|
|
for(size_t i = 0;i < objects.mParticles.size();i++)
|
|
|
|
sceneMgr->destroyParticleSystem(objects.mParticles[i]);
|
|
|
|
for(size_t i = 0;i < objects.mEntities.size();i++)
|
|
|
|
sceneMgr->destroyEntity(objects.mEntities[i]);
|
|
|
|
objects.mControllers.clear();
|
|
|
|
objects.mCameras.clear();
|
|
|
|
objects.mParticles.clear();
|
|
|
|
objects.mEntities.clear();
|
|
|
|
objects.mSkelBase = NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-07 01:05:48 +00:00
|
|
|
Animation::Animation(const MWWorld::Ptr &ptr)
|
2013-04-21 00:13:27 +00:00
|
|
|
: mAnimationBaseValuePtr(OGRE_NEW AnimationValue(this))
|
|
|
|
, mPtr(ptr)
|
2013-01-16 21:09:21 +00:00
|
|
|
, mController(NULL)
|
2013-01-07 01:05:48 +00:00
|
|
|
, mInsert(NULL)
|
2013-04-14 23:56:35 +00:00
|
|
|
, mSkelBase(NULL)
|
2013-01-07 05:18:48 +00:00
|
|
|
, mAccumRoot(NULL)
|
2013-01-07 01:31:53 +00:00
|
|
|
, mNonAccumRoot(NULL)
|
2013-04-16 01:55:28 +00:00
|
|
|
, mAccumulate(0.0f)
|
2013-01-07 05:18:48 +00:00
|
|
|
, mLastPosition(0.0f)
|
2013-04-16 03:37:10 +00:00
|
|
|
, mCurrentAnim(NULL)
|
2013-04-07 21:56:23 +00:00
|
|
|
, mCurrentControllers(NULL)
|
2013-01-17 21:18:40 +00:00
|
|
|
, mCurrentKeys(NULL)
|
2013-01-30 15:34:07 +00:00
|
|
|
, mCurrentTime(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-15 00:58:21 +00:00
|
|
|
for(size_t i = 0;i < mObjectLists.size();i++)
|
|
|
|
destroyObjectList(sceneMgr, mObjectLists[i]);
|
|
|
|
mObjectLists.clear();
|
2013-01-05 07:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-16 01:55:28 +00:00
|
|
|
void Animation::addObjectList(Ogre::SceneNode *node, const std::string &model, bool baseonly)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-16 01:55:28 +00:00
|
|
|
if(!mInsert)
|
|
|
|
{
|
|
|
|
mInsert = node->createChildSceneNode();
|
|
|
|
assert(mInsert);
|
|
|
|
}
|
2013-04-15 00:58:21 +00:00
|
|
|
|
2013-04-16 01:55:28 +00:00
|
|
|
mObjectLists.push_back(!baseonly ? NifOgre::Loader::createObjects(mInsert, model) :
|
|
|
|
NifOgre::Loader::createObjectBase(mInsert, model));
|
|
|
|
NifOgre::ObjectList &objlist = mObjectLists.back();
|
|
|
|
if(objlist.mSkelBase)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-16 01:55:28 +00:00
|
|
|
if(!mSkelBase)
|
|
|
|
mSkelBase = objlist.mSkelBase;
|
2013-04-07 20:46:29 +00:00
|
|
|
|
2013-04-16 01:55:28 +00:00
|
|
|
Ogre::AnimationStateSet *aset = objlist.mSkelBase->getAllAnimationStates();
|
2013-04-15 00:58:21 +00:00
|
|
|
Ogre::AnimationStateIterator asiter = aset->getAnimationStateIterator();
|
|
|
|
while(asiter.hasMoreElements())
|
2013-04-07 20:46:29 +00:00
|
|
|
{
|
2013-04-15 00:58:21 +00:00
|
|
|
Ogre::AnimationState *state = asiter.getNext();
|
|
|
|
state->setEnabled(false);
|
|
|
|
state->setLoop(false);
|
2013-04-07 20:46:29 +00:00
|
|
|
}
|
2013-02-06 00:29:51 +00:00
|
|
|
|
2013-04-15 00:58:21 +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-16 01:55:28 +00:00
|
|
|
Ogre::SkeletonInstance *skelinst = objlist.mSkelBase->getSkeleton();
|
2013-04-15 00:58:21 +00:00
|
|
|
Ogre::Skeleton::BoneIterator boneiter = skelinst->getBoneIterator();
|
|
|
|
while(boneiter.hasMoreElements())
|
|
|
|
boneiter.getNext()->setManuallyControlled(true);
|
2013-04-07 21:56:23 +00:00
|
|
|
|
2013-04-16 01:55:28 +00:00
|
|
|
if(mSkelBase != objlist.mSkelBase)
|
|
|
|
{
|
|
|
|
Ogre::SkeletonInstance *baseinst = mSkelBase->getSkeleton();
|
|
|
|
for(size_t i = 0;i < objlist.mControllers.size();i++)
|
|
|
|
{
|
|
|
|
NifOgre::NodeTargetValue<Ogre::Real> *dstval;
|
|
|
|
dstval = dynamic_cast<NifOgre::NodeTargetValue<Ogre::Real>*>(objlist.mControllers[i].getDestination().getPointer());
|
|
|
|
if(!dstval) continue;
|
|
|
|
|
|
|
|
const Ogre::String &trgtname = dstval->getNode()->getName();
|
|
|
|
if(!baseinst->hasBone(trgtname)) continue;
|
|
|
|
|
|
|
|
Ogre::Bone *bone = baseinst->getBone(trgtname);
|
|
|
|
dstval->setNode(bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 00:58:21 +00:00
|
|
|
Ogre::SkeletonPtr skel = Ogre::SkeletonManager::getSingleton().getByName(skelinst->getName());
|
|
|
|
boneiter = skel->getBoneIterator();
|
2013-02-23 11:34:03 +00:00
|
|
|
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-14 23:56:35 +00:00
|
|
|
mNonAccumRoot = mSkelBase->getSkeleton()->getBone(bone->getName());
|
2013-02-23 11:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-16 01:55:28 +00:00
|
|
|
for(size_t i = 0;i < objlist.mControllers.size();i++)
|
2013-04-06 16:44:10 +00:00
|
|
|
{
|
2013-04-16 01:55:28 +00:00
|
|
|
if(objlist.mControllers[i].getSource().isNull())
|
2013-04-21 00:13:27 +00:00
|
|
|
objlist.mControllers[i].setSource(mAnimationBaseValuePtr);
|
2013-04-06 16:44:10 +00:00
|
|
|
}
|
2013-04-16 01:55:28 +00:00
|
|
|
|
|
|
|
if(!mCurrentControllers || (*mCurrentControllers).size() == 0)
|
|
|
|
mCurrentControllers = &objlist.mControllers;
|
2012-07-13 03:12:18 +00:00
|
|
|
}
|
2012-01-07 03:52:15 +00:00
|
|
|
|
2013-04-19 09:01:50 +00:00
|
|
|
void Animation::setRenderProperties(const NifOgre::ObjectList &objlist, Ogre::uint32 visflags, Ogre::uint8 solidqueue, Ogre::uint8 transqueue)
|
|
|
|
{
|
|
|
|
for(size_t i = 0;i < objlist.mEntities.size();i++)
|
|
|
|
{
|
|
|
|
Ogre::Entity *ent = objlist.mEntities[i];
|
|
|
|
if(visflags != 0)
|
|
|
|
ent->setVisibilityFlags(visflags);
|
|
|
|
|
|
|
|
for(unsigned int j = 0;j < ent->getNumSubEntities();++j)
|
|
|
|
{
|
|
|
|
Ogre::SubEntity* subEnt = ent->getSubEntity(j);
|
|
|
|
subEnt->setRenderQueueGroup(subEnt->getMaterial()->isTransparent() ? transqueue : solidqueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(size_t i = 0;i < objlist.mParticles.size();i++)
|
|
|
|
{
|
|
|
|
Ogre::ParticleSystem *part = objlist.mParticles[i];
|
|
|
|
if(visflags != 0)
|
|
|
|
part->setVisibilityFlags(visflags);
|
|
|
|
// TODO: Check particle material for actual transparency
|
|
|
|
part->setRenderQueueGroup(transqueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 20:51:48 +00:00
|
|
|
|
2013-04-08 12:48:52 +00:00
|
|
|
Ogre::Node *Animation::getNode(const std::string &name)
|
|
|
|
{
|
2013-04-14 23:56:35 +00:00
|
|
|
if(mSkelBase)
|
2013-04-08 12:48:52 +00:00
|
|
|
{
|
2013-04-14 23:56:35 +00:00
|
|
|
Ogre::SkeletonInstance *skel = mSkelBase->getSkeleton();
|
2013-04-08 12:48:52 +00:00
|
|
|
if(skel->hasBone(name))
|
|
|
|
return skel->getBone(name);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-20 05:55:04 +00:00
|
|
|
bool Animation::hasAnimation(const std::string &anim)
|
2013-01-16 23:00:06 +00:00
|
|
|
{
|
2013-04-15 00:58:21 +00:00
|
|
|
for(std::vector<NifOgre::ObjectList>::const_iterator iter(mObjectLists.begin());iter != mObjectLists.end();iter++)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-15 00:58:21 +00:00
|
|
|
if(iter->mSkelBase && iter->mSkelBase->hasAnimationState(anim))
|
2013-02-06 00:29:51 +00:00
|
|
|
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)
|
|
|
|
{
|
2013-04-22 05:13:49 +00:00
|
|
|
const std::string loopstart = mCurrentGroup+": loop start";
|
|
|
|
const std::string loopstop = mCurrentGroup+": loop stop";
|
2013-02-22 17:22:06 +00:00
|
|
|
float loopstarttime = 0.0f;
|
|
|
|
float loopstoptime = mCurrentAnim->getLength();
|
|
|
|
NifOgre::TextKeyMap::const_iterator keyiter = mCurrentKeys->begin();
|
|
|
|
while(keyiter != mCurrentKeys->end())
|
|
|
|
{
|
2013-04-22 05:13:49 +00:00
|
|
|
if(keyiter->second == loopstart)
|
2013-02-22 17:22:06 +00:00
|
|
|
loopstarttime = keyiter->first;
|
2013-04-22 05:13:49 +00:00
|
|
|
else if(keyiter->second == loopstop)
|
2013-02-22 17:22:06 +00:00
|
|
|
{
|
|
|
|
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-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-04-07 23:21:45 +00:00
|
|
|
Ogre::Vector3 Animation::updatePosition()
|
2013-01-07 05:18:48 +00:00
|
|
|
{
|
2013-04-07 23:21:45 +00:00
|
|
|
Ogre::Vector3 posdiff;
|
2013-01-17 04:14:49 +00:00
|
|
|
|
2013-04-07 23:21:45 +00:00
|
|
|
Ogre::TransformKeyFrame kf(0, mCurrentTime);
|
|
|
|
Ogre::Animation::NodeTrackIterator trackiter = mCurrentAnim->getNodeTrackIterator();
|
|
|
|
while(trackiter.hasMoreElements())
|
2013-01-07 05:18:48 +00:00
|
|
|
{
|
2013-04-07 23:21:45 +00:00
|
|
|
const Ogre::NodeAnimationTrack *track = trackiter.getNext();
|
|
|
|
if(track->getAssociatedNode()->getName() == mNonAccumRoot->getName())
|
|
|
|
{
|
|
|
|
track->getInterpolatedKeyFrame(mCurrentAnim->_getTimeIndex(mCurrentTime), &kf);
|
|
|
|
break;
|
|
|
|
}
|
2013-01-07 05:18:48 +00:00
|
|
|
}
|
2013-04-07 23:21:45 +00:00
|
|
|
|
|
|
|
/* Get the non-accumulation root's difference from the last update. */
|
|
|
|
posdiff = (kf.getTranslate() - mLastPosition) * mAccumulate;
|
|
|
|
|
|
|
|
/* Translate the accumulation root back to compensate for the move. */
|
|
|
|
mLastPosition += posdiff;
|
|
|
|
mAccumRoot->setPosition(-mLastPosition);
|
|
|
|
|
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-04-16 03:37:10 +00:00
|
|
|
mStartKey = mCurrentKeys->begin();
|
2013-01-16 19:57:08 +00:00
|
|
|
|
2013-04-16 03:37:10 +00:00
|
|
|
while(mStartKey != mCurrentKeys->end() && mStartKey->second != start)
|
|
|
|
mStartKey++;
|
|
|
|
if(mStartKey != mCurrentKeys->end())
|
|
|
|
mCurrentTime = mStartKey->first;
|
2013-01-22 08:31:45 +00:00
|
|
|
else
|
2013-01-09 11:30:55 +00:00
|
|
|
{
|
2013-04-16 03:37:10 +00:00
|
|
|
mStartKey = mCurrentKeys->begin();
|
|
|
|
mCurrentTime = mStartKey->first;
|
2013-01-09 11:30:55 +00:00
|
|
|
}
|
2013-04-16 03:37:10 +00:00
|
|
|
mNextKey = mStartKey;
|
2013-01-09 11:30:55 +00:00
|
|
|
|
2013-02-23 13:15:10 +00:00
|
|
|
if(stop.length() > 0)
|
|
|
|
{
|
2013-04-16 03:37:10 +00:00
|
|
|
mStopKey = mStartKey;
|
|
|
|
while(mStopKey != mCurrentKeys->end() && mStopKey->second != stop)
|
|
|
|
mStopKey++;
|
|
|
|
if(mStopKey == mCurrentKeys->end())
|
|
|
|
mStopKey--;
|
2013-02-23 13:15:10 +00:00
|
|
|
}
|
|
|
|
|
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.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;
|
|
|
|
}
|
|
|
|
|
2013-04-22 05:13:49 +00:00
|
|
|
if(evt.compare(0, mCurrentGroup.size(), mCurrentGroup) != 0 ||
|
|
|
|
evt.compare(mCurrentGroup.size(), 2, ": ") != 0)
|
|
|
|
{
|
|
|
|
// Not ours
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
size_t off = mCurrentGroup.size()+2;
|
|
|
|
size_t len = evt.size() - off;
|
|
|
|
|
|
|
|
if(evt.compare(off, len, "start") == 0 || evt.compare(off, len, "loop start") == 0)
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(evt.compare(off, len, "loop stop") == 0)
|
2013-02-23 22:39:01 +00:00
|
|
|
{
|
|
|
|
if(mLooping)
|
|
|
|
{
|
2013-04-22 05:13:49 +00:00
|
|
|
reset(mCurrentGroup+": loop start");
|
2013-02-23 22:39:01 +00:00
|
|
|
if(mCurrentTime >= time)
|
|
|
|
return false;
|
2013-04-22 01:38:25 +00:00
|
|
|
return true;
|
2013-02-23 22:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-22 05:13:49 +00:00
|
|
|
else if(evt.compare(off, len, "stop") == 0)
|
2013-02-23 22:39:01 +00:00
|
|
|
{
|
|
|
|
if(mLooping)
|
|
|
|
{
|
2013-04-22 05:13:49 +00:00
|
|
|
reset(mCurrentGroup+": loop start");
|
2013-02-23 22:39:01 +00:00
|
|
|
if(mCurrentTime >= time)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// fall-through
|
|
|
|
}
|
|
|
|
if(mController)
|
2013-04-22 05:13:49 +00:00
|
|
|
mController->markerEvent(time, evt.substr(off));
|
2013-02-23 22:39:01 +00:00
|
|
|
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. */
|
2013-04-15 00:58:21 +00:00
|
|
|
for(std::vector<NifOgre::ObjectList>::reverse_iterator iter(mObjectLists.rbegin());iter != mObjectLists.rend();iter++)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-16 01:55:28 +00:00
|
|
|
if(iter->mSkelBase && iter->mSkelBase->hasAnimationState(groupname))
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-16 01:55:28 +00:00
|
|
|
Ogre::SkeletonInstance *skel = iter->mSkelBase->getSkeleton();
|
|
|
|
mCurrentAnim = skel->getAnimation(groupname);
|
2013-02-06 00:29:51 +00:00
|
|
|
mCurrentKeys = &mTextKeys[groupname];
|
2013-04-22 01:32:34 +00:00
|
|
|
mCurrentGroup = groupname;
|
2013-04-07 21:56:23 +00:00
|
|
|
mCurrentControllers = &iter->mControllers;
|
2013-02-15 10:15:39 +00:00
|
|
|
|
2013-04-22 01:32:34 +00:00
|
|
|
mAnimVelocity = 0.0f;
|
2013-02-15 10:15:39 +00:00
|
|
|
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-04-22 05:13:49 +00:00
|
|
|
reset(mCurrentGroup+": "+start, mCurrentGroup+": "+stop);
|
2013-02-23 13:15:10 +00:00
|
|
|
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-04-09 22:10:14 +00:00
|
|
|
Ogre::Vector3 movement(0.0f);
|
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-04-07 23:21:45 +00:00
|
|
|
float targetTime = mCurrentTime + timepassed;
|
2013-04-16 03:37:10 +00:00
|
|
|
if(mNextKey->first > targetTime)
|
2012-07-24 20:51:48 +00:00
|
|
|
{
|
2013-04-16 03:37:10 +00:00
|
|
|
mCurrentTime = targetTime;
|
2013-04-07 23:21:45 +00:00
|
|
|
if(mNonAccumRoot)
|
|
|
|
movement += updatePosition();
|
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-04-07 23:21:45 +00:00
|
|
|
mCurrentTime = time;
|
|
|
|
if(mNonAccumRoot)
|
|
|
|
movement += updatePosition();
|
2013-04-16 03:37:10 +00:00
|
|
|
|
|
|
|
mPlaying = (mLooping || mStopKey->first > mCurrentTime);
|
2013-04-07 23:21:45 +00:00
|
|
|
timepassed = targetTime - mCurrentTime;
|
2013-01-18 22:50:55 +00:00
|
|
|
|
2013-02-23 22:39:01 +00:00
|
|
|
if(!handleEvent(time, evt))
|
|
|
|
break;
|
2012-07-21 21:41:26 +00:00
|
|
|
}
|
2013-04-16 01:55:28 +00:00
|
|
|
|
|
|
|
for(size_t i = 0;i < (*mCurrentControllers).size();i++)
|
2013-04-07 21:56:23 +00:00
|
|
|
(*mCurrentControllers)[i].update();
|
2013-01-30 15:04:18 +00:00
|
|
|
|
2013-04-14 23:56:35 +00:00
|
|
|
if(mSkelBase)
|
2013-04-07 23:21:45 +00:00
|
|
|
{
|
2013-04-16 01:55:28 +00:00
|
|
|
const Ogre::SkeletonInstance *baseinst = mSkelBase->getSkeleton();
|
|
|
|
for(std::vector<NifOgre::ObjectList>::iterator iter(mObjectLists.begin());iter != mObjectLists.end();iter++)
|
2013-04-15 00:58:21 +00:00
|
|
|
{
|
2013-04-16 01:55:28 +00:00
|
|
|
Ogre::Entity *ent = iter->mSkelBase;
|
2013-04-15 00:58:21 +00:00
|
|
|
if(!ent) continue;
|
|
|
|
|
2013-04-16 01:55:28 +00:00
|
|
|
Ogre::SkeletonInstance *inst = ent->getSkeleton();
|
|
|
|
if(baseinst != inst)
|
|
|
|
updateSkeletonInstance(baseinst, inst);
|
|
|
|
|
2013-04-15 00:58:21 +00:00
|
|
|
// HACK: Dirty the animation state set so that Ogre will apply the
|
|
|
|
// transformations to entities this skeleton instance is shared with.
|
|
|
|
ent->getAllAnimationStates()->_notifyDirty();
|
|
|
|
}
|
2013-04-07 23:21:45 +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
|
|
|
}
|