From: Tim Moore Date: Tue, 8 Sep 2009 10:53:33 +0000 (+0200) Subject: getStateAttribute utility function X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e7e81c663927c463d6ddb7735c8dca20ce7c3346;p=simgear.git getStateAttribute utility function This provides a concise, typesafe way to get attributes from osg::StateSet. Also, blew away BackRefInserter. --- diff --git a/simgear/scene/material/Effect.cxx b/simgear/scene/material/Effect.cxx index 5562c63b..eba40dd7 100644 --- a/simgear/scene/material/Effect.cxx +++ b/simgear/scene/material/Effect.cxx @@ -881,9 +881,7 @@ bool makeParametersFromStateSet(SGPropertyNode* paramRoot, const StateSet* ss) SGPropertyNode* matNode = paramRoot->getChild("material", 0, true); Vec4f ambVal, difVal, specVal, emisVal; float shininess = 0.0f; - const Material* mat - = static_cast(ss->getAttribute(StateAttribute - ::MATERIAL)); + const Material* mat = getStateAttribute(ss); if (mat) { ambVal = mat->getAmbient(Material::FRONT_AND_BACK); difVal = mat->getDiffuse(Material::FRONT_AND_BACK); @@ -897,9 +895,7 @@ bool makeParametersFromStateSet(SGPropertyNode* paramRoot, const StateSet* ss) matNode->getChild("emissive", 0, true)->setValue(toVec4d(toSG(emisVal))); matNode->getChild("shininess", 0, true)->setValue(shininess); matNode->getChild("color-mode", 0, true)->setStringValue("diffuse"); - const ShadeModel* sm - = static_cast(ss->getAttribute(StateAttribute - ::SHADEMODEL)); + const ShadeModel* sm = getStateAttribute(ss); string shadeModelString("smooth"); if (sm) { ShadeModel::Mode smMode = sm->getMode(); @@ -909,9 +905,7 @@ bool makeParametersFromStateSet(SGPropertyNode* paramRoot, const StateSet* ss) paramRoot->getChild("shade-model", 0, true) ->setStringValue(shadeModelString); string cullFaceString("off"); - const CullFace* cullFace - = static_cast(ss->getAttribute(StateAttribute - ::CULLFACE)); + const CullFace* cullFace = getStateAttribute(ss); if (cullFace) { switch (cullFace->getMode()) { case CullFace::FRONT: diff --git a/simgear/structure/OSGUtils.hxx b/simgear/structure/OSGUtils.hxx index 21c5d698..557339d0 100644 --- a/simgear/structure/OSGUtils.hxx +++ b/simgear/structure/OSGUtils.hxx @@ -20,8 +20,9 @@ #ifndef SIMGEAR_OSGUTILS_HXX #define SIMGEAR_OSGUTILS_HXX 1 -#include #include +#include +#include namespace simgear { @@ -99,59 +100,280 @@ T* clone_ref(const osg::ref_ptr& object, return static_cast(object->clone(copyop)); } -template -class BackRefInsertIterator - : public boost::iterator_facade, - BackRefInsertIterator, - boost::incrementable_traversal_tag - > +} + +namespace osg { -public: - typedef typename Container::value_type::element_type* PtrType; - BackRefInsertIterator() : _container(0) {} - explicit BackRefInsertIterator(Container& container) - : _container(&container) - { - } +class AlphaFunc; +class BlendColor; +class BlendEquation; +class BlendFunc; +class ClampColor; +class ColorMask; +class ColorMatrix; +class CullFace; +class Depth; +class Fog; +class FragmentProgram; +class FrontFace; +class LightModel; +class LineStipple; +class LineWidth; +class LogicOp; +class Material; +class Multisample; +class Point; +class PointSprite; +class PolygonMode; +class PolygonOffset; +class PolygonStipple; +class Program; +class Scissor; +class ShadeModel; +class Stencil; +class StencilTwoSided; +class TexEnv; +class TexEnvCombine; +class TexEnvFilter; +class TexGen; +class TexMat; +class Texture1D; +class Texture2D; +class Texture2DArray; +class Texture3D; +class TextureCubeMap; +class TextureRectangle; +class VertexProgram; +class Viewport; +} - BackRefInsertIterator& - operator=(const PtrType ptr) - { - _container->push_back(ptr); - return *this; - } - -private: - friend class boost::iterator_core_access; +namespace simgear +{ +namespace osgutils +{ +using namespace osg; - void increment() - { - } - - BackRefInsertIterator& dereference() - { - return *this; - } +template +struct TypeHolder +{ + static const StateAttribute::Type type = T; +}; - BackRefInsertIterator& dereference() const - { - return const_cast(*this); - } +template struct AttributeType; +template struct TexAttributeType; - bool equal(const BackRefInsertIterator& rhs) - { - return _container == rhs._container; - } - - Container* _container; -}; +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + + +template<> +struct AttributeType // Conflicts with Xlib + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; +template<> +struct AttributeType + : public TypeHolder +{}; -template -inline BackRefInsertIterator -backRefInsertIterator(Container& container) +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct TexAttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +// TexEnvCombine is not a subclass of TexEnv, so we can't do a +// typesafe access of the attribute. +#if 0 +template<> +struct TexAttributeType + : public TypeHolder +{}; + +template<> +struct TexAttributeType + : public TypeHolder +{}; +#endif + +template<> +struct TexAttributeType + : public TypeHolder +{}; + +template<> +struct TexAttributeType + : public TypeHolder +{}; + +template<> +struct TexAttributeType + : public TypeHolder +{}; + +template<> +struct TexAttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; + +template<> +struct AttributeType + : public TypeHolder +{}; +} // namespace osgutils + +template +inline AT* getStateAttribute(osg::StateSet* ss) { - return BackRefInsertIterator(container); + return static_cast(ss->getAttribute(osgutils::AttributeType::type)); } + +template +inline const AT* getStateAttribute(const osg::StateSet* ss) +{ + return static_cast(ss->getAttribute(osgutils::AttributeType::type)); +} + +template +inline AT* getStateAttribute(unsigned int unit, osg::StateSet* ss) +{ + return static_cast(ss->getTextureAttribute(unit, osgutils::TexAttributeType + ::type)); } + +template +inline const AT* getStateAttribute(unsigned int unit, const osg::StateSet* ss) +{ + return static_cast(ss->getTextureAttribute(unit, + osgutils::TexAttributeType + ::type)); +} +} // namespace simgear + #endif