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