Port EffectManager
parent
fcc7aa02ab
commit
68f93294da
@ -1,102 +1,83 @@
|
||||
#include "effectmanager.hpp"
|
||||
|
||||
#include <osg/PositionAttitudeTransform>
|
||||
|
||||
#include <components/misc/resourcehelpers.hpp>
|
||||
|
||||
#include <OgreSceneManager.h>
|
||||
#include <OgreParticleSystem.h>
|
||||
#include <OgreSceneNode.h>
|
||||
#include <OgreTechnique.h>
|
||||
#include <components/resource/resourcesystem.hpp>
|
||||
#include <components/resource/scenemanager.hpp>
|
||||
|
||||
#include <components/sceneutil/controller.hpp>
|
||||
|
||||
#include "animation.hpp"
|
||||
#include "renderconst.hpp"
|
||||
#include "vismask.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
namespace MWRender
|
||||
{
|
||||
|
||||
EffectManager::EffectManager(Ogre::SceneManager *sceneMgr)
|
||||
: mSceneMgr(sceneMgr)
|
||||
EffectManager::EffectManager(osg::ref_ptr<osg::Group> parent, Resource::ResourceSystem* resourceSystem)
|
||||
: mParentNode(parent)
|
||||
, mResourceSystem(resourceSystem)
|
||||
{
|
||||
}
|
||||
|
||||
void EffectManager::addEffect(const std::string &model, std::string textureOverride, const Ogre::Vector3 &worldPosition, float scale)
|
||||
EffectManager::~EffectManager()
|
||||
{
|
||||
Ogre::SceneNode* sceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(worldPosition);
|
||||
sceneNode->setScale(scale,scale,scale);
|
||||
clear();
|
||||
}
|
||||
|
||||
NifOgre::ObjectScenePtr scene = NifOgre::Loader::createObjects(sceneNode, model);
|
||||
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);
|
||||
|
||||
MWRender::Animation::setRenderProperties(scene, RV_Effects,
|
||||
RQG_Main, RQG_Alpha, 0.f, false, NULL);
|
||||
node->setNodeMask(Mask_Effect);
|
||||
|
||||
for(size_t i = 0;i < scene->mControllers.size();i++)
|
||||
{
|
||||
if(scene->mControllers[i].getSource().isNull())
|
||||
scene->mControllers[i].setSource(Ogre::SharedPtr<EffectAnimationTime> (new EffectAnimationTime()));
|
||||
}
|
||||
Effect effect;
|
||||
effect.mAnimTime.reset(new EffectAnimationTime);
|
||||
|
||||
if (!textureOverride.empty())
|
||||
{
|
||||
std::string correctedTexture = Misc::ResourceHelpers::correctTexturePath(textureOverride);
|
||||
for(size_t i = 0;i < scene->mParticles.size(); ++i)
|
||||
{
|
||||
Ogre::ParticleSystem* partSys = scene->mParticles[i];
|
||||
|
||||
Ogre::MaterialPtr mat = scene->mMaterialControllerMgr.getWritableMaterial(partSys);
|
||||
|
||||
for (int t=0; t<mat->getNumTechniques(); ++t)
|
||||
{
|
||||
Ogre::Technique* tech = mat->getTechnique(t);
|
||||
for (int p=0; p<tech->getNumPasses(); ++p)
|
||||
{
|
||||
Ogre::Pass* pass = tech->getPass(p);
|
||||
for (int tex=0; tex<pass->getNumTextureUnitStates(); ++tex)
|
||||
{
|
||||
Ogre::TextureUnitState* tus = pass->getTextureUnitState(tex);
|
||||
tus->setTextureName(correctedTexture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
|
||||
mEffects.push_back(std::make_pair(sceneNode, scene));
|
||||
mParentNode->addChild(trans);
|
||||
|
||||
mEffects[trans] = effect;
|
||||
}
|
||||
|
||||
void EffectManager::update(float dt, Ogre::Camera* camera)
|
||||
void EffectManager::update(float dt)
|
||||
{
|
||||
for (std::vector<std::pair<Ogre::SceneNode*, NifOgre::ObjectScenePtr> >::iterator it = mEffects.begin(); it != mEffects.end(); )
|
||||
for (EffectMap::iterator it = mEffects.begin(); it != mEffects.end(); )
|
||||
{
|
||||
NifOgre::ObjectScenePtr objects = it->second;
|
||||
for(size_t i = 0; i < objects->mControllers.size() ;i++)
|
||||
{
|
||||
EffectAnimationTime* value = dynamic_cast<EffectAnimationTime*>(objects->mControllers[i].getSource().get());
|
||||
if (value)
|
||||
value->addTime(dt);
|
||||
|
||||
objects->mControllers[i].update();
|
||||
}
|
||||
objects->rotateBillboardNodes(camera);
|
||||
it->second.mAnimTime->addTime(dt);
|
||||
|
||||
// Finished playing?
|
||||
if (objects->mControllers[0].getSource()->getValue() >= objects->mMaxControllerLength)
|
||||
if (it->second.mAnimTime->getTime() >= it->second.mMaxControllerLength)
|
||||
{
|
||||
Ogre::SceneNode* node = it->first;
|
||||
it = mEffects.erase(it);
|
||||
mSceneMgr->destroySceneNode(node);
|
||||
continue;
|
||||
mParentNode->removeChild(it->first);
|
||||
mEffects.erase(it++);
|
||||
}
|
||||
++it;
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
void EffectManager::clear()
|
||||
{
|
||||
for (std::vector<std::pair<Ogre::SceneNode*, NifOgre::ObjectScenePtr> >::iterator it = mEffects.begin(); it != mEffects.end(); )
|
||||
for (EffectMap::iterator it = mEffects.begin(); it != mEffects.end(); ++it)
|
||||
{
|
||||
Ogre::SceneNode* node = it->first;
|
||||
it = mEffects.erase(it);
|
||||
mSceneMgr->destroySceneNode(node);
|
||||
mParentNode->removeChild(it->first);
|
||||
}
|
||||
mEffects.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
#include "util.hpp"
|
||||
|
||||
#include <osg/Node>
|
||||
|
||||
#include <components/resource/resourcesystem.hpp>
|
||||
#include <components/resource/texturemanager.hpp>
|
||||
#include <components/misc/resourcehelpers.hpp>
|
||||
|
||||
namespace MWRender
|
||||
{
|
||||
|
||||
void overrideTexture(const std::string &texture, Resource::ResourceSystem *resourceSystem, osg::ref_ptr<osg::Node> node)
|
||||
{
|
||||
if (texture.empty())
|
||||
return;
|
||||
std::string correctedTexture = Misc::ResourceHelpers::correctTexturePath(texture, resourceSystem->getVFS());
|
||||
// Not sure if wrap settings should be pulled from the overridden texture?
|
||||
osg::ref_ptr<osg::Texture2D> tex = resourceSystem->getTextureManager()->getTexture2D(correctedTexture, osg::Texture2D::CLAMP,
|
||||
osg::Texture2D::CLAMP);
|
||||
osg::ref_ptr<osg::StateSet> stateset;
|
||||
if (node->getStateSet())
|
||||
stateset = static_cast<osg::StateSet*>(node->getStateSet()->clone(osg::CopyOp::SHALLOW_COPY));
|
||||
else
|
||||
stateset = new osg::StateSet;
|
||||
|
||||
stateset->setTextureAttribute(0, tex, osg::StateAttribute::OVERRIDE);
|
||||
|
||||
node->setStateSet(stateset);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
#ifndef OPENMW_MWRENDER_UTIL_H
|
||||
#define OPENMW_MWRENDER_UTIL_H
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
#include <string>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
class Node;
|
||||
}
|
||||
|
||||
namespace Resource
|
||||
{
|
||||
class ResourceSystem;
|
||||
}
|
||||
|
||||
namespace MWRender
|
||||
{
|
||||
|
||||
void overrideTexture(const std::string& texture, Resource::ResourceSystem* resourceSystem, osg::ref_ptr<osg::Node> node);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue