]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/EffectBuilder.hxx
generalize support for initializing effect parameters from several properties
[simgear.git] / simgear / scene / material / EffectBuilder.hxx
1 // Copyright (C) 2009  Tim Moore timoore@redhat.com
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17 #ifndef SIMGEAR_EFFECTBUILDER_HXX
18 #define SIMGEAR_EFFECTBUILDER_HXX 1
19
20 #include <algorithm>
21 #include <iterator>
22 #include <map>
23 #include <string>
24 #include <cstring>
25
26 #include <osgDB/Registry>
27
28 #include <boost/bind.hpp>
29 #include <boost/multi_index_container.hpp>
30 #include <boost/multi_index/member.hpp>
31 #include <boost/multi_index/ordered_index.hpp>
32
33 #include <simgear/math/SGMath.hxx>
34 #include <simgear/props/AtomicChangeListener.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
37 #include <simgear/structure/exception.hxx>
38 #include <simgear/structure/SGSharedPtr.hxx>
39 #include <simgear/structure/Singleton.hxx>
40
41 #include "Effect.hxx"
42 /**
43  * Support classes for parsing effects.
44  */
45
46 namespace simgear
47 {
48 class Effect;
49 class Pass;
50 class SGReaderWriterXMLOptions;
51
52 /**
53  * Builder that returns an object, probably an OSG object.
54  */
55 template<typename T>
56 class EffectBuilder : public SGReferenced
57 {
58 public:
59     virtual ~EffectBuilder() {}
60     virtual T* build(Effect* effect, const SGPropertyNode*,
61                      const SGReaderWriterXMLOptions* options) = 0;
62     static T* buildFromType(Effect* effect, const std::string& type,
63                             const SGPropertyNode*props,
64                             const SGReaderWriterXMLOptions* options)
65     {
66         BuilderMap& builderMap = getMap();
67         typename BuilderMap::iterator iter = builderMap.find(type);
68         if (iter != builderMap.end())
69             return iter->second->build(effect, props, options);
70         else
71             return 0;
72     }
73     struct Registrar;
74     friend struct Registrar;
75     struct Registrar
76     {
77         Registrar(const std::string& type, EffectBuilder* builder)
78         {
79             getMap().insert(std::make_pair(type, builder));
80         }
81     };
82 protected:
83     typedef std::map<std::string, SGSharedPtr<EffectBuilder> > BuilderMap;
84     struct BuilderMapSingleton : public simgear::Singleton<BuilderMapSingleton>
85     {
86         BuilderMap _map;
87     };
88     static BuilderMap& getMap()
89     {
90         return BuilderMapSingleton::instance()->_map;
91     }
92 };
93
94 // Tables of strings and constants. We want to reconstruct the effect
95 // property tree from OSG state sets, so the tables should be bi-directional.
96
97 // two-way map for building StateSets from property descriptions, and
98 // vice versa. Mostly copied from the boost documentation.
99
100 namespace effect
101 {
102 using boost::multi_index_container;
103 using namespace boost::multi_index;
104
105 // tags for accessing both sides of a bidirectional map
106
107 struct from{};
108 struct to{};
109
110 template <typename T>
111 struct EffectNameValue
112 {
113     // Don't use std::pair because we want to use aggregate intialization.
114     const char* first;
115     T second;
116 };
117
118 // The class template bidirectional_map wraps the specification
119 // of a bidirectional map based on multi_index_container.
120
121 template<typename FromType,typename ToType>
122 struct bidirectional_map
123 {
124     typedef std::pair<FromType,ToType> value_type;
125
126     /* A bidirectional map can be simulated as a multi_index_container
127      * of pairs of (FromType,ToType) with two unique indices, one
128      * for each member of the pair.
129      */
130
131     typedef multi_index_container<
132         value_type,
133         indexed_by<
134             ordered_unique<
135                 tag<from>, member<value_type, FromType, &value_type::first> >,
136             ordered_unique<
137                 tag<to>,  member<value_type, ToType, &value_type::second> >
138             >
139         > type;
140 };
141
142 template<typename T>
143 struct EffectPropertyMap
144 {
145     typedef typename bidirectional_map<std::string, T>::type BMap;
146     BMap _map;
147     template<int N>
148     EffectPropertyMap(const EffectNameValue<T> (&attrs)[N]);
149 };
150
151 template<typename T>
152 template<int N>
153 EffectPropertyMap<T>::EffectPropertyMap(const EffectNameValue<T> (&attrs)[N])
154 {
155     for (int i = 0; i < N; ++i)
156         _map.insert(typename BMap::value_type(attrs[i].first, attrs[i].second));
157 }
158
159 class BuilderException : public sg_exception
160 {
161 public:
162     BuilderException();
163     BuilderException(const char* message, const char* origin = 0);
164     BuilderException(const std::string& message, const std::string& = "");
165     virtual ~BuilderException() throw();
166 };
167 }
168
169 template<typename T>
170 void findAttr(const effect::EffectPropertyMap<T>& pMap,
171               const char* name,
172               T& result)
173 {
174     using namespace effect;
175     typename EffectPropertyMap<T>::BMap::iterator itr
176         = pMap._map.get<from>().find(name);
177     if (itr == pMap._map.end()) {
178         throw effect::BuilderException(string("findAttr: could not find attribute ")
179                                + string(name));
180     } else {
181         result = itr->second;
182     }
183 }
184
185 template<typename T>
186 inline void findAttr(const effect::EffectPropertyMap<T>& pMap,
187                      const std::string& name,
188                      T& result)
189 {
190     findAttr(pMap, name.c_str(), result);
191 }
192
193 template<typename T>
194 void findAttr(const effect::EffectPropertyMap<T>& pMap,
195               const SGPropertyNode* prop,
196               T& result)
197 {
198     if (!prop)
199         throw effect::BuilderException("findAttr: empty property");
200     const char* name = prop->getStringValue();
201     if (!name)
202         throw effect::BuilderException("findAttr: no name for lookup");
203     findAttr(pMap, name, result);
204 }
205
206 template<typename T>
207 std::string findName(const effect::EffectPropertyMap<T>& pMap, T value)
208 {
209     using namespace effect;
210     std::string result;
211     typename EffectPropertyMap<T>::BMap::template index_iterator<to>::type itr
212         = pMap._map.get<to>().find(value);
213     if (itr != pMap._map.get<to>().end())
214         result = itr->first;
215     return result;
216 }
217
218 template<typename T>
219 std::string findName(const effect::EffectPropertyMap<T>& pMap, GLenum value)
220 {
221     return findName(pMap, static_cast<T>(value));
222 }
223
224 /**
225  * Given a property node from a pass, get its value either from it or
226  * from the effect parameters.
227  */
228
229 const SGPropertyNode* getEffectPropertyNode(Effect* effect,
230                                             const SGPropertyNode* prop);
231 /**
232  * Get a named child property from pass parameters or effect
233  * parameters.
234  */
235 const SGPropertyNode* getEffectPropertyChild(Effect* effect,
236                                              const SGPropertyNode* prop,
237                                              const char* name);
238
239 /**
240  * Get the name of a node mentioned in a <use> clause from the global property
241  * tree.
242  * @return empty if prop doesn't contain a <use> clause; otherwise the
243  * mentioned node name.
244  */
245 std::string getGlobalProperty(const SGPropertyNode* prop,
246                               const SGReaderWriterXMLOptions *);
247
248 template<typename NameItr>
249 std::vector<std::string>
250 getVectorProperties(const SGPropertyNode* prop,
251                     const SGReaderWriterXMLOptions *options, size_t vecSize,
252                     NameItr defaultNames)
253 {
254     using namespace std;
255     vector<string> result;
256     if (!prop)
257         return result;
258     PropertyList useProps = prop->getChildren("use");
259     if (useProps.size() == 1) {
260         string parentName = useProps[0]->getStringValue();
261         if (parentName.size() == 0 || parentName[0] != '/')
262             parentName = options->getPropRoot()->getPath() + "/" + parentName;
263         if (parentName[parentName.size() - 1] != '/')
264             parentName.append("/");
265         NameItr itr = defaultNames;
266         for (int i = 0; i < vecSize; ++i, ++itr)
267             result.push_back(parentName + *itr);
268     } else if (useProps.size() == vecSize) {
269         string parentName = useProps[0]->getStringValue();
270         parentName += "/";
271         for (PropertyList::const_iterator itr = useProps.begin(),
272                  end = useProps.end();
273              itr != end;
274              ++itr) {
275             string childName = (*itr)->getStringValue();
276             if (childName.size() == 0 || childName[0] != '/')
277                 result.push_back(parentName + childName);
278             else
279                 result.push_back(childName);
280         }
281     }
282     return result;
283 }
284
285 class PassAttributeBuilder : public SGReferenced
286 {
287 protected:
288     typedef std::map<const std::string, SGSharedPtr<PassAttributeBuilder> >
289     PassAttrMap;
290
291     struct PassAttrMapSingleton : public simgear::Singleton<PassAttrMapSingleton>
292     {
293         PassAttrMap passAttrMap;
294     };
295 public:
296     virtual void buildAttribute(Effect* effect, Pass* pass,
297                                 const SGPropertyNode* prop,
298                                 const SGReaderWriterXMLOptions* options)
299     = 0;
300     static PassAttributeBuilder* find(const std::string& str)
301     {
302         PassAttrMap::iterator itr
303             = PassAttrMapSingleton::instance()->passAttrMap.find(str);
304         if (itr == PassAttrMapSingleton::instance()->passAttrMap.end())
305             return 0;
306         else
307             return itr->second.ptr();
308     }
309     template<typename T> friend struct InstallAttributeBuilder;
310 };
311
312 template<typename T>
313 struct InstallAttributeBuilder
314 {
315     InstallAttributeBuilder(const string& name)
316     {
317         PassAttributeBuilder::PassAttrMapSingleton::instance()
318             ->passAttrMap.insert(make_pair(name, new T));
319     }
320 };
321
322 // The description of an attribute may exist in a pass' XML, but a
323 // derived effect might want to disable the attribute altogether. So,
324 // some attributes have an "active" property; if it exists and is
325 // false, the OSG attribute is not built at all. This is different
326 // from any OSG mode settings that might be around.
327 bool isAttributeActive(Effect* effect, const SGPropertyNode* prop);
328
329 namespace effect
330 {
331 /**
332  * Bridge between types stored in properties and what OSG wants.
333  */
334 template<typename T> struct OSGBridge;
335
336 template<typename T>
337 struct OSGBridge<const T> : public OSGBridge<T>
338 {
339 };
340
341 template<>
342 struct OSGBridge<osg::Vec3f>
343 {
344     typedef SGVec3d sg_type;
345     static osg::Vec3f getOsgType(const SGVec3d& val) { return toOsg(val); }
346 };
347
348 template<>
349 struct OSGBridge<osg::Vec3d>
350 {
351     typedef SGVec3d sg_type;
352     static osg::Vec3d getOsgType(const SGVec3d& val) { return toOsg(val); }
353 };
354
355 template<>
356 struct OSGBridge<osg::Vec4f>
357 {
358     typedef SGVec4d sg_type;
359     static osg::Vec4f getOsgType(const SGVec4d& val) { return toOsg(val); }
360 };
361
362 template<>
363 struct OSGBridge<osg::Vec4d>
364 {
365     typedef SGVec4d sg_type;
366     static osg::Vec4d getOsgType(const SGVec4d& val) { return toOsg(val); }
367 };
368
369 template<typename Obj, typename OSGParam>
370 struct OSGFunctor : public OSGBridge<OSGParam>
371 {
372     OSGFunctor(Obj* obj, void (Obj::*func)(const OSGParam&))
373         : _obj(obj), _func(func) {}
374     void operator()(const typename OSGBridge<OSGParam>::sg_type& val) const
375     {
376         ((_obj.get())->*_func)(this->getOsgType(val));
377     }
378     osg::ref_ptr<Obj>_obj;
379     void (Obj::*_func)(const OSGParam&);
380 };
381
382 template<typename ObjType, typename OSGParamType>
383 class ScalarChangeListener
384     : public SGPropertyChangeListener, public InitializeWhenAdded,
385       public Effect::Updater
386 {
387 public:
388     typedef void (ObjType::*setter_type)(const OSGParamType);
389     ScalarChangeListener(ObjType* obj, setter_type setter,
390                          const std::string& propName)
391         : _obj(obj), _setter(setter)
392     {
393         _propName = new std::string(propName);
394     }
395     virtual ~ScalarChangeListener()
396     {
397         delete _propName;
398         _propName = 0;
399     }
400     void valueChanged(SGPropertyNode* node)
401     {
402         _obj->*setter(node->getValue<OSGParamType>());
403     }
404     void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
405     {
406         SGPropertyNode* listenProp = makeNode(propRoot, *_propName);
407         delete _propName;
408         _propName = 0;
409         if (listenProp)
410             listenProp->addChangeListener(this, true);
411     }
412 private:
413     osg::ref_ptr<ObjType> _obj;
414     setter_type _setter;
415     std::string* _propName;
416 };
417
418 template<typename T, typename Func>
419 class EffectExtendedPropListener : public InitializeWhenAdded,
420                                    public Effect::Updater
421 {
422 public:
423     template<typename Itr>
424     EffectExtendedPropListener(const Func& func,
425                                const std::string* propName, Itr childNamesBegin,
426                                Itr childNamesEnd)
427         : _propName(0), _func(func)
428     {
429         if (propName)
430             _propName = new std::string(*propName);
431         _childNames = new std::vector<std::string>(childNamesBegin,
432                                                    childNamesEnd);
433     }
434     virtual ~EffectExtendedPropListener()
435     {
436         delete _propName;
437         delete _childNames;
438     }
439     void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
440     {
441         SGPropertyNode* parent = 0;
442         if (_propName)
443             parent = propRoot->getNode(*_propName, true);
444         else
445             parent = propRoot;
446         _propListener
447             = new ExtendedPropListener<T, Func>(parent, _childNames->begin(),
448                                                 _childNames->end(),
449                                                 _func, true);
450         delete _propName;
451         _propName = 0;
452         delete _childNames;
453         _childNames = 0;
454     }
455 private:
456     std::string* _propName;
457     std::vector<std::string>* _childNames;
458     SGSharedPtr<ExtendedPropListener<T, Func> > _propListener;
459     Func _func;
460 };
461
462 /**
463  * Initialize the value and the possible updating of an effect
464  * attribute. If the value is specified directly, set it. Otherwise,
465  * use the <use> tag to look at the parameters. Again, if there is a
466  * value there set it directly. Otherwise, the parameter contains its
467  * own <use> tag referring to a property in the global property tree;
468  * install a change listener that will set the attribute when the
469  * property changes.
470  *
471  * For relative property names, the property root found in options is
472  * used.
473  */
474 template<typename ObjType, typename OSGParamType>
475 void
476 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
477                    void (ObjType::*setter)(const OSGParamType),
478                    const SGReaderWriterXMLOptions* options)
479 {
480     const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
481     if (!valProp)
482         return;
483     if (valProp->nChildren() == 0) {
484         obj->*setter(valProp->getValue<OSGParamType>());
485     } else {
486         std::string propName = getGlobalProperty(prop, options);
487         ScalarChangeListener<ObjType, OSGParamType>* listener
488             = new ScalarChangeListener<ObjType, OSGParamType>(obj, setter,
489                                                               propName);
490         effect->addUpdater(listener);
491     }
492 }
493
494 /*
495  * Initialize vector parameters from individual properties.
496  * The parameter may be updated at runtime.
497  *
498  * If the value is specified directly, set it. Otherwise, use the
499  * <use> tag to look at the parameters. Again, if there is a value
500  * there set it directly. Otherwise, the parameter contains one or several
501  * <use> tags. If there is one tag, it is a property that is the root
502  * for the values needed to update the parameter; nameIter holds the
503  * names of the properties relative to the root. If there are several
504  * <use> tags, they each hold the name of the property holding the
505  * value for the corresponding vector member.
506  *
507  * Install a change listener that will set the attribute when the
508  * property changes.
509  *
510  * For relative property names, the property root found in options is
511  * used.
512  */
513 template<typename ObjType, typename OSGParamType, typename NameItrType>
514 void
515 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
516                    void (ObjType::*setter)(const OSGParamType&),
517                    NameItrType nameItr, const SGReaderWriterXMLOptions* options)
518 {
519     typedef typename OSGBridge<OSGParamType>::sg_type sg_type;
520     const int numComponents = props::NumComponents<sg_type>::num_components;
521     const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
522     if (!valProp)
523         return;
524     if (valProp->nChildren() == 0) { // Has <use>?
525         (obj->*setter)(OSGBridge<OSGParamType>
526                      ::getOsgType(valProp->getValue<sg_type>()));
527     } else {
528         std::vector<std::string> paramNames
529             = getVectorProperties(valProp, options,numComponents, nameItr);
530         if (paramNames.empty())
531             throw BuilderException();
532         std::vector<std::string>::const_iterator pitr = paramNames.begin();
533         typedef OSGFunctor<ObjType, OSGParamType> Functor;
534         Effect::Updater* listener
535             =  new EffectExtendedPropListener<sg_type, Functor>
536             (Functor(obj, setter), 0, pitr, pitr + numComponents);
537         effect->addUpdater(listener);
538     }
539 }
540
541 extern const char* colorFields[];
542 }
543 }
544 #endif