Skip to content

Commit

Permalink
Attach callbacks only if mesh really contains suitable switch nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
akortunov committed Jan 30, 2019
1 parent eff435d commit 0c0caf0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
4 changes: 3 additions & 1 deletion apps/opencs/view/render/lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <osg/NodeVisitor>
#include <osg/Switch>

#include <components/misc/constants.hpp>

class SwitchWindowsGlowVisitor : public osg::NodeVisitor
{
public:
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions apps/openmw/mwrender/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <components/sceneutil/lightutil.hpp>
#include <components/sceneutil/skeleton.hpp>
#include <components/sceneutil/positionattitudetransform.hpp>
#include <components/sceneutil/util.hpp>

#include <components/settings/settings.hpp>

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1977,7 +1978,7 @@ namespace MWRender
visitor.remove();
}

if (mObjectRoot)
if (SceneUtil::hasUserDescription(mObjectRoot, Constants::WindowsGlowLabel))
{
AddSwitchCallbacksVisitor visitor;
mObjectRoot->accept(visitor);
Expand Down
5 changes: 5 additions & 0 deletions components/misc/constants.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef OPENMW_CONSTANTS_H
#define OPENMW_CONSTANTS_H

#include <string>

namespace Constants
{

Expand All @@ -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
5 changes: 5 additions & 0 deletions components/nifosg/nifloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

// resource
#include <components/debug/debuglog.hpp>
#include <components/misc/constants.hpp>
#include <components/misc/stringops.hpp>
#include <components/misc/resourcehelpers.hpp>
#include <components/resource/imagemanager.hpp>
Expand Down Expand Up @@ -608,6 +609,10 @@ namespace NifOsg
const Nif::NiSwitchNode* niSwitchNode = static_cast<const Nif::NiSwitchNode*>(nifNode);
osg::ref_ptr<osg::Switch> 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)
{
Expand Down
20 changes: 20 additions & 0 deletions components/sceneutil/util.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "util.hpp"

#include <osg/Node>

namespace SceneUtil
{

Expand Down Expand Up @@ -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;
}

}
1 change: 1 addition & 0 deletions components/sceneutil/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0c0caf0

Please sign in to comment.