[osg-users] How to read FBO attachment back to the CPU?

Sebastian Messerschmidt sebastian.messerschmidt at gmx.de
Fri Jan 6 04:36:15 PST 2017


Hi Marius,

You need to pass the m_RenderTarget with the callback  and evaluate it 
in the callback like this:

CameraPostDrawCallback::operator()( osg::RenderInfo& render_info ) const
{
	osg::State* state=render_info.getState();
	state->apply(render_info.getCurrentCamera()->getStateSet());
	state->applyTextureAttribute(0, mTexture);
	mTexture->apply(*state);
	mImage->readImageFromCurrentTexture(render_info.getContextID());
}

where the mTexture is your m_RenderTarget and mImage is simply a 
osg::Image*.


HTH

Cheers
Sebastian


> Hi,
>
> I have a scene graph that contains a (pre-render) camera that renders to an FBO with an attached image. Similar to this:
>
> Code:
>
> cam = new osg::Camera;
> cam->setRenderOrder(osg::Camera::PRE_RENDER);
> cam->setRenderTargetImplementation(DsTOsgCamera::FRAME_BUFFER_OBJECT);
> cam->attach(osg::Camera::COLOR_BUFFER, m_RenderTarget, 0, 0, false, 1, 0);
>
>
>
>
> Now I want to read the pixel data of that image back to the CPU each frame.
> I tried using a DrawCall back:
>
> Code:
>
> class ImageReader : public osg::Camera::DrawCallback
> {
> public:
> virtual void operator() (osg::RenderInfo& renderInfo) const
> {
> osg::ref_ptr<osg::Camera> camera = renderInfo.getCurrentCamera();
> int x, y, width, height;
> x = camera->getViewport()->x();
> y = camera->getViewport()->y();
> width = camera->getViewport()->width();
> height = camera->getViewport()->height();
>
> osg::ref_ptr<osg::Image> image = new osg::Image;
> image->readPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE);
>
> osgDB::writeImageFile(*image,"image.png");
> }
> }
>
>
>
> and adding it to the camera:
>
> Code:
>
> cam->setFinalDrawCallback(new ImageReader());
>
>
>
>
> What happens is that not the rendered image of that camera is captured, but instead the output of the whole scene graph.
> How can I correctly access the rendering result of an FBO camera?
>
> Thank you!
>
> Cheers,
> Marius
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=69901#69901
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>



More information about the osg-users mailing list