From 0c0caf0b3bd7b0bacea7fe92fb6d6584b9687ef9 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Mon, 28 Jan 2019 21:21:16 +0400 Subject: [PATCH] Attach callbacks only if mesh really contains suitable switch nodes --- apps/opencs/view/render/lighting.cpp | 4 +++- apps/openmw/mwrender/animation.cpp | 5 +++-- components/misc/constants.hpp | 5 +++++ components/nifosg/nifloader.cpp | 5 +++++ components/sceneutil/util.cpp | 20 ++++++++++++++++++++ components/sceneutil/util.hpp | 1 + 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/opencs/view/render/lighting.cpp b/apps/opencs/view/render/lighting.cpp index c33840079e26..96f5309dc7fe 100644 --- a/apps/opencs/view/render/lighting.cpp +++ b/apps/opencs/view/render/lighting.cpp @@ -4,6 +4,8 @@ #include #include +#include + class SwitchWindowsGlowVisitor : public osg::NodeVisitor { public: @@ -14,7 +16,7 @@ class SwitchWindowsGlowVisitor : public osg::NodeVisitor virtual void apply(osg::Switch &switchNode) { - if (switchNode.getName() == "NightDaySwitch") + if (switchNode.getName() == Constants::WindowsGlowLabel) switchNode.setSingleChildOn(mIndex); traverse(switchNode); diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp index d84c21019b03..21b39ad74ad1 100644 --- a/apps/openmw/mwrender/animation.cpp +++ b/apps/openmw/mwrender/animation.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include @@ -126,7 +127,7 @@ namespace virtual void apply(osg::Switch &switchNode) { - if (switchNode.getName() == "NightDaySwitch") + if (switchNode.getName() == Constants::WindowsGlowLabel) switchNode.addUpdateCallback(new DayNightCallback()); traverse(switchNode); @@ -1977,7 +1978,7 @@ namespace MWRender visitor.remove(); } - if (mObjectRoot) + if (SceneUtil::hasUserDescription(mObjectRoot, Constants::WindowsGlowLabel)) { AddSwitchCallbacksVisitor visitor; mObjectRoot->accept(visitor); diff --git a/components/misc/constants.hpp b/components/misc/constants.hpp index 7174ae88875a..02a6dc51c5d9 100644 --- a/components/misc/constants.hpp +++ b/components/misc/constants.hpp @@ -1,6 +1,8 @@ #ifndef OPENMW_CONSTANTS_H #define OPENMW_CONSTANTS_H +#include + namespace Constants { @@ -22,6 +24,9 @@ const float GravityConst = 8.96f; // Size of one exterior cell in game units const int CellSizeInUnits = 8192; +// A label to mark night/day visual switches +const std::string WindowsGlowLabel = "NightDaySwitch"; + } #endif diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index 1b064c85c3da..50b298baead2 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -11,6 +11,7 @@ // resource #include +#include #include #include #include @@ -608,6 +609,10 @@ namespace NifOsg const Nif::NiSwitchNode* niSwitchNode = static_cast(nifNode); osg::ref_ptr switchNode = handleSwitchNode(niSwitchNode); node->addChild(switchNode); + + if (niSwitchNode->name == Constants::WindowsGlowLabel) + rootNode->getOrCreateUserDataContainer()->addDescription(Constants::WindowsGlowLabel); + const Nif::NodeList &children = niSwitchNode->children; for(size_t i = 0;i < children.length();++i) { diff --git a/components/sceneutil/util.cpp b/components/sceneutil/util.cpp index bbeda683ae4e..a9857bed315f 100644 --- a/components/sceneutil/util.cpp +++ b/components/sceneutil/util.cpp @@ -1,5 +1,7 @@ #include "util.hpp" +#include + namespace SceneUtil { @@ -53,4 +55,22 @@ float makeOsgColorComponent(unsigned int value, unsigned int shift) return float((value >> shift) & 0xFFu) / 255.0f; } +bool hasUserDescription(const osg::Node* node, const std::string pattern) +{ + if (node == nullptr) + return false; + + const osg::UserDataContainer* udc = node->getUserDataContainer(); + if (udc && udc->getNumDescriptions() > 0) + { + for (auto& descr : udc->getDescriptions()) + { + if (descr == pattern) + return true; + } + } + + return false; +} + } diff --git a/components/sceneutil/util.hpp b/components/sceneutil/util.hpp index 156d173d28f4..f293b99758bd 100644 --- a/components/sceneutil/util.hpp +++ b/components/sceneutil/util.hpp @@ -19,6 +19,7 @@ namespace SceneUtil float makeOsgColorComponent (unsigned int value, unsigned int shift); + bool hasUserDescription(const osg::Node* node, const std::string pattern); } #endif