00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef SIMGEAR_OSGUTILS_HXX
00021 #define SIMGEAR_OSGUTILS_HXX 1
00022
00023 #include <osg/CopyOp>
00024 #include <osg/StateAttribute>
00025 #include <osg/StateSet>
00026
00027 namespace simgear
00028 {
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 template <typename U>
00039 struct PointerTraits
00040 {
00041 typedef class NullType {} PointeeType;
00042 };
00043
00044 template <typename U>
00045 struct PointerTraits<U*>
00046 {
00047 typedef U PointeeType;
00048 };
00049
00050 template <typename U>
00051 struct PointerTraits<const U*>
00052 {
00053 typedef U PointeeType;
00054 };
00055
00056 template <typename FuncObj>
00057 class RefPtrAdapter
00058 : public std::unary_function<const osg::ref_ptr<typename PointerTraits<typename FuncObj::argument_type>::PointeeType>,
00059 typename FuncObj::result_type>
00060 {
00061 public:
00062 typedef typename PointerTraits<typename FuncObj::argument_type>::PointeeType PointeeType;
00063 typedef osg::ref_ptr<PointeeType> RefPtrType;
00064 explicit RefPtrAdapter(const FuncObj& funcObj) : _func(funcObj) {}
00065 typename FuncObj::result_type operator()(const RefPtrType& refPtr) const
00066 {
00067 return _func(refPtr.get());
00068 }
00069 protected:
00070 FuncObj _func;
00071 };
00072
00073 template <typename FuncObj>
00074 RefPtrAdapter<FuncObj> refPtrAdapt(const FuncObj& func)
00075 {
00076 return RefPtrAdapter<FuncObj>(func);
00077 }
00078 }
00082 namespace osg
00083 {
00084 template <typename T> class ref_ptr;
00085 class CopyOp;
00086 }
00087
00088 namespace simgear
00089 {
00090 template <typename T>
00091 T* clone(const T* object, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
00092 {
00093 return static_cast<T*>(object->clone(copyop));
00094 }
00095
00096 template<typename T>
00097 T* clone_ref(const osg::ref_ptr<T>& object,
00098 const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
00099 {
00100 return static_cast<T*>(object->clone(copyop));
00101 }
00102
00103 }
00104
00105 namespace osg
00106 {
00107 class AlphaFunc;
00108 class BlendColor;
00109 class BlendEquation;
00110 class BlendFunc;
00111 class ClampColor;
00112 class ColorMask;
00113 class ColorMatrix;
00114 class CullFace;
00115 class Depth;
00116 class Fog;
00117 class FragmentProgram;
00118 class FrontFace;
00119 class LightModel;
00120 class LineStipple;
00121 class LineWidth;
00122 class LogicOp;
00123 class Material;
00124 class Multisample;
00125 class Point;
00126 class PointSprite;
00127 class PolygonMode;
00128 class PolygonOffset;
00129 class PolygonStipple;
00130 class Program;
00131 class Scissor;
00132 class ShadeModel;
00133 class Stencil;
00134 class StencilTwoSided;
00135 class TexEnv;
00136 class TexEnvCombine;
00137 class TexEnvFilter;
00138 class TexGen;
00139 class TexMat;
00140 class Texture1D;
00141 class Texture2D;
00142 class Texture2DArray;
00143 class Texture3D;
00144 class TextureCubeMap;
00145 class TextureRectangle;
00146 class VertexProgram;
00147 class Viewport;
00148 }
00149
00150 namespace simgear
00151 {
00152 namespace osgutils
00153 {
00154 using namespace osg;
00155
00156 template<StateAttribute::Type T>
00157 struct TypeHolder
00158 {
00159 static const StateAttribute::Type type = T;
00160 };
00161
00162 template<typename AT> struct AttributeType;
00163 template<typename AT> struct TexAttributeType;
00164
00165 template<>
00166 struct AttributeType<AlphaFunc>
00167 : public TypeHolder<StateAttribute::ALPHAFUNC>
00168 {};
00169
00170 template<>
00171 struct AttributeType<BlendColor>
00172 : public TypeHolder<StateAttribute::BLENDCOLOR>
00173 {};
00174
00175 template<>
00176 struct AttributeType<BlendEquation>
00177 : public TypeHolder<StateAttribute::BLENDEQUATION>
00178 {};
00179
00180 template<>
00181 struct AttributeType<BlendFunc>
00182 : public TypeHolder<StateAttribute::BLENDFUNC>
00183 {};
00184
00185 template<>
00186 struct AttributeType<ClampColor>
00187 : public TypeHolder<StateAttribute::CLAMPCOLOR>
00188 {};
00189
00190 template<>
00191 struct AttributeType<ColorMask>
00192 : public TypeHolder<StateAttribute::COLORMASK>
00193 {};
00194
00195 template<>
00196 struct AttributeType<ColorMatrix>
00197 : public TypeHolder<StateAttribute::COLORMATRIX>
00198 {};
00199
00200 template<>
00201 struct AttributeType<CullFace>
00202 : public TypeHolder<StateAttribute::CULLFACE>
00203 {};
00204
00205
00206 template<>
00207 struct AttributeType<osg::Depth>
00208 : public TypeHolder<StateAttribute::DEPTH>
00209 {};
00210
00211 template<>
00212 struct AttributeType<Fog>
00213 : public TypeHolder<StateAttribute::FOG>
00214 {};
00215
00216 template<>
00217 struct AttributeType<FragmentProgram>
00218 : public TypeHolder<StateAttribute::FRAGMENTPROGRAM>
00219 {};
00220
00221 template<>
00222 struct AttributeType<FrontFace>
00223 : public TypeHolder<StateAttribute::FRONTFACE>
00224 {};
00225
00226 template<>
00227 struct AttributeType<LightModel>
00228 : public TypeHolder<StateAttribute::LIGHTMODEL>
00229 {};
00230
00231 template<>
00232 struct AttributeType<LineStipple>
00233 : public TypeHolder<StateAttribute::LINESTIPPLE>
00234 {};
00235
00236 template<>
00237 struct AttributeType<LineWidth>
00238 : public TypeHolder<StateAttribute::LINEWIDTH>
00239 {};
00240
00241 template<>
00242 struct AttributeType<LogicOp>
00243 : public TypeHolder<StateAttribute::LOGICOP>
00244 {};
00245
00246 template<>
00247 struct AttributeType<Material>
00248 : public TypeHolder<StateAttribute::MATERIAL>
00249 {};
00250
00251 template<>
00252 struct AttributeType<Multisample>
00253 : public TypeHolder<StateAttribute::MULTISAMPLE>
00254 {};
00255
00256 template<>
00257 struct AttributeType<Point>
00258 : public TypeHolder<StateAttribute::POINT>
00259 {};
00260
00261 template<>
00262 struct TexAttributeType<PointSprite>
00263 : public TypeHolder<StateAttribute::POINTSPRITE>
00264 {};
00265
00266 template<>
00267 struct AttributeType<PolygonMode>
00268 : public TypeHolder<StateAttribute::POLYGONMODE>
00269 {};
00270
00271 template<>
00272 struct AttributeType<PolygonOffset>
00273 : public TypeHolder<StateAttribute::POLYGONOFFSET>
00274 {};
00275
00276 template<>
00277 struct AttributeType<PolygonStipple>
00278 : public TypeHolder<StateAttribute::POLYGONSTIPPLE>
00279 {};
00280
00281 template<>
00282 struct AttributeType<Program>
00283 : public TypeHolder<StateAttribute::PROGRAM>
00284 {};
00285
00286 template<>
00287 struct AttributeType<Scissor>
00288 : public TypeHolder<StateAttribute::SCISSOR>
00289 {};
00290
00291 template<>
00292 struct AttributeType<ShadeModel>
00293 : public TypeHolder<StateAttribute::SHADEMODEL>
00294 {};
00295
00296 template<>
00297 struct AttributeType<Stencil>
00298 : public TypeHolder<StateAttribute::STENCIL>
00299 {};
00300
00301 template<>
00302 struct AttributeType<StencilTwoSided>
00303 : public TypeHolder<StateAttribute::STENCIL>
00304 {};
00305
00306
00307
00308 #if 0
00309 template<>
00310 struct TexAttributeType<TexEnv>
00311 : public TypeHolder<StateAttribute::TEXENV>
00312 {};
00313
00314 template<>
00315 struct TexAttributeType<TexEnvCombine>
00316 : public TypeHolder<StateAttribute::TEXENV>
00317 {};
00318 #endif
00319
00320 template<>
00321 struct TexAttributeType<TexEnvFilter>
00322 : public TypeHolder<StateAttribute::TEXENVFILTER>
00323 {};
00324
00325 template<>
00326 struct TexAttributeType<TexGen>
00327 : public TypeHolder<StateAttribute::TEXGEN>
00328 {};
00329
00330 template<>
00331 struct TexAttributeType<TexMat>
00332 : public TypeHolder<StateAttribute::TEXMAT>
00333 {};
00334
00335 template<>
00336 struct TexAttributeType<Texture>
00337 : public TypeHolder<StateAttribute::TEXTURE>
00338 {};
00339
00340 template<>
00341 struct AttributeType<VertexProgram>
00342 : public TypeHolder<StateAttribute::VERTEXPROGRAM>
00343 {};
00344
00345 template<>
00346 struct AttributeType<Viewport>
00347 : public TypeHolder<StateAttribute::VIEWPORT>
00348 {};
00349 }
00350
00351 template<typename AT>
00352 inline AT* getStateAttribute(osg::StateSet* ss)
00353 {
00354 return static_cast<AT*>(ss->getAttribute(osgutils::AttributeType<AT>::type));
00355 }
00356
00357 template<typename AT>
00358 inline const AT* getStateAttribute(const osg::StateSet* ss)
00359 {
00360 return static_cast<const AT*>(ss->getAttribute(osgutils::AttributeType<AT>::type));
00361 }
00362
00363 template<typename AT>
00364 inline AT* getStateAttribute(unsigned int unit, osg::StateSet* ss)
00365 {
00366 return static_cast<AT*>(ss->getTextureAttribute(unit, osgutils::TexAttributeType<AT>
00367 ::type));
00368 }
00369
00370 template<typename AT>
00371 inline const AT* getStateAttribute(unsigned int unit, const osg::StateSet* ss)
00372 {
00373 return static_cast<const AT*>(ss->getTextureAttribute(unit,
00374 osgutils::TexAttributeType<AT>
00375 ::type));
00376 }
00377 }
00378
00379 #endif