| | 293 | /** Abstract base class for what to do when a screen capture happens. */ |
|---|
| | 294 | class OSGVIEWER_EXPORT CaptureOperation : public osg::Referenced |
|---|
| | 295 | { |
|---|
| | 296 | public: |
|---|
| | 297 | virtual void operator()(const osg::Image& image, const unsigned int context_id) = 0; |
|---|
| | 298 | }; |
|---|
| | 299 | |
|---|
| | 300 | /** Concrete implementation of a CaptureOperation that writes the screen capture to a file. */ |
|---|
| | 301 | class OSGVIEWER_EXPORT WriteToFileCaptureOperation : public osgViewer::CaptureOperation |
|---|
| | 302 | { |
|---|
| | 303 | public: |
|---|
| | 304 | enum SavePolicy |
|---|
| | 305 | { |
|---|
| | 306 | OVERWRITE, |
|---|
| | 307 | SEQUENTIAL_NUMBER |
|---|
| | 308 | // ... any others? |
|---|
| | 309 | }; |
|---|
| | 310 | |
|---|
| | 311 | WriteToFileCaptureOperation(const std::string& filename, const std::string& extension, SavePolicy savePolicy = OVERWRITE); |
|---|
| | 312 | |
|---|
| | 313 | virtual void operator()(const osg::Image& image, const unsigned int context_id); |
|---|
| | 314 | |
|---|
| | 315 | void setSavePolicy(SavePolicy savePolicy) { _savePolicy = savePolicy; } |
|---|
| | 316 | SavePolicy getSavePolicy() const { return _savePolicy; } |
|---|
| | 317 | |
|---|
| | 318 | protected: |
|---|
| | 319 | const std::string _filename; |
|---|
| | 320 | const std::string _extension; |
|---|
| | 321 | |
|---|
| | 322 | SavePolicy _savePolicy; |
|---|
| | 323 | |
|---|
| | 324 | std::vector<unsigned int> _contextSaveCounter; |
|---|
| | 325 | }; |
|---|
| | 326 | |
|---|
| | 327 | // From osgscreencapture example |
|---|
| | 328 | /** Callback which will be added to a viewer's camera to do the actual screen capture. */ |
|---|
| | 329 | class OSGVIEWER_EXPORT WindowCaptureCallback : public osg::Camera::DrawCallback |
|---|
| | 330 | { |
|---|
| | 331 | public: |
|---|
| | 332 | |
|---|
| | 333 | enum Mode |
|---|
| | 334 | { |
|---|
| | 335 | READ_PIXELS, |
|---|
| | 336 | SINGLE_PBO, |
|---|
| | 337 | DOUBLE_PBO, |
|---|
| | 338 | TRIPLE_PBO |
|---|
| | 339 | }; |
|---|
| | 340 | |
|---|
| | 341 | enum FramePosition |
|---|
| | 342 | { |
|---|
| | 343 | START_FRAME, |
|---|
| | 344 | END_FRAME |
|---|
| | 345 | }; |
|---|
| | 346 | |
|---|
| | 347 | struct ContextData : public osg::Referenced |
|---|
| | 348 | { |
|---|
| | 349 | static unsigned int COUNTER; |
|---|
| | 350 | |
|---|
| | 351 | ContextData(osg::GraphicsContext* gc, Mode mode, GLenum readBuffer); |
|---|
| | 352 | |
|---|
| | 353 | void getSize(osg::GraphicsContext* gc, int& width, int& height); |
|---|
| | 354 | |
|---|
| | 355 | void updateTimings(osg::Timer_t tick_start, |
|---|
| | 356 | osg::Timer_t tick_afterReadPixels, |
|---|
| | 357 | osg::Timer_t tick_afterMemCpy, |
|---|
| | 358 | unsigned int dataSize); |
|---|
| | 359 | |
|---|
| | 360 | void read(); |
|---|
| | 361 | void readPixels(); |
|---|
| | 362 | void singlePBO(osg::BufferObject::Extensions* ext); |
|---|
| | 363 | void multiPBO(osg::BufferObject::Extensions* ext); |
|---|
| | 364 | |
|---|
| | 365 | typedef std::vector< osg::ref_ptr<osg::Image> > ImageBuffer; |
|---|
| | 366 | typedef std::vector< GLuint > PBOBuffer; |
|---|
| | 367 | |
|---|
| | 368 | osg::GraphicsContext* _gc; |
|---|
| | 369 | unsigned int _index; |
|---|
| | 370 | Mode _mode; |
|---|
| | 371 | GLenum _readBuffer; |
|---|
| | 372 | |
|---|
| | 373 | GLenum _pixelFormat; |
|---|
| | 374 | GLenum _type; |
|---|
| | 375 | int _width; |
|---|
| | 376 | int _height; |
|---|
| | 377 | |
|---|
| | 378 | unsigned int _currentImageIndex; |
|---|
| | 379 | ImageBuffer _imageBuffer; |
|---|
| | 380 | |
|---|
| | 381 | unsigned int _currentPboIndex; |
|---|
| | 382 | PBOBuffer _pboBuffer; |
|---|
| | 383 | |
|---|
| | 384 | unsigned int _reportTimingFrequency; |
|---|
| | 385 | unsigned int _numTimeValuesRecorded; |
|---|
| | 386 | double _timeForReadPixels; |
|---|
| | 387 | double _timeForFullCopy; |
|---|
| | 388 | double _timeForMemCpy; |
|---|
| | 389 | osg::Timer_t _previousFrameTick; |
|---|
| | 390 | |
|---|
| | 391 | osg::ref_ptr<CaptureOperation> _captureOperation; |
|---|
| | 392 | }; |
|---|
| | 393 | |
|---|
| | 394 | WindowCaptureCallback(Mode mode, FramePosition position, GLenum readBuffer); |
|---|
| | 395 | |
|---|
| | 396 | FramePosition getFramePosition() const { return _position; } |
|---|
| | 397 | |
|---|
| | 398 | ContextData* createContextData(osg::GraphicsContext* gc) const; |
|---|
| | 399 | ContextData* getContextData(osg::GraphicsContext* gc) const; |
|---|
| | 400 | |
|---|
| | 401 | void setCaptureOperation(CaptureOperation* operation); |
|---|
| | 402 | CaptureOperation* getCaptureOperation() { return _contextDataMap.begin()->second->_captureOperation.get(); } |
|---|
| | 403 | |
|---|
| | 404 | virtual void operator () (osg::RenderInfo& renderInfo) const; |
|---|
| | 405 | |
|---|
| | 406 | typedef std::map<osg::GraphicsContext*, osg::ref_ptr<ContextData> > ContextDataMap; |
|---|
| | 407 | |
|---|
| | 408 | Mode _mode; |
|---|
| | 409 | FramePosition _position; |
|---|
| | 410 | GLenum _readBuffer; |
|---|
| | 411 | mutable OpenThreads::Mutex _mutex; |
|---|
| | 412 | mutable ContextDataMap _contextDataMap; |
|---|
| | 413 | |
|---|
| | 414 | }; |
|---|
| | 415 | |
|---|
| | 416 | /** Event handler that will capture the screen on key press. */ |
|---|
| | 417 | class OSGVIEWER_EXPORT ScreenCaptureHandler : public osgGA::GUIEventHandler |
|---|
| | 418 | { |
|---|
| | 419 | public: |
|---|
| | 420 | ScreenCaptureHandler(); |
|---|
| | 421 | |
|---|
| | 422 | void setKeyEventTakeScreenShot(int key) { _keyEventTakeScreenShot = key; } |
|---|
| | 423 | int getKeyEventTakeScreenShot() const { return _keyEventTakeScreenShot; } |
|---|
| | 424 | |
|---|
| | 425 | void setCaptureOperation(CaptureOperation* operation) { _callback->setCaptureOperation(operation); } |
|---|
| | 426 | CaptureOperation* getCaptureOperation() const { return _callback->getCaptureOperation(); } |
|---|
| | 427 | |
|---|
| | 428 | // aa will point to an osgViewer::View, so we will take a screenshot |
|---|
| | 429 | // of that view's graphics contexts. |
|---|
| | 430 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa); |
|---|
| | 431 | |
|---|
| | 432 | /** Get the keyboard and mouse usage of this manipulator.*/ |
|---|
| | 433 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| | 434 | |
|---|
| | 435 | protected: |
|---|
| | 436 | int _keyEventTakeScreenShot; |
|---|
| | 437 | // there could be a key to start taking screenshots every new frame |
|---|
| | 438 | |
|---|
| | 439 | osg::ref_ptr<WindowCaptureCallback> _callback; |
|---|
| | 440 | |
|---|
| | 441 | void addCallbackToViewer(osgViewer::ViewerBase& viewer); |
|---|
| | 442 | }; |
|---|
| | 443 | |
|---|