]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/Effect.cxx
Ugly hack to please Boost 1.51.0
[simgear.git] / simgear / scene / material / Effect.cxx
index 787a9ce2eb6834a9a9eda57b84d1a03b4207a326..c934815760ced8359c0c3678fde2e9b865544deb 100644 (file)
@@ -46,6 +46,7 @@
 #include <osg/Material>
 #include <osg/Math>
 #include <osg/PolygonMode>
+#include <osg/PolygonOffset>
 #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
@@ -89,7 +91,7 @@ Effect::Effect()
 }
 
 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;
@@ -153,7 +155,7 @@ Effect::~Effect()
 }
 
 void buildPass(Effect* effect, Technique* tniq, const SGPropertyNode* prop,
-               const SGReaderWriterXMLOptions* options)
+               const SGReaderWriterOptions* options)
 {
     Pass* pass = new Pass;
     tniq->passes.push_back(pass);
@@ -202,12 +204,12 @@ osg::Vec4f getColor(const SGPropertyNode* prop)
 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)
@@ -221,7 +223,7 @@ InstallAttributeBuilder<LightingBuilder> installLighting("lighting");
 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)
@@ -243,7 +245,7 @@ InstallAttributeBuilder<ShadeModelBuilder> installShadeModel("shade-model");
 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) {
@@ -271,7 +273,7 @@ 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)
@@ -279,7 +281,7 @@ struct ColorMaskBuilder : PassAttributeBuilder
 
         ColorMask *mask = new ColorMask;
         Vec4 m = getColor(realProp);
-        mask->setMask(m.r(), m.g(), m.b(), m.a());
+        mask->setMask(m.r() > 0.0, m.g() > 0.0, m.b() > 0.0, m.a() > 0.0);
         pass->setAttributeAndModes(mask);
     }    
 };
@@ -298,7 +300,7 @@ EffectPropertyMap<StateSet::RenderingHint> renderingHints(renderingHintInit);
 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)
@@ -314,7 +316,7 @@ 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;
@@ -341,7 +343,7 @@ InstallAttributeBuilder<RenderBinBuilder> installRenderBin("render-bin");
 struct MaterialBuilder : public PassAttributeBuilder
 {
     void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
-                        const SGReaderWriterXMLOptions* options);
+                        const SGReaderWriterOptions* options);
 };
 
 EffectNameValue<Material::ColorMode> colorModeInit[] =
@@ -357,7 +359,7 @@ EffectPropertyMap<Material::ColorMode> colorModes(colorModeInit);
 
 void MaterialBuilder::buildAttribute(Effect* effect, Pass* pass,
                                      const SGPropertyNode* prop,
-                                     const SGReaderWriterXMLOptions* options)
+                                     const SGReaderWriterOptions* options)
 {
     if (!isAttributeActive(effect, prop))
         return;
@@ -427,7 +429,7 @@ EffectPropertyMap<BlendFunc::BlendFuncMode> blendFuncModes(blendFuncModesInit);
 struct BlendBuilder : public PassAttributeBuilder
 {
     void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
-                        const SGReaderWriterXMLOptions* options)
+                        const SGReaderWriterOptions* options)
     {
         if (!isAttributeActive(effect, prop))
             return;
@@ -540,7 +542,7 @@ EffectPropertyMap<Stencil::Operation> stencilOperation(stencilOperationInit);
 struct StencilBuilder : public PassAttributeBuilder
 {
     void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
-                        const SGReaderWriterXMLOptions* options)
+                        const SGReaderWriterOptions* options)
     {
         if (!isAttributeActive(effect, prop))
             return;
@@ -618,7 +620,7 @@ alphaComparison(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;
@@ -669,9 +671,9 @@ InstallAttributeBuilder<AlphaTestBuilder> installAlphaTest("alpha-test");
 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);
 }
@@ -724,10 +726,10 @@ void reload_shaders()
 {
     for(ShaderMap::iterator sitr = shaderMap.begin(); sitr != shaderMap.end(); ++sitr)
     {
-       Shader *shader = sitr->second.get();
-        string fileName = osgDB::findDataFile(sitr->first.first);
+        Shader *shader = sitr->second.get();
+        string fileName = SGModelLib::findDataFile(sitr->first.first);
         if (!fileName.empty()) {
-           shader->loadShaderSourceFromFile(fileName);
+            shader->loadShaderSourceFromFile(fileName);
         }
     }
 }
@@ -735,7 +737,7 @@ void reload_shaders()
 struct ShaderProgramBuilder : PassAttributeBuilder
 {
     void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
-                        const SGReaderWriterXMLOptions* options);
+                        const SGReaderWriterOptions* options);
 };
 
 
@@ -762,7 +764,7 @@ geometryOutputType(geometryOutputTypeInit);
 
 void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
                                           const SGPropertyNode* prop,
