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