]> git.mxchange.org Git - simgear.git/blob - simgear/structure/OSGUtils.hxx
Remove SVN sync code.
[simgear.git] / simgear / structure / OSGUtils.hxx
1 // OSGUtils.hxx - Useful templates for interfacing to Open Scene Graph
2 //
3 // Copyright (C) 2008  Tim Moore timoore@redhat.com
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the
17 // Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 // Boston, MA  02110-1301, USA.
19
20 #ifndef SIMGEAR_OSGUTILS_HXX
21 #define SIMGEAR_OSGUTILS_HXX 1
22
23 #include <osg/CopyOp>
24 #include <osg/StateAttribute>
25 #include <osg/StateSet>
26
27 namespace simgear
28 {
29 // RefPtrAdapter also appears in OpenSceneGraph's
30 // osgDB/DatabasePager.cpp. I wrote that code too. -- Tim Moore
31
32 // Convert function objects that take pointer args into functions that a
33 // reference to an osg::ref_ptr. This is quite useful for doing STL
34 // operations on lists of ref_ptr. This code assumes that a function
35 // with an argument const Foo* should be composed into a function of
36 // argument type ref_ptr<Foo>&, not ref_ptr<const Foo>&. Some support
37 // for that should be added to make this more general.
38 template <typename U>
39 struct PointerTraits
40 {
41     typedef class NullType {} PointeeType;
42 };
43
44 template <typename U>
45 struct PointerTraits<U*>
46 {
47     typedef U PointeeType;
48 };
49
50 template <typename U>
51 struct PointerTraits<const U*>
52 {
53     typedef U PointeeType;
54 };
55
56 template <typename FuncObj>
57 class RefPtrAdapter
58     : public std::unary_function<const osg::ref_ptr<typename PointerTraits<typename FuncObj::argument_type>::PointeeType>,
59                                  typename FuncObj::result_type>
60 {
61 public:
62     typedef typename PointerTraits<typename FuncObj::argument_type>::PointeeType PointeeType;
63     typedef osg::ref_ptr<PointeeType> RefPtrType;
64     explicit RefPtrAdapter(const FuncObj& funcObj) : _func(funcObj) {}
65     typename FuncObj::result_type operator()(const RefPtrType& refPtr) const
66     {
67         return _func(refPtr.get());
68     }
69 protected:
70         FuncObj _func;
71 };
72
73 template <typename FuncObj>
74 RefPtrAdapter<FuncObj> refPtrAdapt(const FuncObj& func)
75 {
76     return RefPtrAdapter<FuncObj>(func);
77 }
78 }
79 /** Typesafe wrapper around OSG's object clone function. Something
80  * very similar is in current OSG sources.
81  */
82 namespace osg
83 {
84 template <typename T> class ref_ptr;
85 class CopyOp;
86 }
87
88 namespace simgear
89 {
90 template <typename T>
91 T* clone(const T* object, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
92 {
93     return static_cast<T*>(object->clone(copyop));
94 }
95
96 template<typename T>
97 T* clone_ref(const osg::ref_ptr<T>& object,
98              const osg::CopyOp& copyop  = osg::CopyOp::SHALLOW_COPY)
99 {
100     return static_cast<T*>(object->clone(copyop));
101 }
102
103 }
104
105 namespace osg
106 {
107 class AlphaFunc;
108 class BlendColor;
109 class BlendEquation;
110 class BlendFunc;
111 class ClampColor;
112 class ColorMask;
113 class ColorMatrix;
114 class CullFace;
115 class Depth;
116 class Fog;
117 class FragmentProgram;
118 class FrontFace;
119 class LightModel;
120 class LineStipple;
121 class LineWidth;
122 class LogicOp;
123 class Material;
124 class Multisample;
125 class Point;
126 class PointSprite;
127 class PolygonMode;
128 class PolygonOffset;
129 class PolygonStipple;
130 class Program;
131 class Scissor;
132 class ShadeModel;
133 class Stencil;
134 class StencilTwoSided;
135 class TexEnv;
136 class TexEnvCombine;
137 class TexEnvFilter;
138 class TexGen;
139 class TexMat;
140 class Texture1D;
141 class Texture2D;
142 class Texture2DArray;
143 class Texture3D;
144 class TextureCubeMap;
145 class TextureRectangle;
146 class VertexProgram;
147 class Viewport;
148 }
149
150 namespace simgear
151 {
152 namespace osgutils
153 {
154 using namespace osg;
155
156 template<StateAttribute::Type T>
157 struct TypeHolder
158 {
159     static const StateAttribute::Type type = T;
160 };
161
162 template<typename AT> struct AttributeType;
163 template<typename AT> struct TexAttributeType;
164
165 template<>
166 struct AttributeType<AlphaFunc>
167     : public TypeHolder<StateAttribute::ALPHAFUNC>
168 {};
169
170 template<>
171 struct AttributeType<BlendColor>
172     : public TypeHolder<StateAttribute::BLENDCOLOR>
173 {};
174
175 template<>
176 struct AttributeType<BlendEquation>
177     : public TypeHolder<StateAttribute::BLENDEQUATION>
178 {};
179
180 template<>
181 struct AttributeType<BlendFunc>
182     : public TypeHolder<StateAttribute::BLENDFUNC>
183 {};
184
185 template<>
186 struct AttributeType<ClampColor>
187     : public TypeHolder<StateAttribute::CLAMPCOLOR>
188 {};
189
190 template<>
191 struct AttributeType<ColorMask>
192     : public TypeHolder<StateAttribute::COLORMASK>
193 {};
194
195 template<>
196 struct AttributeType<ColorMatrix>
197     : public TypeHolder<StateAttribute::COLORMATRIX>
198 {};
199
200 template<>
201 struct AttributeType<CullFace>
202     : public TypeHolder<StateAttribute::CULLFACE>
203 {};
204
205
206 template<>
207 struct AttributeType<osg::Depth> // Conflicts with Xlib
208     : public TypeHolder<StateAttribute::DEPTH>
209 {};
210
211 template<>
212 struct AttributeType<Fog>
213     : public TypeHolder<StateAttribute::FOG>
214 {};
215
216 template<>
217 struct AttributeType<FragmentProgram>
218     : public TypeHolder<StateAttribute::FRAGMENTPROGRAM>
219 {};
220
221 template<>
222 struct AttributeType<FrontFace>
223     : public TypeHolder<StateAttribute::FRONTFACE>
224 {};
225
226 template<>
227 struct AttributeType<LightModel>
228     : public TypeHolder<StateAttribute::LIGHTMODEL>
229 {};
230
231 template<>
232 struct AttributeType<LineStipple>
233     : public TypeHolder<StateAttribute::LINESTIPPLE>
234 {};
235
236 template<>
237 struct AttributeType<LineWidth>
238     : public TypeHolder<StateAttribute::LINEWIDTH>
239 {};
240
241 template<>
242 struct AttributeType<LogicOp>
243     : public TypeHolder<StateAttribute::LOGICOP>
244 {};
245
246 template<>
247 struct AttributeType<Material>
248     : public TypeHolder<StateAttribute::MATERIAL>
249 {};
250
251 template<>
252 struct AttributeType<Multisample>
253     : public TypeHolder<StateAttribute::MULTISAMPLE>
254 {};
255
256 template<>
257 struct AttributeType<Point>
258     : public TypeHolder<StateAttribute::POINT>
259 {};
260
261 template<>
262 struct TexAttributeType<PointSprite>
263     : public TypeHolder<StateAttribute::POINTSPRITE>
264 {};
265
266 template<>
267 struct AttributeType<PolygonMode>
268     : public TypeHolder<StateAttribute::POLYGONMODE>
269 {};
270
271 template<>
272 struct AttributeType<PolygonOffset>
273     : public TypeHolder<StateAttribute::POLYGONOFFSET>
274 {};
275
276 template<>
277 struct AttributeType<PolygonStipple>
278     : public TypeHolder<StateAttribute::POLYGONSTIPPLE>
279 {};
280
281 template<>
282 struct AttributeType<Program>
283     : public TypeHolder<StateAttribute::PROGRAM>
284 {};
285
286 template<>
287 struct AttributeType<Scissor>
288     : public TypeHolder<StateAttribute::SCISSOR>
289 {};
290
291 template<>
292 struct AttributeType<ShadeModel>
293     : public TypeHolder<StateAttribute::SHADEMODEL>
294 {};
295
296 template<>
297 struct AttributeType<Stencil>
298     : public TypeHolder<StateAttribute::STENCIL>
299 {};
300
301 template<>
302 struct AttributeType<StencilTwoSided>
303     : public TypeHolder<StateAttribute::STENCIL>
304 {};
305
306 // TexEnvCombine is not a subclass of TexEnv, so we can't do a
307 // typesafe access of the attribute.
308 #if 0
309 template<>
310 struct TexAttributeType<TexEnv>
311     : public TypeHolder<StateAttribute::TEXENV>
312 {};
313
314 template<>
315 struct TexAttributeType<TexEnvCombine>
316     : public TypeHolder<StateAttribute::TEXENV>
317 {};
318 #endif
319
320 template<>
321 struct TexAttributeType<TexEnvFilter>
322     : public TypeHolder<StateAttribute::TEXENVFILTER>
323 {};
324
325 template<>
326 struct TexAttributeType<TexGen>
327     : public TypeHolder<StateAttribute::TEXGEN>
328 {};
329
330 template<>
331 struct TexAttributeType<TexMat>
332     : public TypeHolder<StateAttribute::TEXMAT>
333 {};
334
335 template<>
336 struct TexAttributeType<Texture>
337     : public TypeHolder<StateAttribute::TEXTURE>
338 {};
339
340 template<>
341 struct AttributeType<VertexProgram>
342     : public TypeHolder<StateAttribute::VERTEXPROGRAM>
343 {};
344
345 template<>
346 struct AttributeType<Viewport>
347     : public TypeHolder<StateAttribute::VIEWPORT>
348 {};
349 } // namespace osgutils
350
351 template<typename AT>
352 inline AT* getStateAttribute(osg::StateSet* ss)
353 {
354     return static_cast<AT*>(ss->getAttribute(osgutils::AttributeType<AT>::type));
355 }
356
357 template<typename AT>
358 inline const AT* getStateAttribute(const osg::StateSet* ss)
359 {
360     return static_cast<const AT*>(ss->getAttribute(osgutils::AttributeType<AT>::type));
361 }
362
363 template<typename AT>
364 inline AT* getStateAttribute(unsigned int unit, osg::StateSet* ss)
365 {
366     return static_cast<AT*>(ss->getTextureAttribute(unit, osgutils::TexAttributeType<AT>
367                                                     ::type));
368 }
369
370 template<typename AT>
371 inline const AT* getStateAttribute(unsigned int unit, const osg::StateSet* ss)
372 {
373     return static_cast<const AT*>(ss->getTextureAttribute(unit,
374                                                           osgutils::TexAttributeType<AT>
375                                                           ::type));
376 }
377 } // namespace simgear
378
379 #endif