I'm trying to get the stencil index of a node using offline rendering in OSG. My main procedures are listed below:
- deeply copy the node I want to offline render and get the stencil index;
- create a offline render camera, and make the previous copied node as the camera's child, and then add this camera as child node to the original node I want to offline render;
- create a DrawCallback which gets the stencil buffer and checks stencil indices of every pixel, and get the maximum stencil index.
Ok, the procedures seem a bit long. Now I can get the correct maximum stencil index, however, I get the warnings and errors:
"RenderStage::runCameraSetUp(), FBO setup failed, FBO status=0x8cdd" "Warning: detected OpenGL error' invalid operation' at end of SceneView::draw() ..."
The whole codes are a bit long and tedious. Some main code snippets are listed below:
void OfflineCallback::operator()(osg::RenderInfo &renderInfo)const
{
osg::Image *image = new osg::Image;
image->readPixels(0, 0, 512, 512, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE);
unsigned char *dtBuf = (unsigned char *)(image->getDataPointer());
int maxStencil = 0;
for (int i = 0; i != image->s(); ++i){
for (int j = 0; j != image->t(); ++j){
unsigned char dt = dtBuf[image->s() * i + j];
if(dt > maxStencil)
maxStencil = dt;
}
}
}
Then I create the offline render camera, and attach the stencil buffer:
osg::Camera *camera = new osg::Camera;
camera->setClearMasks(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
camera->attach(osg::Camera::STENCIL_BUFFER, GL_UNSIGNED_BYTE);
osg::Stencil *stencil = new osg::Stencil;
stencil->setFunction(osg::Stencil::GREATER, 1, 0xff);
stencil->setOperation(osg::Stencil::INCR, osg::Stencil::INCR, osg::Stencil::INCR);
osg::Depth *depth = new osg::Depth;
depth->setFunction(osg::Depth::ALWAYS);
osg::StateSet *ss = camera->getOrCreateStateSet();
ss->setAttributeAndModes(stencil, osg::StateAttribute::ON);
ss->setAttributeAndModes(depth, osg::StateAttribute::ON);
camera->setPostDrawCallback(new OfflineCallback);
To test the codes, I create a sphere as the main node, and then I get the maximum stencil index as 2 which is correct but with the errors and warnings mentioned at the beginning.
What am I missing? Which step is wrong? Any suggestion would be appreciated. Thanks!
Sincerely, Jimmy