1 // Copyright (C) 2009 Tim Moore timoore@redhat.com
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.
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.
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.
17 #ifndef SIMGEAR_EFFECTBUILDER_HXX
18 #define SIMGEAR_EFFECTBUILDER_HXX 1
27 #include <osgDB/Registry>
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>
34 #include <simgear/math/SGMath.hxx>
35 #include <simgear/props/AtomicChangeListener.hxx>
36 #include <simgear/props/props.hxx>
37 #include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
38 #include <simgear/structure/exception.hxx>
39 #include <simgear/structure/SGSharedPtr.hxx>
40 #include <simgear/structure/Singleton.hxx>
44 * Support classes for parsing effects.
51 class SGReaderWriterXMLOptions;
54 * Builder that returns an object, probably an OSG object.
57 class EffectBuilder : public SGReferenced
60 virtual ~EffectBuilder() {}
61 virtual T* build(Effect* effect, const SGPropertyNode*,
62 const SGReaderWriterXMLOptions* options) = 0;
63 static T* buildFromType(Effect* effect, const std::string& type,
64 const SGPropertyNode*props,
65 const SGReaderWriterXMLOptions* options)
67 BuilderMap& builderMap = getMap();
68 typename BuilderMap::iterator iter = builderMap.find(type);
69 if (iter != builderMap.end())
70 return iter->second->build(effect, props, options);
75 friend struct Registrar;
78 Registrar(const std::string& type, EffectBuilder* builder)
80 getMap().insert(std::make_pair(type, builder));
84 typedef std::map<std::string, SGSharedPtr<EffectBuilder> > BuilderMap;
85 struct BuilderMapSingleton : public simgear::Singleton<BuilderMapSingleton>
89 static BuilderMap& getMap()
91 return BuilderMapSingleton::instance()->_map;
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.
98 // two-way map for building StateSets from property descriptions, and
99 // vice versa. Mostly copied from the boost documentation.
103 using boost::multi_index_container;
104 using namespace boost::multi_index;
106 // tags for accessing both sides of a bidirectional map
111 template <typename T>
112 struct EffectNameValue
114 // Don't use std::pair because we want to use aggregate intialization.
119 // The class template bidirectional_map wraps the specification
120 // of a bidirectional map based on multi_index_container.
122 template<typename FromType,typename ToType>
123 struct bidirectional_map
129 value_type(FromType f, ToType s) : first(f),second(s){}
132 typedef std::pair<FromType,ToType> value_type;
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.
139 typedef multi_index_container<
143 tag<from>, member<value_type, FromType, &value_type::first> >,
145 tag<to>, member<value_type, ToType, &value_type::second> >
151 struct EffectPropertyMap
153 typedef typename bidirectional_map<std::string, T>::type BMap;
156 EffectPropertyMap(const EffectNameValue<T> (&attrs)[N]);
161 EffectPropertyMap<T>::EffectPropertyMap(const EffectNameValue<T> (&attrs)[N])
163 for (int i = 0; i < N; ++i)
164 _map.insert(typename BMap::value_type(attrs[i].first, attrs[i].second));
167 // A one-way map that can be initialized using an array
169 struct SimplePropertyMap
171 typedef std::map<string, T> map_type;
174 SimplePropertyMap(const EffectNameValue<T> (&attrs)[N])
176 for (int i = 0; i < N; ++i)
177 _map.insert(typename map_type::value_type(attrs[i].first,
182 class BuilderException : public sg_exception
186 BuilderException(const char* message, const char* origin = 0);
187 BuilderException(const std::string& message, const std::string& = "");
188 virtual ~BuilderException() throw();
193 void findAttr(const effect::EffectPropertyMap<T>& pMap,
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 ")
204 result = itr->second;
209 inline void findAttr(const effect::EffectPropertyMap<T>& pMap,
210 const std::string& name,
213 findAttr(pMap, name.c_str(), result);
217 void findAttr(const effect::EffectPropertyMap<T>& pMap,
218 const SGPropertyNode* prop,
222 throw effect::BuilderException("findAttr: empty property");
223 const char* name = prop->getStringValue();
225 throw effect::BuilderException("findAttr: no name for lookup");
226 findAttr(pMap, name, result);
229 // Versions that don't throw an error
232 const T* findAttr(const effect::EffectPropertyMap<T>& pMap,
235 using namespace effect;
236 typename EffectPropertyMap<T>::BMap::iterator itr
237 = pMap._map.get<from>().find(name);
238 if (itr == pMap._map.end())
245 const T* findAttr(const effect::SimplePropertyMap<T>& pMap,
248 using namespace effect;
249 typename SimplePropertyMap<T>::map_type::const_iterator itr
250 = pMap._map.find(name);
251 if (itr == pMap._map.end())
257 template<typename T, template<class> class Map>
258 const T* findAttr(const Map<T>& pMap,
259 const std::string& name)
261 return findAttr(pMap, name.c_str());
266 std::string findName(const effect::EffectPropertyMap<T>& pMap, T value)
268 using namespace effect;
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())
278 std::string findName(const effect::EffectPropertyMap<T>& pMap, GLenum value)
280 return findName(pMap, static_cast<T>(value));
284 * Given a property node from a pass, get its value either from it or
285 * from the effect parameters.
288 const SGPropertyNode* getEffectPropertyNode(Effect* effect,
289 const SGPropertyNode* prop);
291 * Get a named child property from pass parameters or effect
294 const SGPropertyNode* getEffectPropertyChild(Effect* effect,
295 const SGPropertyNode* prop,
299 * Get the name of a node mentioned in a <use> clause from the global property
301 * @return empty if prop doesn't contain a <use> clause; otherwise the
302 * mentioned node name.
304 std::string getGlobalProperty(const SGPropertyNode* prop,
305 const SGReaderWriterXMLOptions *);
307 template<typename NameItr>
308 std::vector<std::string>
309 getVectorProperties(const SGPropertyNode* prop,
310 const SGReaderWriterXMLOptions *options, size_t vecSize,
311 NameItr defaultNames)
314 vector<string> 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->getPropRoot()->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();
330 for (PropertyList::const_iterator itr = useProps.begin(),
331 end = useProps.end();
334 string childName = (*itr)->getStringValue();
335 if (childName.size() == 0 || childName[0] != '/')
336 result.push_back(parentName + childName);
338 result.push_back(childName);
344 class PassAttributeBuilder : public SGReferenced
347 typedef std::map<const std::string, SGSharedPtr<PassAttributeBuilder> >
350 struct PassAttrMapSingleton : public simgear::Singleton<PassAttrMapSingleton>
352 PassAttrMap passAttrMap;
355 virtual void buildAttribute(Effect* effect, Pass* pass,
356 const SGPropertyNode* prop,
357 const SGReaderWriterXMLOptions* options)
359 static PassAttributeBuilder* find(const std::string& str)
361 PassAttrMap::iterator itr
362 = PassAttrMapSingleton::instance()->passAttrMap.find(str);
363 if (itr == PassAttrMapSingleton::instance()->passAttrMap.end())
366 return itr->second.ptr();
368 template<typename T> friend struct InstallAttributeBuilder;
372 struct InstallAttributeBuilder
374 InstallAttributeBuilder(const string& name)
376 PassAttributeBuilder::PassAttrMapSingleton::instance()
377 ->passAttrMap.insert(make_pair(name, new T));
381 // The description of an attribute may exist in a pass' XML, but a
382 // derived effect might want to disable the attribute altogether. So,
383 // some attributes have an "active" property; if it exists and is
384 // false, the OSG attribute is not built at all. This is different
385 // from any OSG mode settings that might be around.
386 bool isAttributeActive(Effect* effect, const SGPropertyNode* prop);
391 * Bridge between types stored in properties and what OSG or the
394 template<typename T> struct Bridge;
397 * Default just passes on the same type.
404 static T get(const T& val) { return val; }
408 struct Bridge<const T> : public Bridge<T>
412 // Save some typing...
413 template<typename InType, typename OutType>
416 typedef InType sg_type;
417 static OutType get(const InType& val) { return toOsg(val); }
420 struct Bridge<osg::Vec3f> : public BridgeOSGVec<SGVec3d, osg::Vec3f>
425 struct Bridge<osg::Vec3d> : public BridgeOSGVec<SGVec3d, osg::Vec3d>
430 struct Bridge<osg::Vec4f> : public BridgeOSGVec<SGVec4d, osg::Vec4f>
435 struct Bridge<osg::Vec4d> : public BridgeOSGVec<SGVec4d, osg::Vec4d>
440 * Functor for calling a function on an osg::Referenced object and a
441 * value (e.g., an SGVec4d from a property) which will be converted to
442 * the equivalent OSG type.
444 * General version, function takes obj, val
446 template<typename OSGParam, typename Obj, typename Func>
447 struct OSGFunctor : public Bridge<OSGParam>
449 OSGFunctor(Obj* obj, const Func& func)
450 : _obj(obj), _func(func) {}
451 void operator()(const typename Bridge<OSGParam>::sg_type& val) const
453 _func(_obj, this->get(val));
455 osg::ref_ptr<Obj>_obj;
460 * Version which uses a pointer to member function instead.
462 template<typename OSGParam, typename Obj>
463 struct OSGFunctor<OSGParam, Obj, void (Obj::* const)(const OSGParam&)>
464 : public Bridge<OSGParam>
466 typedef void (Obj::*const MemFunc)(const OSGParam&);
467 OSGFunctor(Obj* obj, MemFunc func)
468 : _obj(obj), _func(func) {}
469 void operator()(const typename Bridge<OSGParam>::sg_type& val) const
471 (_obj->*_func)(this->get(val));
473 osg::ref_ptr<Obj>_obj;
478 * Typical convenience constructors
480 template<typename OSGParam, typename Obj, typename Func>
481 OSGFunctor<OSGParam, Obj, Func> make_OSGFunctor(Obj* obj, const Func& func)
483 return OSGFunctor<OSGParam, Obj, Func>(obj, func);
486 template<typename OSGParam, typename Obj>
487 OSGFunctor<OSGParam, Obj, void (Obj::*const)(const OSGParam&)>
488 make_OSGFunctor(Obj* obj, void (Obj::*const func)(const OSGParam&))
490 return OSGFunctor<OSGParam, Obj,
491 void (Obj::* const)(const OSGParam&)>(obj, func);
494 template<typename OSGParamType, typename ObjType, typename F>
495 class ScalarChangeListener
496 : public SGPropertyChangeListener, public InitializeWhenAdded,
497 public Effect::Updater
500 ScalarChangeListener(ObjType* obj, const F& setter,
501 const std::string& propName)
502 : _obj(obj), _setter(setter)
504 _propName = new std::string(propName);
506 virtual ~ScalarChangeListener()
511 void valueChanged(SGPropertyNode* node)
513 _setter(_obj.get(), node->getValue<OSGParamType>());
515 void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
517 SGPropertyNode* listenProp = makeNode(propRoot, *_propName);
521 listenProp->addChangeListener(this, true);
524 osg::ref_ptr<ObjType> _obj;
526 std::string* _propName;
529 template<typename T, typename Func>
530 class EffectExtendedPropListener : public InitializeWhenAdded,
531 public Effect::Updater
534 template<typename Itr>
535 EffectExtendedPropListener(const Func& func,
536 const std::string* propName, Itr childNamesBegin,
538 : _propName(0), _func(func)
541 _propName = new std::string(*propName);
542 _childNames = new std::vector<std::string>(childNamesBegin,
545 virtual ~EffectExtendedPropListener()
550 void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
552 SGPropertyNode* parent = 0;
554 parent = propRoot->getNode(*_propName, true);
558 = new ExtendedPropListener<T, Func>(parent, _childNames->begin(),
567 std::string* _propName;
568 std::vector<std::string>* _childNames;
569 SGSharedPtr<ExtendedPropListener<T, Func> > _propListener;
573 template<typename T, typename Func, typename Itr>
575 new_EEPropListener(const Func& func, const std::string* propName,
576 const Itr& namesBegin, const Itr& namesEnd)
578 return new EffectExtendedPropListener<T, Func>
579 (func, 0, namesBegin, namesEnd);
583 * Set DYNAMIC data variance on an osg::Object.
586 inline void setDynamicVariance(void* obj)
590 inline void setDynamicVariance(osg::Object* obj)
592 obj->setDataVariance(osg::Object::DYNAMIC);
596 * Initialize the value and the possible updating of an effect
597 * attribute. If the value is specified directly, set it. Otherwise,
598 * use the <use> tag to look at the parameters. Again, if there is a
599 * value there set it directly. Otherwise, the parameter contains its
600 * own <use> tag referring to a property in the global property tree;
601 * install a change listener that will set the attribute when the
604 * For relative property names, the property root found in options is
607 template<typename OSGParamType, typename ObjType, typename F>
609 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
610 const F& setter, const SGReaderWriterXMLOptions* options)
612 const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
615 setDynamicVariance(obj);
616 if (valProp->nChildren() == 0) {
617 setter(obj, valProp->getValue<OSGParamType>());
619 std::string propName = getGlobalProperty(valProp, options);
620 ScalarChangeListener<OSGParamType, ObjType, F>* listener
621 = new ScalarChangeListener<OSGParamType, ObjType, F>(obj, setter,
623 effect->addUpdater(listener);
627 template<typename OSGParamType, typename ObjType, typename SetterReturn>
629 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
630 SetterReturn (ObjType::*setter)(const OSGParamType),
631 const SGReaderWriterXMLOptions* options)
633 initFromParameters<OSGParamType>(effect, prop, obj,
634 boost::bind(setter, _1, _2), options);
638 * Initialize vector parameters from individual properties.
639 * The parameter may be updated at runtime.
641 * If the value is specified directly, set it. Otherwise, use the
642 * <use> tag to look at the parameters. Again, if there is a value
643 * there set it directly. Otherwise, the parameter contains one or several
644 * <use> tags. If there is one tag, it is a property that is the root
645 * for the values needed to update the parameter; nameIter holds the
646 * names of the properties relative to the root. If there are several
647 * <use> tags, they each hold the name of the property holding the
648 * value for the corresponding vector member.
650 * Install a change listener that will set the attribute when the
653 * For relative property names, the property root found in options is
656 template<typename OSGParamType, typename ObjType, typename NameItrType,
659 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
661 NameItrType nameItr, const SGReaderWriterXMLOptions* options)
663 typedef typename Bridge<OSGParamType>::sg_type sg_type;
664 const int numComponents = props::NumComponents<sg_type>::num_components;
665 const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
668 setDynamicVariance(obj);
669 if (valProp->nChildren() == 0) { // Has <use>?
670 setter(obj, Bridge<OSGParamType>::get(valProp->getValue<sg_type>()));
672 std::vector<std::string> paramNames
673 = getVectorProperties(valProp, options,numComponents, nameItr);
674 if (paramNames.empty())
675 throw BuilderException();
676 std::vector<std::string>::const_iterator pitr = paramNames.begin();
677 Effect::Updater* listener
678 = new_EEPropListener<sg_type>(make_OSGFunctor<OSGParamType>
680 0, pitr, pitr + numComponents);
681 effect->addUpdater(listener);
685 template<typename OSGParamType, typename ObjType, typename NameItrType,
686 typename SetterReturn>
688 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
689 SetterReturn (ObjType::*setter)(const OSGParamType&),
690 NameItrType nameItr, const SGReaderWriterXMLOptions* options)
692 initFromParameters<OSGParamType>(effect, prop, obj,
693 boost::bind(setter, _1, _2), nameItr,
696 extern const char* colorFields[];