[osg-users] basic method to save a screenshot from 3D model?

lucas Grijander dev_crisalix at hotmail.com
Fri Nov 6 10:19:35 PST 2009


Hi everybody,

this is what I added to my code to try to save a screenshot. however I just got a black jpg file... any clue?

    //Save screenshot
    osg::Group *root = new osg::Group;
    osg::Geode *geode = new osg::Geode;
    //Add my geometry to display to the scene
    geode->addDrawable(myBody->myBody);

    //Allocate an image of 800x800
    osg::Image *shot = new osg::Image;
    shot->allocateImage(800, 800, 24, GL_RGB, GL_UNSIGNED_BYTE);

    osgViewer::Viewer *v = new osgViewer::Viewer;
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;

    traits->x =0;
    traits->y = 0;
    traits->width = 800;
    traits->height = 800;
    traits->windowDecoration = false;
    traits->doubleBuffer = false;
    traits->sharedContext = 0;
    /*here you put pbuffer to true*/
    traits->pbuffer = true;
    osg::ref_ptr<osg::GraphicsContext> _gc= osg::GraphicsContext::createGraphicsContext(traits.get());

    v->getCamera()->setGraphicsContext(_gc.get());
    v->getCamera()->setViewport(new osg::Viewport(0,0,800,800));
    v->getCamera()->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    
    v->getCamera()->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
    v->getCamera()->attach(osg::Camera::COLOR_BUFFER, shot);

    v->setSceneData(root);
    
    v->frame();

    osgDB::writeImageFile(*shot,"test.jpg");   

Do I have to add anything else??

Jaime. 



Date: Thu, 5 Nov 2009 17:59:41 +0100
From: jtorresfabra at gmail.com
To: osg-users at lists.openscenegraph.org
Subject: Re: [osg-users] basic method to save a screenshot from 3D model?

Hi Jaime,

 Unless you have a context you can't do a screenshot. So you can intialite an OpenGL graphic context yourself (see osg::GraphicsContext::Traits) and set your viewer in "background" mode. You can initialite your context this way, or something similar, to have a pbuffer:


osgViewer::Viewer *v = new osgViewer::Viewer;
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;

    traits->x =x;
    traits->y = y;
    traits->width = width;

    traits->height = height;
    traits->windowDecoration = false;
    traits->doubleBuffer = false;
    traits->sharedContext = 0;
   /*here you put pbuffer to true*/
   traits->pbuffer = true;


    osg::ref_ptr<osg::GraphicsContext> _gc= osg::GraphicsContext::createGraphicsContext(traits.get());

    if (!_gc)
    {
        osg::notify(osg::NOTICE)<<"Failed to create pbuffer, failing back to normal graphics window."<<std::endl;

        traits->pbuffer = false;
        _gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    }
    v->getCamera()->setGraphicsContext(_gc.get());
    v->getCamera()->setViewport(new osg::Viewport(x,y,width,height));


Hope this help.

Cheers 


2009/11/5 lucas Grijander <dev_crisalix at hotmail.com>






thanks Robert.
However it forces me to create a viewer... is there any option to just create a camera and make a single render to obtain the screenshot?
J.



> Date: Thu, 5 Nov 2009 14:14:28 +0000
> From: robert.osfield at gmail.com
> To: osg-users at lists.openscenegraph.org

> Subject: Re: [osg-users] basic method to save a screenshot from 3D model?
> 
> Hi Jaime,
> 
> Have a look at osgprerender for inspiration.  The key is to use a

> pbuffer graphics context on your main camera.
> 
> Robert.
> 
> On Thu, Nov 5, 2009 at 1:49 PM, lucas Grijander
> <dev_crisalix at hotmail.com> wrote:

> >
> >
> > Hello everybody!
> > in my application I do some stuff with geometries, textures, etc... but I do
> > not visualize the results using a viewer, I just save the 3D models in .obj

> > which are then used in another application. I would need also to create a
> > screenshot from the model and save it in jpg or bmp. I would like to know
> > what is the best and simple way to extract a screenshot from a geometry, I

> > suppose creating a viewer + camera?
> > thanks!
> > Jaime.
> > ________________________________
> > Windows Live: Friends get your Flickr, Yelp, and Digg updates when they

> > e-mail you.
> > _______________________________________________
> > osg-users mailing list
> > osg-users at lists.openscenegraph.org

> > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> >
> >
> _______________________________________________

> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

 		 	   		  
Windows Live: Make it easier for your friends to see  what you’re up to on Facebook.


_______________________________________________

osg-users mailing list

osg-users at lists.openscenegraph.org

http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




-- 
Jordi Torres Fabra

Instituto de Automática e Informática Industrial 
http://www.ai2.upv.es
 		 	   		  
_________________________________________________________________
Keep your friends updated—even when you’re not signed in.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_5:092010
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20091106/e687f25a/attachment.htm>


More information about the osg-users mailing list