| | 193 | struct SnapImage : public osg::Camera::DrawCallback |
| | 194 | { |
| | 195 | SnapImage(const std::string& filename): |
| | 196 | _filename(filename), |
| | 197 | _snapImage(false) |
| | 198 | { |
| | 199 | _image = new osg::Image; |
| | 200 | } |
| | 201 | |
| | 202 | virtual void operator () (osg::RenderInfo& renderInfo) const |
| | 203 | { |
| | 204 | |
| | 205 | if (!_snapImage) return; |
| | 206 | |
| | 207 | osg::notify(osg::NOTICE)<<"Camera callback"<<std::endl; |
| | 208 | |
| | 209 | osg::Camera* camera = renderInfo.getCurrentCamera(); |
| | 210 | osg::Viewport* viewport = camera ? camera->getViewport() : 0; |
| | 211 | |
| | 212 | osg::notify(osg::NOTICE)<<"Camera callback "<<camera<<" "<<viewport<<std::endl; |
| | 213 | |
| | 214 | if (viewport && _image.valid()) |
| | 215 | { |
| | 216 | _image->readPixels(int(viewport->x()),int(viewport->y()),int(viewport->width()),int(viewport->height()), |
| | 217 | GL_RGBA, |
| | 218 | GL_UNSIGNED_BYTE); |
| | 219 | osgDB::writeImageFile(*_image, _filename); |
| | 220 | |
| | 221 | osg::notify(osg::NOTICE)<<"Taken screenshot, and written to '"<<_filename<<"'"<<std::endl; |
| | 222 | } |
| | 223 | |
| | 224 | _snapImage = false; |
| | 225 | } |
| | 226 | |
| | 227 | std::string _filename; |
| | 228 | mutable bool _snapImage; |
| | 229 | mutable osg::ref_ptr<osg::Image> _image; |
| | 230 | }; |
| | 231 | |
| | 232 | struct SnapeImageHandler : public osgGA::GUIEventHandler |
| | 233 | { |
| | 234 | |
| | 235 | SnapeImageHandler(int key,SnapImage* si): |
| | 236 | _key(key), |
| | 237 | _snapImage(si) {} |
| | 238 | |
| | 239 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
| | 240 | { |
| | 241 | if (ea.getHandled()) return false; |
| | 242 | |
| | 243 | switch(ea.getEventType()) |
| | 244 | { |
| | 245 | case(osgGA::GUIEventAdapter::KEYUP): |
| | 246 | { |
| | 247 | if (ea.getKey() == _key) |
| | 248 | { |
| | 249 | osg::notify(osg::NOTICE)<<"event handler"<<std::endl; |
| | 250 | _snapImage->_snapImage = true; |
| | 251 | return true; |
| | 252 | } |
| | 253 | |
| | 254 | break; |
| | 255 | } |
| | 256 | default: |
| | 257 | break; |
| | 258 | } |
| | 259 | |
| | 260 | return false; |
| | 261 | } |
| | 262 | |
| | 263 | int _key; |
| | 264 | osg::ref_ptr<SnapImage> _snapImage; |
| | 265 | }; |
| | 266 | |
| | 267 | |