]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/Effect.cxx
Compile even if OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION is not set.
[simgear.git] / simgear / scene / material / Effect.cxx
index 3ad9316216fd0904e039f9c8caeb79f76092ab2c..33ceacdae2c94b4eba791b02c226f98ce1564ac3 100644 (file)
@@ -98,6 +98,8 @@ Effect::Effect(const Effect& rhs, const CopyOp& copyop)
          itr != end;
          ++itr)
         techniques.push_back(static_cast<Technique*>(copyop(itr->get())));
+
+    generator = rhs.generator;
 }
 
 // Assume that the last technique is always valid.
@@ -110,6 +112,13 @@ StateSet* Effect::getDefaultStateSet()
     return pass;
 }
 
+int Effect::getGenerator(Effect::Generator what) const
+{
+    std::map<Generator,int>::const_iterator it = generator.find(what);
+    if(it == generator.end()) return -1;
+    else return it->second;
+}
+
 // There should always be a valid technique in an effect.
 
 Technique* Effect::chooseTechnique(RenderInfo* info)
@@ -160,6 +169,10 @@ void buildPass(Effect* effect, Technique* tniq, const SGPropertyNode* prop,
     }
 }
 
+// Default names for vector property components
+const char* vec3Names[] = {"x", "y", "z"};
+const char* vec4Names[] = {"x", "y", "z", "w"};
+
 osg::Vec4f getColor(const SGPropertyNode* prop)
 {
     if (prop->nChildren() == 0) {
@@ -719,6 +732,28 @@ struct ShaderProgramBuilder : PassAttributeBuilder
                         const SGReaderWriterXMLOptions* options);
 };
 
+
+EffectNameValue<GLint> geometryInputTypeInit[] =
+{
+    {"points", GL_POINTS},
+    {"lines", GL_LINES},
+    {"lines-adjacency", GL_LINES_ADJACENCY_EXT},
+    {"triangles", GL_TRIANGLES},
+    {"triangles-adjacency", GL_TRIANGLES_ADJACENCY_EXT},
+};
+EffectPropertyMap<GLint>
+geometryInputType(geometryInputTypeInit);
+
+
+EffectNameValue<GLint> geometryOutputTypeInit[] =
+{
+    {"points", GL_POINTS},
+    {"line-strip", GL_LINE_STRIP},
+    {"triangle-strip", GL_TRIANGLE_STRIP}
+};
+EffectPropertyMap<GLint>
+geometryOutputType(geometryOutputTypeInit);
+
 void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
                                           const SGPropertyNode* prop,
                                           const SGReaderWriterXMLOptions*
@@ -728,6 +763,7 @@ void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
     if (!isAttributeActive(effect, prop))
         return;
     PropertyList pVertShaders = prop->getChildren("vertex-shader");
+    PropertyList pGeomShaders = prop->getChildren("geometry-shader");
     PropertyList pFragShaders = prop->getChildren("fragment-shader");
     PropertyList pAttributes = prop->getChildren("attribute");
     ProgramKey prgKey;
@@ -737,6 +773,12 @@ void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
          ++itr)
         prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(),
                                            Shader::VERTEX));
+    for (PropertyList::iterator itr = pGeomShaders.begin(),
+             e = pGeomShaders.end();
+         itr != e;
+         ++itr)
+        prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(),
+                                           Shader::GEOMETRY));
     for (PropertyList::iterator itr = pFragShaders.begin(),
              e = pFragShaders.end();
          itr != e;
@@ -768,7 +810,7 @@ void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
         // Add vertex shaders, then fragment shaders
         PropertyList& pvec = pVertShaders;
         Shader::Type stype = Shader::VERTEX;
-        for (int i = 0; i < 2; ++i) {
+        for (int i = 0; i < 3; ++i) {
             for (PropertyList::iterator nameItr = pvec.begin(), e = pvec.end();
                  nameItr != e;
                  ++nameItr) {
@@ -789,13 +831,35 @@ void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
                     }
                 }
             }
-            pvec = pFragShaders;
-            stype = Shader::FRAGMENT;
+            if (i == 0) {
+                pvec = pGeomShaders;
+                stype = Shader::GEOMETRY;
+            } else {
+                pvec = pFragShaders;
+                stype = Shader::FRAGMENT;
+            }
         }
         BOOST_FOREACH(const ProgramKey::AttribKey& key, prgKey.attributes) {
             program->addBindAttribLocation(key.first, key.second);
         }
-       programMap.insert(ProgramMap::value_type(prgKey, program));
+        const SGPropertyNode* pGeometryVerticesOut = getEffectPropertyChild(effect, prop, "geometry-vertices-out");
+       if ( pGeometryVerticesOut ) {
+           program->setParameter( GL_GEOMETRY_VERTICES_OUT_EXT, pGeometryVerticesOut->getIntValue() );
+       }
+        const SGPropertyNode* pGeometryInputType = getEffectPropertyChild(effect, prop, "geometry-input-type");
+       if ( pGeometryInputType ) {
+           GLint type;
+           findAttr( geometryInputType, pGeometryInputType->getStringValue(), type );
+           program->setParameter( GL_GEOMETRY_INPUT_TYPE_EXT, type );
+       }
+        const SGPropertyNode* pGeometryOutputType = getEffectPropertyChild(effect, prop, "geometry-output-type");
+       if ( pGeometryOutputType ) {
+           GLint type;
+           findAttr( geometryOutputType, pGeometryOutputType->getStringValue(), type );
+           program->setParameter( GL_GEOMETRY_OUTPUT_TYPE_EXT, type );
+       }
+
+        programMap.insert(ProgramMap::value_type(prgKey, program));
     }
     pass->setAttributeAndModes(program);
 }
