Move Controller base classes to SceneUtil, add visitor to assign ControllerSources

c++11
scrawl 10 years ago
parent de2c85e0f8
commit c516e897ee

@ -74,7 +74,7 @@ protected:
virtual void setValue(Ogre::Real value);
};
class NullAnimationTime : public NifOsg::ControllerSource
class NullAnimationTime : public SceneUtil::ControllerSource
{
public:
virtual float getValue(osg::NodeVisitor *nv)

@ -13,7 +13,7 @@
#include <components/sceneutil/lightmanager.hpp>
#include <components/sceneutil/statesetcontroller.hpp>
#include <components/sceneutil/statesetupdater.hpp>
#include <components/esm/loadcell.hpp>
@ -22,7 +22,7 @@
namespace MWRender
{
class StateUpdater : public SceneUtil::StateSetController
class StateUpdater : public SceneUtil::StateSetUpdater
{
public:
virtual void setDefaults(osg::StateSet *stateset)

@ -24,7 +24,7 @@
#include <components/vfs/manager.hpp>
#include <components/sceneutil/util.hpp>
#include <components/sceneutil/statesetcontroller.hpp>
#include <components/sceneutil/statesetupdater.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
@ -37,14 +37,6 @@
namespace
{
osg::StateSet* getWritableStateSet(osg::Node* node)
{
osg::StateSet* stateset = node->getOrCreateStateSet();
osg::ref_ptr<osg::StateSet> cloned = static_cast<osg::StateSet*>(stateset->clone(osg::CopyOp::SHALLOW_COPY));
node->setStateSet(cloned);
return cloned;
}
osg::ref_ptr<osg::Material> createAlphaTrackingUnlitMaterial()
{
osg::ref_ptr<osg::Material> mat = new osg::Material;
@ -95,7 +87,7 @@ namespace
namespace MWRender
{
class AtmosphereUpdater : public SceneUtil::StateSetController
class AtmosphereUpdater : public SceneUtil::StateSetUpdater
{
public:
void setEmissionColor(osg::Vec4f emissionColor)

@ -39,7 +39,7 @@ add_component_dir (resource
)
add_component_dir (sceneutil
clone attach lightmanager visitor util statesetcontroller
clone attach lightmanager visitor util statesetupdater controller
)
add_component_dir (nif

@ -64,15 +64,6 @@ float ControllerFunction::calculate(float value)
}
}
FrameTimeSource::FrameTimeSource()
{
}
float FrameTimeSource::getValue(osg::NodeVisitor *nv)
{
return nv->getFrameStamp()->getSimulationTime();
}
KeyframeController::KeyframeController()
{
}
@ -207,20 +198,6 @@ void KeyframeController::operator() (osg::Node* node, osg::NodeVisitor* nv)
traverse(node, nv);
}
Controller::Controller()
{
}
bool Controller::hasInput() const
{
return mSource.get() != NULL;
}
float Controller::getInputValue(osg::NodeVisitor* nv)
{
return mFunction->calculate(mSource->getValue(nv));
}
GeomMorpherController::GeomMorpherController()
{
}
@ -278,7 +255,7 @@ UVController::UVController(const Nif::NiUVData *data, std::set<int> textureUnits
}
UVController::UVController(const UVController& copy, const osg::CopyOp& copyop)
: osg::Object(copy, copyop), StateSetController(copy, copyop), Controller(copy)
: osg::Object(copy, copyop), StateSetUpdater(copy, copyop), Controller(copy)
, mUTrans(copy.mUTrans)
, mVTrans(copy.mVTrans)
, mUScale(copy.mUScale)
@ -367,7 +344,7 @@ AlphaController::AlphaController()
}
AlphaController::AlphaController(const AlphaController &copy, const osg::CopyOp &copyop)
: StateSetController(copy, copyop), ValueInterpolator(), Controller(copy)
: StateSetUpdater(copy, copyop), ValueInterpolator(), Controller(copy)
, mData(copy.mData)
{
}
@ -394,7 +371,7 @@ MaterialColorController::MaterialColorController()
}
MaterialColorController::MaterialColorController(const MaterialColorController &copy, const osg::CopyOp &copyop)
: StateSetController(copy, copyop), Controller(copy)
: StateSetUpdater(copy, copyop), Controller(copy)
, mData(copy.mData)
{
}
@ -423,7 +400,7 @@ FlipController::FlipController()
}
FlipController::FlipController(const FlipController &copy, const osg::CopyOp &copyop)
: StateSetController(copy, copyop)
: StateSetUpdater(copy, copyop)
, Controller(copy)
, mTexSlot(copy.mTexSlot)
, mDelta(copy.mDelta)

@ -8,7 +8,8 @@
#include <components/nifcache/nifcache.hpp>
#include <components/sceneutil/statesetcontroller.hpp>
#include <components/sceneutil/controller.hpp>
#include <components/sceneutil/statesetupdater.hpp>
#include <boost/shared_ptr.hpp>
@ -43,7 +44,6 @@ namespace osgAnimation
namespace NifOsg
{
// FIXME: Should not be here. We might also want to use this for non-NIF model formats
class ValueInterpolator
{
protected:
@ -76,8 +76,7 @@ namespace NifOsg
}
};
// FIXME: Should not be here. We might also want to use this for non-NIF model formats
class ControllerFunction
class ControllerFunction : public SceneUtil::ControllerFunction
{
private:
float mFrequency;
@ -98,35 +97,7 @@ namespace NifOsg
float calculate(float value);
};
class ControllerSource
{
public:
virtual float getValue(osg::NodeVisitor* nv) = 0;
};
class FrameTimeSource : public ControllerSource
{
public:
FrameTimeSource();
virtual float getValue(osg::NodeVisitor* nv);
};
class Controller
{
public:
Controller();
bool hasInput() const;
float getInputValue(osg::NodeVisitor* nv);
boost::shared_ptr<ControllerSource> mSource;
// The source value gets passed through this function before it's passed on to the DestValue.
boost::shared_ptr<ControllerFunction> mFunction;
};
class GeomMorpherController : public osg::Drawable::UpdateCallback, public Controller, public ValueInterpolator
class GeomMorpherController : public osg::Drawable::UpdateCallback, public SceneUtil::Controller, public ValueInterpolator
{
public:
GeomMorpherController(const Nif::NiMorphData* data);
@ -141,7 +112,7 @@ namespace NifOsg
std::vector<Nif::FloatKeyMapPtr> mKeyFrames;
};
class KeyframeController : public osg::NodeCallback, public Controller, public ValueInterpolator
class KeyframeController : public osg::NodeCallback, public SceneUtil::Controller, public ValueInterpolator
{
public:
KeyframeController(const Nif::NiKeyframeData *data);
@ -191,7 +162,7 @@ namespace NifOsg
bool mEnabled;
};
class UVController : public SceneUtil::StateSetController, public Controller, public ValueInterpolator
class UVController : public SceneUtil::StateSetUpdater, public SceneUtil::Controller, public ValueInterpolator
{
public:
UVController();
@ -211,7 +182,7 @@ namespace NifOsg
std::set<int> mTextureUnits;
};
class VisController : public osg::NodeCallback, public Controller
class VisController : public osg::NodeCallback, public SceneUtil::Controller
{
private:
std::vector<Nif::NiVisData::VisData> mData;
@ -228,7 +199,7 @@ namespace NifOsg
virtual void operator() (osg::Node* node, osg::NodeVisitor* nv);
};
class AlphaController : public SceneUtil::StateSetController, public Controller, public ValueInterpolator
class AlphaController : public SceneUtil::StateSetUpdater, public SceneUtil::Controller, public ValueInterpolator
{
private:
Nif::FloatKeyMapPtr mData;
@ -243,7 +214,7 @@ namespace NifOsg
META_Object(NifOsg, AlphaController)
};
class MaterialColorController : public SceneUtil::StateSetController, public Controller, public ValueInterpolator
class MaterialColorController : public SceneUtil::StateSetUpdater, public SceneUtil::Controller, public ValueInterpolator
{
private:
Nif::Vector3KeyMapPtr mData;
@ -258,7 +229,7 @@ namespace NifOsg
virtual void apply(osg::StateSet* stateset, osg::NodeVisitor* nv);
};
class FlipController : public SceneUtil::StateSetController, public Controller
class FlipController : public SceneUtil::StateSetUpdater, public SceneUtil::Controller
{
private:
int mTexSlot;
@ -275,7 +246,7 @@ namespace NifOsg
virtual void apply(osg::StateSet *stateset, osg::NodeVisitor *nv);
};
class ParticleSystemController : public osg::NodeCallback, public Controller
class ParticleSystemController : public osg::NodeCallback, public SceneUtil::Controller
{
public:
ParticleSystemController(const Nif::NiParticleSystemController* ctrl);

@ -719,7 +719,7 @@ namespace NifOsg
return skel;
}
static void applyNodeProperties(const Nif::Node *nifNode, osg::Node *applyTo, SceneUtil::CompositeStateSetController* composite, Resource::TextureManager* textureManager, std::map<int, int>& boundTextures, int animflags)
static void applyNodeProperties(const Nif::Node *nifNode, osg::Node *applyTo, SceneUtil::CompositeStateSetUpdater* composite, Resource::TextureManager* textureManager, std::map<int, int>& boundTextures, int animflags)
{
const Nif::PropertyList& props = nifNode->props;
for (size_t i = 0; i <props.length();++i)
@ -729,12 +729,11 @@ namespace NifOsg
}
}
static void setupController(const Nif::Controller* ctrl, Controller* toSetup, int animflags)
static void setupController(const Nif::Controller* ctrl, SceneUtil::Controller* toSetup, int animflags)
{
// TODO: uncomment this, currently commented for easier testing
//bool autoPlay = animflags & Nif::NiNode::AnimFlag_AutoPlay;
//if (autoPlay)
toSetup->mSource = boost::shared_ptr<ControllerSource>(new FrameTimeSource);
bool autoPlay = animflags & Nif::NiNode::AnimFlag_AutoPlay;
if (autoPlay)
toSetup->mSource = boost::shared_ptr<SceneUtil::ControllerSource>(new SceneUtil::FrameTimeSource);
toSetup->mFunction = boost::shared_ptr<ControllerFunction>(new ControllerFunction(ctrl));
}
@ -825,7 +824,7 @@ namespace NifOsg
transformNode->setNodeMask(0x1);
}
osg::ref_ptr<SceneUtil::CompositeStateSetController> composite = new SceneUtil::CompositeStateSetController;
osg::ref_ptr<SceneUtil::CompositeStateSetUpdater> composite = new SceneUtil::CompositeStateSetUpdater;
applyNodeProperties(nifNode, transformNode, composite, textureManager, boundTextures, animflags);
@ -871,7 +870,7 @@ namespace NifOsg
return transformNode;
}
static void handleMeshControllers(const Nif::Node *nifNode, SceneUtil::CompositeStateSetController* composite, const std::map<int, int> &boundTextures, int animflags)
static void handleMeshControllers(const Nif::Node *nifNode, SceneUtil::CompositeStateSetUpdater* composite, const std::map<int, int> &boundTextures, int animflags)
{
for (Nif::ControllerPtr ctrl = nifNode->controller; !ctrl.empty(); ctrl = ctrl->next)
{
@ -921,7 +920,7 @@ namespace NifOsg
static void handleMaterialControllers(const Nif::Property *materialProperty, osg::Node* node, int animflags)
{
osg::ref_ptr<SceneUtil::CompositeStateSetController> composite = new SceneUtil::CompositeStateSetController;
osg::ref_ptr<SceneUtil::CompositeStateSetUpdater> composite = new SceneUtil::CompositeStateSetUpdater;
for (Nif::ControllerPtr ctrl = materialProperty->controller; !ctrl.empty(); ctrl = ctrl->next)
{
if (!(ctrl->flags & Nif::NiNode::ControllerFlag_Active))
@ -947,7 +946,7 @@ namespace NifOsg
node->addUpdateCallback(composite);
}
static void handleTextureControllers(const Nif::Property *texProperty, SceneUtil::CompositeStateSetController* composite, Resource::TextureManager* textureManager, osg::StateSet *stateset, int animflags)
static void handleTextureControllers(const Nif::Property *texProperty, SceneUtil::CompositeStateSetUpdater* composite, Resource::TextureManager* textureManager, osg::StateSet *stateset, int animflags)
{
for (Nif::ControllerPtr ctrl = texProperty->controller; !ctrl.empty(); ctrl = ctrl->next)
{
@ -1426,7 +1425,7 @@ namespace NifOsg
static void handleProperty(const Nif::Property *property,
osg::Node *node, SceneUtil::CompositeStateSetController* composite, Resource::TextureManager* textureManager, std::map<int, int>& boundTextures, int animflags)
osg::Node *node, SceneUtil::CompositeStateSetUpdater* composite, Resource::TextureManager* textureManager, std::map<int, int>& boundTextures, int animflags)
{
osg::StateSet* stateset = node->getOrCreateStateSet();

@ -0,0 +1,86 @@
#include "controller.hpp"
#include "statesetupdater.hpp"
#include <osg/Drawable>
#include <osg/Geode>
namespace SceneUtil
{
Controller::Controller()
{
}
bool Controller::hasInput() const
{
return mSource.get() != NULL;
}
float Controller::getInputValue(osg::NodeVisitor* nv)
{
return mFunction->calculate(mSource->getValue(nv));
}
FrameTimeSource::FrameTimeSource()
{
}
float FrameTimeSource::getValue(osg::NodeVisitor *nv)
{
return nv->getFrameStamp()->getSimulationTime();
}
AssignControllerSourcesVisitor::AssignControllerSourcesVisitor()
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{
}
AssignControllerSourcesVisitor::AssignControllerSourcesVisitor(boost::shared_ptr<ControllerSource> toAssign)
: mToAssign(toAssign)
, osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{
}
void AssignControllerSourcesVisitor::apply(osg::Node &node)
{
osg::NodeCallback* callback = node.getUpdateCallback();
while (callback)
{
if (Controller* ctrl = dynamic_cast<Controller*>(callback))
assign(node, *ctrl);
if (CompositeStateSetUpdater* composite = dynamic_cast<CompositeStateSetUpdater*>(callback))
{
for (unsigned int i=0; i<composite->getNumControllers(); ++i)
{
StateSetUpdater* statesetcontroller = composite->getController(i);
if (Controller* ctrl = dynamic_cast<Controller*>(statesetcontroller))
assign(node, *ctrl);
}
}
callback = callback->getNestedCallback();
}
traverse(node);
}
void AssignControllerSourcesVisitor::apply(osg::Geode &geode)
{
for (unsigned int i=0; i<geode.getNumDrawables(); ++i)
{
osg::Drawable* drw = geode.getDrawable(i);
osg::Drawable::UpdateCallback* callback = drw->getUpdateCallback();
if (Controller* ctrl = dynamic_cast<Controller*>(callback))
assign(geode, *ctrl);
}
}
void AssignControllerSourcesVisitor::assign(osg::Node&, Controller &ctrl)
{
if (!ctrl.mSource.get())
ctrl.mSource = mToAssign;
}
}

@ -0,0 +1,64 @@
#ifndef OPENMW_COMPONENTS_SCENEUTIL_CONTROLLER_H
#define OPENMW_COMPONENTS_SCENEUTIL_CONTROLLER_H
#include <boost/shared_ptr.hpp>
#include <osg/NodeVisitor>
namespace SceneUtil
{
class ControllerSource
{
public:
virtual float getValue(osg::NodeVisitor* nv) = 0;
};
class FrameTimeSource : public ControllerSource
{
public:
FrameTimeSource();
virtual float getValue(osg::NodeVisitor* nv);
};
class ControllerFunction
{
public:
virtual float calculate(float input) = 0;
};
class Controller
{
public:
Controller();
bool hasInput() const;
float getInputValue(osg::NodeVisitor* nv);
boost::shared_ptr<ControllerSource> mSource;
// The source value gets passed through this function before it's passed on to the DestValue.
boost::shared_ptr<ControllerFunction> mFunction;
};
class AssignControllerSourcesVisitor : public osg::NodeVisitor
{
public:
AssignControllerSourcesVisitor();
AssignControllerSourcesVisitor(boost::shared_ptr<ControllerSource> toAssign);
virtual void apply(osg::Node& node);
virtual void apply(osg::Geode& geode);
/// Assign the wanted ControllerSource. May be overriden in derived classes.
/// By default assigns the ControllerSource passed to the constructor of this class if no ControllerSource is assigned to that controller yet.
virtual void assign(osg::Node& node, Controller& ctrl);
private:
boost::shared_ptr<ControllerSource> mToAssign;
};
}
#endif

@ -1,11 +1,11 @@
#include "statesetcontroller.hpp"
#include "statesetupdater.hpp"
#include <osg/Node>
namespace SceneUtil
{
void StateSetController::operator()(osg::Node* node, osg::NodeVisitor* nv)
void StateSetUpdater::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
if (!mStateSets[0])
{
@ -28,45 +28,51 @@ namespace SceneUtil
traverse(node, nv);
}
StateSetController::StateSetController()
StateSetUpdater::StateSetUpdater()
{
}
StateSetController::StateSetController(const StateSetController &copy, const osg::CopyOp &copyop)
StateSetUpdater::StateSetUpdater(const StateSetUpdater &copy, const osg::CopyOp &copyop)
: osg::NodeCallback(copy, copyop)
{
}
// ----------------------------------------------------------------------------------
void CompositeStateSetController::apply(osg::StateSet *stateset, osg::NodeVisitor *nv)
void CompositeStateSetUpdater::apply(osg::StateSet *stateset, osg::NodeVisitor *nv)
{
for (unsigned int i=0; i<mCtrls.size(); ++i)
mCtrls[i]->apply(stateset, nv);
}
void CompositeStateSetController::setDefaults(osg::StateSet *stateset)
void CompositeStateSetUpdater::setDefaults(osg::StateSet *stateset)
{
for (unsigned int i=0; i<mCtrls.size(); ++i)
mCtrls[i]->setDefaults(stateset);
}
CompositeStateSetController::CompositeStateSetController()
CompositeStateSetUpdater::CompositeStateSetUpdater()
{
}
CompositeStateSetController::CompositeStateSetController(const CompositeStateSetController &copy, const osg::CopyOp &copyop)
: StateSetController(copy, copyop)
, mCtrls(copy.mCtrls)
CompositeStateSetUpdater::CompositeStateSetUpdater(const CompositeStateSetUpdater &copy, const osg::CopyOp &copyop)
: StateSetUpdater(copy, copyop)
{
for (unsigned int i=0; i<copy.mCtrls.size(); ++i)
mCtrls.push_back(static_cast<StateSetUpdater*>(osg::clone(copy.mCtrls[i].get(), copyop)));
}
unsigned int CompositeStateSetController::getNumControllers()
unsigned int CompositeStateSetUpdater::getNumControllers()
{
return mCtrls.size();
}
void CompositeStateSetController::addController(StateSetController *ctrl)
StateSetUpdater* CompositeStateSetUpdater::getController(int i)
{
return mCtrls[i];
}
void CompositeStateSetUpdater::addController(StateSetUpdater *ctrl)
{
mCtrls.push_back(ctrl);
}

@ -15,14 +15,14 @@ namespace SceneUtil
/// the first StateSet is the one we can write to, the second is the one currently in use by the draw traversal of the last frame.
/// After a frame is completed the places are swapped.
/// @par Must be set as UpdateCallback on a Node.
/// @note Do not add multiple StateSetControllers on the same Node as they will conflict - instead use the CompositeStateSetController.
class StateSetController : public osg::NodeCallback
/// @note Do not add multiple StateSetControllers on the same Node as they will conflict - instead use the CompositeStateSetUpdater.
class StateSetUpdater : public osg::NodeCallback
{
public:
StateSetController();
StateSetController(const StateSetController& copy, const osg::CopyOp& copyop);
StateSetUpdater();
StateSetUpdater(const StateSetUpdater& copy, const osg::CopyOp& copyop);
META_Object(SceneUtil, StateSetController)
META_Object(SceneUtil, StateSetUpdater)
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
@ -40,17 +40,18 @@ namespace SceneUtil
};
/// @brief A variant of the StateSetController that can be made up of multiple controllers all controlling the same target.
class CompositeStateSetController : public StateSetController
class CompositeStateSetUpdater : public StateSetUpdater
{
public:
CompositeStateSetController();
CompositeStateSetController(const CompositeStateSetController& copy, const osg::CopyOp& copyop);
CompositeStateSetUpdater();
CompositeStateSetUpdater(const CompositeStateSetUpdater& copy, const osg::CopyOp& copyop);
META_Object(SceneUtil, CompositeStateSetController)
META_Object(SceneUtil, CompositeStateSetUpdater)
unsigned int getNumControllers();
StateSetUpdater* getController(int i);
void addController(StateSetController* ctrl);
void addController(StateSetUpdater* ctrl);
virtual void apply(osg::StateSet* stateset, osg::NodeVisitor* nv);
@ -58,7 +59,7 @@ namespace SceneUtil
virtual void setDefaults(osg::StateSet *stateset);
std::vector<osg::ref_ptr<StateSetController> > mCtrls;
std::vector<osg::ref_ptr<StateSetUpdater> > mCtrls;
};
}
Loading…
Cancel
Save