// Взыто из файла viewer.cpp
#include <osgDB/WriteFile>

class SnapImageDrawCallback : public osg::CameraNode::DrawCallback
{
public:

        SnapImageDrawCallback()
        {
                _snapImageOnNextFrame = false;
        }

        void setFileName(const std::string& filename) { _filename = filename; }
        const std::string& getFileName() const { return _filename; }

        void setSnapImageOnNextFrame(bool flag) { _snapImageOnNextFrame = flag; 
}
        bool getSnapImageOnNextFrame() const { return _snapImageOnNextFrame; }

        virtual void operator () (const osg::CameraNode& camera) const
        {
                //osg::notify(osg::NOTICE) << "Saved screen image to
`"<<_filename<<"`"<< std::endl;
                if (!_snapImageOnNextFrame) return;

                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);

                if (osgDB::writeImageFile(*image,_filename))
                {
                        std::cout  << "Saved screen image to 
`"<<_filename<<"`"<< std::endl;
                }

                _snapImageOnNextFrame = false;
        }

protected:

        std::string _filename;
        mutable bool        _snapImageOnNextFrame;


};

////////////

//для screenshot'a
        osg::ref_ptr<SnapImageDrawCallback> snapImageDrawCallback = new
SnapImageDrawCallback();
        viewer->getCamera()->setPostDrawCallback (snapImageDrawCallback.get());

///////////

void takeScreenshot(std::string filename)
{
        osg::ref_ptr<SnapImageDrawCallback> snapImageDrawCallback =
dynamic_cast<SnapImageDrawCallback*>
(viewer->getCamera()->getPostDrawCallback());
        if(snapImageDrawCallback.get())
        {
                std::cout << "make screenshot" << std::endl;
                snapImageDrawCallback->setFileName(filename);
                snapImageDrawCallback->setSnapImageOnNextFrame(true);
        }
        else
        {
                std::cout << "Warning: no make screenshot" << std::endl;
        }
}

2010/11/17 Igor Galochkin <isee...@yandex.com>:
> I have to write a series of classes wrapping OSG model, which loads OSG graph 
> from an .osg file, uses some data from it, saves the scene back to .osg file 
> etc.
>
> I can't find out how (if it's possible) to render an OSG graph into an image 
> file.
>
> So, what i am trying to do is:
>
> void renderSceneToImage(::osg::Node* pNode, const ::std::string& sFileName_, 
> <some camera parameteres here>)
>
> The function should take the root node of the OSG scene, render it ONCE to an 
> image (::osg::Image?) and then save the image into the file.
>
> I don't need to open any windows.
>
> From what I found on the net so far:
>
> //----------------------------------------------------
>
> ::osg::Image* capImage = new ::osg::Image();
> capImage->allocateImage(nWidth, nHeight, 1, GL_RGBA, GL_FLOAT);
> capImage->setInternalTextureFormat(GL_RGBA16F_ARB);
>
>
> ::osg::ref_ptr< ::osg::Camera> camera = new ::osg::Camera();
> camera->setRenderTargetImplementation(::osg::CameraNode::FRAME_BUFFER_OBJECT);
> camera->setRenderOrder(::osg::Camera::PRE_RENDER);
> camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
> camera->setViewport(0, 0, nWidth, nHeight);
>
> camera->attach(::osg::CameraNode::COLOR_BUFFER, capImage);
>
> // temporarily wrap the whole scene with camera
> camera->addChild(pScene->root());
>
> ::osg::ref_ptr< ::osg::Group> tempRoot = new ::osg::Group();
> tempRoot->addChild(camera.get());
>
>
> // HOW TO ORDER THE CAMERA TO RENDER?
>
> ::osgDB::writeImageFile(*capImage, sFileName_);
>
> //-------------------------------------------------
>
> So, i can't find any way to order the camera to render once. All examples 
> show how to make the camera render to a texture, but when it renders is 
> decided by OpenGL (?). I don't need all that, i just need to grab the picture 
> once and return.
>
> Is this possible with OSG?
>
> Thanks!
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=33809#33809
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>



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

Reply via email to