StencilTwoSided 9.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_STENCILTWOSIDED
#define OSG_STENCILTWOSIDED 1

#include <osg/Stencil>

namespace osg {

#ifndef GL_STENCIL_TEST_TWO_SIDE
#define GL_STENCIL_TEST_TWO_SIDE 0x8910
#endif

/** Provides OpenGL two sided stencil functionality, also known as separate stencil.
 *  It enables to specify different stencil function for front and back facing polygons.
 *  Two sided stenciling is used usually to eliminate the need of two rendering passes
 *  when using standard stenciling functions. See also \sa osg::Stencil.
 *
 *  Two sided stenciling is available since OpenGL 2.0. It is also supported by
 *  EXT_stencil_two_side extension especially on Nvidia cards.
 *  Another extension introduced by ATI is ATI_separate_stencil. However, ATI's extension
 *  is limited to have reference and mask value the same for both faces.
 *  ATI's extension is currently not supported by the current implementation.
 *
 *  osg::StencilTwoSided does nothing if OpenGL 2.0 or EXT_stencil_two_side are not available.
*/
class OSG_EXPORT StencilTwoSided : public StateAttribute
{
    public :


        StencilTwoSided();

        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
        StencilTwoSided(const StencilTwoSided& stencil,const CopyOp& copyop=CopyOp::SHALLOW_COPY);

        META_StateAttribute(osg, StencilTwoSided, STENCIL);

        /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
        virtual int compare(const StateAttribute& sa) const;

        virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
        {
            usage.usesMode(GL_STENCIL_TEST);
            return true;
        }

        enum Face
        {
            FRONT = 0,
            BACK = 1
        };

        enum Function
        {
            NEVER = GL_NEVER,
            LESS = GL_LESS,
            EQUAL = GL_EQUAL,
            LEQUAL = GL_LEQUAL,
            GREATER = GL_GREATER,
            NOTEQUAL = GL_NOTEQUAL,
            GEQUAL = GL_GEQUAL,
            ALWAYS = GL_ALWAYS
        };

        inline void setFunction(Face face, Function func,int ref,unsigned int mask)
        {
            _func[face] = func;
            _funcRef[face] = ref;
            _funcMask[face] = mask;
        }

        inline void setFunction(Face face, Function func) { _func[face] = func; }
        inline Function getFunction(Face face) const { return _func[face]; }

        inline void setFunctionRef(Face face, int ref) { _funcRef[face]=ref; }
        inline int getFunctionRef(Face face) const { return _funcRef[face]; }

        inline void setFunctionMask(Face face, unsigned int mask) { _funcMask[face]=mask; }
        inline unsigned int getFunctionMask(Face face) const { return _funcMask[face]; }


        enum Operation
        {
            KEEP = GL_KEEP,
            ZERO = GL_ZERO,
            REPLACE = GL_REPLACE,
            INCR = GL_INCR,
            DECR = GL_DECR,
            INVERT = GL_INVERT,
            INCR_WRAP = GL_INCR_WRAP,
            DECR_WRAP = GL_DECR_WRAP
        };

        /** set the operations to apply when the various stencil and depth 
          * tests fail or pass.  First parameter is to control the operation
          * when the stencil test fails.  The second parameter is to control the
          * operation when the stencil test passes, but depth test fails. The
          * third parameter controls the operation when both the stencil test
          * and depth pass.  Ordering of parameter is the same as if using
          * glStencilOp(,,).*/
        inline void setOperation(Face face, Operation sfail, Operation zfail, Operation zpass)
        {
            _sfail[face] = sfail;
            _zfail[face] = zfail;
            _zpass[face] = zpass;
        }

        /** set the operation when the stencil test fails.*/
        inline void setStencilFailOperation(Face face, Operation sfail) { _sfail[face] = sfail; }

        /** get the operation when the stencil test fails.*/
        inline Operation getStencilFailOperation(Face face) const { return _sfail[face]; }

        /** set the operation when the stencil test passes but the depth test fails.*/
        inline void setStencilPassAndDepthFailOperation(Face face, Operation zfail) { _zfail[face]=zfail; }