@@ -808,8 +872,11 @@ EffectNameValue<Uniform::Type> uniformTypesInit[] =
     {"float-vec3", Uniform::FLOAT_VEC3},
     {"float-vec4", Uniform::FLOAT_VEC4},
     {"sampler-1d", Uniform::SAMPLER_1D},
+    {"sampler-1d-shadow", Uniform::SAMPLER_1D_SHADOW},
     {"sampler-2d", Uniform::SAMPLER_2D},
-    {"sampler-3d", Uniform::SAMPLER_3D}
+    {"sampler-2d-shadow", Uniform::SAMPLER_2D_SHADOW},
+    {"sampler-3d", Uniform::SAMPLER_3D},
+    {"sampler-cube", Uniform::SAMPLER_CUBE}
 };
 EffectPropertyMap<Uniform::Type> uniformTypes(uniformTypesInit);
 
@@ -822,8 +889,7 @@ struct UniformBuilder :public PassAttributeBuilder
             return;
         const SGPropertyNode* nameProp = prop->getChild("name");
         const SGPropertyNode* typeProp = prop->getChild("type");
-        const SGPropertyNode* valProp
-            = getEffectPropertyChild(effect, prop, "value");
+        const SGPropertyNode* valProp = prop->getChild("value");
         string name;
         Uniform::Type uniformType = Uniform::FLOAT;
         if (nameProp) {
@@ -862,18 +928,29 @@ struct UniformBuilder :public PassAttributeBuilder
         uniform->setType(uniformType);
         switch (uniformType) {
         case Uniform::FLOAT:
-            uniform->set(valProp->getValue<float>());
+            initFromParameters(effect, valProp, uniform.get(),
+                               static_cast<bool (Uniform::*)(float)>(&Uniform::set),
+                               options);
             break;
         case Uniform::FLOAT_VEC3:
-            uniform->set(toOsg(valProp->getValue<SGVec3d>()));
+            initFromParameters(effect, valProp, uniform.get(),
+                               static_cast<bool (Uniform::*)(const Vec3&)>(&Uniform::set),
+                               vec3Names, options);
             break;
         case Uniform::FLOAT_VEC4:
-            uniform->set(toOsg(valProp->getValue<SGVec4d>()));
+            initFromParameters(effect, valProp, uniform.get(),
+                               static_cast<bool (Uniform::*)(const Vec4&)>(&Uniform::set),
+                               vec4Names, options);
             break;
         case Uniform::SAMPLER_1D:
         case Uniform::SAMPLER_2D:
         case Uniform::SAMPLER_3D:
-            uniform->set(valProp->getValue<int>());
+        case Uniform::SAMPLER_1D_SHADOW:
+        case Uniform::SAMPLER_2D_SHADOW:
+        case Uniform::SAMPLER_CUBE:
+            initFromParameters(effect, valProp, uniform.get(),
+                               static_cast<bool (Uniform::*)(int)>(&Uniform::set),
+                               options);
             break;
         default: // avoid compiler warning
             break;
@@ -1202,14 +1279,15 @@ osgDB::RegisterDotOsgWrapperProxy effectProxy
 }
 
 // Property expressions for technique predicates
-class PropertyExpression : public SGExpression<bool>
+template<typename T>
+class PropertyExpression : public SGExpression<T>
 {
 public:
     PropertyExpression(SGPropertyNode* pnode) : _pnode(pnode) {}
     
-    void eval(bool& value, const expression::Binding*) const
+    void eval(T& value, const expression::Binding*) const
     {
-        value = _pnode->getValue<bool>();
+        value = _pnode->getValue<T>();
     }
 protected:
     SGPropertyNode_ptr _pnode;
@@ -1228,12 +1306,13 @@ protected:
     osg::ref_ptr<Technique> _tniq;
 };
 
+template<typename T>
 Expression* propertyExpressionParser(const SGPropertyNode* exp,
                                      expression::Parser* parser)
 {
     SGPropertyNode_ptr pnode = getPropertyRoot()->getNode(exp->getStringValue(),
                                                           true);
-    PropertyExpression* pexp = new PropertyExpression(pnode);
+    PropertyExpression<T>* pexp = new PropertyExpression<T>(pnode);
     TechniquePredParser* predParser
         = dynamic_cast<TechniquePredParser*>(parser);
     if (predParser)
@@ -1243,6 +1322,9 @@ Expression* propertyExpressionParser(const SGPropertyNode* exp,
 }
 
 expression::ExpParserRegistrar propertyRegistrar("property",
-                                                 propertyExpressionParser);
+                                                 propertyExpressionParser<bool>);
+
+expression::ExpParserRegistrar propvalueRegistrar("float-property",
+                                                 propertyExpressionParser<float>);
 
 }