| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_STENCILTWOSIDED |
|---|
| 15 | #define OSG_STENCILTWOSIDED 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Stencil> |
|---|
| 18 | |
|---|
| 19 | namespace osg { |
|---|
| 20 | |
|---|
| 21 | #ifndef GL_STENCIL_TEST_TWO_SIDE |
|---|
| 22 | #define GL_STENCIL_TEST_TWO_SIDE 0x8910 |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | /** Provides OpenGL two sided stencil functionality, also known as separate stencil. |
|---|
| 26 | * It enables to specify different stencil function for front and back facing polygons. |
|---|
| 27 | * Two sided stenciling is used usually to eliminate the need of two rendering passes |
|---|
| 28 | * when using standard stenciling functions. See also \sa osg::Stencil. |
|---|
| 29 | * |
|---|
| 30 | * Two sided stenciling is available since OpenGL 2.0. It is also supported by |
|---|
| 31 | * EXT_stencil_two_side extension especially on Nvidia cards. |
|---|
| 32 | * Another extension introduced by ATI is ATI_separate_stencil. However, ATI's extension |
|---|
| 33 | * is limited to have reference and mask value the same for both faces. |
|---|
| 34 | * ATI's extension is currently not supported by the current implementation. |
|---|
| 35 | * |
|---|
| 36 | * osg::StencilTwoSided does nothing if OpenGL 2.0 or EXT_stencil_two_side are not available. |
|---|
| 37 | */ |
|---|
| 38 | class OSG_EXPORT StencilTwoSided : public StateAttribute |
|---|
| 39 | { |
|---|
| 40 | public : |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | StencilTwoSided(); |
|---|
| 44 | |
|---|
| 45 | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
|---|
| 46 | StencilTwoSided(const StencilTwoSided& stencil,const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
|---|
| 47 | |
|---|
| 48 | META_StateAttribute(osg, StencilTwoSided, STENCIL); |
|---|
| 49 | |
|---|
| 50 | /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ |
|---|
| 51 | virtual int compare(const StateAttribute& sa) const; |
|---|
| 52 | |
|---|
| 53 | virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const |
|---|
| 54 | { |
|---|
| 55 | usage.usesMode(GL_STENCIL_TEST); |
|---|
| 56 | return true; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | enum Face |
|---|
| 60 | { |
|---|
| 61 | FRONT = 0, |
|---|
| 62 | BACK = 1 |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | enum Function |
|---|
| 66 | { |
|---|
| 67 | NEVER = GL_NEVER, |
|---|
| 68 | LESS = GL_LESS, |
|---|
| 69 | EQUAL = GL_EQUAL, |
|---|
| 70 | LEQUAL = GL_LEQUAL, |
|---|
| 71 | GREATER = GL_GREATER, |
|---|
| 72 | NOTEQUAL = GL_NOTEQUAL, |
|---|
| 73 | GEQUAL = GL_GEQUAL, |
|---|
| 74 | ALWAYS = GL_ALWAYS |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | inline void setFunction(Face face, Function func,int ref,unsigned int mask) |
|---|
| 78 | { |
|---|
| 79 | _func[face] = func; |
|---|
| 80 | _funcRef[face] = ref; |
|---|
| 81 | _funcMask[face] = mask; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | inline void setFunction(Face face, Function func) { _func[face] = func; } |
|---|
| 85 | inline Function getFunction(Face face) const { return _func[face]; } |
|---|
| 86 | |
|---|
| 87 | inline void setFunctionRef(Face face, int ref) { _funcRef[face]=ref; } |
|---|
| 88 | inline int getFunctionRef(Face face) const { return _funcRef[face]; } |
|---|
| 89 | |
|---|
| 90 | inline void setFunctionMask(Face face, unsigned int mask) { _funcMask[face]=mask; } |
|---|
| 91 | inline unsigned int getFunctionMask(Face face) const { return _funcMask[face]; } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | enum Operation |
|---|
| 95 | { |
|---|
| 96 | KEEP = GL_KEEP, |
|---|
| 97 | ZERO = GL_ZERO, |
|---|
| 98 | REPLACE = GL_REPLACE, |
|---|
| 99 | INCR = GL_INCR, |
|---|
| 100 | DECR = GL_DECR, |
|---|
| 101 | INVERT = GL_INVERT, |
|---|
| 102 | INCR_WRAP = GL_INCR_WRAP, |
|---|
| 103 | DECR_WRAP = GL_DECR_WRAP |
|---|
| 104 | }; |
|---|
| 105 | |
|---|
| 106 | /** set the operations to apply when the various stencil and depth |
|---|
| 107 | * tests fail or pass. First parameter is to control the operation |
|---|
| 108 | * when the stencil test fails. The second parameter is to control the |
|---|
| 109 | * operation when the stencil test passes, but depth test fails. The |
|---|
| 110 | * third parameter controls the operation when both the stencil test |
|---|
| 111 | * and depth pass. Ordering of parameter is the same as if using |
|---|
| 112 | * glStencilOp(,,).*/ |
|---|
| 113 | inline void setOperation(Face face, Operation sfail, Operation zfail, Operation zpass) |
|---|
| 114 | { |
|---|
| 115 | _sfail[face] = sfail; |
|---|
| 116 | _zfail[face] = zfail; |
|---|
| 117 | _zpass[face] = zpass; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** set the operation when the stencil test fails.*/ |
|---|
| 121 | inline void setStencilFailOperation(Face face, Operation sfail) { _sfail[face] = sfail; } |
|---|
| 122 | |
|---|
| 123 | /** get the operation when the stencil test fails.*/ |
|---|
| 124 | inline Operation getStencilFailOperation(Face face) const { return _sfail[face]; } |
|---|
| 125 | |
|---|
| 126 | /** set the operation when the stencil test passes but the depth test fails.*/ |
|---|
| 127 | inline void setStencilPassAndDepthFailOperation(Face face, Operation zfail) { _zfail[face]=zfail; } |
|---|
| 128 | |
|---|
| 129 | /** get the operation when the stencil test passes but the depth test fails.*/ |
|---|
| 130 | inline Operation getStencilPassAndDepthFailOperation(Face face) const { return _zfail[face]; } |
|---|
| 131 | |
|---|
| 132 | /** set the operation when both the stencil test and the depth test pass.*/ |
|---|
| 133 | inline void setStencilPassAndDepthPassOperation(Face face, Operation zpass) { _zpass[face]=zpass; } |
|---|
| 134 | |
|---|
| 135 | /** get the operation when both the stencil test and the depth test pass.*/ |
|---|
| 136 | inline Operation getStencilPassAndDepthPassOperation(Face face) const { return _zpass[face]; } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | inline void setWriteMask(Face face, unsigned int mask) { _writeMask[face] = mask; } |
|---|
| 140 | |
|---|
| 141 | inline unsigned int getWriteMask(Face face) const { return _writeMask[face]; } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | virtual void apply(State& state) const; |
|---|
| 145 | |
|---|
| 146 | public: |
|---|
| 147 | |
|---|
| 148 | /** Extensions class which encapsulates the querying of extensions and |
|---|
| 149 | * associated function pointers, and provide convenience wrappers to |
|---|
| 150 | * check for the extensions or use the associated functions. |
|---|
| 151 | */ |
|---|
| 152 | class OSG_EXPORT Extensions : public osg::Referenced |
|---|
| 153 | { |
|---|
| 154 | public: |
|---|
| 155 | Extensions(unsigned int contextID); |
|---|
| 156 | |
|---|
| 157 | Extensions(const Extensions& rhs); |
|---|
| 158 | |
|---|
| 159 | void lowestCommonDenominator(const Extensions& rhs); |
|---|
| 160 | |
|---|
| 161 | void setupGLExtensions(unsigned int contextID); |
|---|
| 162 | |
|---|
| 163 | void setStencilTwoSidedSupported(bool flag) { _isStencilTwoSidedSupported=flag; } |
|---|
| 164 | bool isStencilTwoSidedSupported() const { return _isStencilTwoSidedSupported; } |
|---|
| 165 | void setOpenGL20Supported(bool flag) { _isOpenGL20Supported=flag; } |
|---|
| 166 | bool isOpenGL20Supported() const { return _isOpenGL20Supported; } |
|---|
| 167 | void setSeparateStencilSupported(bool flag) { _isSeparateStencilSupported=flag; } |
|---|
| 168 | bool isSeparateStencilSupported() const { return _isSeparateStencilSupported; } |
|---|
| 169 | |
|---|
| 170 | void glActiveStencilFace(GLenum face) const; |
|---|
| 171 | void glStencilOpSeparate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass) const; |
|---|
| 172 | void glStencilMaskSeparate(GLenum face, GLuint mask) const; |
|---|
| 173 | void glStencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask) const; |
|---|
| 174 | void glStencilFuncSeparateATI(GLenum frontfunc, GLenum backfunc, GLint ref, GLuint mask) const; |
|---|
| 175 | |
|---|
| 176 | protected: |
|---|
| 177 | |
|---|
| 178 | ~Extensions() {} |
|---|
| 179 | |
|---|
| 180 | bool _isStencilTwoSidedSupported; |
|---|
| 181 | bool _isOpenGL20Supported; |
|---|
| 182 | bool _isSeparateStencilSupported; |
|---|
| 183 | |
|---|
| 184 | typedef void (GL_APIENTRY * ActiveStencilFaceProc) (GLenum); |
|---|
| 185 | typedef void (GL_APIENTRY * StencilOpSeparate) (GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass); |
|---|
| 186 | typedef void (GL_APIENTRY * StencilMaskSeparate) (GLenum face, GLuint mask); |
|---|
| 187 | typedef void (GL_APIENTRY * StencilFuncSeparate) (GLenum face, GLenum func, GLint ref, GLuint mask); |
|---|
| 188 | typedef void (GL_APIENTRY * StencilFuncSeparateATI) (GLenum frontfunc, GLenum backfunc, GLint ref, GLuint mask); |
|---|
| 189 | |
|---|
| 190 | ActiveStencilFaceProc _glActiveStencilFace; |
|---|
| 191 | StencilOpSeparate _glStencilOpSeparate; |
|---|
| 192 | StencilMaskSeparate _glStencilMaskSeparate; |
|---|
| 193 | StencilFuncSeparate _glStencilFuncSeparate; |
|---|
| 194 | StencilFuncSeparate _glStencilFuncSeparateATI; |
|---|
| 195 | }; |
|---|
| 196 | |
|---|
| 197 | /** Function to call to get the extension of a specified context. |
|---|
| 198 | * If the Extension object for that context has not yet been created |
|---|
| 199 | * and the 'createIfNotInitalized' flag been set to false then returns NULL. |
|---|
| 200 | * If 'createIfNotInitalized' is true then the Extensions object is |
|---|
| 201 | * automatically created. However, in this case the extension object |
|---|
| 202 | * will only be created with the graphics context associated with ContextID. |
|---|
| 203 | */ |
|---|
| 204 | static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized); |
|---|
| 205 | |
|---|
| 206 | /** The setExtensions method allows users to override the extensions across graphics contexts. |
|---|
| 207 | * Typically used when you have different extensions supported across graphics pipes |
|---|
| 208 | * but need to ensure that they all use the same low common denominator extensions. |
|---|
| 209 | */ |
|---|
| 210 | static void setExtensions(unsigned int contextID,Extensions* extensions); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | protected: |
|---|
| 214 | |
|---|
| 215 | virtual ~StencilTwoSided(); |
|---|
| 216 | |
|---|
| 217 | Function _func[2]; |
|---|
| 218 | int _funcRef[2]; |
|---|
| 219 | unsigned int _funcMask[2]; |
|---|
| 220 | |
|---|
| 221 | Operation _sfail[2]; |
|---|
| 222 | Operation _zfail[2]; |
|---|
| 223 | Operation _zpass[2]; |
|---|
| 224 | |
|---|
| 225 | unsigned int _writeMask[2]; |
|---|
| 226 | |
|---|
| 227 | }; |
|---|
| 228 | |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | #endif |
|---|