3 # include <simgear_config.h>
7 #include "Technique.hxx"
15 #include <boost/lexical_cast.hpp>
16 #include <boost/tuple/tuple.hpp>
17 #include <boost/tuple/tuple_comparison.hpp>
19 #include <OpenThreads/ReentrantMutex>
20 #include <OpenThreads/ScopedLock>
22 #include <osg/Material>
23 #include <osg/Program>
24 #include <osg/Referenced>
25 #include <osg/Texture2D>
28 #include <osgDB/FileUtils>
29 #include <osgDB/ReadFile>
30 #include <osgDB/Registry>
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>
42 typedef vector<const SGPropertyNode*> RawPropVector;
43 typedef map<const string, ref_ptr<Effect> > EffectMap;
48 OpenThreads::ReentrantMutex effectMutex;
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.
58 * Nodes are considered equal if their names and indexes are equal.
62 : public unary_function<const SGPropertyNode*, bool>
64 PropPredicate(const SGPropertyNode* node_) : node(node_) {}
65 bool operator()(const SGPropertyNode* arg) const
67 if (strcmp(node->getName(), arg->getName()))
69 return node->getIndex() == arg->getIndex();
71 const SGPropertyNode* node;
74 void mergePropertyTrees(SGPropertyNode* resultNode,
75 const SGPropertyNode* left, const SGPropertyNode* right)
77 if (left->nChildren() == 0) {
78 copyProperties(left, resultNode);
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(),
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);
97 copyProperties(node, newChild);
100 // Now copy nodes remaining in the left tree
101 for (RawPropVector::iterator itr = leftChildren.begin(),
102 e = leftChildren.end();
105 SGPropertyNode* newChild
106 = resultNode->getChild((*itr)->getName(), (*itr)->getIndex(), true);
107 copyProperties(*itr, newChild);
111 Effect* makeEffect(const string& name,
112 bool realizeTechniques,
113 const osgDB::ReaderWriter::Options* options)
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";
122 = osgDB::findDataFile(effectFileName, options);
123 if (absFileName.empty()) {
124 SG_LOG(SG_INPUT, SG_WARN, "can't find \"" << effectFileName << "\"");
127 SGPropertyNode_ptr effectProps = new SGPropertyNode();
128 readProperties(absFileName, effectProps.ptr(), 0, true);
129 Effect* result = makeEffect(effectProps.ptr(), realizeTechniques, options);
131 effectMap.insert(make_pair(name, result));
136 Effect* makeEffect(SGPropertyNode* prop,
137 bool realizeTechniques,
138 const osgDB::ReaderWriter::Options* options)
140 // Give default names to techniques and passes
141 vector<SGPropertyNode_ptr> techniques = prop->getChildren("technique");
142 for (int i = 0; i < 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 < 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 < 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));
163 Effect* effect = new Effect;
164 // Merge with the parent effect, if any
165 const SGPropertyNode* inheritProp = prop->getChild("inherits-from");
168 parent = makeEffect(inheritProp->getStringValue(), realizeTechniques,
172 effect->root = new SGPropertyNode;
173 mergePropertyTrees(effect->root, prop, parent->root);
174 effect->root->removeChild("inherits-from");
177 effect->root->removeChild("inherits-from");
182 effect->parametersProp = effect->root->getChild("parameters");
183 if (realizeTechniques)
184 effect->realizeTechniques(options);