        /** get the operation when the stencil test passes but the depth test fails.*/
        inline Operation getStencilPassAndDepthFailOperation(Face face) const { return _zfail[face]; }

        /** set the operation when both the stencil test and the depth test pass.*/
        inline void setStencilPassAndDepthPassOperation(Face face, Operation zpass) { _zpass[face]=zpass; }

        /** get the operation when both the stencil test and the depth test pass.*/
        inline Operation getStencilPassAndDepthPassOperation(Face face) const { return _zpass[face]; }


        inline void setWriteMask(Face face, unsigned int mask) { _writeMask[face] = mask; }

        inline unsigned int getWriteMask(Face face) const { return _writeMask[face]; }


        virtual void apply(State& state) const;

    public:

        /** Extensions class which encapsulates the querying of extensions and
          * associated function pointers, and provide convenience wrappers to 
          * check for the extensions or use the associated functions.
        */
        class OSG_EXPORT Extensions : public osg::Referenced
        {
            public:
                Extensions(unsigned int contextID);

                Extensions(const Extensions& rhs);

                void lowestCommonDenominator(const Extensions& rhs);

                void setupGLExtensions(unsigned int contextID);

                void setStencilTwoSidedSupported(bool flag) { _isStencilTwoSidedSupported=flag; }
                bool isStencilTwoSidedSupported() const { return _isStencilTwoSidedSupported; }
                void setOpenGL20Supported(bool flag) { _isOpenGL20Supported=flag; }
                bool isOpenGL20Supported() const { return _isOpenGL20Supported; }
                void setSeparateStencilSupported(bool flag) { _isSeparateStencilSupported=flag; }
                bool isSeparateStencilSupported() const { return _isSeparateStencilSupported; }

                void glActiveStencilFace(GLenum face) const;
                void glStencilOpSeparate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass) const;
                void glStencilMaskSeparate(GLenum face, GLuint mask) const;
                void glStencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask) const;
                void glStencilFuncSeparateATI(GLenum frontfunc, GLenum backfunc, GLint ref, GLuint mask) const;

            protected:

                ~Extensions() {}

                bool _isStencilTwoSidedSupported;
                bool _isOpenGL20Supported;
                bool _isSeparateStencilSupported;

                typedef void (GL_APIENTRY * ActiveStencilFaceProc) (GLenum);
                typedef void (GL_APIENTRY * StencilOpSeparate) (GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass);
                typedef void (GL_APIENTRY * StencilMaskSeparate) (GLenum face, GLuint mask);
                typedef void (GL_APIENTRY * StencilFuncSeparate) (GLenum face, GLenum func, GLint ref, GLuint mask);
                typedef void (GL_APIENTRY * StencilFuncSeparateATI) (GLenum frontfunc, GLenum backfunc, GLint ref, GLuint mask);

                ActiveStencilFaceProc _glActiveStencilFace;
                StencilOpSeparate _glStencilOpSeparate;
                StencilMaskSeparate _glStencilMaskSeparate;
                StencilFuncSeparate _glStencilFuncSeparate;
                StencilFuncSeparate _glStencilFuncSeparateATI;
        };

        /** Function to call to get the extension of a specified context.
          * If the Extension object for that context has not yet been created
          * and the 'createIfNotInitalized' flag been set to false then returns NULL.
          * If 'createIfNotInitalized' is true then the Extensions object is
          * automatically created. However, in this case the extension object
          * will only be created with the graphics context associated with ContextID.
        */
        static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized);

        /** The setExtensions method allows users to override the extensions across graphics contexts.
          * Typically used when you have different extensions supported across graphics pipes
          * but need to ensure that they all use the same low common denominator extensions.
        */
        static void setExtensions(unsigned int contextID,Extensions* extensions);


    protected:

        virtual ~StencilTwoSided();

        Function            _func[2];
        int                 _funcRef[2];
        unsigned int        _funcMask[2];

        Operation           _sfail[2];
        Operation           _zfail[2];
        Operation           _zpass[2];

        unsigned int        _writeMask[2];

};

}

#endif