]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/makeEffect.cxx
Random buildings - initial commit.
[simgear.git] / simgear / scene / material / makeEffect.cxx
index 1a8efcdbb058912c2e95f3b2bc16073ee8403dcf..06bf47b229b1d8d118816f85a3c60a2f63e41e53 100644 (file)
@@ -31,6 +31,7 @@
 #include <osgDB/Registry>
 
 #include <simgear/debug/logstream.hxx>
+#include <simgear/scene/util/SGReaderWriterOptions.hxx>
 #include <simgear/props/props_io.hxx>
 #include <simgear/scene/util/SGSceneFeatures.hxx>
 #include <simgear/scene/util/SplicingVisitor.hxx>
@@ -116,18 +117,19 @@ void mergePropertyTrees(SGPropertyNode* resultNode,
 
 Effect* makeEffect(const string& name,
                    bool realizeTechniques,
-                   const osgDB::ReaderWriter::Options* options)
+                   const SGReaderWriterOptions* options)
 {
     {
         OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(effectMutex);
         EffectMap::iterator itr = effectMap.find(name);
-        if (itr != effectMap.end())
+        if ((itr != effectMap.end())&&
+            itr->second.valid())
             return itr->second.get();
     }
     string effectFileName(name);
     effectFileName += ".eff";
     string absFileName
-        = osgDB::findDataFile(effectFileName, options);
+        = SGModelLib::findDataFile(effectFileName, options);
     if (absFileName.empty()) {
         SG_LOG(SG_INPUT, SG_ALERT, "can't find \"" << effectFileName << "\"");
         return 0;
@@ -159,7 +161,7 @@ Effect* makeEffect(const string& name,
 
 Effect* makeEffect(SGPropertyNode* prop,
                    bool realizeTechniques,
-                   const osgDB::ReaderWriter::Options* options)
+                   const SGReaderWriterOptions* options)
 {
     // Give default names to techniques and passes
     vector<SGPropertyNode_ptr> techniques = prop->getChildren("technique");
@@ -204,8 +206,11 @@ Effect* makeEffect(SGPropertyNode* prop,
                     lock(effectMutex);
                 cache = parent->getCache();
                 itr = cache->find(key);
-                if (itr != cache->end()) 
-                    effect = itr->second.get();
+                if ((itr != cache->end())&&
+                    itr->second.lock(effect))
+                {
+                    effect->generator = parent->generator;  // Copy the generators
+                }
             }
             if (!effect.valid()) {
                 effect = new Effect;
@@ -216,8 +221,14 @@ Effect* makeEffect(SGPropertyNode* prop,
                     lock(effectMutex);
                 pair<Effect::Cache::iterator, bool> irslt
                     = cache->insert(make_pair(key, effect));
-                if (!irslt.second)
-                    effect = irslt.first->second;
+                if (!irslt.second) {
+                    ref_ptr<Effect> old;
+                    if (irslt.first->second.lock(old))
+                        effect = old; // Another thread beat us in creating it! Discard our own...
+                    else
+                        irslt.first->second = effect; // update existing, but empty observer
+                }
+                effect->generator = parent->generator;  // Copy the generators
             }
         } else {
             SG_LOG(SG_INPUT, SG_ALERT, "can't find base effect " <<
@@ -229,6 +240,21 @@ Effect* makeEffect(SGPropertyNode* prop,
         effect->root = prop;
         effect->parametersProp = effect->root->getChild("parameters");
     }
+    const SGPropertyNode *generateProp = prop->getChild("generate");
+    if(generateProp)
+    {
+        effect->generator.clear();
+
+        // Effect needs some generated properties, like tangent vectors
+        const SGPropertyNode *parameter = generateProp->getChild("normal");
+        if(parameter) effect->setGenerator(Effect::NORMAL, parameter->getIntValue());
+
+        parameter = generateProp->getChild("tangent");
+        if(parameter) effect->setGenerator(Effect::TANGENT, parameter->getIntValue());
+
+        parameter = generateProp->getChild("binormal");
+        if(parameter) effect->setGenerator(Effect::BINORMAL, parameter->getIntValue());
+    }
     if (realizeTechniques) {
         try {
             OpenThreads::ScopedLock<OpenThreads::ReentrantMutex>