From 6319254d71ce4fe8b1672c0e23363a77b504ea9f Mon Sep 17 00:00:00 2001 From: James Turner Date: Thu, 11 Feb 2021 11:23:38 +0000 Subject: [PATCH] Fix ownership of SGLight::appendLight, using a ref_ptr Fix some other C++11-isms as well. --- simgear/scene/model/SGLight.cxx | 51 ++++++++++++++++++++++++++------- simgear/scene/model/SGLight.hxx | 31 +++++--------------- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/simgear/scene/model/SGLight.cxx b/simgear/scene/model/SGLight.cxx index 613c75a2..ba37f863 100644 --- a/simgear/scene/model/SGLight.cxx +++ b/simgear/scene/model/SGLight.cxx @@ -33,27 +33,58 @@ class SGLightDebugListener : public SGPropertyChangeListener { public: SGLightDebugListener(osg::Switch *sw) : _sw(sw) {} - virtual void valueChanged(SGPropertyNode *node) { + void valueChanged(SGPropertyNode* node) override + { _sw->setValue(0, node->getBoolValue()); } + private: osg::ref_ptr _sw; }; + +void SGLight::UpdateCallback::configure(const osg::Vec4& ambient, + const osg::Vec4& diffuse, + const osg::Vec4& specular) +{ + _ambient = ambient; + _diffuse = diffuse; + _specular = specular; +} + +void SGLight::UpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) +{ + double value = 1.0; + if (_expression) { + value = _expression->getValue(); + } + if (value != _prev_value) { + SGLight* light = dynamic_cast(node); + if (light) { + light->setAmbient(_ambient * value); + light->setDiffuse(_diffuse * value); + light->setSpecular(_specular * value); + } + _prev_value = value; + } +} + class SGLightConfigListener : public SGPropertyChangeListener { public: SGLightConfigListener(SGLight *light) : _light(light) {} - virtual void valueChanged(SGPropertyNode *node) { + void valueChanged(SGPropertyNode* node) override + { while (strcmp(node->getName(), "light") && node->getParent()) { node = node->getParent(); } _light->configure(node); - auto *cb = _light->getUpdateCallback(); - if (cb != nullptr) { - dynamic_cast(cb)->configure(_light->getAmbient(), _light->getDiffuse(), _light->getSpecular()); + auto cb = dynamic_cast(_light->getUpdateCallback()); + if (cb) { + cb->configure(_light->getAmbient(), _light->getDiffuse(), _light->getSpecular()); } } + private: SGLight *_light; }; @@ -72,10 +103,10 @@ SGLight::SGLight(const SGLight& l, const osg::CopyOp& copyop) : _spot_cutoff(l._spot_cutoff) {} -osg::Node * -SGLight::appendLight(const SGPropertyNode *configNode, - SGPropertyNode *modelRoot, - const osgDB::Options *options) +osg::ref_ptr +SGLight::appendLight(const SGPropertyNode* configNode, + SGPropertyNode* modelRoot, + const osgDB::Options* options) { //-- create return variable osg::ref_ptr align = new osg::MatrixTransform; @@ -88,7 +119,7 @@ SGLight::appendLight(const SGPropertyNode *configNode, const std::string propName {"light"}; SGPropertyNode_ptr _pConfig = simgear::getPropertyRoot()->getNode(propPath, true) ->addChild(propName); - + copyProperties(configNode, _pConfig); //-- setup osg::NodeCallback -- diff --git a/simgear/scene/model/SGLight.hxx b/simgear/scene/model/SGLight.hxx index 25cb0622..5fd513d6 100644 --- a/simgear/scene/model/SGLight.hxx +++ b/simgear/scene/model/SGLight.hxx @@ -40,28 +40,11 @@ public: public: UpdateCallback(const SGExpressiond *expression) : _expression(expression) {} UpdateCallback() {} - - void configure(const osg::Vec4 ambient, const osg::Vec4 diffuse, const osg::Vec4 specular) { - _ambient = ambient; - _diffuse = diffuse; - _specular = specular; - } - virtual void operator()(osg::Node *node, osg::NodeVisitor *nv) { - double value = 1; - if (_expression) { - value = _expression->getValue(); - } - if (value != _prev_value) { - SGLight *light = dynamic_cast(node); - if (light) { - light->setAmbient(_ambient * value); - light->setDiffuse(_diffuse * value); - light->setSpecular(_specular * value); - } - _prev_value = value; - } - } + void configure(const osg::Vec4& ambient, const osg::Vec4& diffuse, const osg::Vec4& specular); + + void operator()(osg::Node* node, osg::NodeVisitor* nv) override; + private: SGSharedPtr _expression {nullptr}; osg::Vec4 _ambient, _diffuse, _specular; @@ -74,9 +57,9 @@ public: META_Node(simgear, SGLight); - static osg::Node *appendLight(const SGPropertyNode *configNode, - SGPropertyNode *modelRoot, - const osgDB::Options *options); + static osg::ref_ptr appendLight(const SGPropertyNode* configNode, + SGPropertyNode* modelRoot, + const osgDB::Options* options); void configure(const SGPropertyNode *config_root);