From Jan Peciva, "please, find attached improved Stencil and StencilTwoSided classes.
Goals:
- to handle INCR_WRAP values nicely if not supported by OpenGL (old hardware)
- to support two side stenciling of OpenGL 2.0. Current implementation does not work on ATI as it uses Nvidia extension.
Ready for commit:
- Stencil and Stencil.cpp - please, review them
Ready with "hack":
- StencilTwoSided.cpp: please, see the line 113 in apply():
glEnable(GL_STENCIL_TEST_TWO_SIDE);
This line used to be in getModeUsage() as
usage.usesMode(GL_STENCIL_TEST_TWO_SIDE);
but it produces OpenGL errors on ATI as it is unknown value there (it is Nvidia extension).
Problems with my "glEnable" solution:
- it enables two side stenciling forever, and it will disturb any other single-side stenciling in the scene graph.
"
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
||||
/* -*-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
|
||||
* 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
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
#include <osg/Stencil>
|
||||
#include <osg/GLExtensions>
|
||||
#include <osg/State>
|
||||
#include <osg/Notify>
|
||||
|
||||
using namespace osg;
|
||||
|
||||
@@ -20,7 +23,7 @@ Stencil::Stencil()
|
||||
_func = ALWAYS;
|
||||
_funcRef = 0;
|
||||
_funcMask = ~0u;
|
||||
|
||||
|
||||
// set up same defaults as glStencilOp.
|
||||
_sfail = KEEP;
|
||||
_zfail = KEEP;
|
||||
@@ -30,15 +33,80 @@ Stencil::Stencil()
|
||||
_writeMask = ~0u;
|
||||
}
|
||||
|
||||
|
||||
Stencil::~Stencil()
|
||||
{
|
||||
}
|
||||
|
||||
void Stencil::apply(State&) const
|
||||
static Stencil::Operation validateOperation(State& state,
|
||||
const Stencil::Extensions* extensions, Stencil::Operation op)
|
||||
{
|
||||
// only wrap requires validation
|
||||
if (op != Stencil::INCR_WRAP && op != Stencil::DECR_WRAP)
|
||||
return op;
|
||||
|
||||
// get extension object
|
||||
if (!extensions)
|
||||
{
|
||||
const unsigned int contextID = state.getContextID();
|
||||
extensions = Stencil::getExtensions(contextID, true);
|
||||
}
|
||||
|
||||
// wrap support
|
||||
if (extensions->isStencilWrapSupported())
|
||||
return op;
|
||||
else
|
||||
return op==Stencil::INCR_WRAP ? Stencil::INCR : Stencil::DECR;
|
||||
}
|
||||
|
||||
void Stencil::apply(State& state) const
|
||||
{
|
||||
const Extensions* extensions = NULL;
|
||||
Operation sf = validateOperation(state, extensions, _sfail);
|
||||
Operation zf = validateOperation(state, extensions, _zfail);
|
||||
Operation zp = validateOperation(state, extensions, _zpass);
|
||||
|
||||
glStencilFunc((GLenum)_func,_funcRef,_funcMask);
|
||||
glStencilOp((GLenum)_sfail,(GLenum)_zfail,(GLenum)_zpass);
|
||||
glStencilOp((GLenum)sf,(GLenum)zf,(GLenum)zp);
|
||||
glStencilMask(_writeMask);
|
||||
}
|
||||
|
||||
|
||||
typedef buffered_value< ref_ptr<Stencil::Extensions> > BufferedExtensions;
|
||||
static BufferedExtensions s_extensions;
|
||||
|
||||
Stencil::Extensions* Stencil::getExtensions(unsigned int contextID, bool createIfNotInitalized)
|
||||
{
|
||||
if (!s_extensions[contextID] && createIfNotInitalized)
|
||||
s_extensions[contextID] = new Extensions(contextID);
|
||||
return s_extensions[contextID].get();
|
||||
}
|
||||
|
||||
void Stencil::setExtensions(unsigned int contextID, Extensions* extensions)
|
||||
{
|
||||
s_extensions[contextID] = extensions;
|
||||
}
|
||||
|
||||
Stencil::Extensions::Extensions(unsigned int contextID)
|
||||
{
|
||||
setupGLExtensions(contextID);
|
||||
}
|
||||
|
||||
Stencil::Extensions::Extensions(const Extensions& rhs) :
|
||||
Referenced()
|
||||
{
|
||||
_isStencilWrapSupported = rhs._isStencilWrapSupported;
|
||||
}
|
||||
|
||||
|
||||
void Stencil::Extensions::lowestCommonDenominator(const Extensions& rhs)
|
||||
{
|
||||
if (!rhs._isStencilWrapSupported)
|
||||
_isStencilWrapSupported = false;
|
||||
}
|
||||
|
||||
void Stencil::Extensions::setupGLExtensions(unsigned int contextID)
|
||||
{
|
||||
_isStencilWrapSupported = isGLExtensionOrVersionSupported(contextID, "GL_EXT_stencil_wrap", 1.4f);
|
||||
|
||||
OSG_INFO << "Stencil wrap: " << (_isStencilWrapSupported ? "supported" : "not supported") << std::endl;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
using namespace osg;
|
||||
|
||||
|
||||
StencilTwoSided::StencilTwoSided()
|
||||
{
|
||||
// set up same defaults as glStencilFunc.
|
||||
@@ -85,21 +86,46 @@ int StencilTwoSided::compare(const StateAttribute& sa) const
|
||||
|
||||
void StencilTwoSided::apply(State& state) const
|
||||
{
|
||||
// get "per-context" extensions
|
||||
const unsigned int contextID = state.getContextID();
|
||||
const Extensions* extensions = getExtensions(contextID,true);
|
||||
|
||||
if (!extensions->isStencilTwoSidedSupported())
|
||||
return;
|
||||
|
||||
extensions->glActiveStencilFace(GL_BACK);
|
||||
glStencilOp((GLenum)_sfail[BACK],(GLenum)_zfail[BACK],(GLenum)_zpass[BACK]);
|
||||
glStencilMask(_writeMask[BACK]);
|
||||
glStencilFunc((GLenum)_func[BACK],_funcRef[BACK],_funcMask[BACK]);
|
||||
// use OpenGL 2.0 functions if available
|
||||
if (extensions->isOpenGL20Supported())
|
||||
{
|
||||
// front face
|
||||
extensions->glStencilOpSeparate(GL_FRONT, (GLenum)_sfail[FRONT],(GLenum)_zfail[FRONT],(GLenum)_zpass[FRONT]);
|
||||
extensions->glStencilMaskSeparate(GL_FRONT, _writeMask[FRONT]);
|
||||
extensions->glStencilFuncSeparate(GL_FRONT, (GLenum)_func[FRONT],_funcRef[FRONT],_funcMask[FRONT]);
|
||||
|
||||
extensions->glActiveStencilFace(GL_FRONT);
|
||||
glStencilOp((GLenum)_sfail[FRONT],(GLenum)_zfail[FRONT],(GLenum)_zpass[FRONT]);
|
||||
glStencilMask(_writeMask[FRONT]);
|
||||
glStencilFunc((GLenum)_func[FRONT],_funcRef[FRONT],_funcMask[FRONT]);
|
||||
// back face
|
||||
extensions->glStencilOpSeparate(GL_BACK, (GLenum)_sfail[BACK],(GLenum)_zfail[BACK],(GLenum)_zpass[BACK]);
|
||||
extensions->glStencilMaskSeparate(GL_BACK, _writeMask[BACK]);
|
||||
extensions->glStencilFuncSeparate(GL_BACK, (GLenum)_func[BACK],_funcRef[BACK],_funcMask[BACK]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// try to use two sided stencil extension
|
||||
if (extensions->isStencilTwoSidedSupported())
|
||||
{
|
||||
// enable two sided stenciling
|
||||
glEnable(GL_STENCIL_TEST_TWO_SIDE);
|
||||
|
||||
// back face
|
||||
extensions->glActiveStencilFace(GL_BACK);
|
||||
glStencilOp((GLenum)_sfail[BACK],(GLenum)_zfail[BACK],(GLenum)_zpass[BACK]);
|
||||
glStencilMask(_writeMask[BACK]);
|
||||
glStencilFunc((GLenum)_func[BACK],_funcRef[BACK],_funcMask[BACK]);
|
||||
|
||||
// front face
|
||||
extensions->glActiveStencilFace(GL_FRONT);
|
||||
glStencilOp((GLenum)_sfail[FRONT],(GLenum)_zfail[FRONT],(GLenum)_zpass[FRONT]);
|
||||
glStencilMask(_writeMask[FRONT]);
|
||||
glStencilFunc((GLenum)_func[FRONT],_funcRef[FRONT],_funcMask[FRONT]);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -126,34 +152,87 @@ StencilTwoSided::Extensions::Extensions(const Extensions& rhs):
|
||||
Referenced()
|
||||
{
|
||||
_isStencilTwoSidedSupported = rhs._isStencilTwoSidedSupported;
|
||||
_isOpenGL20Supported = rhs._isOpenGL20Supported;
|
||||
_isSeparateStencilSupported = rhs._isSeparateStencilSupported;
|
||||
_glActiveStencilFace = rhs._glActiveStencilFace;
|
||||
_glStencilOpSeparate = rhs._glStencilOpSeparate;
|
||||
_glStencilMaskSeparate = rhs._glStencilMaskSeparate;
|
||||
_glStencilFuncSeparate = rhs._glStencilFuncSeparate;
|
||||
_glStencilFuncSeparateATI = rhs._glStencilFuncSeparateATI;
|
||||
}
|
||||
|
||||
|
||||
void StencilTwoSided::Extensions::lowestCommonDenominator(const Extensions& rhs)
|
||||
{
|
||||
if (!rhs._isStencilTwoSidedSupported) _isStencilTwoSidedSupported = false;
|
||||
if (!rhs._isOpenGL20Supported) _isOpenGL20Supported = false;
|
||||
if (!rhs._isSeparateStencilSupported) _isSeparateStencilSupported = false;
|
||||
|
||||
if (!rhs._glActiveStencilFace) _glActiveStencilFace = 0;
|
||||
|
||||
if (!rhs._glActiveStencilFace) _glActiveStencilFace = NULL;
|
||||
if (!rhs._glStencilOpSeparate) _glStencilOpSeparate = NULL;
|
||||
if (!rhs._glStencilMaskSeparate) _glStencilMaskSeparate = NULL;
|
||||
if (!rhs._glStencilFuncSeparate) _glStencilFuncSeparate = NULL;
|
||||
if (!rhs._glStencilFuncSeparateATI) _glStencilFuncSeparateATI = NULL;
|
||||
}
|
||||
|
||||
void StencilTwoSided::Extensions::setupGLExtensions(unsigned int contextID)
|
||||
{
|
||||
_isStencilTwoSidedSupported = isGLExtensionSupported(contextID,"GL_EXT_stencil_two_side");
|
||||
// extension support
|
||||
_isStencilTwoSidedSupported = isGLExtensionSupported(contextID, "GL_EXT_stencil_two_side");
|
||||
_isOpenGL20Supported = getGLVersionNumber() >= 2.0;
|
||||
_isSeparateStencilSupported = isGLExtensionSupported(contextID, "GL_ATI_separate_stencil");
|
||||
|
||||
setGLExtensionFuncPtr(_glActiveStencilFace, "glActiveStencilFace","glActiveStencilFaceEXT");
|
||||
// function pointers
|
||||
setGLExtensionFuncPtr(_glActiveStencilFace, "glActiveStencilFaceEXT");
|
||||
setGLExtensionFuncPtr(_glStencilOpSeparate, "glStencilOpSeparate", "glStencilOpSeparateATI");
|
||||
setGLExtensionFuncPtr(_glStencilMaskSeparate, "glStencilMaskSeparate");
|
||||
setGLExtensionFuncPtr(_glStencilFuncSeparate, "glStencilFuncSeparate");
|
||||
setGLExtensionFuncPtr(_glStencilFuncSeparateATI, "glStencilFuncSeparateATI");
|
||||
|
||||
// protect against buggy drivers (maybe not necessary)
|
||||
if (!_glActiveStencilFace) _isStencilTwoSidedSupported = false;
|
||||
if (!_glStencilOpSeparate) { _isOpenGL20Supported = false; _isSeparateStencilSupported = false; }
|
||||
if (!_glStencilMaskSeparate) _isOpenGL20Supported = false;
|
||||
if (!_glStencilFuncSeparate) _isOpenGL20Supported = false;
|
||||
if (!_glStencilFuncSeparateATI) _isSeparateStencilSupported = false;
|
||||
}
|
||||
|
||||
void StencilTwoSided::Extensions::glActiveStencilFace(GLenum face) const
|
||||
{
|
||||
if (_glActiveStencilFace)
|
||||
if (_isStencilTwoSidedSupported)
|
||||
{
|
||||
_glActiveStencilFace(face);
|
||||
}
|
||||
else
|
||||
{
|
||||
OSG_WARN<<"Error: glActiveStencilFace not supported by OpenGL driver"<<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void StencilTwoSided::Extensions::glStencilOpSeparate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass) const
|
||||
{
|
||||
if (_isOpenGL20Supported || _isSeparateStencilSupported)
|
||||
{
|
||||
_glStencilOpSeparate(face, sfail, dpfail, dppass);
|
||||
}
|
||||
}
|
||||
|
||||
void StencilTwoSided::Extensions::glStencilMaskSeparate(GLenum face, GLuint mask) const
|
||||
{
|
||||
if (_isOpenGL20Supported)
|
||||
{
|
||||
_glStencilMaskSeparate(face, mask);
|
||||
}
|
||||
}
|
||||
|
||||
void StencilTwoSided::Extensions::glStencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask) const
|
||||
{
|
||||
if (_isOpenGL20Supported)
|
||||
{
|
||||
_glStencilFuncSeparate(face, func, ref, mask);
|
||||
}
|
||||
}
|
||||
|
||||
void StencilTwoSided::Extensions::glStencilFuncSeparateATI(GLenum frontfunc, GLenum backfunc, GLint ref, GLuint mask) const
|
||||
{
|
||||
if (_isSeparateStencilSupported)
|
||||
{
|
||||
_glStencilFuncSeparateATI(frontfunc, backfunc, ref, mask);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user