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

Robert Osfield robert.osfield at gmail.com
Fri Jan 6 06:21:18 PST 2017


Hi Marius.

It's a long while since I tested the code path but you might want to
look at using osg::Camera's ability to attach an osg::Image as a
buffer attachment:

        /** Attach a Image to specified buffer component.*/
        void attach(BufferComponent buffer, osg::Image* image,
            unsigned int multisampleSamples = 0,
            unsigned int multisampleColorSamples = 0);

If you do this the OSG's rendering backend will automatically call
read pixels on the image for you on each frame.

Robert.



On 6 January 2017 at 12:05, Marius Dransfeld <marius.dransfeld at gmail.com> wrote:
> 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