-                                          const SGReaderWriterXMLOptions*
+                                          const SGReaderWriterOptions*
                                           options)
 {
     using namespace boost;
@@ -775,11 +777,11 @@ void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
     ProgramKey prgKey;
     std::back_insert_iterator<vector<ShaderKey> > inserter(prgKey.shaders);
     transform(pVertShaders.begin(), pVertShaders.end(), inserter,
-              bind(makeShaderKey, _1, Shader::VERTEX));
+              boost::bind(makeShaderKey, _1, Shader::VERTEX));
     transform(pGeomShaders.begin(), pGeomShaders.end(), inserter,
-              bind(makeShaderKey, _1, Shader::GEOMETRY));
+              boost::bind(makeShaderKey, _1, Shader::GEOMETRY));
     transform(pFragShaders.begin(), pFragShaders.end(), inserter,
-              bind(makeShaderKey, _1, Shader::FRAGMENT));
+              boost::bind(makeShaderKey, _1, Shader::FRAGMENT));
     for (PropertyList::iterator itr = pAttributes.begin(),
              e = pAttributes.end();
          itr != e;
@@ -813,8 +815,8 @@ void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
     BOOST_FOREACH(const ShaderKey& shaderKey, prgKey.shaders)
     {
         const string& shaderName = shaderKey.first;
-        Shader::Type stype = shaderKey.second;
-        string fileName = osgDB::findDataFile(shaderName, options);
+        Shader::Type stype = (Shader::Type)shaderKey.second;
+        string fileName = SGModelLib::findDataFile(shaderName, options);
         if (fileName.empty())
             throw BuilderException(string("couldn't find shader ") +
                                    shaderName);
@@ -831,12 +833,13 @@ void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
     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));
@@ -889,15 +892,32 @@ EffectNameValue<Uniform::Type> uniformTypesInit[] =
 };
 EffectPropertyMap<Uniform::Type> uniformTypes(uniformTypesInit);
 
+// Optimization hack for common uniforms.
+// XXX protect these with a mutex?
+
+ref_ptr<Uniform> texture0;
+ref_ptr<Uniform> colorMode[3];
+
 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");
+            texture0->set(0);
+            texture0->setDataVariance(Object::STATIC);
+            for (int i = 0; i < 3; ++i) {
+                colorMode[i] = new Uniform(Uniform::INT, "colorMode");
+                colorMode[i]->set(i);
+                colorMode[i]->setDataVariance(Object::STATIC);
+            }
+        }
         if (!isAttributeActive(effect, prop))
             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;
@@ -942,6 +962,11 @@ struct UniformBuilder :public PassAttributeBuilder
         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),
@@ -957,6 +982,7 @@ struct UniformBuilder :public PassAttributeBuilder
                                static_cast<bool (Uniform::*)(const Vec4&)>(&Uniform::set),
                                vec4Names, options);
             break;
+        case Uniform::INT:
         case Uniform::SAMPLER_1D:
         case Uniform::SAMPLER_2D:
         case Uniform::SAMPLER_3D:
@@ -970,7 +996,25 @@ struct UniformBuilder :public PassAttributeBuilder
         default: // avoid compiler warning
             break;
         }
+        // optimize common uniforms
+        if (uniformType == Uniform::SAMPLER_2D || uniformType == Uniform::INT)
+        {
+            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;
+            } else if (uniformType == Uniform::INT && val >= 0 && val < 3
+                       && name == "colorMode") {
+                uniform = colorMode[val];
+            }
+        }
         pass->addUniform(uniform.get());
+        if (positionedProp && positionedProp->getBoolValue() && uniformType == Uniform::FLOAT_VEC4) {
+            osg::Vec4 offset;
+            uniform->get(offset);
+            pass->addPositionedUniform( name, offset );
+        }
     }
 };
 
@@ -982,7 +1026,7 @@ InstallAttributeBuilder<UniformBuilder> installUniform("uniform");
 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();
@@ -1004,7 +1048,7 @@ EffectPropertyMap<PolygonMode::Mode> polygonModeModes(polygonModeModesInit);
 struct PolygonModeBuilder : public PassAttributeBuilder
 {
     void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
-                        const SGReaderWriterXMLOptions* options)
+                        const SGReaderWriterOptions* options)
     {
         if (!isAttributeActive(effect, prop))
             return;
@@ -1029,10 +1073,38 @@ struct PolygonModeBuilder : public PassAttributeBuilder
 
 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)
@@ -1049,7 +1121,7 @@ installTwoSide("vertex-program-two-side");
 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)
@@ -1079,7 +1151,7 @@ EffectPropertyMap<Depth::Function> depthFunction(depthFunctionInit);
 struct DepthBuilder : public PassAttributeBuilder
 {
     void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
-                        const SGReaderWriterXMLOptions* options)
+                        const SGReaderWriterOptions* options)
     {
         if (!isAttributeActive(effect, prop))
             return;
@@ -1098,19 +1170,22 @@ struct DepthBuilder : public PassAttributeBuilder
         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);
@@ -1219,7 +1294,7 @@ bool makeParametersFromStateSet(SGPropertyNode* effectRoot, const StateSet* ss)
 
 // 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;
@@ -1318,10 +1393,11 @@ public:
     
     void valueChanged(SGPropertyNode* node)
     {
-        _tniq->refreshValidity();
+        if (_tniq.valid())
+            _tniq->refreshValidity();
     }
 protected:
-    osg::ref_ptr<Technique> _tniq;
+    osg::observer_ptr<Technique> _tniq;
 };
 
 template<typename T>