| | 39 | struct ContextData : public osg::Referenced |
|---|
| | 40 | { |
|---|
| | 41 | |
|---|
| | 42 | ContextData(osg::GraphicsContext* gc, const std::string& name): |
|---|
| | 43 | _gc(gc), |
|---|
| | 44 | _fileName(name), |
|---|
| | 45 | _pixelFormat(GL_RGB), |
|---|
| | 46 | _type(GL_UNSIGNED_BYTE), |
|---|
| | 47 | _width(0), |
|---|
| | 48 | _height(0), |
|---|
| | 49 | _currentImageIndex(0), |
|---|
| | 50 | _currentPboIndex(0) |
|---|
| | 51 | { |
|---|
| | 52 | getSize(gc, _width, _height); |
|---|
| | 53 | |
|---|
| | 54 | std::cout<<"Window size "<<_width<<", "<<_height<<std::endl; |
|---|
| | 55 | |
|---|
| | 56 | // single buffered image |
|---|
| | 57 | _imageBuffer.push_back(new osg::Image); |
|---|
| | 58 | |
|---|
| | 59 | // double buffer PBO. |
|---|
| | 60 | _pboBuffer.push_back(new osg::PixelBufferObject); |
|---|
| | 61 | _pboBuffer.push_back(new osg::PixelBufferObject); |
|---|
| | 62 | } |
|---|
| | 63 | |
|---|
| | 64 | void getSize(osg::GraphicsContext* gc, int& width, int& height) |
|---|
| | 65 | { |
|---|
| | 66 | if (gc->getTraits()) |
|---|
| | 67 | { |
|---|
| | 68 | width = gc->getTraits()->width; |
|---|
| | 69 | height = gc->getTraits()->height; |
|---|
| | 70 | } |
|---|
| | 71 | } |
|---|
| | 72 | |
|---|
| | 73 | void read() |
|---|
| | 74 | { |
|---|
| | 75 | std::cout<<"Read to "<<_fileName<<" image "<<_currentImageIndex<<" "<<_currentPboIndex<<std::endl; |
|---|
| | 76 | |
|---|
| | 77 | unsigned int nextImageIndex = (_currentImageIndex+1)%_imageBuffer.size(); |
|---|
| | 78 | unsigned int nextPboIndex = (_currentPboIndex+1)%_pboBuffer.size(); |
|---|
| | 79 | |
|---|
| | 80 | int width=0, height=0; |
|---|
| | 81 | getSize(_gc, width, height); |
|---|
| | 82 | if (width!=_width || _height!=height) |
|---|
| | 83 | { |
|---|
| | 84 | std::cout<<" Window resized "<<width<<", "<<height<<std::endl; |
|---|
| | 85 | _width = width; |
|---|
| | 86 | _height = height; |
|---|
| | 87 | } |
|---|
| | 88 | |
|---|
| | 89 | osg::Image* image = _imageBuffer[_currentImageIndex].get(); |
|---|
| | 90 | |
|---|
| | 91 | image->readPixels(0,0,_width,_height, |
|---|
| | 92 | _pixelFormat,_type); |
|---|
| | 93 | |
|---|
| | 94 | if (!_fileName.empty()) |
|---|
| | 95 | { |
|---|
| | 96 | osgDB::writeImageFile(*image, _fileName); |
|---|
| | 97 | } |
|---|
| | 98 | |
|---|
| | 99 | _currentImageIndex = nextImageIndex; |
|---|
| | 100 | _currentPboIndex = nextPboIndex; |
|---|
| | 101 | |
|---|
| | 102 | } |
|---|
| | 103 | |
|---|
| | 104 | typedef std::vector< osg::ref_ptr<osg::Image> > ImageBuffer; |
|---|
| | 105 | typedef std::vector< osg::ref_ptr<osg::PixelBufferObject> > PBOBuffer; |
|---|
| | 106 | |
|---|
| | 107 | osg::GraphicsContext* _gc; |
|---|
| | 108 | std::string _fileName; |
|---|
| | 109 | |
|---|
| | 110 | GLenum _pixelFormat; |
|---|
| | 111 | GLenum _type; |
|---|
| | 112 | int _width; |
|---|
| | 113 | int _height; |
|---|
| | 114 | |
|---|
| | 115 | unsigned int _currentImageIndex; |
|---|
| | 116 | ImageBuffer _imageBuffer; |
|---|
| | 117 | |
|---|
| | 118 | unsigned int _currentPboIndex; |
|---|
| | 119 | PBOBuffer _pboBuffer; |
|---|
| | 120 | }; |
|---|
| | 121 | |
|---|
| | 124 | } |
|---|
| | 125 | |
|---|
| | 126 | ContextData* createContextData(osg::GraphicsContext* gc) const |
|---|
| | 127 | { |
|---|
| | 128 | std::stringstream filename; |
|---|
| | 129 | filename << "test_"<<_contextDataMap.size()<<".jpg"; |
|---|
| | 130 | return new ContextData(gc,filename.str()); |
|---|
| | 131 | } |
|---|
| | 132 | |
|---|
| | 133 | ContextData* getContextData(osg::GraphicsContext* gc) const |
|---|
| | 134 | { |
|---|
| | 135 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| | 136 | osg::ref_ptr<ContextData>& data = _contextDataMap[gc]; |
|---|
| | 137 | if (!data) data = createContextData(gc); |
|---|
| | 138 | |
|---|
| | 139 | return data.get(); |
|---|