SGLight: Add support for dimming through a SGExpression ala Rembrandt

This commit is contained in:
Fernando García Liñán
2020-12-30 09:27:30 +01:00
parent 28015085ec
commit 8f2e7824ab
2 changed files with 37 additions and 0 deletions

View File

@@ -27,6 +27,8 @@
#include <simgear/math/SGMath.hxx>
#include <simgear/scene/tgdb/userdata.hxx>
#include "animation.hxx"
class SGLightDebugListener : public SGPropertyChangeListener {
public:
SGLightDebugListener(osg::Switch *sw) : _sw(sw) {}
@@ -109,6 +111,17 @@ SGLight::appendLight(const SGPropertyNode *configNode,
}
align->setMatrix(r * t);
const SGPropertyNode *dim_factor = configNode->getChild("dim-factor");
if (dim_factor) {
const SGExpressiond *expression =
read_value(dim_factor, modelRoot, "", 0, 1);
light->setUpdateCallback(
new SGLight::UpdateCallback(expression,
light->getAmbient(),
light->getDiffuse(),
light->getSpecular()));
}
osg::Shape *debug_shape = nullptr;
if (light->getType() == SGLight::Type::POINT) {
debug_shape = new osg::Sphere(osg::Vec3(0, 0, 0), light->getRange());

View File

@@ -21,6 +21,7 @@
#include <osg/Group>
#include <simgear/props/props.hxx>
#include <simgear/structure/SGExpression.hxx>
class SGLight : public osg::Node {
public:
@@ -29,6 +30,29 @@ public:
SPOT
};
class UpdateCallback : public osg::NodeCallback {
public:
UpdateCallback(const SGExpressiond *expression,
osg::Vec4 ambient, osg::Vec4 diffuse, osg::Vec4 specular) :
_expression(expression),
_ambient(ambient), _diffuse(diffuse), _specular(specular) {}
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv) {
double value = _expression->getValue();
if (value != _prev_value) {
SGLight *light = dynamic_cast<SGLight *>(node);
if (light) {
light->setAmbient(_ambient * value);
light->setDiffuse(_diffuse * value);
light->setSpecular(_specular * value);
}
}
}
private:
SGSharedPtr<SGExpressiond const> _expression;
osg::Vec4 _ambient, _diffuse, _specular;
double _prev_value = -1.0;
};
SGLight();
SGLight(const SGLight& l,