Hi Aviral,

It could be, thanks to the default multi-threading of the viewer, you are
detaching the callback before it's being called by the draw thread.  The
way I usually tackle tasks like this is to attach the callback at setup and
leave it there, but have a flag on the callback to say whether it is active
or not.  Your event handler would then just toggle on flag to tell it to
capture an image, then it you just one frame captured have the callback
reset it's flag after capture.

Robert.


On 28 February 2014 09:36, Aviral Goel <aviral.g...@outlook.com> wrote:

> Hi,
>
> In my application I want to save the current scene as an image every time
> the user presses 's'.
>
> I have the basic framework to detect the keypress and it works fine. I am
> facing issues in saving the scene.
>
> I wrote the following callback -
>
>
> Code:
> class SnapshotCallback : public Camera::DrawCallback
> {
> public:
>     SnapshotCallback()
>     {
>
>     }
>
>     void
>     operator() (osg::RenderInfo &renderInfo) const
>     {
>         int width;
>         int height;
>
>         Camera * camera = renderInfo.getCurrentCamera();
>
>         width  = camera -> getViewport() -> width();
>         height = camera -> getViewport() -> height();
>         cerr << "W : " <<width<< "H : "<< height;
>         osg::ref_ptr< ::osg::Image> image = new ::osg::Image();
>         image->readPixels(0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE);
>         if (osgDB::writeImageFile(*image, "./saved_image.bmp"))
>         {
>             std::cout << "Saved screen image to '" << std::endl;
>         }
>         else
>         {
>             cerr << "Could not save image!";
>         }
>     }
> };
>
>
>
>
> Now in my osgGA::GUIEventHandler derived class i execute the following
> function every time the user presses 's' ->
>
>
> Code:
> void
> snapshot( const osgGA::GUIEventAdapter& ea
>                , osgGA::GUIActionAdapter& aa
>                )
> {
>   osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
>   viewer -> getCamera() -> setFinalDrawCallback(new SnapshotCallback());
>   viewer -> renderingTraversals();
>   viewer -> getCamera() -> setFinalDrawCallback(NULL);
> }
>
>
>
>
> But the callback doesn't get executed.
>
> I have 2 questions -
>
> 1) what am I doing wrong
> 2) Is this the preferred way to get the snapshot of the scene
>
> Thank you!
>
> Cheers,
> Aviral Goel
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=58398#58398
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to