1 // OSGUtils.hxx - Useful templates for interfacing to Open Scene Graph
3 // Copyright (C) 2008 Tim Moore timoore@redhat.com
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.
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.
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.
20 #ifndef SIMGEAR_OSGUTILS_HXX
21 #define SIMGEAR_OSGUTILS_HXX 1
24 #include <osg/StateAttribute>
25 #include <osg/StateSet>
29 // RefPtrAdapter also appears in OpenSceneGraph's
30 // osgDB/DatabasePager.cpp. I wrote that code too. -- Tim Moore
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.
41 typedef class NullType {} PointeeType;
45 struct PointerTraits<U*>
47 typedef U PointeeType;
51 struct PointerTraits<const U*>
53 typedef U PointeeType;
56 template <typename FuncObj>
58 : public std::unary_function<const osg::ref_ptr<typename PointerTraits<typename FuncObj::argument_type>::PointeeType>,
59 typename FuncObj::result_type>
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
67 return _func(refPtr.get());
73 template <typename FuncObj>
74 RefPtrAdapter<FuncObj> refPtrAdapt(const FuncObj& func)
76 return RefPtrAdapter<FuncObj>(func);
79 /** Typesafe wrapper around OSG's object clone function. Something
80 * very similar is in current OSG sources.
84 template <typename T> class ref_ptr;
91 T* clone(const T* object, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
93 return static_cast<T*>(object->clone(copyop));
97 T* clone_ref(const osg::ref_ptr<T>& object,
98 const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
100 return static_cast<T*>(object->clone(copyop));
117 class FragmentProgram;
129 class PolygonStipple;
134 class StencilTwoSided;
142 class Texture2DArray;
144 class TextureCubeMap;
145 class TextureRectangle;
156 template<StateAttribute::Type T>
159 static const StateAttribute::Type type = T;
162 template<typename AT> struct AttributeType;
163 template<typename AT> struct TexAttributeType;
166 struct AttributeType<AlphaFunc>
167 : public TypeHolder<StateAttribute::ALPHAFUNC>
171 struct AttributeType<BlendColor>
172 : public TypeHolder<StateAttribute::BLENDCOLOR>
176 struct AttributeType<BlendEquation>
177 : public TypeHolder<StateAttribute::BLENDEQUATION>
181 struct AttributeType<BlendFunc>
182 : public TypeHolder<StateAttribute::BLENDFUNC>
186 struct AttributeType<ClampColor>
187 : public TypeHolder<StateAttribute::CLAMPCOLOR>
191 struct AttributeType<ColorMask>
192 : public TypeHolder<StateAttribute::COLORMASK>
196 struct AttributeType<ColorMatrix>
197 : public TypeHolder<StateAttribute::COLORMATRIX>
201 struct AttributeType<CullFace>
202 : public TypeHolder<StateAttribute::CULLFACE>
207 struct AttributeType<osg::Depth> // Conflicts with Xlib
208 : public TypeHolder<StateAttribute::DEPTH>
212 struct AttributeType<Fog>
213 : public TypeHolder<StateAttribute::FOG>
217 struct AttributeType<FragmentProgram>
218 : public TypeHolder<StateAttribute::FRAGMENTPROGRAM>
222 struct AttributeType<FrontFace>
223 : public TypeHolder<StateAttribute::FRONTFACE>
227 struct AttributeType<LightModel>
228 : public TypeHolder<StateAttribute::LIGHTMODEL>
232 struct AttributeType<LineStipple>
233 : public TypeHolder<StateAttribute::LINESTIPPLE>
237 struct AttributeType<LineWidth>
238 : public TypeHolder<StateAttribute::LINEWIDTH>
242 struct AttributeType<LogicOp>
243 : public TypeHolder<StateAttribute::LOGICOP>
247 struct AttributeType<Material>
248 : public TypeHolder<StateAttribute::MATERIAL>
252 struct AttributeType<Multisample>
253 : public TypeHolder<StateAttribute::MULTISAMPLE>
257 struct AttributeType<Point>
258 : public TypeHolder<StateAttribute::POINT>
262 struct TexAttributeType<PointSprite>
263 : public TypeHolder<StateAttribute::POINTSPRITE>
267 struct AttributeType<PolygonMode>
268 : public TypeHolder<StateAttribute::POLYGONMODE>
272 struct AttributeType<PolygonOffset>
273 : public TypeHolder<StateAttribute::POLYGONOFFSET>
277 struct AttributeType<PolygonStipple>
278 : public TypeHolder<StateAttribute::POLYGONSTIPPLE>
282 struct AttributeType<Program>
283 : public TypeHolder<StateAttribute::PROGRAM>
287 struct AttributeType<Scissor>
288 : public TypeHolder<StateAttribute::SCISSOR>
292 struct AttributeType<ShadeModel>
293 : public TypeHolder<StateAttribute::SHADEMODEL>
297 struct AttributeType<Stencil>
298 : public TypeHolder<StateAttribute::STENCIL>
302 struct AttributeType<StencilTwoSided>
303 : public TypeHolder<StateAttribute::STENCIL>
306 // TexEnvCombine is not a subclass of TexEnv, so we can't do a
307 // typesafe access of the attribute.
310 struct TexAttributeType<TexEnv>
311 : public TypeHolder<StateAttribute::TEXENV>
315 struct TexAttributeType<TexEnvCombine>
316 : public TypeHolder<StateAttribute::TEXENV>
321 struct TexAttributeType<TexEnvFilter>
322 : public TypeHolder<StateAttribute::TEXENVFILTER>
326 struct TexAttributeType<TexGen>
327 : public TypeHolder<StateAttribute::TEXGEN>
331 struct TexAttributeType<TexMat>
332 : public TypeHolder<StateAttribute::TEXMAT>
336 struct TexAttributeType<Texture>
337 : public TypeHolder<StateAttribute::TEXTURE>
341 struct AttributeType<VertexProgram>
342 : public TypeHolder<StateAttribute::VERTEXPROGRAM>
346 struct AttributeType<Viewport>
347 : public TypeHolder<StateAttribute::VIEWPORT>
349 } // namespace osgutils
351 template<typename AT>
352 inline AT* getStateAttribute(osg::StateSet* ss)
354 return static_cast<AT*>(ss->getAttribute(osgutils::AttributeType<AT>::type));
357 template<typename AT>
358 inline const AT* getStateAttribute(const osg::StateSet* ss)
360 return static_cast<const AT*>(ss->getAttribute(osgutils::AttributeType<AT>::type));
363 template<typename AT>
364 inline AT* getStateAttribute(unsigned int unit, osg::StateSet* ss)
366 return static_cast<AT*>(ss->getTextureAttribute(unit, osgutils::TexAttributeType<AT>
370 template<typename AT>
371 inline const AT* getStateAttribute(unsigned int unit, const osg::StateSet* ss)
373 return static_cast<const AT*>(ss->getTextureAttribute(unit,
374 osgutils::TexAttributeType<AT>
377 } // namespace simgear