2015-12-12 17:52:06 +00:00
|
|
|
#include "keyframemanager.hpp"
|
|
|
|
|
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
|
|
2021-10-05 12:21:12 +00:00
|
|
|
#include <osg/Stats>
|
2020-11-18 23:11:56 +00:00
|
|
|
#include <osgAnimation/Animation>
|
|
|
|
#include <osgAnimation/BasicAnimationManager>
|
|
|
|
#include <osgAnimation/Channel>
|
|
|
|
|
2022-07-21 11:51:34 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2021-09-11 13:49:47 +00:00
|
|
|
#include <components/misc/pathhelpers.hpp>
|
2022-08-02 22:00:54 +00:00
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
2020-11-18 20:48:47 +00:00
|
|
|
#include <components/nifosg/nifloader.hpp>
|
2020-11-18 23:11:56 +00:00
|
|
|
#include <components/sceneutil/keyframe.hpp>
|
|
|
|
#include <components/sceneutil/osgacontroller.hpp>
|
2020-11-18 20:48:47 +00:00
|
|
|
|
2020-11-18 23:11:56 +00:00
|
|
|
#include "animation.hpp"
|
2015-12-12 17:52:06 +00:00
|
|
|
#include "objectcache.hpp"
|
2020-11-18 20:48:47 +00:00
|
|
|
#include "scenemanager.hpp"
|
|
|
|
|
2020-11-28 13:03:10 +00:00
|
|
|
namespace Resource
|
2020-11-18 20:48:47 +00:00
|
|
|
{
|
|
|
|
|
2021-02-04 21:14:21 +00:00
|
|
|
RetrieveAnimationsVisitor::RetrieveAnimationsVisitor(SceneUtil::KeyframeHolder& target,
|
|
|
|
osg::ref_ptr<osgAnimation::BasicAnimationManager> animationManager, const std::string& normalized,
|
|
|
|
const VFS::Manager* vfs)
|
|
|
|
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
|
|
|
|
, mTarget(target)
|
|
|
|
, mAnimationManager(animationManager)
|
|
|
|
, mNormalized(normalized)
|
|
|
|
, mVFS(vfs)
|
|
|
|
{
|
|
|
|
}
|
2020-11-18 20:48:47 +00:00
|
|
|
|
2020-11-18 23:11:56 +00:00
|
|
|
void RetrieveAnimationsVisitor::apply(osg::Node& node)
|
|
|
|
{
|
2022-08-02 21:57:09 +00:00
|
|
|
if (node.libraryName() == std::string_view("osgAnimation") && node.className() == std::string_view("Bone")
|
|
|
|
&& Misc::StringUtils::lowerCase(node.getName()) == std::string_view("bip01"))
|
2020-11-18 23:11:56 +00:00
|
|
|
{
|
2020-11-28 13:03:10 +00:00
|
|
|
osg::ref_ptr<SceneUtil::OsgAnimationController> callback = new SceneUtil::OsgAnimationController();
|
2020-11-18 23:11:56 +00:00
|
|
|
|
2020-11-28 13:03:10 +00:00
|
|
|
std::vector<SceneUtil::EmulatedAnimation> emulatedAnimations;
|
2020-11-18 23:11:56 +00:00
|
|
|
|
2021-08-29 13:15:45 +00:00
|
|
|
for (const auto& animation : mAnimationManager->getAnimationList())
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2020-11-18 23:11:56 +00:00
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
if (animation->getName()
|
|
|
|
== "Default") //"Default" is osg dae plugin's default naming scheme for unnamed animations
|
|
|
|
{
|
|
|
|
animation->setName(
|
|
|
|
std::string("idle")); // animation naming scheme "idle: start" and "idle: stop" is the
|
|
|
|
// default idle animation that OpenMW seems to want to play
|
|
|
|
}
|
2021-02-04 21:14:21 +00:00
|
|
|
|
|
|
|
osg::ref_ptr<Resource::Animation> mergedAnimationTrack = new Resource::Animation;
|
|
|
|
const std::string animationName = animation->getName();
|
|
|
|
mergedAnimationTrack->setName(animationName);
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2021-02-04 21:14:21 +00:00
|
|
|
const osgAnimation::ChannelList& channels = animation->getChannels();
|
|
|
|
for (const auto& channel : channels)
|
|
|
|
{
|
2021-02-12 18:36:03 +00:00
|
|
|
mergedAnimationTrack->addChannel(channel.get()->clone()); // is ->clone needed?
|
2021-02-04 21:14:21 +00:00
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2020-11-18 23:11:56 +00:00
|
|
|
callback->addMergedAnimationTrack(mergedAnimationTrack);
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2020-11-18 23:11:56 +00:00
|
|
|
float startTime = animation->getStartTime();
|
|
|
|
float stopTime = startTime + animation->getDuration();
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2020-11-28 13:03:10 +00:00
|
|
|
SceneUtil::EmulatedAnimation emulatedAnimation;
|
2020-11-18 23:11:56 +00:00
|
|
|
emulatedAnimation.mStartTime = startTime;
|
|
|
|
emulatedAnimation.mStopTime = stopTime;
|
|
|
|
emulatedAnimation.mName = animationName;
|
|
|
|
emulatedAnimations.emplace_back(emulatedAnimation);
|
2021-02-04 21:14:21 +00:00
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 11:43:00 +00:00
|
|
|
// mTextKeys is a nif-thing, used by OpenMW's animation system
|
|
|
|
// Format is likely "AnimationName: [Keyword_optional] [Start OR Stop]"
|
2021-02-04 21:14:21 +00:00
|
|
|
// AnimationNames are keywords like idle2, idle3... AiPackages and various mechanics control which
|
2021-04-19 11:43:00 +00:00
|
|
|
// animations are played Keywords can be stuff like Loop, Equip, Unequip, Block, InventoryHandtoHand,
|
|
|
|
// InventoryWeaponOneHand, PickProbe, Slash, Thrust, Chop... even "Slash Small Follow" osgAnimation formats
|
|
|
|
// should have a .txt file with the same name, each line holding a textkey and whitespace separated time
|
2021-02-04 21:14:21 +00:00
|
|
|
// value e.g. idle: start 0.0333
|
2022-09-22 18:26:05 +00:00
|
|
|
try
|
|
|
|
{
|
2021-02-04 21:14:21 +00:00
|
|
|
Files::IStreamPtr textKeysFile = mVFS->get(changeFileExtension(mNormalized, "txt"));
|
2021-04-19 11:43:00 +00:00
|
|
|
std::string line;
|
|
|
|
while (getline(*textKeysFile, line))
|
2021-02-04 21:14:21 +00:00
|
|
|
{
|
|
|
|
mTarget.mTextKeys.emplace(parseTimeSignature(line), parseTextKey(line));
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2020-11-18 23:11:56 +00:00
|
|
|
catch (std::exception&)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2020-11-18 23:11:56 +00:00
|
|
|
Log(Debug::Warning) << "No textkey file found for " << mNormalized;
|
|
|
|
}
|
2020-11-18 20:48:47 +00:00
|
|
|
|
2020-11-18 23:11:56 +00:00
|
|
|
callback->setEmulatedAnimations(emulatedAnimations);
|
|
|
|
mTarget.mKeyframeControllers.emplace(node.getName(), callback);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 23:11:56 +00:00
|
|
|
traverse(node);
|
|
|
|
}
|
2021-02-04 21:14:21 +00:00
|
|
|
|
|
|
|
std::string RetrieveAnimationsVisitor::parseTextKey(const std::string& line)
|
|
|
|
{
|
|
|
|
size_t spacePos = line.find_last_of(' ');
|
|
|
|
if (spacePos != std::string::npos)
|
|
|
|
return line.substr(0, spacePos);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
double RetrieveAnimationsVisitor::parseTimeSignature(const std::string& line)
|
|
|
|
{
|
|
|
|
size_t spacePos = line.find_last_of(' ');
|
|
|
|
double time = 0.0;
|
|
|
|
if (spacePos != std::string::npos && spacePos + 1 < line.size())
|
|
|
|
time = std::stod(line.substr(spacePos + 1));
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2021-06-23 19:56:08 +00:00
|
|
|
std::string RetrieveAnimationsVisitor::changeFileExtension(const std::string& file, const std::string& ext)
|
2021-02-04 21:14:21 +00:00
|
|
|
{
|
|
|
|
size_t extPos = file.find_last_of('.');
|
|
|
|
if (extPos != std::string::npos && extPos + 1 < file.size())
|
|
|
|
{
|
|
|
|
return file.substr(0, extPos + 1) + ext;
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:48:47 +00:00
|
|
|
}
|
2015-12-12 17:52:06 +00:00
|
|
|
|
|
|
|
namespace Resource
|
|
|
|
{
|
|
|
|
|
2020-11-18 20:48:47 +00:00
|
|
|
KeyframeManager::KeyframeManager(const VFS::Manager* vfs, SceneManager* sceneManager)
|
2016-02-06 15:57:54 +00:00
|
|
|
: ResourceManager(vfs)
|
2020-11-18 20:48:47 +00:00
|
|
|
, mSceneManager(sceneManager)
|
2015-12-12 17:52:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyframeManager::~KeyframeManager() {}
|
|
|
|
|
2020-11-18 20:48:47 +00:00
|
|
|
osg::ref_ptr<const SceneUtil::KeyframeHolder> KeyframeManager::get(const std::string& name)
|
2015-12-12 17:52:06 +00:00
|
|
|
{
|
2021-09-06 20:01:41 +00:00
|
|
|
const std::string normalized = mVFS->normalizeFilename(name);
|
2015-12-12 17:52:06 +00:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(normalized);
|
|
|
|
if (obj)
|
2020-11-18 20:48:47 +00:00
|
|
|
return osg::ref_ptr<const SceneUtil::KeyframeHolder>(static_cast<SceneUtil::KeyframeHolder*>(obj.get()));
|
2015-12-12 17:52:06 +00:00
|
|
|
else
|
|
|
|
{
|
2020-11-18 20:48:47 +00:00
|
|
|
osg::ref_ptr<SceneUtil::KeyframeHolder> loaded(new SceneUtil::KeyframeHolder);
|
2021-09-11 13:49:47 +00:00
|
|
|
if (Misc::getFileExtension(normalized) == "kf")
|
2020-11-18 20:48:47 +00:00
|
|
|
{
|
2022-09-17 17:24:42 +00:00
|
|
|
auto file = std::make_shared<Nif::NIFFile>(normalized);
|
|
|
|
Nif::Reader reader(*file);
|
|
|
|
reader.parse(mVFS->getNormalized(normalized));
|
2022-09-18 12:12:49 +00:00
|
|
|
NifOsg::Loader::loadKf(*file, *loaded.get());
|
2020-11-18 20:48:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-18 23:11:56 +00:00
|
|
|
osg::ref_ptr<osg::Node> scene = const_cast<osg::Node*>(mSceneManager->getTemplate(normalized).get());
|
|
|
|
osg::ref_ptr<osgAnimation::BasicAnimationManager> bam
|
|
|
|
= dynamic_cast<osgAnimation::BasicAnimationManager*>(scene->getUpdateCallback());
|
2020-11-20 17:46:08 +00:00
|
|
|
if (bam)
|
|
|
|
{
|
2021-02-04 21:14:21 +00:00
|
|
|
Resource::RetrieveAnimationsVisitor rav(*loaded.get(), bam, normalized, mVFS);
|
2020-11-20 17:46:08 +00:00
|
|
|
scene->accept(rav);
|
|
|
|
}
|
2020-11-18 20:48:47 +00:00
|
|
|
}
|
2015-12-14 14:11:06 +00:00
|
|
|
mCache->addEntryToObjectCache(normalized, loaded);
|
2015-12-12 17:52:06 +00:00
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 03:02:06 +00:00
|
|
|
void KeyframeManager::reportStats(unsigned int frameNumber, osg::Stats* stats) const
|
2017-02-22 01:18:18 +00:00
|
|
|
{
|
|
|
|
stats->setAttribute(frameNumber, "Keyframe", mCache->getCacheSize());
|
|
|
|
}
|
|
|
|
|
2015-12-12 17:52:06 +00:00
|
|
|
}
|