1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
17#include <osg/StateAttribute>
22#define GL_INCR_WRAP 0x8507
23#define GL_DECR_WRAP 0x8508
27/** Encapsulate OpenGL glStencilFunc/Op/Mask functions.
29* All functionality except INCR_WRAP and DECR_WRAP is supported by OpenGL 1.1.
30* INCR_WRAP an DECR_WRAP are available since OpenGL 1.4 or when
31* GL_EXT_stencil_wrap extension is present.
33* If INCR_WRAP or DECR_WRAP values are used while they are detected to be not supported,
34* the INCR or DECR values are sent to OpenGL instead.
36* OpenGL 2.0 introduced two side stenciling that is available through
37* osg::StencilTwoSided class.
39class OSG_EXPORT Stencil : public StateAttribute
46 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
47 Stencil(const Stencil& stencil,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
48 StateAttribute(stencil,copyop),
50 _funcRef(stencil._funcRef),
51 _funcMask(stencil._funcMask),
52 _sfail(stencil._sfail),
53 _zfail(stencil._zfail),
54 _zpass(stencil._zpass),
55 _writeMask(stencil._writeMask) {}
58 META_StateAttribute(osg, Stencil, STENCIL);
60 /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
61 virtual int compare(const StateAttribute& sa) const
63 // check the types are equal and then create the rhs variable
64 // used by the COMPARE_StateAttribute_Parameter macros below.
65 COMPARE_StateAttribute_Types(Stencil,sa)
67 // compare each parameter in turn against the rhs.
68 COMPARE_StateAttribute_Parameter(_func)
69 COMPARE_StateAttribute_Parameter(_funcRef)
70 COMPARE_StateAttribute_Parameter(_funcMask)
71 COMPARE_StateAttribute_Parameter(_sfail)
72 COMPARE_StateAttribute_Parameter(_zfail)
73 COMPARE_StateAttribute_Parameter(_zpass)
74 COMPARE_StateAttribute_Parameter(_writeMask)
76 return 0; // passed all the above comparison macros, must be equal.
79 virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
81 usage.usesMode(GL_STENCIL_TEST);
92 NOTEQUAL = GL_NOTEQUAL,
97 inline void setFunction(Function func,int ref,unsigned int mask)
104 inline void setFunction(Function func) { _func = func; }
105 inline Function getFunction() const { return _func; }
107 inline void setFunctionRef(int ref) { _funcRef=ref; }
108 inline int getFunctionRef() const { return _funcRef; }
110 inline void setFunctionMask(unsigned int mask) { _funcMask=mask; }
111 inline unsigned int getFunctionMask() const { return _funcMask; }
118 REPLACE = GL_REPLACE,
122 INCR_WRAP = GL_INCR_WRAP,
123 DECR_WRAP = GL_DECR_WRAP
126 /** set the operations to apply when the various stencil and depth
127 * tests fail or pass. First parameter is to control the operation
128 * when the stencil test fails. The second parameter is to control the
129 * operation when the stencil test passes, but depth test fails. The
130 * third parameter controls the operation when both the stencil test
131 * and depth pass. Ordering of parameter is the same as if using
133 inline void setOperation(Operation sfail, Operation zfail, Operation zpass)
140 /** set the operation when the stencil test fails.*/
141 inline void setStencilFailOperation(Operation sfail) { _sfail = sfail; }
143 /** get the operation when the stencil test fails.*/
144 inline Operation getStencilFailOperation() const { return _sfail; }
146 /** set the operation when the stencil test passes but the depth test fails.*/
147 inline void setStencilPassAndDepthFailOperation(Operation zfail) { _zfail=zfail; }
149 /** get the operation when the stencil test passes but the depth test fails.*/
150 inline Operation getStencilPassAndDepthFailOperation() const { return _zfail; }
152 /** set the operation when both the stencil test and the depth test pass.*/
153 inline void setStencilPassAndDepthPassOperation(Operation zpass) { _zpass=zpass; }
155 /** get the operation when both the stencil test and the depth test pass.*/
156 inline Operation getStencilPassAndDepthPassOperation() const { return _zpass; }
159 inline void setWriteMask(unsigned int mask) { _writeMask = mask; }
161 inline unsigned int getWriteMask() const { return _writeMask; }
164 virtual void apply(State& state) const;
173 unsigned int _funcMask;
179 unsigned int _writeMask;