From dff18fd2f6c059488266fadf83b566d041a85bfc Mon Sep 17 00:00:00 2001 From: ouv Date: Wed, 18 Aug 2010 07:48:32 +0000 Subject: [PATCH] Introducing dumpContents() method --- src/GLViewer/GLViewer_ViewFrame.cxx | 31 +---------- src/GLViewer/GLViewer_ViewPort2d.cxx | 79 ++++++++++++++++++++++++++++ src/GLViewer/GLViewer_ViewPort2d.h | 5 +- 3 files changed, 85 insertions(+), 30 deletions(-) diff --git a/src/GLViewer/GLViewer_ViewFrame.cxx b/src/GLViewer/GLViewer_ViewFrame.cxx index b3a2cbfcd..5a6035c5d 100644 --- a/src/GLViewer/GLViewer_ViewFrame.cxx +++ b/src/GLViewer/GLViewer_ViewFrame.cxx @@ -26,7 +26,6 @@ #include "GLViewer_ViewFrame.h" #include "GLViewer_Viewer.h" #include "GLViewer_ViewPort2d.h" -#include "GLViewer_FrameBuffer.h" #include #include @@ -272,36 +271,10 @@ void GLViewer_ViewFrame::onUpdate( int ) QImage GLViewer_ViewFrame::dumpView() { GLViewer_ViewPort2d* aViewPort = ((GLViewer_ViewPort2d*)myVP); - int aWidth = aViewPort->getWidth(); - int aHeight = aViewPort->getHeight(); - - GLViewer_FrameBuffer aFrameBuffer; - if( aFrameBuffer.init( aWidth, aHeight ) ) - { - glPushAttrib( GL_VIEWPORT_BIT ); - glViewport( 0, 0, aWidth, aHeight ); - aFrameBuffer.bind(); - - // draw scene - aViewPort->getGLWidget()->updateGL(); - - aFrameBuffer.unbind(); - glPopAttrib(); - - QImage anImage( aWidth, aHeight, QImage::Format_RGB32 ); - - aFrameBuffer.bind(); - - glReadPixels( 0, 0, aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE, anImage.bits() ); - - aFrameBuffer.unbind(); - - anImage = anImage.rgbSwapped(); - anImage = anImage.mirrored(); + QImage anImage = aViewPort->dumpContents( false ); + if( !anImage.isNull() ) return anImage; - } - QImage anImage; GLViewer_Widget* aWidget = aViewPort->getGLWidget(); if ( aWidget ) anImage = aWidget->grabFrameBuffer(); diff --git a/src/GLViewer/GLViewer_ViewPort2d.cxx b/src/GLViewer/GLViewer_ViewPort2d.cxx index 78b79e2c8..669df64dc 100644 --- a/src/GLViewer/GLViewer_ViewPort2d.cxx +++ b/src/GLViewer/GLViewer_ViewPort2d.cxx @@ -33,6 +33,7 @@ #include "GLViewer_Compass.h" #include "GLViewer_Grid.h" #include "GLViewer_Drawer.h" +#include "GLViewer_FrameBuffer.h" #include @@ -1435,3 +1436,81 @@ void GLViewer_ViewPort2d::onMaybeTip( QPoint thePoint, QString& theText, QFont& } } } + +/*! + Dumps contents of the scene + \param theWholeScene - flag, allowing to dump the whole scene, + not only its visible regeion + \return image with the scene contents +*/ +QImage GLViewer_ViewPort2d::dumpContents( bool theWholeScene ) +{ + QImage aResult; + + int aWidth = theWholeScene ? myBorder->width() : myWidth; + int aHeight = theWholeScene ? myBorder->height() : myHeight; + + // try to initialize framebuffer + GLViewer_FrameBuffer aFrameBuffer; + if( !aFrameBuffer.init( aWidth, aHeight ) ) + return aResult; + + if( theWholeScene ) + { + glMatrixMode( GL_PROJECTION ); + glPushMatrix(); + glLoadIdentity(); + glOrtho( -aWidth/2, aWidth/2, -aHeight/2, aHeight/2, -100, 100 ); + + glPushAttrib( GL_VIEWPORT_BIT ); + glViewport( 0, 0, aWidth, aHeight ); + } + + // bind the framebuffer + aFrameBuffer.bind(); + + if( theWholeScene ) + { + // centre the scene and reset the scale + int aXOffset = myBorder->left(); + int aYOffset = myBorder->bottom(); + myGLWidget->setPan( -aWidth/2 - aXOffset, -aHeight/2 - aYOffset, 0.0 ); + myGLWidget->setScale( 1.0, 1.0, 1.0 ); + } + + // draw the scene to the framebuffer + myGLWidget->updateGL(); + + // unbind the framebuffer + aFrameBuffer.unbind(); + + if( theWholeScene ) + { + glPopAttrib(); // GL_VIEWPORT_BIT + + glMatrixMode( GL_PROJECTION ); + glPopMatrix(); + glMatrixMode( GL_MODELVIEW ); + glPopMatrix(); + } + + // get an image by reading pixels from the framebuffer + QImage anImage( aWidth, aHeight, QImage::Format_RGB32 ); + + aFrameBuffer.bind(); + glReadPixels( 0, 0, aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE, anImage.bits() ); + aFrameBuffer.unbind(); + + if( theWholeScene ) + { + // restore the scene parameters + myGLWidget->setPan( myXPan, myYPan, 0.0 ); + myGLWidget->setScale( myXScale, myYScale, 1.0 ); + myGLWidget->updateGL(); + } + + anImage = anImage.rgbSwapped(); + anImage = anImage.mirrored(); + + return anImage; +} diff --git a/src/GLViewer/GLViewer_ViewPort2d.h b/src/GLViewer/GLViewer_ViewPort2d.h index 3812e62a4..fed5967d4 100644 --- a/src/GLViewer/GLViewer_ViewPort2d.h +++ b/src/GLViewer/GLViewer_ViewPort2d.h @@ -59,7 +59,7 @@ class QRubberBand; * Class GLViewer_ViewPort * 2D visualisation canvas of GLViewer */ -class GLViewer_ViewPort2d: public GLViewer_ViewPort +class GLVIEWER_API GLViewer_ViewPort2d: public GLViewer_ViewPort { Q_OBJECT friend class GLViewer_Widget; @@ -167,6 +167,9 @@ public: //! Transforms global rect to window rect QRect GLV2win( const GLViewer_Rect& ) const; + //! Dumps contents of the scene + QImage dumpContents( bool theWholeScene = false ); + signals: //! Emits after any transformation void vpUpdateValues(); -- 2.39.2