#include "Technique.hxx"
#include "Pass.hxx"
#include "TextureBuilder.hxx"
+#include "parseBlendFunc.hxx"
#include <algorithm>
#include <functional>
#include <osg/Material>
#include <osg/Math>
#include <osg/PolygonMode>
+#include <osg/PolygonOffset>
+#include <osg/Point>
#include <osg/Program>
#include <osg/Referenced>
#include <osg/RenderInfo>
#include <osgDB/ReadFile>
#include <osgDB/Registry>
-#include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
+#include <simgear/scene/util/SGReaderWriterOptions.hxx>
#include <simgear/scene/tgdb/userdata.hxx>
+#include <simgear/scene/util/OsgMath.hxx>
#include <simgear/scene/util/SGSceneFeatures.hxx>
#include <simgear/scene/util/StateAttributeFactory.hxx>
#include <simgear/structure/OSGUtils.hxx>
#include <simgear/structure/SGExpression.hxx>
-
+#include <simgear/props/vectorPropTemplates.hxx>
namespace simgear
}
Effect::Effect(const Effect& rhs, const CopyOp& copyop)
- : root(rhs.root), parametersProp(rhs.parametersProp), _cache(0),
+ : osg::Object(rhs,copyop), root(rhs.root), parametersProp(rhs.parametersProp), _cache(0),
_isRealized(rhs._isRealized)
{
typedef vector<ref_ptr<Technique> > TechniqueList;
}
void buildPass(Effect* effect, Technique* tniq, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
Pass* pass = new Pass;
tniq->passes.push_back(pass);
struct LightingBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options);
+ const SGReaderWriterOptions* options);
};
void LightingBuilder::buildAttribute(Effect* effect, Pass* pass,
const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
struct ShadeModelBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
struct CullFaceBuilder : PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp) {
pass->setMode(GL_CULL_FACE, StateAttribute::OFF);
else
SG_LOG(SG_INPUT, SG_ALERT,
- "invalid cull face property " << propVal);
- }
+ "invalid cull face property " << propVal);
+ }
};
InstallAttributeBuilder<CullFaceBuilder> installCullFace("cull-face");
struct ColorMaskBuilder : PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
Vec4 m = getColor(realProp);
mask->setMask(m.r() > 0.0, m.g() > 0.0, m.b() > 0.0, m.a() > 0.0);
pass->setAttributeAndModes(mask);
- }
+ }
};
InstallAttributeBuilder<ColorMaskBuilder> installColorMask("color-mask");
struct HintBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
StateSet::RenderingHint renderingHint = StateSet::DEFAULT_BIN;
findAttr(renderingHints, realProp, renderingHint);
pass->setRenderingHint(renderingHint);
- }
+ }
};
InstallAttributeBuilder<HintBuilder> installHint("rendering-hint");
struct RenderBinBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
struct MaterialBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options);
+ const SGReaderWriterOptions* options);
};
EffectNameValue<Material::ColorMode> colorModeInit[] =
void MaterialBuilder::buildAttribute(Effect* effect, Pass* pass,
const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
if ((color = getEffectPropertyChild(effect, prop, "emissive")))
mat->setEmission(Material::FRONT_AND_BACK, getColor(color));
if ((color = getEffectPropertyChild(effect, prop, "emissive-front")))
- mat->setEmission(Material::FRONT, getColor(color));
+ mat->setEmission(Material::FRONT, getColor(color));
if ((color = getEffectPropertyChild(effect, prop, "emissive-back")))
- mat->setEmission(Material::BACK, getColor(color));
+ mat->setEmission(Material::BACK, getColor(color));
const SGPropertyNode* shininess = 0;
mat->setShininess(Material::FRONT_AND_BACK, 0.0f);
if ((shininess = getEffectPropertyChild(effect, prop, "shininess")))
struct BlendBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
pass->setMode(GL_BLEND, StateAttribute::OFF);
return;
}
- const SGPropertyNode* psource
- = getEffectPropertyChild(effect, prop, "source");
- const SGPropertyNode* pdestination
- = getEffectPropertyChild(effect, prop, "destination");
- const SGPropertyNode* psourceRGB
- = getEffectPropertyChild(effect, prop, "source-rgb");
- const SGPropertyNode* psourceAlpha
- = getEffectPropertyChild(effect, prop, "source-alpha");
- const SGPropertyNode* pdestRGB
- = getEffectPropertyChild(effect, prop, "destination-rgb");
- const SGPropertyNode* pdestAlpha
- = getEffectPropertyChild(effect, prop, "destination-alpha");
- BlendFunc::BlendFuncMode sourceMode = BlendFunc::ONE;
- BlendFunc::BlendFuncMode destMode = BlendFunc::ZERO;
- if (psource)
- findAttr(blendFuncModes, psource, sourceMode);
- if (pdestination)
- findAttr(blendFuncModes, pdestination, destMode);
- if (psource && pdestination
- && !(psourceRGB || psourceAlpha || pdestRGB || pdestAlpha)
- && sourceMode == BlendFunc::SRC_ALPHA
- && destMode == BlendFunc::ONE_MINUS_SRC_ALPHA) {
- pass->setAttributeAndModes(StateAttributeFactory::instance()
- ->getStandardBlendFunc());
- return;
- }
- BlendFunc* blendFunc = new BlendFunc;
- if (psource)
- blendFunc->setSource(sourceMode);
- if (pdestination)
- blendFunc->setDestination(destMode);
- if (psourceRGB) {
- BlendFunc::BlendFuncMode sourceRGBMode;
- findAttr(blendFuncModes, psourceRGB, sourceRGBMode);
- blendFunc->setSourceRGB(sourceRGBMode);
- }
- if (pdestRGB) {
- BlendFunc::BlendFuncMode destRGBMode;
- findAttr(blendFuncModes, pdestRGB, destRGBMode);
- blendFunc->setDestinationRGB(destRGBMode);
- }
- if (psourceAlpha) {
- BlendFunc::BlendFuncMode sourceAlphaMode;
- findAttr(blendFuncModes, psourceAlpha, sourceAlphaMode);
- blendFunc->setSourceAlpha(sourceAlphaMode);
- }
- if (pdestAlpha) {
- BlendFunc::BlendFuncMode destAlphaMode;
- findAttr(blendFuncModes, pdestAlpha, destAlphaMode);
- blendFunc->setDestinationAlpha(destAlphaMode);
- }
- pass->setAttributeAndModes(blendFunc);
+
+ parseBlendFunc(
+ pass,
+ getEffectPropertyChild(effect, prop, "source"),
+ getEffectPropertyChild(effect, prop, "destination"),
+ getEffectPropertyChild(effect, prop, "source-rgb"),
+ getEffectPropertyChild(effect, prop, "destination-rgb"),
+ getEffectPropertyChild(effect, prop, "source-alpha"),
+ getEffectPropertyChild(effect, prop, "destination-alpha")
+ );
}
};
struct StencilBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
findAttr(stencilFunction, pfunction, func);
if (pvalue)
ref = pvalue->getIntValue();
- if (pmask)
+ if (pmask)
mask = pmask->getIntValue();
if (psfail)
InstallAttributeBuilder<StencilBuilder> installStencil("stencil");
+struct AlphaToCoverageBuilder : public PassAttributeBuilder
+{
+ void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
+ const SGReaderWriterOptions* options);
+};
+
+#ifndef GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
+#define GL_SAMPLE_ALPHA_TO_COVERAGE_ARB 0x809E
+#endif
+
+void AlphaToCoverageBuilder::buildAttribute(Effect* effect, Pass* pass,
+ const SGPropertyNode* prop,
+ const SGReaderWriterOptions* options)
+{
+ const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
+ if (!realProp)
+ return;
+ pass->setMode(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB, (realProp->getValue<bool>() ?
+ StateAttribute::ON : StateAttribute::OFF));
+}
+
+InstallAttributeBuilder<AlphaToCoverageBuilder> installAlphaToCoverage("alpha-to-coverage");
EffectNameValue<AlphaFunc::ComparisonFunction> alphaComparisonInit[] =
{
struct AlphaTestBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
"comparison");
const SGPropertyNode* pRef = getEffectPropertyChild(effect, prop,
"reference");
+
AlphaFunc::ComparisonFunction func = AlphaFunc::ALWAYS;
float refValue = 1.0f;
if (pComp)
InstallAttributeBuilder<TextureUnitBuilder> textureUnitBuilder("texture-unit");
// Shader key, used both for shaders with relative and absolute names
-typedef pair<string, Shader::Type> ShaderKey;
+typedef pair<string, int> ShaderKey;
-inline ShaderKey makeShaderKey(SGPropertyNode_ptr& ptr, Shader::Type shaderType)
+inline ShaderKey makeShaderKey(SGPropertyNode_ptr& ptr, int shaderType)
{
return ShaderKey(ptr->getStringValue(), shaderType);
}
{
for(ShaderMap::iterator sitr = shaderMap.begin(); sitr != shaderMap.end(); ++sitr)
{
- Shader *shader = sitr->second.get();
+ Shader *shader = sitr->second.get();
string fileName = SGModelLib::findDataFile(sitr->first.first);
if (!fileName.empty()) {
- shader->loadShaderSourceFromFile(fileName);
+ shader->loadShaderSourceFromFile(fileName);
}
}
}
struct ShaderProgramBuilder : PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options);
+ const SGReaderWriterOptions* options);
};
void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions*
+ const SGReaderWriterOptions*
options)
{
using namespace boost;
BOOST_FOREACH(const ShaderKey& shaderKey, prgKey.shaders)
{
const string& shaderName = shaderKey.first;
- Shader::Type stype = shaderKey.second;
+ Shader::Type stype = (Shader::Type)shaderKey.second;
string fileName = SGModelLib::findDataFile(shaderName, options);
if (fileName.empty())
throw BuilderException(string("couldn't find shader ") +
BOOST_FOREACH(const ShaderKey& skey, resolvedKey.shaders)
{
const string& fileName = skey.first;
- Shader::Type stype = skey.second;
+ Shader::Type stype = (Shader::Type)skey.second;
ShaderMap::iterator sitr = shaderMap.find(skey);
if (sitr != shaderMap.end()) {
program->addShader(sitr->second.get());
} else {
ref_ptr<Shader> shader = new Shader(stype);
+ shader->setName(fileName);
if (shader->loadShaderSourceFromFile(fileName)) {
program->addShader(shader.get());
shaderMap.insert(ShaderMap::value_type(skey, shader));
struct UniformBuilder :public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
if (!texture0.valid()) {
texture0 = new Uniform(Uniform::SAMPLER_2D, "texture");
return;
const SGPropertyNode* nameProp = prop->getChild("name");
const SGPropertyNode* typeProp = prop->getChild("type");
+ const SGPropertyNode* positionedProp = prop->getChild("positioned");
const SGPropertyNode* valProp = prop->getChild("value");
string name;
Uniform::Type uniformType = Uniform::FLOAT;
uniform->setName(name);
uniform->setType(uniformType);
switch (uniformType) {
+ case Uniform::BOOL:
+ initFromParameters(effect, valProp, uniform.get(),
+ static_cast<bool (Uniform::*)(bool)>(&Uniform::set),
+ options);
+ break;
case Uniform::FLOAT:
initFromParameters(effect, valProp, uniform.get(),
static_cast<bool (Uniform::*)(float)>(&Uniform::set),
// optimize common uniforms
if (uniformType == Uniform::SAMPLER_2D || uniformType == Uniform::INT)
{
- int val;
- uniform->get(val);
+ int val = 0;
+ uniform->get(val); // 'val' remains unchanged in case of error (Uniform is a non-scalar)
if (uniformType == Uniform::SAMPLER_2D && val == 0
&& name == "texture") {
uniform = texture0;
}
}
pass->addUniform(uniform.get());
+ if (positionedProp && positionedProp->getBoolValue() && uniformType == Uniform::FLOAT_VEC4) {
+ osg::Vec4 offset;
+ uniform->get(offset);
+ pass->addPositionedUniform( name, offset );
+ }
}
};
struct NameBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
// name can't use <use>
string name = prop->getStringValue();
struct PolygonModeBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
InstallAttributeBuilder<PolygonModeBuilder> installPolygonMode("polygon-mode");
+struct PolygonOffsetBuilder : public PassAttributeBuilder
+{
+ void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
+ const SGReaderWriterOptions* options)
+ {
+ if (!isAttributeActive(effect, prop))
+ return;
+
+ const SGPropertyNode* factor
+ = getEffectPropertyChild(effect, prop, "factor");
+ const SGPropertyNode* units
+ = getEffectPropertyChild(effect, prop, "units");
+
+ ref_ptr<PolygonOffset> polyoffset = new PolygonOffset;
+
+ polyoffset->setFactor(factor->getFloatValue());
+ polyoffset->setUnits(units->getFloatValue());
+
+ SG_LOG(SG_INPUT, SG_BULK,
+ "Set PolygonOffset to " << polyoffset->getFactor() << polyoffset->getUnits() );
+
+ pass->setAttributeAndModes(polyoffset.get(),
+ StateAttribute::OVERRIDE|StateAttribute::ON);
+ }
+};
+
+InstallAttributeBuilder<PolygonOffsetBuilder> installPolygonOffset("polygon-offset");
+
struct VertexProgramTwoSideBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
struct VertexProgramPointSizeBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
InstallAttributeBuilder<VertexProgramPointSizeBuilder>
installPointSize("vertex-program-point-size");
+struct PointBuilder : public PassAttributeBuilder
+{
+ void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
+ const SGReaderWriterOptions* options)
+ {
+ float minsize = 1.0;
+ float maxsize = 1.0;
+ float size = 1.0;
+ osg::Vec3f attenuation = osg::Vec3f(1.0, 1.0, 1.0);
+
+ const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
+ if (!realProp)
+ return;
+
+ const SGPropertyNode* pminsize
+ = getEffectPropertyChild(effect, prop, "min-size");
+ const SGPropertyNode* pmaxsize
+ = getEffectPropertyChild(effect, prop, "max-size");
+ const SGPropertyNode* psize
+ = getEffectPropertyChild(effect, prop, "size");
+ const SGPropertyNode* pattenuation
+ = getEffectPropertyChild(effect, prop, "attenuation");
+
+ if (pminsize)
+ minsize = pminsize->getFloatValue();
+ if (pmaxsize)
+ maxsize = pmaxsize->getFloatValue();
+ if (psize)
+ size = psize->getFloatValue();
+ if (pattenuation)
+ attenuation = osg::Vec3f(pattenuation->getChild("x")->getFloatValue(),
+ pattenuation->getChild("y")->getFloatValue(),
+ pattenuation->getChild("z")->getFloatValue());
+
+ osg::Point* point = new osg::Point;
+ point->setMinSize(minsize);
+ point->setMaxSize(maxsize);
+ point->setSize(size);
+ point->setDistanceAttenuation(attenuation);
+ pass->setAttributeAndModes(point);
+ }
+};
+
+InstallAttributeBuilder<PointBuilder>
+installPoint("point");
+
EffectNameValue<Depth::Function> depthFunctionInit[] =
{
{"never", Depth::NEVER},
struct DepthBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
const SGPropertyNode* pfar
= getEffectPropertyChild(effect, prop, "far");
if (pfar)
- depth->setZFar(pnear->getValue<double>());
+ depth->setZFar(pfar->getValue<double>());
const SGPropertyNode* pmask
= getEffectPropertyChild(effect, prop, "write-mask");
if (pmask)
depth->setWriteMask(pmask->getValue<bool>());
- pass->setAttribute(depth.get());
+ const SGPropertyNode* penabled
+ = getEffectPropertyChild(effect, prop, "enabled");
+ bool enabled = ( penabled == 0 || penabled->getBoolValue() );
+ pass->setAttributeAndModes(depth.get(), enabled ? osg::StateAttribute::ON : osg::StateAttribute::OFF);
}
};
InstallAttributeBuilder<DepthBuilder> installDepth("depth");
void buildTechnique(Effect* effect, const SGPropertyNode* prop,
- const SGReaderWriterXMLOptions* options)
+ const SGReaderWriterOptions* options)
{
Technique* tniq = new Technique;
effect->techniques.push_back(tniq);
// Walk the techniques property tree, building techniques and
// passes.
-bool Effect::realizeTechniques(const SGReaderWriterXMLOptions* options)
+bool Effect::realizeTechniques(const SGReaderWriterOptions* options)
{
if (_isRealized)
return true;
}
}
+void Effect::UpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
+{
+ EffectGeode* eg = dynamic_cast<EffectGeode*>(node);
+ if (!eg)
+ return;
+ Effect* effect = eg->getEffect();
+ if (!effect)
+ return;
+
+ for (vector<SGSharedPtr<Updater> >::iterator itr = effect->_extraData.begin(),
+ end = effect->_extraData.end();
+ itr != end;
+ ++itr) {
+ PropertyPoller * poller
+ = dynamic_cast<PropertyPoller*>(itr->ptr());
+ if (poller)
+ poller->pollProperties(effect);
+ }
+
+ traverse(node, nv);
+}
+
bool Effect::Key::EqualTo::operator()(const Effect::Key& lhs,
const Effect::Key& rhs) const
{
class PropertyExpression : public SGExpression<T>
{
public:
- PropertyExpression(SGPropertyNode* pnode) : _pnode(pnode) {}
-
+ PropertyExpression(SGPropertyNode* pnode) : _pnode(pnode), _listener(NULL) {}
+
+ ~PropertyExpression()
+ {
+ delete _listener;
+ }
+
void eval(T& value, const expression::Binding*) const
{
value = _pnode->getValue<T>();
}
+
+ void setListener(SGPropertyChangeListener* l)
+ {
+ _listener = l;
+ }
protected:
SGPropertyNode_ptr _pnode;
+ SGPropertyChangeListener* _listener;
};
class EffectPropertyListener : public SGPropertyChangeListener
{
public:
EffectPropertyListener(Technique* tniq) : _tniq(tniq) {}
-
+
void valueChanged(SGPropertyNode* node)
{
- _tniq->refreshValidity();
+ if (_tniq.valid())
+ _tniq->refreshValidity();
}
+
+ virtual ~EffectPropertyListener() { }
+
protected:
- osg::ref_ptr<Technique> _tniq;
+ osg::observer_ptr<Technique> _tniq;
};
template<typename T>
PropertyExpression<T>* pexp = new PropertyExpression<T>(pnode);
TechniquePredParser* predParser
= dynamic_cast<TechniquePredParser*>(parser);
- if (predParser)
- pnode->addChangeListener(new EffectPropertyListener(predParser
- ->getTechnique()));
+ if (predParser) {
+ EffectPropertyListener* l = new EffectPropertyListener(predParser
+ ->getTechnique());
+ pexp->setListener(l);
+ pnode->addChangeListener(l);
+ }
return pexp;
}