[osg-users] [forum] How to take screen shot of osgviewer

Yeedunuri Hema hema.yeedunuri at amigooptima.in
Tue Sep 15 04:24:00 PDT 2015


Hi,
i want to take screen shot of osgviewer and save it as .png. i referred to osgposter example. I am using osg and qt for my application. when i am trying to implemennt the same as shown in poster the image is not rendered properly.


this is the code im using .images are not properly rendered.how can it be done .

    QWidget* addViewWidget( osg::Camera* camera, osg::Node* scene )
    {
        // Poster arguments
           bool activeMode = true;
           bool outputPoster = true;
           //bool outputTiles = false;
           int tileWidth = 640, tileHeight = 480;
           int posterWidth = 640*2, posterHeight = 480*2;
           std::string posterName = "poster.png", extName = "png";
           osg::Vec4 bgColor(0.2f, 0.2f, 0.6f, 1.0f);
        osgViewer::View* view = new osgViewer::View;
        view->setCamera( camera );
      camera->addChild(scene); // added for poster
        addView( view );
        osg::Group *group = new osg::Group; // added for poster
        group->addChild(camera); // added for poster
        group->addChild(scene); // added for poster
        osg::ref_ptr<PosterPrinter> printer = new PosterPrinter;
        printer->setTileSize( tileWidth, tileHeight );
        printer->setPosterSize( posterWidth, posterHeight );
        printer->setCamera( camera );

        osg::ref_ptr<osg::Image> posterImage = 0;
        if ( outputPoster )
        {
            posterImage = new osg::Image;
            posterImage->allocateImage( posterWidth, posterHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE );
            printer->setFinalPoster( posterImage.get() );
            printer->setOutputPosterName( posterName );
        }
        view->setSceneData( scene ); // added for poster
        view->addEventHandler( new osgViewer::StatsHandler );
        view->setCameraManipulator( new osgGA::TrackballManipulator );
        view->addEventHandler( new PrintPosterHandler(printer.get()) );
        osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
        return gw ? gw->getGLWidget() : NULL;
    }

    osg::Camera* createCamera( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
    {
        osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
        traits->windowName = name;
        traits->windowDecoration = windowDecoration;
        traits->x = x;
        traits->y = y;
        traits->width = w;
        traits->height = h;
        traits->doubleBuffer = true;
        traits->alpha = ds->getMinimumNumAlphaBits();
        traits->stencil = ds->getMinimumNumStencilBits();
        traits->sampleBuffers = ds->getMultiSamples();
        traits->samples = ds->getNumMultiSamples();

        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );

        camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
        camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
        camera->setProjectionMatrixAsPerspective(
            30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
        camera->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
          // camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
          // camera->setRenderOrder( osg::Camera::PRE_RENDER );
           osg::Camera::RenderTargetImplementation renderImplementation = osg::Camera::FRAME_BUFFER;

    camera->setRenderTargetImplementation( renderImplementation );
           camera->setViewport( 0, 0, 640, 480 );
//           camera->addChild( scene );

           // Set the printer


        return camera.release();
    }



... 


Thank you!

Cheers,
y

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=65135#65135



-------------- next part --------------
A non-text attachment was scrubbed...
Name: poster.png
Type: image/png
Size: 222635 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20150915/114e49a9/attachment-0002.png>


More information about the osg-users mailing list