]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/makeEffect.cxx
Merge branch 'frohlich/weak' into next
[simgear.git] / simgear / scene / material / makeEffect.cxx
1
2 #ifdef HAVE_CONFIG_H
3 #  include <simgear_config.h>
4 #endif
5
6 #include "Effect.hxx"
7 #include "Technique.hxx"
8 #include "Pass.hxx"
9
10 #include <algorithm>
11 #include <cstring>
12 #include <map>
13 #include <sstream>
14
15 #include <boost/lexical_cast.hpp>
16 #include <boost/tuple/tuple.hpp>
17 #include <boost/tuple/tuple_comparison.hpp>
18
19 #include <OpenThreads/ReentrantMutex>
20 #include <OpenThreads/ScopedLock>
21
22 #include <osg/Material>
23 #include <osg/Program>
24 #include <osg/Referenced>
25 #include <osg/Texture2D>
26 #include <osg/Vec4d>
27
28 #include <osgDB/FileUtils>
29 #include <osgDB/ReadFile>
30 #include <osgDB/Registry>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/props/props_io.hxx>
34 #include <simgear/scene/util/SGSceneFeatures.hxx>
35 #include <simgear/structure/SGExpression.hxx>
36
37 namespace simgear
38 {
39 using namespace std;
40 using namespace osg;
41
42 typedef vector<const SGPropertyNode*> RawPropVector;
43 typedef map<const string, ref_ptr<Effect> > EffectMap;
44
45 namespace
46 {
47 EffectMap effectMap;
48 OpenThreads::ReentrantMutex effectMutex;
49 }
50
51 /** Merge two property trees, producing a new tree.
52  * If the nodes are both leaves, value comes from left leaf.
53  * Otherwise, The children are examined. If a left and right child are
54  * "identical," they are merged and the result placed in the children
55  * of the result. Otherwise the left children are placed after the
56  * right children in the result.
57  *
58  * Nodes are considered equal if their names and indexes are equal.
59  */
60
61 struct PropPredicate
62     : public unary_function<const SGPropertyNode*, bool>
63 {
64     PropPredicate(const SGPropertyNode* node_) : node(node_) {}
65     bool operator()(const SGPropertyNode* arg) const
66     {
67         if (strcmp(node->getName(), arg->getName()))
68             return false;
69         return node->getIndex() == arg->getIndex();
70     }
71     const SGPropertyNode* node;
72 };
73
74 void mergePropertyTrees(SGPropertyNode* resultNode,
75                         const SGPropertyNode* left, const SGPropertyNode* right)
76 {
77     if (left->nChildren() == 0) {
78         copyProperties(left, resultNode);
79         return;
80     }
81     resultNode->setAttributes(right->getAttributes());
82     RawPropVector leftChildren;
83     for (int i = 0; i < left->nChildren(); ++i)
84         leftChildren.push_back(left->getChild(i));
85     // Merge identical nodes
86     for (int i = 0; i < right->nChildren(); ++i) {
87         const SGPropertyNode* node = right->getChild(i);
88         RawPropVector::iterator litr
89             = find_if(leftChildren.begin(), leftChildren.end(),
90                       PropPredicate(node));
91         SGPropertyNode* newChild
92             = resultNode->getChild(node->getName(), node->getIndex(), true);
93         if (litr != leftChildren.end()) {
94             mergePropertyTrees(newChild, *litr, node);
95             leftChildren.erase(litr);
96         } else {
97             copyProperties(node, newChild);
98         }
99     }
100     // Now copy nodes remaining in the left tree
101     for (RawPropVector::iterator itr = leftChildren.begin(),
102              e = leftChildren.end();
103          itr != e;
104          ++itr) {
105         SGPropertyNode* newChild
106             = resultNode->getChild((*itr)->getName(), (*itr)->getIndex(), true);
107         copyProperties(*itr, newChild);
108     }
109 }
110
111 Effect* makeEffect(const string& name,
112                    bool realizeTechniques,
113                    const osgDB::ReaderWriter::Options* options)
114 {
115     OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(effectMutex);
116     EffectMap::iterator itr = effectMap.find(name);
117     if (itr != effectMap.end())
118         return itr->second.get();
119     string effectFileName(name);
120     effectFileName += ".eff";
121     string absFileName
122         = osgDB::findDataFile(effectFileName, options);
123     if (absFileName.empty()) {
124         SG_LOG(SG_INPUT, SG_WARN, "can't find \"" << effectFileName << "\"");
125         return 0;
126     }
127     SGPropertyNode_ptr effectProps = new SGPropertyNode();
128     readProperties(absFileName, effectProps.ptr(), 0, true);
129     Effect* result = makeEffect(effectProps.ptr(), realizeTechniques, options);
130     if (result)
131         effectMap.insert(make_pair(name, result));
132     return result;
133 }
134
135
136 Effect* makeEffect(SGPropertyNode* prop,
137                    bool realizeTechniques,
138                    const osgDB::ReaderWriter::Options* options)
139 {
140     // Give default names to techniques and passes
141     vector<SGPropertyNode_ptr> techniques = prop->getChildren("technique");
142     for (int i = 0; i < (int)techniques.size(); ++i) {
143         SGPropertyNode* tniqProp = techniques[i].ptr();
144         if (!tniqProp->hasChild("name"))
145             setValue(tniqProp->getChild("name", 0, true),
146                      boost::lexical_cast<string>(i));
147         vector<SGPropertyNode_ptr> passes = tniqProp->getChildren("pass");
148         for (int j = 0; j < (int)passes.size(); ++j) {
149             SGPropertyNode* passProp = passes[j].ptr();
150             if (!passProp->hasChild("name"))
151                 setValue(passProp->getChild("name", 0, true),
152                          boost::lexical_cast<string>(j));
153             vector<SGPropertyNode_ptr> texUnits
154                 = passProp->getChildren("texture-unit");
155             for (int k = 0; k < (int)texUnits.size(); ++k) {
156                 SGPropertyNode* texUnitProp = texUnits[k].ptr();
157                 if (!texUnitProp->hasChild("name"))
158                     setValue(texUnitProp->getChild("name", 0, true),
159                              boost::lexical_cast<string>(k));
160             }
161         }
162     }
163     Effect* effect = new Effect;
164     // Merge with the parent effect, if any
165     const SGPropertyNode* inheritProp = prop->getChild("inherits-from");
166     Effect* parent = 0;
167     if (inheritProp) {
168         parent = makeEffect(inheritProp->getStringValue(), realizeTechniques,
169                             options);
170         if(parent)
171         {
172             effect->root = new SGPropertyNode;
173             mergePropertyTrees(effect->root, prop, parent->root);
174             effect->root->removeChild("inherits-from");
175         } else {
176             effect->root = prop;
177             effect->root->removeChild("inherits-from");
178         }
179     } else {
180         effect->root = prop;
181     }
182     effect->parametersProp = effect->root->getChild("parameters");
183     if (realizeTechniques)
184         effect->realizeTechniques(options);
185     return effect;
186 }
187
188 }