]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/EffectBuilder.hxx
Merge branch 'jmt/magvar' into next
[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 // A one-way map that can be initialized using an array
160 template<typename T>
161 struct SimplePropertyMap
162 {
163     typedef std::map<string, T> map_type;
164     map_type _map;
165     template<int N>
166     SimplePropertyMap(const EffectNameValue<T> (&attrs)[N])
167     {
168         for (int i = 0; i < N; ++i)
169         _map.insert(typename map_type::value_type(attrs[i].first,
170                                                   attrs[i].second));
171     }
172 };
173
174 class BuilderException : public sg_exception
175 {
176 public:
177     BuilderException();
178     BuilderException(const char* message, const char* origin = 0);
179     BuilderException(const std::string& message, const std::string& = "");
180     virtual ~BuilderException() throw();
181 };
182 }
183
184 template<typename T>
185 void findAttr(const effect::EffectPropertyMap<T>& pMap,
186               const char* name,
187               T& result)
188 {
189     using namespace effect;
190     typename EffectPropertyMap<T>::BMap::iterator itr
191         = pMap._map.get<from>().find(name);
192     if (itr == pMap._map.end()) {
193         throw effect::BuilderException(string("findAttr: could not find attribute ")
194                                + string(name));
195     } else {
196         result = itr->second;
197     }
198 }
199
200 template<typename T>
201 inline void findAttr(const effect::EffectPropertyMap<T>& pMap,
202                      const std::string& name,
203                      T& result)
204 {
205     findAttr(pMap, name.c_str(), result);
206 }
207
208 template<typename T>
209 void findAttr(const effect::EffectPropertyMap<T>& pMap,
210               const SGPropertyNode* prop,
211               T& result)
212 {
213     if (!prop)
214         throw effect::BuilderException("findAttr: empty property");
215     const char* name = prop->getStringValue();
216     if (!name)
217         throw effect::BuilderException("findAttr: no name for lookup");
218     findAttr(pMap, name, result);
219 }
220
221 // Versions that don't throw an error
222
223 template<typename T>
224 const T* findAttr(const effect::EffectPropertyMap<T>& pMap,
225                   const char* name)
226 {
227     using namespace effect;
228     typename EffectPropertyMap<T>::BMap::iterator itr
229         = pMap._map.get<from>().find(name);
230     if (itr == pMap._map.end())
231         return 0;
232     else 
233         return &itr->second;
234 }
235
236 template<typename T>
237 const T* findAttr(const effect::SimplePropertyMap<T>& pMap,
238                   const char* name)
239 {
240     using namespace effect;
241     typename SimplePropertyMap<T>::map_type::const_iterator itr
242         = pMap._map.find(name);
243     if (itr == pMap._map.end())
244         return 0;
245     else 
246         return &itr->second;
247 }
248
249 template<typename T, template<class> class Map>
250 const T* findAttr(const Map<T>& pMap,
251                      const std::string& name)
252 {
253     return findAttr(pMap, name.c_str());
254 }
255
256
257 template<typename T>
258 std::string findName(const effect::EffectPropertyMap<T>& pMap, T value)
259 {
260     using namespace effect;
261     std::string result;
262     typename EffectPropertyMap<T>::BMap::template index_iterator<to>::type itr
263         = pMap._map.get<to>().find(value);
264     if (itr != pMap._map.get<to>().end())
265         result = itr->first;
266     return result;
267 }
268
269 template<typename T>
270 std::string findName(const effect::EffectPropertyMap<T>& pMap, GLenum value)
271 {
272     return findName(pMap, static_cast<T>(value));
273 }
274
275 /**
276  * Given a property node from a pass, get its value either from it or
277  * from the effect parameters.
278  */
279
280 const SGPropertyNode* getEffectPropertyNode(Effect* effect,
281                                             const SGPropertyNode* prop);
282 /**
283  * Get a named child property from pass parameters or effect
284  * parameters.
285  */
286 const SGPropertyNode* getEffectPropertyChild(Effect* effect,
287                                              const SGPropertyNode* prop,
288                                              const char* name);
289
290 /**
291  * Get the name of a node mentioned in a <use> clause from the global property
292  * tree.
293  * @return empty if prop doesn't contain a <use> clause; otherwise the
294  * mentioned node name.
295  */
296 std::string getGlobalProperty(const SGPropertyNode* prop,
297                               const SGReaderWriterXMLOptions *);
298
299 template<typename NameItr>
300 std::vector<std::string>
301 getVectorProperties(const SGPropertyNode* prop,
302                     const SGReaderWriterXMLOptions *options, size_t vecSize,
303                     NameItr defaultNames)
304 {
305     using namespace std;
306     vector<string> result;
307     if (!prop)
308         return result;
309     PropertyList useProps = prop->getChildren("use");
310     if (useProps.size() == 1) {
311         string parentName = useProps[0]->getStringValue();
312         if (parentName.size() == 0 || parentName[0] != '/')
313             parentName = options->getPropRoot()->getPath() + "/" + parentName;
314         if (parentName[parentName.size() - 1] != '/')
315             parentName.append("/");
316         NameItr itr = defaultNames;
317         for (int i = 0; i < vecSize; ++i, ++itr)
318             result.push_back(parentName + *itr);
319     } else if (useProps.size() == vecSize) {
320         string parentName = useProps[0]->getStringValue();
321         parentName += "/";
322         for (PropertyList::const_iterator itr = useProps.begin(),
323                  end = useProps.end();
324              itr != end;
325              ++itr) {
326             string childName = (*itr)->getStringValue();
327             if (childName.size() == 0 || childName[0] != '/')
328                 result.push_back(parentName + childName);
329             else
330                 result.push_back(childName);
331         }
332     }
333     return result;
334 }
335
336 class PassAttributeBuilder : public SGReferenced
337 {
338 protected:
339     typedef std::map<const std::string, SGSharedPtr<PassAttributeBuilder> >
340     PassAttrMap;
341
342     struct PassAttrMapSingleton : public simgear::Singleton<PassAttrMapSingleton>
343     {
344         PassAttrMap passAttrMap;
345     };
346 public:
347     virtual void buildAttribute(Effect* effect, Pass* pass,
348                                 const SGPropertyNode* prop,
349                                 const SGReaderWriterXMLOptions* options)
350     = 0;
351     static PassAttributeBuilder* find(const std::string& str)
352     {
353         PassAttrMap::iterator itr
354             = PassAttrMapSingleton::instance()->passAttrMap.find(str);
355         if (itr == PassAttrMapSingleton::instance()->passAttrMap.end())
356             return 0;
357         else
358             return itr->second.ptr();
359     }
360     template<typename T> friend struct InstallAttributeBuilder;
361 };
362
363 template<typename T>
364 struct InstallAttributeBuilder
365 {
366     InstallAttributeBuilder(const string& name)
367     {
368         PassAttributeBuilder::PassAttrMapSingleton::instance()
369             ->passAttrMap.insert(make_pair(name, new T));
370     }
371 };
372
373 // The description of an attribute may exist in a pass' XML, but a
374 // derived effect might want to disable the attribute altogether. So,
375 // some attributes have an "active" property; if it exists and is
376 // false, the OSG attribute is not built at all. This is different
377 // from any OSG mode settings that might be around.
378 bool isAttributeActive(Effect* effect, const SGPropertyNode* prop);
379
380 namespace effect
381 {
382 /**
383  * Bridge between types stored in properties and what OSG or the
384  * effects code want.
385  */
386 template<typename T> struct Bridge;
387
388 /**
389  * Default just passes on the same type.
390  *
391  */
392 template<typename T>
393 struct Bridge
394 {
395     typedef T sg_type;
396     static T get(const T& val) { return val; }
397 };
398
399 template<typename T>
400 struct Bridge<const T> : public Bridge<T>
401 {
402 };
403
404 // Save some typing...
405 template<typename InType, typename OutType>
406 struct BridgeOSGVec
407 {
408     typedef InType sg_type;
409     static OutType get(const InType& val) { return toOsg(val); }
410 };
411 template<>
412 struct Bridge<osg::Vec3f> : public BridgeOSGVec<SGVec3d, osg::Vec3f>
413 {
414 };
415
416 template<>
417 struct Bridge<osg::Vec3d> : public BridgeOSGVec<SGVec3d, osg::Vec3d>
418 {
419 };
420
421 template<>
422 struct Bridge<osg::Vec4f> : public BridgeOSGVec<SGVec4d, osg::Vec4f>
423 {
424 };
425
426 template<>
427 struct Bridge<osg::Vec4d> : public BridgeOSGVec<SGVec4d, osg::Vec4d>
428 {
429 };
430
431 /**
432  * Functor for calling a function on an osg::Referenced object and a
433  * value (e.g., an SGVec4d from a property) which will be converted to
434  * the equivalent OSG type.
435  *
436  * General version, function takes obj, val
437  */
438 template<typename OSGParam, typename Obj, typename Func>
439 struct OSGFunctor : public Bridge<OSGParam>
440 {
441     OSGFunctor(Obj* obj, const Func& func)
442         : _obj(obj), _func(func) {}
443     void operator()(const typename Bridge<OSGParam>::sg_type& val) const
444     {
445         _func(_obj, this->get(val));
446     }
447     osg::ref_ptr<Obj>_obj;
448     const Func _func;
449 };
450
451 /**
452  * Version which uses a pointer to member function instead.
453  */
454 template<typename OSGParam, typename Obj>
455 struct OSGFunctor<OSGParam, Obj, void (Obj::* const)(const OSGParam&)>
456     : public Bridge<OSGParam>
457 {
458     typedef void (Obj::*const MemFunc)(const OSGParam&);
459     OSGFunctor(Obj* obj, MemFunc func)
460         : _obj(obj), _func(func) {}
461     void operator()(const typename Bridge<OSGParam>::sg_type& val) const
462     {
463         (_obj->*_func)(this->get(val));
464     }
465     osg::ref_ptr<Obj>_obj;
466     MemFunc _func;
467 };
468
469 /**
470  * Typical convenience constructors
471  */
472 template<typename OSGParam, typename Obj, typename Func>
473 OSGFunctor<OSGParam, Obj, Func> make_OSGFunctor(Obj* obj, const Func& func)
474 {
475     return OSGFunctor<OSGParam, Obj, Func>(obj, func);
476 }
477
478 template<typename OSGParam, typename Obj>
479 OSGFunctor<OSGParam, Obj, void (Obj::*const)(const OSGParam&)>
480 make_OSGFunctor(Obj* obj, void (Obj::*const func)(const OSGParam&))
481 {
482     return OSGFunctor<OSGParam, Obj,
483         void (Obj::* const)(const OSGParam&)>(obj, func);
484 }
485
486 template<typename OSGParamType, typename ObjType, typename F>
487 class ScalarChangeListener
488     : public SGPropertyChangeListener, public InitializeWhenAdded,
489       public Effect::Updater
490 {
491 public:
492     ScalarChangeListener(ObjType* obj, const F& setter,
493                          const std::string& propName)
494         : _obj(obj), _setter(setter)
495     {
496         _propName = new std::string(propName);
497     }
498     virtual ~ScalarChangeListener()
499     {
500         delete _propName;
501         _propName = 0;
502     }
503     void valueChanged(SGPropertyNode* node)
504     {
505         _setter(_obj.get(), node->getValue<OSGParamType>());
506     }
507     void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
508     {
509         SGPropertyNode* listenProp = makeNode(propRoot, *_propName);
510         delete _propName;
511         _propName = 0;
512         if (listenProp)
513             listenProp->addChangeListener(this, true);
514     }
515 private:
516     osg::ref_ptr<ObjType> _obj;
517     F _setter;
518     std::string* _propName;
519 };
520
521 template<typename T, typename Func>
522 class EffectExtendedPropListener : public InitializeWhenAdded,
523                                    public Effect::Updater
524 {
525 public:
526     template<typename Itr>
527     EffectExtendedPropListener(const Func& func,
528                                const std::string* propName, Itr childNamesBegin,
529                                Itr childNamesEnd)
530         : _propName(0), _func(func)
531     {
532         if (propName)
533             _propName = new std::string(*propName);
534         _childNames = new std::vector<std::string>(childNamesBegin,
535                                                    childNamesEnd);
536     }
537     virtual ~EffectExtendedPropListener()
538     {
539         delete _propName;
540         delete _childNames;
541     }
542     void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
543     {
544         SGPropertyNode* parent = 0;
545         if (_propName)
546             parent = propRoot->getNode(*_propName, true);
547         else
548             parent = propRoot;
549         _propListener
550             = new ExtendedPropListener<T, Func>(parent, _childNames->begin(),
551                                                 _childNames->end(),
552                                                 _func, true);
553         delete _propName;
554         _propName = 0;
555         delete _childNames;
556         _childNames = 0;
557     }
558 private:
559     std::string* _propName;
560     std::vector<std::string>* _childNames;
561     SGSharedPtr<ExtendedPropListener<T, Func> > _propListener;
562     Func _func;
563 };
564
565 template<typename T, typename Func, typename Itr>
566 Effect::Updater*
567 new_EEPropListener(const Func& func, const std::string* propName,
568                    const Itr& namesBegin, const Itr& namesEnd)
569 {
570     return new EffectExtendedPropListener<T, Func>
571         (func, 0, namesBegin, namesEnd);
572 }
573
574 /**
575  * Initialize the value and the possible updating of an effect
576  * attribute. If the value is specified directly, set it. Otherwise,
577  * use the <use> tag to look at the parameters. Again, if there is a
578  * value there set it directly. Otherwise, the parameter contains its
579  * own <use> tag referring to a property in the global property tree;
580  * install a change listener that will set the attribute when the
581  * property changes.
582  *
583  * For relative property names, the property root found in options is
584  * used.
585  */
586 template<typename OSGParamType, typename ObjType, typename F>
587 void
588 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
589                    const F& setter, const SGReaderWriterXMLOptions* options)
590 {
591     const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
592     if (!valProp)
593         return;
594     if (valProp->nChildren() == 0) {
595         setter(obj, valProp->getValue<OSGParamType>());
596     } else {
597         std::string propName = getGlobalProperty(valProp, options);
598         ScalarChangeListener<OSGParamType, ObjType, F>* listener
599             = new ScalarChangeListener<OSGParamType, ObjType, F>(obj, setter,
600                                                                  propName);
601         effect->addUpdater(listener);
602     }
603 }
604
605 template<typename OSGParamType, typename ObjType, typename SetterReturn>
606 inline void
607 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
608                    SetterReturn (ObjType::*setter)(const OSGParamType),
609                    const SGReaderWriterXMLOptions* options)
610 {
611     initFromParameters<OSGParamType>(effect, prop, obj,
612                                      boost::bind(setter, _1, _2), options);
613 }
614
615 /*
616  * Initialize vector parameters from individual properties.
617  * The parameter may be updated at runtime.
618  *
619  * If the value is specified directly, set it. Otherwise, use the
620  * <use> tag to look at the parameters. Again, if there is a value
621  * there set it directly. Otherwise, the parameter contains one or several
622  * <use> tags. If there is one tag, it is a property that is the root
623  * for the values needed to update the parameter; nameIter holds the
624  * names of the properties relative to the root. If there are several
625  * <use> tags, they each hold the name of the property holding the
626  * value for the corresponding vector member.
627  *
628  * Install a change listener that will set the attribute when the
629  * property changes.
630  *
631  * For relative property names, the property root found in options is
632  * used.
633  */
634 template<typename OSGParamType, typename ObjType, typename NameItrType,
635          typename F>
636 void
637 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
638                    const F& setter,
639                    NameItrType nameItr, const SGReaderWriterXMLOptions* options)
640 {
641     typedef typename Bridge<OSGParamType>::sg_type sg_type;
642     const int numComponents = props::NumComponents<sg_type>::num_components;
643     const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
644     if (!valProp)
645         return;
646     if (valProp->nChildren() == 0) { // Has <use>?
647         setter(obj, Bridge<OSGParamType>::get(valProp->getValue<sg_type>()));
648     } else {
649         std::vector<std::string> paramNames
650             = getVectorProperties(valProp, options,numComponents, nameItr);
651         if (paramNames.empty())
652             throw BuilderException();
653         std::vector<std::string>::const_iterator pitr = paramNames.begin();
654         Effect::Updater* listener
655             =  new_EEPropListener<sg_type>(make_OSGFunctor<OSGParamType>
656                                            (obj, setter),
657                                            0, pitr, pitr + numComponents);
658         effect->addUpdater(listener);
659     }
660 }
661
662 template<typename OSGParamType, typename ObjType, typename NameItrType,
663          typename SetterReturn>
664 inline void
665 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
666                    SetterReturn (ObjType::*setter)(const OSGParamType&),
667                    NameItrType nameItr, const SGReaderWriterXMLOptions* options)
668 {
669     initFromParameters<OSGParamType>(effect, prop, obj,
670                                      boost::bind(setter, _1, _2), nameItr,
671                                      options);
672 }
673 extern const char* colorFields[];
674 }
675 }
676 #endif