I'm very sorry to ask you this question, because I encountered a very difficult problem in the process of OSG development, which bothered me for three weeks.
The problem is that I plan to use OSG to implement similar functions. Open multiple sectioning at the same time and cover with cover.
1. First of all, refer to this algorithm, and use the Stencil to test the building cover, which can achieve the following results.
void CappingTechnique::redefine_passes(int openedClipNum)
{
if (_openedClipNum != openedClipNum)
{
this->dirtyPasses();
{
osg::ref_ptr<osg::StateSet> ss = new osg::StateSet;
osg::ColorMask* colorMask = new osg::ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
ss->setAttribute(colorMask);
osg::Stencil *stencil = new osg::Stencil;
stencil->setFunction(osg::Stencil::ALWAYS, 0x0, ~0);
stencil->setOperation(osg::Stencil::INVERT, osg::Stencil::INVERT, osg::Stencil::INVERT);
ss->setAttributeAndModes(stencil, osg::StateAttribute::ON |
osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
ss->setMode(GL_CLIP_PLANE0, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CLIP_PLANE1, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CLIP_PLANE2, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CLIP_PLANE3, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CLIP_PLANE4, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
if (openedClipNum >= 0)
{
ss->setMode(GL_CLIP_PLANE0 + openedClipNum, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
}
addPass(ss.get());
}
// pass #1
{
osg::ref_ptr<osg::StateSet> ss = new osg::StateSet;
osg::Stencil *stencil = new osg::Stencil;
stencil->setFunction(osg::Stencil::NOTEQUAL, 0x0, ~0);
stencil->setOperation(osg::Stencil::ZERO, osg::Stencil::ZERO, osg::Stencil::ZERO);
ss->setAttributeAndModes(stencil, osg::StateAttribute::ON |
osg::StateAttribute::OVERRIDE);
osg::Depth *depth = new osg::Depth();
depth->setWriteMask(false);
ss->setAttributeAndModes(depth, osg::StateAttribute::ON);
ss->setMode(GL_CLIP_PLANE0, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CLIP_PLANE1, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CLIP_PLANE2, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CLIP_PLANE3, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
ss->setMode(GL_CLIP_PLANE4, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
if (openedClipNum >= 0)
{
ss->setMode(GL_CLIP_PLANE0 + openedClipNum, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
}
addPass(ss.get());
}
}
2. here, create and display the cover hanging under the relatively independent node.when opening a section, the cover surface can be created and displayed correctly.
3 However, when opening multiple cover surfaces at the same time, the following error will appear, which seems to be caused by the incorrect cleaning of the stencil buffer.
4. Searching data through Google did not find out how to clean the stencil buffer when drawing between two nodes.