]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/EffectBuilder.hxx
Move Texture unit builder into TexBuilder.cxx
[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 <map>
22 #include <string>
23 #include <cstring>
24
25 #include <osgDB/Registry>
26
27 #include <boost/multi_index_container.hpp>
28 #include <boost/multi_index/member.hpp>
29 #include <boost/multi_index/ordered_index.hpp>
30
31 #include <simgear/props/props.hxx>
32 #include <simgear/structure/exception.hxx>
33 #include <simgear/structure/SGSharedPtr.hxx>
34 #include <simgear/structure/Singleton.hxx>
35
36 /**
37  * Support classes for parsing effects.
38  */
39
40 namespace simgear
41 {
42 class Effect;
43 class Pass;
44
45 /**
46  * Builder that returns an object, probably an OSG object.
47  */
48 template<typename T>
49 class EffectBuilder : public SGReferenced
50 {
51 public:
52     virtual ~EffectBuilder() {}
53     virtual T* build(Effect* effect, const SGPropertyNode*,
54                      const osgDB::ReaderWriter::Options* options) = 0;
55     static T* buildFromType(Effect* effect, const std::string& type,
56                             const SGPropertyNode*props,
57                             const osgDB::ReaderWriter::Options* options)
58     {
59         BuilderMap& builderMap = getMap();
60         typename BuilderMap::iterator iter = builderMap.find(type);
61         if (iter != builderMap.end())
62             return iter->second->build(effect, props, options);
63         else
64             return 0;
65     }
66     struct Registrar;
67     friend struct Registrar;
68     struct Registrar
69     {
70         Registrar(const std::string& type, EffectBuilder* builder)
71         {
72             getMap().insert(std::make_pair(type, builder));
73         }
74     };
75 protected:
76     typedef std::map<std::string, SGSharedPtr<EffectBuilder> > BuilderMap;
77     struct BuilderMapSingleton : public simgear::Singleton<BuilderMapSingleton>
78     {
79         BuilderMap _map;
80     };
81     static BuilderMap& getMap()
82     {
83         return BuilderMapSingleton::instance()->_map;
84     }
85 };
86
87 // Tables of strings and constants. We want to reconstruct the effect
88 // property tree from OSG state sets, so the tables should be bi-directional.
89
90 // two-way map for building StateSets from property descriptions, and
91 // vice versa. Mostly copied from the boost documentation.
92
93 namespace effect
94 {
95 using boost::multi_index_container;
96 using namespace boost::multi_index;
97
98 // tags for accessing both sides of a bidirectional map
99
100 struct from{};
101 struct to{};
102
103 template <typename T>
104 struct EffectNameValue
105 {
106     // Don't use std::pair because we want to use aggregate intialization.
107     const char* first;
108     T second;
109 };
110
111 // The class template bidirectional_map wraps the specification
112 // of a bidirectional map based on multi_index_container.
113
114 template<typename FromType,typename ToType>
115 struct bidirectional_map
116 {
117     typedef std::pair<FromType,ToType> value_type;
118
119     /* A bidirectional map can be simulated as a multi_index_container
120      * of pairs of (FromType,ToType) with two unique indices, one
121      * for each member of the pair.
122      */
123
124     typedef multi_index_container<
125         value_type,
126         indexed_by<
127             ordered_unique<
128                 tag<from>, member<value_type, FromType, &value_type::first> >,
129             ordered_unique<
130                 tag<to>,  member<value_type, ToType, &value_type::second> >
131             >
132         > type;
133 };
134
135 template<typename T>
136 struct EffectPropertyMap
137 {
138     typedef typename bidirectional_map<std::string, T>::type BMap;
139     BMap _map;
140     template<int N>
141     EffectPropertyMap(const EffectNameValue<T> (&attrs)[N]);
142 };
143
144 template<typename T>
145 template<int N>
146 EffectPropertyMap<T>::EffectPropertyMap(const EffectNameValue<T> (&attrs)[N])
147 {
148     for (int i = 0; i < N; ++i)
149         _map.insert(typename BMap::value_type(attrs[i].first, attrs[i].second));
150 }
151
152 }
153
154 template<typename T>
155 bool findAttr(const effect::EffectPropertyMap<T>& pMap,
156               const SGPropertyNode* prop,
157               T& result)
158 {
159     using namespace effect;
160     if (!prop)
161         return false;
162     const char* name = prop->getStringValue();
163     if (!name)
164         return false;
165     typename EffectPropertyMap<T>::BMap::iterator itr
166         = pMap._map.get<from>().find(name);
167     if (itr == pMap._map.end()) {
168         return false;
169     } else {
170         result = itr->second;
171         return true;
172     }
173 }
174
175 template<typename T>
176 std::string findName(const effect::EffectPropertyMap<T>& pMap, T value)
177 {
178     using namespace effect;
179     std::string result;
180     typename EffectPropertyMap<T>::BMap::template index_iterator<to>::type itr
181         = pMap._map.get<to>().find(value);
182     if (itr != pMap._map.get<to>().end())
183         result = itr->first;
184     return result;
185 }
186
187 template<typename T>
188 std::string findName(const effect::EffectPropertyMap<T>& pMap, GLenum value)
189 {
190     return findName(pMap, static_cast<T>(value));
191 }
192
193 /**
194  * Given a property node from a pass, get its value either from it or
195  * from the effect parameters.
196  */
197
198 const SGPropertyNode* getEffectPropertyNode(Effect* effect,
199                                             const SGPropertyNode* prop);
200 /**
201  * Get a named child property from pass parameters or effect
202  * parameters.
203  */
204 const SGPropertyNode* getEffectPropertyChild(Effect* effect,
205                                              const SGPropertyNode* prop,
206                                              const char* name);
207
208 class BuilderException : public sg_exception
209 {
210 public:
211     BuilderException();
212     BuilderException(const char* message, const char* origin = 0);
213     BuilderException(const std::string& message, const std::string& = "");
214     virtual ~BuilderException() throw();
215 };
216
217 class PassAttributeBuilder : public SGReferenced
218 {
219 protected:
220     typedef std::map<const std::string, SGSharedPtr<PassAttributeBuilder> >
221     PassAttrMap;
222
223     struct PassAttrMapSingleton : public simgear::Singleton<PassAttrMapSingleton>
224     {
225         PassAttrMap passAttrMap;
226     };
227 public:
228     virtual void buildAttribute(Effect* effect, Pass* pass,
229                                 const SGPropertyNode* prop,
230                                 const osgDB::ReaderWriter::Options* options)
231     = 0;
232     static PassAttributeBuilder* find(const std::string& str)
233     {
234         PassAttrMap::iterator itr
235             = PassAttrMapSingleton::instance()->passAttrMap.find(str);
236         if (itr == PassAttrMapSingleton::instance()->passAttrMap.end())
237             return 0;
238         else
239             return itr->second.ptr();
240     }
241     template<typename T> friend class InstallAttributeBuilder;
242 };
243
244 template<typename T>
245 struct InstallAttributeBuilder
246 {
247     InstallAttributeBuilder(const string& name)
248     {
249         PassAttributeBuilder::PassAttrMapSingleton::instance()
250             ->passAttrMap.insert(make_pair(name, new T));
251     }
252 };
253
254 // The description of an attribute may exist in a pass' XML, but a
255 // derived effect might want to disable the attribute altogether. So,
256 // some attributes have an "active" property; if it exists and is
257 // false, the OSG attribute is not built at all. This is different
258 // from any OSG mode settings that might be around.
259 bool isAttributeActive(Effect* effect, const SGPropertyNode* prop);
260 }
261 #endif