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-23 06:35:50 +00:00
|
|
|
Animation::AnimLayer::AnimLayer()
|
|
|
|
: mControllers(NULL)
|
|
|
|
, mTextKeys(NULL)
|
|
|
|
, mTime(0.0f)
|
|
|
|
, mPlaying(false)
|
|
|
|
, mLooping(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ogre::Real Animation::AnimationValue::getValue() const
|
|
|
|
{
|
|
|
|
size_t idx = mIndex;
|
|
|
|
while(idx > 0 && mAnimation->mLayer[idx].mGroupName.empty())
|
|
|
|
idx--;
|
|
|
|
if(!mAnimation->mLayer[idx].mGroupName.empty())
|
|
|
|
return mAnimation->mLayer[idx].mTime;
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::AnimationValue::setValue(Ogre::Real value)
|
|
|
|
{
|
|
|
|
mAnimation->mLayer[mIndex].mTime = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-23 06:35:50 +00:00
|
|
|
: mAnimationBaseValuePtr(OGRE_NEW AnimationValue(this, 0))
|
2013-04-21 00:13:27 +00:00
|
|
|
, 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-22 11:10:46 +00:00
|
|
|
, mNonAccumCtrl(NULL)
|
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
|
|
|
{
|
2013-04-23 10:42:00 +00:00
|
|
|
/* As long as we remain under 128 active controllers, we can avoid
|
|
|
|
* reallocations. */
|
|
|
|
mActiveCtrls.reserve(128);
|
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-23 09:48:11 +00:00
|
|
|
for(size_t i = 0;i < mObjects.size();i++)
|
|
|
|
destroyObjectList(sceneMgr, mObjects[i].mObjectList);
|
|
|
|
mObjects.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-23 09:48:11 +00:00
|
|
|
mObjects.push_back(ObjectInfo());
|
|
|
|
ObjectInfo &obj = mObjects.back();
|
|
|
|
obj.mActiveLayers = 0;
|
|
|
|
obj.mObjectList = (!baseonly ? NifOgre::Loader::createObjects(mInsert, model) :
|
|
|
|
NifOgre::Loader::createObjectBase(mInsert, model));
|
|
|
|
|
|
|
|
NifOgre::ObjectList &objlist = obj.mObjectList;
|
2013-04-16 01:55:28 +00:00
|
|
|
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-22 07:01:30 +00:00
|
|
|
Ogre::SkeletonInstance *baseinst = mSkelBase->getSkeleton();
|
2013-04-22 07:22:31 +00:00
|
|
|
if(mSkelBase == objlist.mSkelBase)
|
|
|
|
{
|
|
|
|
if(objlist.mTextKeys.size() > 0)
|
|
|
|
{
|
|
|
|
mAccumRoot = mInsert;
|
|
|
|
mNonAccumRoot = baseinst->getBone(objlist.mTextKeys.begin()->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-04-16 01:55:28 +00:00
|
|
|
{
|
|
|
|
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-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-24 02:43:26 +00:00
|
|
|
|
|
|
|
mActiveCtrls.insert(mActiveCtrls.end(), objlist.mControllers.begin(), objlist.mControllers.end());
|
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-23 10:42:00 +00:00
|
|
|
void Animation::updateActiveControllers()
|
|
|
|
{
|
|
|
|
mActiveCtrls.clear();
|
|
|
|
|
|
|
|
/* First, get all controllers that don't target a node, or that target
|
|
|
|
* nodes that don't belong to any particular layer.
|
|
|
|
*/
|
|
|
|
std::vector<ObjectInfo>::iterator obj(mObjects.begin());
|
|
|
|
for(;obj != mObjects.end();obj++)
|
|
|
|
{
|
|
|
|
std::vector<Ogre::Controller<Ogre::Real> >::const_iterator ctrl(obj->mObjectList.mControllers.begin());
|
|
|
|
for(;ctrl != obj->mObjectList.mControllers.end();ctrl++)
|
|
|
|
{
|
|
|
|
NifOgre::NodeTargetValue<Ogre::Real> *dstval;
|
|
|
|
dstval = dynamic_cast<NifOgre::NodeTargetValue<Ogre::Real>*>(ctrl->getDestination().getPointer());
|
|
|
|
if(dstval)
|
|
|
|
{
|
|
|
|
/*if(getLayerByName(dstval->getNode()->getName()) >= 0)*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mActiveCtrls.insert(mActiveCtrls.end(), *ctrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Ogre::Controller<Ogre::Real> > *ctrls = NULL;
|
|
|
|
size_t layer = 0;
|
|
|
|
while(layer < sMaxLayers)
|
|
|
|
{
|
|
|
|
/* Now get controllers that target nodes that belong to this layer from
|
|
|
|
* whatever objectlist is active on this layer.
|
|
|
|
*/
|
|
|
|
std::vector<ObjectInfo>::iterator obj(mObjects.begin());
|
|
|
|
for(;obj != mObjects.end();obj++)
|
|
|
|
{
|
|
|
|
if((obj->mActiveLayers&(1<<layer)))
|
|
|
|
{
|
|
|
|
ctrls = &obj->mObjectList.mControllers;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if any objectlists are active on subsequent layers. Include
|
|
|
|
* those layers if not.
|
|
|
|
*/
|
|
|
|
size_t nextlayer = layer+1;
|
|
|
|
for(;nextlayer < sMaxLayers;nextlayer++)
|
|
|
|
{
|
|
|
|
for(obj = mObjects.begin();obj != mObjects.end();obj++)
|
|
|
|
{
|
2013-04-24 02:43:26 +00:00
|
|
|
if((obj->mActiveLayers&(1<<nextlayer)))
|
2013-04-23 10:42:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ctrls != NULL);
|
|
|
|
std::vector<Ogre::Controller<Ogre::Real> >::const_iterator ctrl(ctrls->begin());
|
|
|
|
for(;ctrl != ctrls->end();ctrl++)
|
|
|
|
{
|
|
|
|
NifOgre::NodeTargetValue<Ogre::Real> *dstval;
|
|
|
|
dstval = dynamic_cast<NifOgre::NodeTargetValue<Ogre::Real>*>(ctrl->getDestination().getPointer());
|
|
|
|
if(dstval)
|
|
|
|
{
|
|
|
|
/*ssize_t idx = getLayerByName(dstval->getNode()->getName());
|
|
|
|
if(idx >= (ssize_t)layer && idx < (ssize_t)nextlayer)*/
|
|
|
|
mActiveCtrls.insert(mActiveCtrls.end(), *ctrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
layer = nextlayer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-04-23 03:59:55 +00:00
|
|
|
NifOgre::TextKeyMap::const_iterator Animation::findGroupStart(const NifOgre::TextKeyMap &keys, const std::string &groupname)
|
|
|
|
{
|
|
|
|
NifOgre::TextKeyMap::const_iterator iter(keys.begin());
|
|
|
|
for(;iter != keys.end();iter++)
|
|
|
|
{
|
|
|
|
if(iter->second.compare(0, groupname.size(), groupname) == 0 &&
|
|
|
|
iter->second.compare(groupname.size(), 2, ": ") == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-23 09:48:11 +00:00
|
|
|
for(std::vector<ObjectInfo>::const_iterator iter(mObjects.begin());iter != mObjects.end();iter++)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-23 09:48:11 +00:00
|
|
|
if(iter->mObjectList.mTextKeys.size() == 0)
|
2013-04-23 03:59:55 +00:00
|
|
|
continue;
|
|
|
|
|
2013-04-23 09:48:11 +00:00
|
|
|
const NifOgre::TextKeyMap &keys = iter->mObjectList.mTextKeys.begin()->second;
|
2013-04-23 03:59:55 +00:00
|
|
|
if(findGroupStart(keys, anim) != keys.end())
|
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
|
|
|
|
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-04-23 03:41:54 +00:00
|
|
|
float Animation::calcAnimVelocity(const NifOgre::TextKeyMap &keys, NifOgre::NodeTargetValue<Ogre::Real> *nonaccumctrl, const std::string &groupname)
|
2013-02-22 17:22:06 +00:00
|
|
|
{
|
2013-04-23 04:34:39 +00:00
|
|
|
const std::string start = groupname+": start";
|
2013-04-22 11:10:46 +00:00
|
|
|
const std::string loopstart = groupname+": loop start";
|
|
|
|
const std::string loopstop = groupname+": loop stop";
|
2013-04-23 04:34:39 +00:00
|
|
|
const std::string stop = groupname+": stop";
|
|
|
|
float starttime = std::numeric_limits<float>::max();
|
|
|
|
float stoptime = 0.0f;
|
|
|
|
NifOgre::TextKeyMap::const_iterator keyiter(keys.begin());
|
2013-04-23 03:41:54 +00:00
|
|
|
while(keyiter != keys.end())
|
2013-02-22 17:22:06 +00:00
|
|
|
{
|
2013-04-23 04:34:39 +00:00
|
|
|
if(keyiter->second == start || keyiter->second == loopstart)
|
|
|
|
starttime = keyiter->first;
|
|
|
|
else if(keyiter->second == loopstop || keyiter->second == stop)
|
2013-02-22 17:22:06 +00:00
|
|
|
{
|
2013-04-23 04:34:39 +00:00
|
|
|
stoptime = keyiter->first;
|
2013-04-22 11:10:46 +00:00
|
|
|
break;
|
2013-02-22 17:22:06 +00:00
|
|
|
}
|
2013-04-22 11:10:46 +00:00
|
|
|
keyiter++;
|
|
|
|
}
|
2013-02-22 17:22:06 +00:00
|
|
|
|
2013-04-23 04:34:39 +00:00
|
|
|
if(stoptime > starttime)
|
2013-04-22 11:10:46 +00:00
|
|
|
{
|
2013-04-23 04:34:39 +00:00
|
|
|
Ogre::Vector3 startpos = nonaccumctrl->getTranslation(starttime);
|
|
|
|
Ogre::Vector3 endpos = nonaccumctrl->getTranslation(stoptime);
|
2013-02-22 17:22:06 +00:00
|
|
|
|
2013-04-23 04:34:39 +00:00
|
|
|
return startpos.distance(endpos) / (stoptime-starttime);
|
2013-02-22 17:22:06 +00:00
|
|
|
}
|
2013-04-22 08:40:41 +00:00
|
|
|
|
|
|
|
return 0.0f;
|
2013-02-22 17:22:06 +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-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
|
|
|
/* Get the non-accumulation root's difference from the last update. */
|
2013-04-23 06:35:50 +00:00
|
|
|
posdiff = (mNonAccumCtrl->getTranslation(mLayer[0].mTime) - mLastPosition) * mAccumulate;
|
2013-04-07 23:21:45 +00:00
|
|
|
|
|
|
|
/* 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-04-23 06:35:50 +00:00
|
|
|
bool Animation::reset(size_t layeridx, const NifOgre::TextKeyMap &keys, NifOgre::NodeTargetValue<Ogre::Real> *nonaccumctrl, const std::string &groupname, const std::string &start, const std::string &stop)
|
2013-01-07 05:18:48 +00:00
|
|
|
{
|
2013-04-23 03:41:54 +00:00
|
|
|
std::string tag = groupname+": "+start;
|
|
|
|
NifOgre::TextKeyMap::const_iterator startkey(keys.begin());
|
|
|
|
while(startkey != keys.end() && startkey->second != tag)
|
|
|
|
startkey++;
|
|
|
|
if(startkey == keys.end() && tag == "loop start")
|
2013-01-09 11:30:55 +00:00
|
|
|
{
|
2013-04-23 03:41:54 +00:00
|
|
|
tag = groupname+": start";
|
|
|
|
startkey = keys.begin();
|
|
|
|
while(startkey != keys.end() && startkey->second != tag)
|
|
|
|
startkey++;
|
2013-01-09 11:30:55 +00:00
|
|
|
}
|
2013-04-23 03:41:54 +00:00
|
|
|
if(startkey == keys.end())
|
|
|
|
return false;
|
2013-04-22 12:08:52 +00:00
|
|
|
|
2013-04-23 03:41:54 +00:00
|
|
|
tag = groupname+": "+stop;
|
|
|
|
NifOgre::TextKeyMap::const_iterator stopkey(startkey);
|
|
|
|
while(stopkey != keys.end() && stopkey->second != tag)
|
|
|
|
stopkey++;
|
|
|
|
if(stopkey == keys.end())
|
|
|
|
return false;
|
2013-04-22 12:08:52 +00:00
|
|
|
|
2013-04-23 03:41:54 +00:00
|
|
|
if(startkey == stopkey)
|
|
|
|
return false;
|
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
mLayer[layeridx].mStartKey = startkey;
|
|
|
|
mLayer[layeridx].mLoopStartKey = startkey;
|
|
|
|
mLayer[layeridx].mStopKey = stopkey;
|
|
|
|
mLayer[layeridx].mNextKey = startkey;
|
|
|
|
mLayer[layeridx].mNextKey++;
|
2013-01-09 11:30:55 +00:00
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
mLayer[layeridx].mTime = mLayer[layeridx].mStartKey->first;
|
2013-04-23 03:41:54 +00:00
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
if(layeridx == 0 && nonaccumctrl)
|
|
|
|
mLastPosition = nonaccumctrl->getTranslation(mLayer[layeridx].mTime) * mAccumulate;
|
2013-04-23 03:41:54 +00:00
|
|
|
|
|
|
|
return true;
|
2013-04-22 12:08:52 +00:00
|
|
|
}
|
2013-02-23 13:15:10 +00:00
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
void Animation::doLoop(size_t layeridx)
|
2013-04-22 12:08:52 +00:00
|
|
|
{
|
2013-04-23 06:35:50 +00:00
|
|
|
mLayer[layeridx].mTime = mLayer[layeridx].mLoopStartKey->first;
|
|
|
|
mLayer[layeridx].mNextKey = mLayer[layeridx].mLoopStartKey;
|
|
|
|
mLayer[layeridx].mNextKey++;
|
2013-04-23 12:34:08 +00:00
|
|
|
mLayer[layeridx].mPlaying = true;
|
2013-04-23 06:35:50 +00:00
|
|
|
if(layeridx == 0 && mNonAccumCtrl)
|
|
|
|
mLastPosition = mNonAccumCtrl->getTranslation(mLayer[layeridx].mTime) * mAccumulate;
|
2012-07-24 20:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
bool Animation::handleTextKey(size_t layeridx, const NifOgre::TextKeyMap::const_iterator &key)
|
2013-02-23 22:39:01 +00:00
|
|
|
{
|
2013-04-22 12:08:52 +00:00
|
|
|
float time = key->first;
|
|
|
|
const std::string &evt = key->second;
|
|
|
|
|
2013-02-23 22:39:01 +00:00
|
|
|
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-23 06:35:50 +00:00
|
|
|
if(evt.compare(0, mLayer[layeridx].mGroupName.size(), mLayer[layeridx].mGroupName) != 0 ||
|
|
|
|
evt.compare(mLayer[layeridx].mGroupName.size(), 2, ": ") != 0)
|
2013-04-22 05:13:49 +00:00
|
|
|
{
|
2013-04-23 06:35:50 +00:00
|
|
|
// Not ours, skip it
|
2013-04-22 05:13:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-04-23 06:35:50 +00:00
|
|
|
size_t off = mLayer[layeridx].mGroupName.size()+2;
|
2013-04-22 05:13:49 +00:00
|
|
|
size_t len = evt.size() - off;
|
|
|
|
|
|
|
|
if(evt.compare(off, len, "start") == 0 || evt.compare(off, len, "loop start") == 0)
|
|
|
|
{
|
2013-04-23 06:35:50 +00:00
|
|
|
mLayer[layeridx].mLoopStartKey = key;
|
2013-04-22 05:13:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-22 12:08:52 +00:00
|
|
|
if(evt.compare(off, len, "loop stop") == 0 || evt.compare(off, len, "stop") == 0)
|
2013-02-23 22:39:01 +00:00
|
|
|
{
|
2013-04-23 06:35:50 +00:00
|
|
|
if(mLayer[layeridx].mLooping)
|
2013-02-23 22:39:01 +00:00
|
|
|
{
|
2013-04-23 06:35:50 +00:00
|
|
|
doLoop(layeridx);
|
|
|
|
if(mLayer[layeridx].mTime >= time)
|
2013-02-23 22:39:01 +00:00
|
|
|
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-04-23 06:35:50 +00:00
|
|
|
// TODO: parameterize this
|
|
|
|
size_t layeridx = 0;
|
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
for(std::vector<ObjectInfo>::iterator iter(mObjects.begin());iter != mObjects.end();iter++)
|
|
|
|
iter->mActiveLayers &= ~(1<<layeridx);
|
2013-04-23 09:48:11 +00:00
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
bool foundanim = false;
|
|
|
|
if(groupname.empty())
|
|
|
|
{
|
|
|
|
// Do not allow layer 0 to be disabled
|
|
|
|
assert(layeridx != 0);
|
2013-04-23 09:48:11 +00:00
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
mLayer[layeridx].mGroupName.clear();
|
|
|
|
mLayer[layeridx].mTextKeys = NULL;
|
|
|
|
mLayer[layeridx].mControllers = NULL;
|
|
|
|
mLayer[layeridx].mLooping = false;
|
|
|
|
mLayer[layeridx].mPlaying = false;
|
2013-04-23 09:48:11 +00:00
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
foundanim = true;
|
|
|
|
}
|
|
|
|
/* Look in reverse; last-inserted source has priority. */
|
|
|
|
else for(std::vector<ObjectInfo>::reverse_iterator iter(mObjects.rbegin());iter != mObjects.rend();iter++)
|
|
|
|
{
|
|
|
|
NifOgre::ObjectList &objlist = iter->mObjectList;
|
|
|
|
if(objlist.mTextKeys.size() == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const NifOgre::TextKeyMap &keys = objlist.mTextKeys.begin()->second;
|
|
|
|
NifOgre::NodeTargetValue<Ogre::Real> *nonaccumctrl = NULL;
|
|
|
|
if(layeridx == 0)
|
|
|
|
{
|
|
|
|
for(size_t i = 0;i < objlist.mControllers.size();i++)
|
2013-04-23 09:48:11 +00:00
|
|
|
{
|
2013-04-23 10:42:00 +00:00
|
|
|
NifOgre::NodeTargetValue<Ogre::Real> *dstval;
|
|
|
|
dstval = dynamic_cast<NifOgre::NodeTargetValue<Ogre::Real>*>(objlist.mControllers[i].getDestination().getPointer());
|
|
|
|
if(dstval && dstval->getNode() == mNonAccumRoot)
|
|
|
|
{
|
|
|
|
nonaccumctrl = dstval;
|
|
|
|
break;
|
|
|
|
}
|
2013-04-23 09:48:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
if(!foundanim)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-23 10:42:00 +00:00
|
|
|
if(!reset(layeridx, keys, nonaccumctrl, groupname, start, stop))
|
2013-04-23 03:41:54 +00:00
|
|
|
continue;
|
2013-04-23 10:42:00 +00:00
|
|
|
mLayer[layeridx].mGroupName = groupname;
|
|
|
|
mLayer[layeridx].mTextKeys = &keys;
|
|
|
|
mLayer[layeridx].mControllers = &objlist.mControllers;
|
|
|
|
mLayer[layeridx].mLooping = loop;
|
|
|
|
mLayer[layeridx].mPlaying = true;
|
2013-04-23 03:41:54 +00:00
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
if(layeridx == 0)
|
2013-02-06 00:29:51 +00:00
|
|
|
{
|
2013-04-23 10:42:00 +00:00
|
|
|
mNonAccumCtrl = nonaccumctrl;
|
|
|
|
mAnimVelocity = 0.0f;
|
2013-04-23 03:41:54 +00:00
|
|
|
}
|
2013-04-22 11:10:46 +00:00
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
iter->mActiveLayers |= (1<<layeridx);
|
|
|
|
foundanim = true;
|
|
|
|
}
|
2013-04-22 08:40:41 +00:00
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
if(!nonaccumctrl)
|
|
|
|
break;
|
2013-04-22 08:40:41 +00:00
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
mAnimVelocity = calcAnimVelocity(keys, nonaccumctrl, groupname);
|
|
|
|
if(mAnimVelocity > 0.0f) break;
|
2013-01-07 13:56:03 +00:00
|
|
|
}
|
2013-04-23 10:42:00 +00:00
|
|
|
if(!foundanim)
|
|
|
|
std::cerr<< "Failed to find animation "<<groupname <<std::endl;
|
|
|
|
|
|
|
|
updateActiveControllers();
|
2012-07-24 20:51:48 +00:00
|
|
|
}
|
2011-12-15 05:33:10 +00:00
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
Ogre::Vector3 Animation::runAnimation(float duration)
|
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-04-23 06:35:50 +00:00
|
|
|
duration *= mAnimSpeedMult;
|
|
|
|
for(size_t layeridx = 0;layeridx < sMaxLayers;layeridx++)
|
2012-07-21 21:41:26 +00:00
|
|
|
{
|
2013-04-23 06:35:50 +00:00
|
|
|
if(mLayer[layeridx].mGroupName.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float timepassed = duration;
|
|
|
|
while(mLayer[layeridx].mPlaying)
|
2012-07-24 20:51:48 +00:00
|
|
|
{
|
2013-04-23 06:35:50 +00:00
|
|
|
float targetTime = mLayer[layeridx].mTime + timepassed;
|
|
|
|
if(mLayer[layeridx].mNextKey->first > targetTime)
|
|
|
|
{
|
|
|
|
mLayer[layeridx].mTime = targetTime;
|
|
|
|
if(layeridx == 0 && mNonAccumRoot)
|
|
|
|
movement += updatePosition();
|
|
|
|
break;
|
|
|
|
}
|
2012-07-21 21:41:26 +00:00
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
NifOgre::TextKeyMap::const_iterator key(mLayer[layeridx].mNextKey++);
|
|
|
|
mLayer[layeridx].mTime = key->first;
|
|
|
|
if(layeridx == 0 && mNonAccumRoot)
|
|
|
|
movement += updatePosition();
|
2013-04-16 03:37:10 +00:00
|
|
|
|
2013-04-23 12:34:08 +00:00
|
|
|
mLayer[layeridx].mPlaying = (key != mLayer[layeridx].mStopKey);
|
2013-04-23 06:35:50 +00:00
|
|
|
timepassed = targetTime - mLayer[layeridx].mTime;
|
2013-01-18 22:50:55 +00:00
|
|
|
|
2013-04-23 06:35:50 +00:00
|
|
|
if(!handleTextKey(layeridx, key))
|
|
|
|
break;
|
|
|
|
}
|
2012-07-21 21:41:26 +00:00
|
|
|
}
|
2013-04-16 01:55:28 +00:00
|
|
|
|
2013-04-23 10:42:00 +00:00
|
|
|
for(size_t i = 0;i < mActiveCtrls.size();i++)
|
|
|
|
mActiveCtrls[i].update();
|
|
|
|
|
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();
|
2013-04-23 09:48:11 +00:00
|
|
|
for(std::vector<ObjectInfo>::iterator iter(mObjects.begin());iter != mObjects.end();iter++)
|
2013-04-15 00:58:21 +00:00
|
|
|
{
|
2013-04-23 09:48:11 +00:00
|
|
|
Ogre::Entity *ent = iter->mObjectList.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
|
|
|
}
|