1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-12 03:36:32 +00:00
OpenMW/apps/openmw/mwrender/effectmanager.cpp

82 lines
2.0 KiB
C++
Raw Normal View History

2014-01-17 09:52:44 +00:00
#include "effectmanager.hpp"
2015-04-19 15:55:56 +00:00
#include <osg/PositionAttitudeTransform>
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/sceneutil/controller.hpp>
2014-01-17 09:52:44 +00:00
#include "animation.hpp"
2015-04-19 15:55:56 +00:00
#include "vismask.hpp"
#include "util.hpp"
2014-01-17 09:52:44 +00:00
namespace MWRender
{
2015-04-19 15:55:56 +00:00
EffectManager::EffectManager(osg::ref_ptr<osg::Group> parent, Resource::ResourceSystem* resourceSystem)
: mParentNode(parent)
, mResourceSystem(resourceSystem)
2014-01-17 09:52:44 +00:00
{
}
2015-04-19 15:55:56 +00:00
EffectManager::~EffectManager()
2014-01-17 09:52:44 +00:00
{
2015-04-19 15:55:56 +00:00
clear();
}
2014-01-17 09:52:44 +00:00
2015-04-19 15:55:56 +00:00
void EffectManager::addEffect(const std::string &model, const std::string& textureOverride, const osg::Vec3f &worldPosition, float scale)
{
osg::ref_ptr<osg::Node> node = mResourceSystem->getSceneManager()->createInstance(model);
2014-01-17 09:52:44 +00:00
2015-04-19 15:55:56 +00:00
node->setNodeMask(Mask_Effect);
2014-01-17 09:52:44 +00:00
2015-04-19 15:55:56 +00:00
Effect effect;
effect.mAnimTime.reset(new EffectAnimationTime);
2014-01-17 09:52:44 +00:00
2015-04-19 15:55:56 +00:00
SceneUtil::FindMaxControllerLengthVisitor findMaxLengthVisitor;
node->accept(findMaxLengthVisitor);
effect.mMaxControllerLength = findMaxLengthVisitor.getMaxLength();
osg::ref_ptr<osg::PositionAttitudeTransform> trans = new osg::PositionAttitudeTransform;
trans->setPosition(worldPosition);
trans->setScale(osg::Vec3f(scale, scale, scale));
trans->addChild(node);
SceneUtil::AssignControllerSourcesVisitor assignVisitor(effect.mAnimTime);
node->accept(assignVisitor);
overrideTexture(textureOverride, mResourceSystem, node);
2014-01-17 09:52:44 +00:00
2015-04-19 15:55:56 +00:00
mParentNode->addChild(trans);
mEffects[trans] = effect;
2014-01-17 09:52:44 +00:00
}
2015-04-19 15:55:56 +00:00
void EffectManager::update(float dt)
2014-01-17 09:52:44 +00:00
{
2015-04-19 15:55:56 +00:00
for (EffectMap::iterator it = mEffects.begin(); it != mEffects.end(); )
2014-01-17 09:52:44 +00:00
{
2015-04-19 15:55:56 +00:00
it->second.mAnimTime->addTime(dt);
2014-01-17 09:52:44 +00:00
2015-04-19 15:55:56 +00:00
if (it->second.mAnimTime->getTime() >= it->second.mMaxControllerLength)
2014-01-17 09:52:44 +00:00
{
2015-04-19 15:55:56 +00:00
mParentNode->removeChild(it->first);
mEffects.erase(it++);
2014-01-17 09:52:44 +00:00
}
2015-04-19 15:55:56 +00:00
else
++it;
2014-01-17 09:52:44 +00:00
}
}
void EffectManager::clear()
{
2015-04-19 15:55:56 +00:00
for (EffectMap::iterator it = mEffects.begin(); it != mEffects.end(); ++it)
2014-01-17 09:52:44 +00:00
{
2015-04-19 15:55:56 +00:00
mParentNode->removeChild(it->first);
2014-01-17 09:52:44 +00:00
}
2015-04-19 15:55:56 +00:00
mEffects.clear();
2014-01-17 09:52:44 +00:00
}
}