[osg-users] What is the right way to save a FBO texture to disk?

532 views
Skip to first unread message

John Kaniarz

unread,
Jun 5, 2012, 5:08:58 PM6/5/12
to osg-...@lists.openscenegraph.org
My program has 4 render to texture cameras that get composited into the frame buffer via custom shader. I want to make a screen capture of one of the texture as a response to an event.

Attaching an Image to the camera works, but it copies the data to system memory after every frame. Not just when I need it.

I tried calling camera->attach(image); viewer->frame(); osgDB::writeImageFile(image); camera->attach(texture); in my event handler, but the image saved was blank.

I also tried calling texture->setImage(image), but that image is never updated with fresh data.

I also tried calling osg::Image::readPixels() in a Camera::postDrawCallback. This doesn't work because RenderStage detaches the FBO before the callback and I get a screenshot of the framebuffer instead of the texture. Would calling RenderStage::setDisableFboAfterRender(false) fix this? Whats the place to call that function from. Will this screw something up when I try to render to the actual framebuffer?

Is there another option I haven't thought of? Perhaps manually attaching the FBO and calling readPixels?

Thanks,
John

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=48059#48059





_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Robert Osfield

unread,
Jun 6, 2012, 4:45:40 AM6/6/12
to osg-...@lists.openscenegraph.org
Hi John,

I haven't done what you want before but the way I'd tackle it would be
to use a Camera post draw callback to call
state.applyAttribute(texture); then
image->readImageFromCurrentTexture(..).

Robert.

John Kaniarz

unread,
Jun 6, 2012, 10:20:35 AM6/6/12
to osg-...@lists.openscenegraph.org
Thanks, I'll try that. Do I need to unapply the texture state when I'm done?

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=48076#48076

Robert Osfield

unread,
Jun 6, 2012, 10:40:36 AM6/6/12
to osg-...@lists.openscenegraph.org
On 6 June 2012 15:20, John Kaniarz <john.k...@us.army.mil> wrote:
> Thanks, I'll try that. Do I need to unapply the texture state when I'm done?

In theory osg::State should unapply it next time it does an
State::apply() of the current state.

Robert.

John Kaniarz

unread,
Jun 6, 2012, 10:57:27 AM6/6/12
to osg-...@lists.openscenegraph.org
It captured a texture, but not the texture I was looking for.


Code:
osg::State * state=renderInfo.getState();
state->applyAttribute(texture);
osg::Image *image=new Image();
image->readImageFromCurrentTexture(renderInfo.getContextID(),false,GL_UNSIGNED_BYTE);
osgDB::writeImageFile(*image,"snap.png");



I'm guessing I'm either using the wrong context ID, or that I'm not applying the texture in the right way.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=48079#48079

Robert Osfield

unread,
Jun 6, 2012, 11:48:07 AM6/6/12
to osg-...@lists.openscenegraph.org
On 6 June 2012 15:57, John Kaniarz <john.k...@us.army.mil> wrote:
> It captured a texture, but not the texture I was looking for.
>
>
> Code:
> osg::State * state=renderInfo.getState();
> state->applyAttribute(texture);
> osg::Image *image=new Image();
> image->readImageFromCurrentTexture(renderInfo.getContextID(),false,GL_UNSIGNED_BYTE);
> osgDB::writeImageFile(*image,"snap.png");
>
>
>
> I'm guessing I'm either using the wrong context ID, or that I'm not applying the texture in the right way.
>

I'm afraid I have tried specifically what you are trying before so
can't help debug it. Perhaps others will have done something similar
and be able to help.

Robert.

John Kaniarz

unread,
Jun 6, 2012, 4:59:45 PM6/6/12
to osg-...@lists.openscenegraph.org
I got it working. The issue is that readImageFromCurrentTexture() doesn't support TextureRectangle. I modified my program to use a Texture2D instead and it works now.

Adding TextureRectangle support to Image doesn't look like it would be too hard except that GL_TEXTURE_BINDING_RECTANGLE isn't a readily available constant. (it shows up in gl3.h in the 3.1 section, but I'm sure there's an earlier extension)

Thanks for the help,
John

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=48085#48085
Reply all
Reply to author
Forward
0 new messages