openscenegraph
StencilTwoSided
Go to the documentation of this file.
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
19namespace 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*/
38class 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 protected:
147
148 virtual ~StencilTwoSided();
149
150 Function _func[2];
151 int _funcRef[2];
152 unsigned int _funcMask[2];
153
154 Operation _sfail[2];
155 Operation _zfail[2];
156 Operation _zpass[2];
157
158 unsigned int _writeMask[2];
159
160};
161
162}
163
164#endif