From aaf7b423d6796169f382883a669ebfb9c248dc96 Mon Sep 17 00:00:00 2001 From: Bo Svensson <90132211+bosvensson1@users.noreply.github.com> Date: Mon, 4 Oct 2021 08:56:55 +0000 Subject: [PATCH] adds a replacement for osg::NodeCallback (#3144) * nodecallback.hpp * lightmanager.hpp * lightmanager.cpp * lightmanager.hpp * nodecallback.hpp * nodecallback.hpp * [ci skip] * lightmanager.hpp * nodecallback.hpp * nodecallback.hpp * lightmanager.cpp * lightmanager.cpp * nodecallback.hpp * [ci skip] * [ci skip] * controller.cpp * [ci skip] * osgacontroller.cpp * keyframe.hpp * controller.hpp * keyframe.hpp * [ci skip] * keyframe.hpp * animation.hpp * [ci skip] * weaponanimation.cpp * nodecallback.hpp --- apps/openmw/mwrender/animation.hpp | 2 +- apps/openmw/mwrender/weaponanimation.cpp | 2 +- apps/openmw/mwrender/weaponanimation.hpp | 2 +- components/nifosg/controller.cpp | 1 + components/nifosg/controller.hpp | 5 +-- components/sceneutil/keyframe.hpp | 8 ++--- components/sceneutil/lightmanager.cpp | 24 ++++---------- components/sceneutil/lightmanager.hpp | 7 ++-- components/sceneutil/nodecallback.hpp | 41 ++++++++++++++++++++++++ components/sceneutil/osgacontroller.cpp | 2 +- components/sceneutil/osgacontroller.hpp | 5 +-- 11 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 components/sceneutil/nodecallback.hpp diff --git a/apps/openmw/mwrender/animation.hpp b/apps/openmw/mwrender/animation.hpp index ffcd9b3260..c2f0f351f3 100644 --- a/apps/openmw/mwrender/animation.hpp +++ b/apps/openmw/mwrender/animation.hpp @@ -245,7 +245,7 @@ protected: // Keep track of controllers that we added to our scene graph. // We may need to rebuild these controllers when the active animation groups / sources change. - std::vector, osg::ref_ptr>> mActiveControllers; + std::vector, osg::ref_ptr>> mActiveControllers; std::shared_ptr mAnimationTimePtr[sNumBlendMasks]; diff --git a/apps/openmw/mwrender/weaponanimation.cpp b/apps/openmw/mwrender/weaponanimation.cpp index fb92a4980a..b89cfd8df1 100644 --- a/apps/openmw/mwrender/weaponanimation.cpp +++ b/apps/openmw/mwrender/weaponanimation.cpp @@ -173,7 +173,7 @@ void WeaponAnimation::releaseArrow(MWWorld::Ptr actor, float attackStrength) } void WeaponAnimation::addControllers(const std::map >& nodes, - std::vector, osg::ref_ptr>> &map, osg::Node* objectRoot) + std::vector, osg::ref_ptr>> &map, osg::Node* objectRoot) { for (int i=0; i<2; ++i) { diff --git a/apps/openmw/mwrender/weaponanimation.hpp b/apps/openmw/mwrender/weaponanimation.hpp index 333dccc915..1f614463a6 100644 --- a/apps/openmw/mwrender/weaponanimation.hpp +++ b/apps/openmw/mwrender/weaponanimation.hpp @@ -43,7 +43,7 @@ namespace MWRender /// Add WeaponAnimation-related controllers to \a nodes and store the added controllers in \a map. void addControllers(const std::map >& nodes, - std::vector, osg::ref_ptr>>& map, osg::Node* objectRoot); + std::vector, osg::ref_ptr>>& map, osg::Node* objectRoot); void deleteControllers(); diff --git a/components/nifosg/controller.cpp b/components/nifosg/controller.cpp index d48c55ad7d..c8a6ef74ab 100644 --- a/components/nifosg/controller.cpp +++ b/components/nifosg/controller.cpp @@ -72,6 +72,7 @@ KeyframeController::KeyframeController() KeyframeController::KeyframeController(const KeyframeController ©, const osg::CopyOp ©op) : SceneUtil::KeyframeController(copy, copyop) + , SceneUtil::NodeCallback(copy, copyop) , mRotations(copy.mRotations) , mXRotations(copy.mXRotations) , mYRotations(copy.mYRotations) diff --git a/components/nifosg/controller.hpp b/components/nifosg/controller.hpp index b459166931..078b7e14ce 100644 --- a/components/nifosg/controller.hpp +++ b/components/nifosg/controller.hpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -223,7 +224,7 @@ namespace NifOsg std::vector mKeyFrames; }; - class KeyframeController : public SceneUtil::KeyframeController + class KeyframeController : public SceneUtil::KeyframeController, public SceneUtil::NodeCallback { public: // This is used if there's no interpolator but there is data (Morrowind meshes). @@ -241,7 +242,7 @@ namespace NifOsg osg::Vec3f getTranslation(float time) const override; - void operator() (osg::Node*, osg::NodeVisitor*) override; + void operator() (osg::Node*, osg::NodeVisitor*); private: QuaternionInterpolator mRotations; diff --git a/components/sceneutil/keyframe.hpp b/components/sceneutil/keyframe.hpp index e09541cb9a..5be6924a09 100644 --- a/components/sceneutil/keyframe.hpp +++ b/components/sceneutil/keyframe.hpp @@ -3,7 +3,7 @@ #include -#include +#include #include #include @@ -11,20 +11,18 @@ namespace SceneUtil { - class KeyframeController : public osg::NodeCallback, public SceneUtil::Controller + class KeyframeController : public SceneUtil::Controller, public virtual osg::Callback { public: KeyframeController() {} KeyframeController(const KeyframeController& copy, const osg::CopyOp& copyop) - : osg::NodeCallback(copy, copyop) + : osg::Callback(copy, copyop) , SceneUtil::Controller(copy) {} META_Object(SceneUtil, KeyframeController) virtual osg::Vec3f getTranslation(float time) const { return osg::Vec3f(); } - - virtual void operator() (osg::Node* node, osg::NodeVisitor* nodeVisitor) override { traverse(node, nodeVisitor); } }; /// Wrapper object containing an animation track as a ref-countable osg::Object. diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index ba60cb3185..09fa302457 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -605,19 +605,19 @@ namespace SceneUtil // Set on a LightSource. Adds the light source to its light manager for the current frame. // This allows us to keep track of the current lights in the scene graph without tying creation & destruction to the manager. - class CollectLightCallback : public osg::NodeCallback + class CollectLightCallback : public SceneUtil::NodeCallback { public: CollectLightCallback() : mLightManager(nullptr) { } CollectLightCallback(const CollectLightCallback& copy, const osg::CopyOp& copyop) - : osg::NodeCallback(copy, copyop) + : SceneUtil::NodeCallback(copy, copyop) , mLightManager(nullptr) { } META_Object(SceneUtil, SceneUtil::CollectLightCallback) - void operator()(osg::Node* node, osg::NodeVisitor* nv) override + void operator()(osg::Node* node, osg::NodeVisitor* nv) { if (!mLightManager) { @@ -637,19 +637,11 @@ namespace SceneUtil }; // Set on a LightManager. Clears the data from the previous frame. - class LightManagerUpdateCallback : public osg::NodeCallback + class LightManagerUpdateCallback : public SceneUtil::NodeCallback { public: - LightManagerUpdateCallback() - { } - - LightManagerUpdateCallback(const LightManagerUpdateCallback& copy, const osg::CopyOp& copyop) - : osg::NodeCallback(copy, copyop) - { } - - META_Object(SceneUtil, LightManagerUpdateCallback) - void operator()(osg::Node* node, osg::NodeVisitor* nv) override + void operator()(osg::Node* node, osg::NodeVisitor* nv) { LightManager* lightManager = static_cast(node); lightManager->update(nv->getTraversalNumber()); @@ -1285,12 +1277,10 @@ namespace SceneUtil mLight[i] = new osg::Light(*copy.mLight[i].get(), copyop); } - void LightListCallback::operator()(osg::Node *node, osg::NodeVisitor *nv) + void LightListCallback::operator()(osg::Node *node, osgUtil::CullVisitor *cv) { - osgUtil::CullVisitor* cv = static_cast(nv); - bool pushedState = pushLightState(node, cv); - traverse(node, nv); + traverse(node, cv); if (pushedState) cv->popStateSet(); } diff --git a/components/sceneutil/lightmanager.hpp b/components/sceneutil/lightmanager.hpp index 6dbe7a3f75..4048bf9b09 100644 --- a/components/sceneutil/lightmanager.hpp +++ b/components/sceneutil/lightmanager.hpp @@ -16,6 +16,7 @@ #include #include +#include namespace osgUtil { @@ -254,7 +255,7 @@ namespace SceneUtil /// starting point is to attach a LightListCallback to each game object's base node. /// @note Not thread safe for CullThreadPerCamera threading mode. /// @note Due to lack of OSG support, the callback does not work on Drawables. - class LightListCallback : public osg::NodeCallback + class LightListCallback : public SceneUtil::NodeCallback { public: LightListCallback() @@ -262,7 +263,7 @@ namespace SceneUtil , mLastFrameNumber(0) {} LightListCallback(const LightListCallback& copy, const osg::CopyOp& copyop) - : osg::Object(copy, copyop), osg::NodeCallback(copy, copyop) + : osg::Object(copy, copyop), SceneUtil::NodeCallback(copy, copyop) , mLightManager(copy.mLightManager) , mLastFrameNumber(0) , mIgnoredLightSources(copy.mIgnoredLightSources) @@ -270,7 +271,7 @@ namespace SceneUtil META_Object(SceneUtil, LightListCallback) - void operator()(osg::Node* node, osg::NodeVisitor* nv) override; + void operator()(osg::Node* node, osgUtil::CullVisitor* nv); bool pushLightState(osg::Node* node, osgUtil::CullVisitor* nv); diff --git a/components/sceneutil/nodecallback.hpp b/components/sceneutil/nodecallback.hpp new file mode 100644 index 0000000000..6f0140d64c --- /dev/null +++ b/components/sceneutil/nodecallback.hpp @@ -0,0 +1,41 @@ +#ifndef SCENEUTIL_NODECALLBACK_H +#define SCENEUTIL_NODECALLBACK_H + +#include + +namespace osg +{ + class Node; + class NodeVisitor; +} + +namespace SceneUtil +{ + +template +class NodeCallback : public virtual osg::Callback +{ +public: + NodeCallback(){} + NodeCallback(const NodeCallback& nc,const osg::CopyOp& copyop): + osg::Callback(nc, copyop) {} + META_Object(SceneUtil, NodeCallback) + + bool run(osg::Object* object, osg::Object* data) override + { + static_cast(this)->operator()((NodeType)object, (VisitorType)data->asNodeVisitor()); + return true; + } + + template + void traverse(NodeType object, VT data) + { + if (_nestedCallback.valid()) + _nestedCallback->run(object, data); + else + data->traverse(*object); + } +}; + +} +#endif diff --git a/components/sceneutil/osgacontroller.cpp b/components/sceneutil/osgacontroller.cpp index 02c9558ab3..b986c92884 100644 --- a/components/sceneutil/osgacontroller.cpp +++ b/components/sceneutil/osgacontroller.cpp @@ -102,7 +102,7 @@ namespace SceneUtil apply(static_cast(node)); } - OsgAnimationController::OsgAnimationController(const OsgAnimationController ©, const osg::CopyOp ©op) : SceneUtil::KeyframeController(copy, copyop) + OsgAnimationController::OsgAnimationController(const OsgAnimationController ©, const osg::CopyOp ©op) : SceneUtil::KeyframeController(copy, copyop), SceneUtil::NodeCallback(copy, copyop) , mEmulatedAnimations(copy.mEmulatedAnimations) { mLinker = nullptr; diff --git a/components/sceneutil/osgacontroller.hpp b/components/sceneutil/osgacontroller.hpp index 46538fa813..427a79ca97 100644 --- a/components/sceneutil/osgacontroller.hpp +++ b/components/sceneutil/osgacontroller.hpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -44,7 +45,7 @@ namespace SceneUtil Resource::Animation* mAnimation; }; - class OsgAnimationController : public SceneUtil::KeyframeController + class OsgAnimationController : public SceneUtil::KeyframeController, public SceneUtil::NodeCallback { public: /// @brief Handles the animation for osgAnimation formats @@ -61,7 +62,7 @@ namespace SceneUtil void update(float time, const std::string& animationName); /// @brief Called every frame for osgAnimation - void operator() (osg::Node*, osg::NodeVisitor*) override; + void operator() (osg::Node*, osg::NodeVisitor*); /// @brief Sets details of the animations void setEmulatedAnimations(const std::vector& emulatedAnimations);