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/structure/exception.hxx>
37 #include <simgear/structure/SGSharedPtr.hxx>
38 #include <simgear/structure/Singleton.hxx>
42 * Support classes for parsing effects.
51 * Builder that returns an object, probably an OSG object.
54 class EffectBuilder : public SGReferenced
57 virtual ~EffectBuilder() {}
58 virtual T* build(Effect* effect, const SGPropertyNode*,
59 const osgDB::ReaderWriter::Options* options) = 0;
60 static T* buildFromType(Effect* effect, const std::string& type,
61 const SGPropertyNode*props,
62 const osgDB::ReaderWriter::Options* options)
64 BuilderMap& builderMap = getMap();
65 typename BuilderMap::iterator iter = builderMap.find(type);
66 if (iter != builderMap.end())
67 return iter->second->build(effect, props, options);
72 friend struct Registrar;
75 Registrar(const std::string& type, EffectBuilder* builder)
77 getMap().insert(std::make_pair(type, builder));
81 typedef std::map<std::string, SGSharedPtr<EffectBuilder> > BuilderMap;
82 struct BuilderMapSingleton : public simgear::Singleton<BuilderMapSingleton>
86 static BuilderMap& getMap()
88 return BuilderMapSingleton::instance()->_map;
92 // Tables of strings and constants. We want to reconstruct the effect
93 // property tree from OSG state sets, so the tables should be bi-directional.
95 // two-way map for building StateSets from property descriptions, and
96 // vice versa. Mostly copied from the boost documentation.
100 using boost::multi_index_container;
101 using namespace boost::multi_index;
103 // tags for accessing both sides of a bidirectional map
108 template <typename T>
109 struct EffectNameValue
111 // Don't use std::pair because we want to use aggregate intialization.
116 // The class template bidirectional_map wraps the specification
117 // of a bidirectional map based on multi_index_container.
119 template<typename FromType,typename ToType>
120 struct bidirectional_map
122 typedef std::pair<FromType,ToType> value_type;
124 /* A bidirectional map can be simulated as a multi_index_container
125 * of pairs of (FromType,ToType) with two unique indices, one
126 * for each member of the pair.
129 typedef multi_index_container<
133 tag<from>, member<value_type, FromType, &value_type::first> >,
135 tag<to>, member<value_type, ToType, &value_type::second> >
141 struct EffectPropertyMap
143 typedef typename bidirectional_map<std::string, T>::type BMap;
146 EffectPropertyMap(const EffectNameValue<T> (&attrs)[N]);
151 EffectPropertyMap<T>::EffectPropertyMap(const EffectNameValue<T> (&attrs)[N])
153 for (int i = 0; i < N; ++i)
154 _map.insert(typename BMap::value_type(attrs[i].first, attrs[i].second));
160 bool findAttr(const effect::EffectPropertyMap<T>& pMap,
164 using namespace effect;
165 typename EffectPropertyMap<T>::BMap::iterator itr
166 = pMap._map.get<from>().find(name);
167 if (itr == pMap._map.end()) {
170 result = itr->second;
176 inline bool findAttr(const effect::EffectPropertyMap<T>& pMap,
177 const std::string& name,
180 return findAttr(pMap, name.c_str(), result);
184 bool findAttr(const effect::EffectPropertyMap<T>& pMap,
185 const SGPropertyNode* prop,
190 const char* name = prop->getStringValue();
193 return findAttr(pMap, name, result);
197 std::string findName(const effect::EffectPropertyMap<T>& pMap, T value)
199 using namespace effect;
201 typename EffectPropertyMap<T>::BMap::template index_iterator<to>::type itr
202 = pMap._map.get<to>().find(value);
203 if (itr != pMap._map.get<to>().end())
209 std::string findName(const effect::EffectPropertyMap<T>& pMap, GLenum value)
211 return findName(pMap, static_cast<T>(value));
215 * Given a property node from a pass, get its value either from it or
216 * from the effect parameters.
219 const SGPropertyNode* getEffectPropertyNode(Effect* effect,
220 const SGPropertyNode* prop);
222 * Get a named child property from pass parameters or effect
225 const SGPropertyNode* getEffectPropertyChild(Effect* effect,
226 const SGPropertyNode* prop,
230 * Get the name of a node mentioned in a <use> clause from the global property
232 * @return empty if prop doesn't contain a <use> clause; otherwise the
233 * mentioned node name.
235 std::string getGlobalProperty(const SGPropertyNode* prop);
237 class BuilderException : public sg_exception
241 BuilderException(const char* message, const char* origin = 0);
242 BuilderException(const std::string& message, const std::string& = "");
243 virtual ~BuilderException() throw();
246 class PassAttributeBuilder : public SGReferenced
249 typedef std::map<const std::string, SGSharedPtr<PassAttributeBuilder> >
252 struct PassAttrMapSingleton : public simgear::Singleton<PassAttrMapSingleton>
254 PassAttrMap passAttrMap;
257 virtual void buildAttribute(Effect* effect, Pass* pass,
258 const SGPropertyNode* prop,
259 const osgDB::ReaderWriter::Options* options)
261 static PassAttributeBuilder* find(const std::string& str)
263 PassAttrMap::iterator itr
264 = PassAttrMapSingleton::instance()->passAttrMap.find(str);
265 if (itr == PassAttrMapSingleton::instance()->passAttrMap.end())
268 return itr->second.ptr();
270 template<typename T> friend class InstallAttributeBuilder;
274 struct InstallAttributeBuilder
276 InstallAttributeBuilder(const string& name)
278 PassAttributeBuilder::PassAttrMapSingleton::instance()
279 ->passAttrMap.insert(make_pair(name, new T));
283 // The description of an attribute may exist in a pass' XML, but a
284 // derived effect might want to disable the attribute altogether. So,
285 // some attributes have an "active" property; if it exists and is
286 // false, the OSG attribute is not built at all. This is different
287 // from any OSG mode settings that might be around.
288 bool isAttributeActive(Effect* effect, const SGPropertyNode* prop);
293 * Bridge between types stored in properties and what OSG wants.
295 template<typename T> struct OSGBridge;
298 struct OSGBridge<const T> : public OSGBridge<T>
303 struct OSGBridge<osg::Vec3f>
305 typedef SGVec3d sg_type;
306 static osg::Vec3f getOsgType(const SGVec3d& val) { return toOsg(val); }
310 struct OSGBridge<osg::Vec3d>
312 typedef SGVec3d sg_type;
313 static osg::Vec3d getOsgType(const SGVec3d& val) { return toOsg(val); }
317 struct OSGBridge<osg::Vec4f>
319 typedef SGVec4d sg_type;
320 static osg::Vec4f getOsgType(const SGVec4d& val) { return toOsg(val); }
324 struct OSGBridge<osg::Vec4d>
326 typedef SGVec4d sg_type;
327 static osg::Vec4d getOsgType(const SGVec4d& val) { return toOsg(val); }
330 template<typename Obj, typename OSGParam>
331 struct OSGFunctor : public OSGBridge<OSGParam>
333 OSGFunctor(Obj* obj, void (Obj::*func)(const OSGParam&))
334 : _obj(obj), _func(func) {}
335 void operator()(const typename OSGBridge<OSGParam>::sg_type& val) const
337 ((_obj.get())->*_func)(this->getOsgType(val));
339 osg::ref_ptr<Obj>_obj;
340 void (Obj::*_func)(const OSGParam&);
343 template<typename ObjType, typename OSGParamType>
344 class ScalarChangeListener
345 : public SGPropertyChangeListener, public InitializeWhenAdded,
346 public Effect::Updater
349 typedef void (ObjType::*setter_type)(const OSGParamType);
350 ScalarChangeListener(ObjType* obj, setter_type setter,
351 const std::string& propName)
352 : _obj(obj), _setter(setter)
354 _propName = new std::string(propName);
356 virtual ~ScalarChangeListener()
361 void valueChanged(SGPropertyNode* node)
363 _obj->*setter(node->getValue<OSGParamType>());
365 void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
367 SGPropertyNode* listenProp = makeNode(propRoot, *_propName);
371 listenProp->addChangeListener(this, true);
374 osg::ref_ptr<ObjType> _obj;
376 std::string* _propName;
379 template<typename T, typename Func>
380 class EffectExtendedPropListener : public InitializeWhenAdded,
381 public Effect::Updater
384 template<typename Itr>
385 EffectExtendedPropListener(const Func& func,
386 const std::string& propName, Itr childNamesBegin,
390 _propName = new std::string(propName);
391 _childNames = new std::vector<std::string>(childNamesBegin,
394 virtual ~EffectExtendedPropListener()
399 void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
401 SGPropertyNode* parent = propRoot->getNode(*_propName, true);
403 = new ExtendedPropListener<T, Func>(parent, _childNames->begin(),
412 std::string* _propName;
413 std::vector<std::string>* _childNames;
414 SGSharedPtr<ExtendedPropListener<T, Func> > _propListener;
419 * Initialize the value and the possible updating of an effect
420 * attribute. If the value is specified directly, set it. Otherwise,
421 * use the <use> tag to look at the parameters. Again, if there is a
422 * value there set it directly. Otherwise, the parameter contains its
423 * own <use> tag referring to a property in the global property tree;
424 * install a change listener that will set the attribute when the
427 template<typename ObjType, typename OSGParamType>
429 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
430 void (ObjType::*setter)(const OSGParamType))
432 const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
435 if (valProp->nChildren() == 0) {
436 obj->*setter(valProp->getValue<OSGParamType>());
438 std::string propName = getGlobalProperty(prop);
439 ScalarChangeListener<ObjType, OSGParamType>* listener
440 = new ScalarChangeListener<ObjType, OSGParamType>(obj, setter,
442 effect->addUpdater(listener);
446 template<typename ObjType, typename OSGParamType, typename NameItrType>
448 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
449 void (ObjType::*setter)(const OSGParamType&),
452 typedef typename OSGBridge<OSGParamType>::sg_type sg_type;
453 const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
456 if (valProp->nChildren() == 0) {
457 (obj->*setter)(OSGBridge<OSGParamType>
458 ::getOsgType(valProp->getValue<sg_type>()));
460 string listenPropName = getGlobalProperty(valProp);
461 if (listenPropName.empty())
463 typedef OSGFunctor<ObjType, OSGParamType> Functor;
464 Effect::Updater* listener
465 = new EffectExtendedPropListener<sg_type, Functor>
466 (Functor(obj, setter), listenPropName, nameItr,
467 nameItr + props::NumComponents<sg_type>::num_components);
468 effect->addUpdater(listener);
472 extern const char* colorFields[];