diff --git a/examples/osgscreencapture/osgscreencapture.cpp b/examples/osgscreencapture/osgscreencapture.cpp index f95a0aa39..a1d80c7b0 100644 --- a/examples/osgscreencapture/osgscreencapture.cpp +++ b/examples/osgscreencapture/osgscreencapture.cpp @@ -10,6 +10,8 @@ */ #include +#include + #include #include @@ -28,21 +30,129 @@ #include #include +#include class WindowCaptureCallback : public osg::Camera::DrawCallback { public: + struct ContextData : public osg::Referenced + { + + ContextData(osg::GraphicsContext* gc, const std::string& name): + _gc(gc), + _fileName(name), + _pixelFormat(GL_RGB), + _type(GL_UNSIGNED_BYTE), + _width(0), + _height(0), + _currentImageIndex(0), + _currentPboIndex(0) + { + getSize(gc, _width, _height); + + std::cout<<"Window size "<<_width<<", "<<_height<getTraits()) + { + width = gc->getTraits()->width; + height = gc->getTraits()->height; + } + } + + void read() + { + std::cout<<"Read to "<<_fileName<<" image "<<_currentImageIndex<<" "<<_currentPboIndex<readPixels(0,0,_width,_height, + _pixelFormat,_type); + + if (!_fileName.empty()) + { + osgDB::writeImageFile(*image, _fileName); + } + + _currentImageIndex = nextImageIndex; + _currentPboIndex = nextPboIndex; + + } + + typedef std::vector< osg::ref_ptr > ImageBuffer; + typedef std::vector< osg::ref_ptr > PBOBuffer; + + osg::GraphicsContext* _gc; + std::string _fileName; + + GLenum _pixelFormat; + GLenum _type; + int _width; + int _height; + + unsigned int _currentImageIndex; + ImageBuffer _imageBuffer; + + unsigned int _currentPboIndex; + PBOBuffer _pboBuffer; + }; + WindowCaptureCallback() { } + ContextData* createContextData(osg::GraphicsContext* gc) const + { + std::stringstream filename; + filename << "test_"<<_contextDataMap.size()<<".jpg"; + return new ContextData(gc,filename.str()); + } + + ContextData* getContextData(osg::GraphicsContext* gc) const + { + OpenThreads::ScopedLock lock(_mutex); + osg::ref_ptr& data = _contextDataMap[gc]; + if (!data) data = createContextData(gc); + + return data.get(); + } + virtual void operator () (osg::RenderInfo& renderInfo) const { osg::GraphicsContext* gc = renderInfo.getState()->getGraphicsContext(); osg::notify(osg::NOTICE)<<"Capture screen image "< cd = getContextData(gc); + cd->read(); } + + typedef std::map > ContextDataMap; + + mutable OpenThreads::Mutex _mutex; + mutable ContextDataMap _contextDataMap; + }; void addCallbackToViewer(osgViewer::ViewerBase& viewer, WindowCaptureCallback* callback)