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
26 #include <osgDB/Registry>
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>
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>
43 * Support classes for parsing effects.
50 class SGReaderWriterXMLOptions;
53 * Builder that returns an object, probably an OSG object.
56 class EffectBuilder : public SGReferenced
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)
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);
74 friend struct Registrar;
77 Registrar(const std::string& type, EffectBuilder* builder)
79 getMap().insert(std::make_pair(type, builder));
83 typedef std::map<std::string, SGSharedPtr<EffectBuilder> > BuilderMap;
84 struct BuilderMapSingleton : public simgear::Singleton<BuilderMapSingleton>
88 static BuilderMap& getMap()
90 return BuilderMapSingleton::instance()->_map;
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.
97 // two-way map for building StateSets from property descriptions, and
98 // vice versa. Mostly copied from the boost documentation.
102 using boost::multi_index_container;
103 using namespace boost::multi_index;
105 // tags for accessing both sides of a bidirectional map
110 template <typename T>
111 struct EffectNameValue
113 // Don't use std::pair because we want to use aggregate intialization.
118 // The class template bidirectional_map wraps the specification
119 // of a bidirectional map based on multi_index_container.
121 template<typename FromType,typename ToType>
122 struct bidirectional_map
124 typedef std::pair<FromType,ToType> value_type;
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.
131 typedef multi_index_container<
135 tag<from>, member<value_type, FromType, &value_type::first> >,
137 tag<to>, member<value_type, ToType, &value_type::second> >
143 struct EffectPropertyMap
145 typedef typename bidirectional_map<std::string, T>::type BMap;
148 EffectPropertyMap(const EffectNameValue<T> (&attrs)[N]);
153 EffectPropertyMap<T>::EffectPropertyMap(const EffectNameValue<T> (&attrs)[N])
155 for (int i = 0; i < N; ++i)
156 _map.insert(typename BMap::value_type(attrs[i].first, attrs[i].second));
159 class BuilderException : public sg_exception
163 BuilderException(const char* message, const char* origin = 0);
164 BuilderException(const std::string& message, const std::string& = "");
165 virtual ~BuilderException() throw();
170 void findAttr(const effect::EffectPropertyMap<T>& pMap,
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 ")
181 result = itr->second;
186 inline void findAttr(const effect::EffectPropertyMap<T>& pMap,
187 const std::string& name,
190 findAttr(pMap, name.c_str(), result);
194 void findAttr(const effect::EffectPropertyMap<T>& pMap,
195 const SGPropertyNode* prop,
199 throw effect::BuilderException("findAttr: empty property");
200 const char* name = prop->getStringValue();
202 throw effect::BuilderException("findAttr: no name for lookup");
203 findAttr(pMap, name, result);
207 std::string findName(const effect::EffectPropertyMap<T>& pMap, T value)
209 using namespace effect;
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())
219 std::string findName(const effect::EffectPropertyMap<T>& pMap, GLenum value)
221 return findName(pMap, static_cast<T>(value));
225 * Given a property node from a pass, get its value either from it or
226 * from the effect parameters.
229 const SGPropertyNode* getEffectPropertyNode(Effect* effect,
230 const SGPropertyNode* prop);
232 * Get a named child property from pass parameters or effect
235 const SGPropertyNode* getEffectPropertyChild(Effect* effect,
236 const SGPropertyNode* prop,
240 * Get the name of a node mentioned in a <use> clause from the global property
242 * @return empty if prop doesn't contain a <use> clause; otherwise the
243 * mentioned node name.
245 std::string getGlobalProperty(const SGPropertyNode* prop,
246 const SGReaderWriterXMLOptions *);
248 template<typename NameItr>
249 std::vector<std::string>
250 getVectorProperties(const SGPropertyNode* prop,
251 const SGReaderWriterXMLOptions *options, size_t vecSize,
252 NameItr defaultNames)
255 vector<string> 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();
271 for (PropertyList::const_iterator itr = useProps.begin(),
272 end = useProps.end();
275 string childName = (*itr)->getStringValue();
276 if (childName.size() == 0 || childName[0] != '/')
277 result.push_back(parentName + childName);
279 result.push_back(childName);
285 class PassAttributeBuilder : public SGReferenced
288 typedef std::map<const std::string, SGSharedPtr<PassAttributeBuilder> >
291 struct PassAttrMapSingleton : public simgear::Singleton<PassAttrMapSingleton>
293 PassAttrMap passAttrMap;
296 virtual void buildAttribute(Effect* effect, Pass* pass,
297 const SGPropertyNode* prop,
298 const SGReaderWriterXMLOptions* options)
300 static PassAttributeBuilder* find(const std::string& str)
302 PassAttrMap::iterator itr
303 = PassAttrMapSingleton::instance()->passAttrMap.find(str);
304 if (itr == PassAttrMapSingleton::instance()->passAttrMap.end())
307 return itr->second.ptr();
309 template<typename T> friend struct InstallAttributeBuilder;
313 struct InstallAttributeBuilder
315 InstallAttributeBuilder(const string& name)
317 PassAttributeBuilder::PassAttrMapSingleton::instance()
318 ->passAttrMap.insert(make_pair(name, new T));
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);
332 * Bridge between types stored in properties and what OSG or the
335 template<typename T> struct Bridge;
338 * Default just passes on the same type.
345 static T get(const T& val) { return val; }
349 struct Bridge<const T> : public Bridge<T>
353 // Save some typing...
354 template<typename InType, typename OutType>
357 typedef InType sg_type;
358 static OutType get(const InType& val) { return toOsg(val); }
361 struct Bridge<osg::Vec3f> : public BridgeOSGVec<SGVec3d, osg::Vec3f>
366 struct Bridge<osg::Vec3d> : public BridgeOSGVec<SGVec3d, osg::Vec3d>
371 struct Bridge<osg::Vec4f> : public BridgeOSGVec<SGVec4d, osg::Vec4f>
376 struct Bridge<osg::Vec4d> : public BridgeOSGVec<SGVec4d, osg::Vec4d>
381 * Functor for calling a function on an osg::Referenced object and a
382 * value (e.g., an SGVec4d from a property) which will be converted to
383 * the equivalent OSG type.
385 * General version, function takes obj, val
387 template<typename Obj, typename OSGParam, typename Func>
388 struct OSGFunctor : public Bridge<OSGParam>
390 OSGFunctor(Obj* obj, const Func& func)
391 : _obj(obj), _func(func) {}
392 void operator()(const typename Bridge<OSGParam>::sg_type& val) const
394 _func(_obj, this->get(val));
396 osg::ref_ptr<Obj>_obj;
401 * Version which uses a pointer to member function instead.
403 template<typename Obj, typename OSGParam>
404 struct OSGFunctor<Obj, OSGParam, void (Obj::* const)(const OSGParam&)>
405 : public Bridge<OSGParam>
407 typedef void (Obj::*const MemFunc)(const OSGParam&);
408 OSGFunctor(Obj* obj, MemFunc func)
409 : _obj(obj), _func(func) {}
410 void operator()(const typename Bridge<OSGParam>::sg_type& val) const
412 (_obj->*_func)(this->get(val));
414 osg::ref_ptr<Obj>_obj;
419 * Typical convenience constructors
421 template<typename Obj, typename OSGParam, typename Func>
422 OSGFunctor<Obj, OSGParam, Func> make_OSGFunctor(Obj* obj, const Func& func)
424 return OSGFunctor<Obj, OSGParam, Func>(obj, func);
427 template<typename Obj, typename OSGParam>
428 OSGFunctor<Obj, OSGParam, void (Obj::*const)(const OSGParam&)>
429 make_OSGFunctor(Obj* obj, void (Obj::*const func)(const OSGParam&))
431 return OSGFunctor<Obj, OSGParam,
432 void (Obj::* const)(const OSGParam&)>(obj, func);
435 template<typename ObjType, typename OSGParamType>
436 class ScalarChangeListener
437 : public SGPropertyChangeListener, public InitializeWhenAdded,
438 public Effect::Updater
441 typedef void (ObjType::*setter_type)(const OSGParamType);
442 ScalarChangeListener(ObjType* obj, setter_type setter,
443 const std::string& propName)
444 : _obj(obj), _setter(setter)
446 _propName = new std::string(propName);
448 virtual ~ScalarChangeListener()
453 void valueChanged(SGPropertyNode* node)
455 _obj->*setter(node->getValue<OSGParamType>());
457 void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
459 SGPropertyNode* listenProp = makeNode(propRoot, *_propName);
463 listenProp->addChangeListener(this, true);
466 osg::ref_ptr<ObjType> _obj;
468 std::string* _propName;
471 template<typename T, typename Func>
472 class EffectExtendedPropListener : public InitializeWhenAdded,
473 public Effect::Updater
476 template<typename Itr>
477 EffectExtendedPropListener(const Func& func,
478 const std::string* propName, Itr childNamesBegin,
480 : _propName(0), _func(func)
483 _propName = new std::string(*propName);
484 _childNames = new std::vector<std::string>(childNamesBegin,
487 virtual ~EffectExtendedPropListener()
492 void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
494 SGPropertyNode* parent = 0;
496 parent = propRoot->getNode(*_propName, true);
500 = new ExtendedPropListener<T, Func>(parent, _childNames->begin(),
509 std::string* _propName;
510 std::vector<std::string>* _childNames;
511 SGSharedPtr<ExtendedPropListener<T, Func> > _propListener;
515 template<typename T, typename Func, typename Itr>
517 new_EEPropListener(const Func& func, const std::string* propName,
518 const Itr& namesBegin, const Itr& namesEnd)
520 return new EffectExtendedPropListener<T, Func>
521 (func, 0, namesBegin, namesEnd);
525 * Initialize the value and the possible updating of an effect
526 * attribute. If the value is specified directly, set it. Otherwise,
527 * use the <use> tag to look at the parameters. Again, if there is a
528 * value there set it directly. Otherwise, the parameter contains its
529 * own <use> tag referring to a property in the global property tree;
530 * install a change listener that will set the attribute when the
533 * For relative property names, the property root found in options is
536 template<typename ObjType, typename OSGParamType>
538 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
539 void (ObjType::*setter)(const OSGParamType),
540 const SGReaderWriterXMLOptions* options)
542 const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
545 if (valProp->nChildren() == 0) {
546 obj->*setter(valProp->getValue<OSGParamType>());
548 std::string propName = getGlobalProperty(prop, options);
549 ScalarChangeListener<ObjType, OSGParamType>* listener
550 = new ScalarChangeListener<ObjType, OSGParamType>(obj, setter,
552 effect->addUpdater(listener);
557 * Initialize vector parameters from individual properties.
558 * The parameter may be updated at runtime.
560 * If the value is specified directly, set it. Otherwise, use the
561 * <use> tag to look at the parameters. Again, if there is a value
562 * there set it directly. Otherwise, the parameter contains one or several
563 * <use> tags. If there is one tag, it is a property that is the root
564 * for the values needed to update the parameter; nameIter holds the
565 * names of the properties relative to the root. If there are several
566 * <use> tags, they each hold the name of the property holding the
567 * value for the corresponding vector member.
569 * Install a change listener that will set the attribute when the
572 * For relative property names, the property root found in options is
575 template<typename ObjType, typename OSGParamType, typename NameItrType>
577 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
578 void (ObjType::*setter)(const OSGParamType&),
579 NameItrType nameItr, const SGReaderWriterXMLOptions* options)
581 typedef typename Bridge<OSGParamType>::sg_type sg_type;
582 const int numComponents = props::NumComponents<sg_type>::num_components;
583 const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
586 if (valProp->nChildren() == 0) { // Has <use>?
587 (obj->*setter)(Bridge<OSGParamType>
588 ::get(valProp->getValue<sg_type>()));
590 std::vector<std::string> paramNames
591 = getVectorProperties(valProp, options,numComponents, nameItr);
592 if (paramNames.empty())
593 throw BuilderException();
594 std::vector<std::string>::const_iterator pitr = paramNames.begin();
595 Effect::Updater* listener
596 = new_EEPropListener<sg_type>(make_OSGFunctor(obj, setter), 0,
597 pitr, pitr + numComponents);
598 effect->addUpdater(listener);
602 extern const char* colorFields[];