[osg-users] Saving Screenshot

608 views
Skip to first unread message

Danny Lesnik

unread,
Jan 18, 2010, 5:54:01 PM1/18/10
to osg-...@lists.openscenegraph.org
Hi,

I'm playing with OSG activeX example and trying to make screenshot of current scene, but I get black screenshot.

the Viewer initiation is the following:


Code:

_traits = new osg::GraphicsContext::Traits;
osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData( GetSafeHwnd() );
_traits->x = 0;
_traits->y = 0;
_traits->width = rect.right - rect.left;
_traits->height = rect.bottom - rect.top;
_traits->windowDecoration = false;
_traits->doubleBuffer = true;
_traits->sharedContext = 0;
_traits->setInheritedWindowPixelFormat = true;
_traits->inheritedWindowData = windata;
_gc = osg::GraphicsContext::createGraphicsContext( _traits.get() );
_gc->setClearColor( osg::Vec4f(0.2f, 0.2f, 0.6f, 1.0f) );
_gc->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Create camera
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(_gc);
camera->setViewport( new osg::Viewport(_traits->x, _traits->y, _traits->width, _traits->height) );
camera->setProjectionMatrixAsPerspective( 30.0f, (double)_traits->width/(double)_traits->height, 1.0f, 10000.0f );
camera->setClearMask( GL_DEPTH_BUFFER_BIT );

// Create viewer
_viewer = new osgViewer::Viewer;
_viewer->setThreadingModel( osgViewer::Viewer::SingleThreaded );
_viewer->setCamera( camera.get() );
_viewer->setCameraManipulator( new osgGA::TrackballManipulator );


The part of the code which grabs screenshot is the following:

Code:

osgViewer::ScreenCaptureHandler* scrn = new osgViewer::ScreenCaptureHandler();
osgViewer::ScreenCaptureHandler::WriteToFile* captureOper = new osgViewer::ScreenCaptureHandler::WriteToFile(tmpStr.m_szBuffer, "png");
scrn->setCaptureOperation(captureOper);
scrn->captureNextFrame(*_viewer);
_viewer->frame();


I think I need to take screenshot from GraphicContext instead of viewer, but I have no idea how to do it.

Can you please help?

Thank you!

Cheers,
Danny

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

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Danny Lesnik

unread,
Jan 20, 2010, 4:34:34 AM1/20/10
to osg-...@lists.openscenegraph.org
Hi,

Does anybody have any clue? how to solve this problem?

Thank you!

Cheers,
Danny

------------------
Read this topic online here:

http://forum.openscenegraph.org/viewtopic.php?p=22922#22922

J.P. Delport

unread,
Jan 20, 2010, 4:39:23 AM1/20/10
to osg-...@lists.openscenegraph.org
See osgscreencapture example or search for screencapturehandler.

jp

--
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean. MailScanner thanks Transtec Computers for their support.

Danny Lesnik

unread,
Jan 21, 2010, 8:27:37 PM1/21/10
to osg-...@lists.openscenegraph.org
Hi,


I tried the following code


Code:

osg::ref_ptr<osg::Image> shot = new osg::Image();
shot->allocateImage(640, 480, 24, GL_RGB, GL_UNSIGNED_BYTE);
osg::ref_ptr<osg::Camera> camera = _viewer->getCamera();
_viewer->frame();
camera->attach(osg::Camera::COLOR_BUFFER, shot.get());
osgDB::writeImageFile(*shot, "test.png" );


but still black image. How can I solve the issue.

Thank you!

Cheers,
Danny

------------------
Read this topic online here:

http://forum.openscenegraph.org/viewtopic.php?p=23033#23033

J.P. Delport

unread,
Jan 22, 2010, 3:38:35 AM1/22/10
to osg-...@lists.openscenegraph.org
Hi,

try attach before frame. I'm not sure why you want to call frame
yourself, is this part of a larger app?

jp

--

This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean. MailScanner thanks Transtec Computers for their support.

_______________________________________________

Trajce Nikolov

unread,
Jan 22, 2010, 5:59:59 AM1/22/10
to osg-...@lists.openscenegraph.org
you have to render the scene first then save the image. Have a look at osgprerender examples. Mimic the code and save the image from the Camera DrawCallback

Danny Lesnik

unread,
Jan 22, 2010, 7:30:37 AM1/22/10
to osg-...@lists.openscenegraph.org
Hi,

This is activeX control and inside the contol I have a method which prints screenshot.

This is how I do initiate control AND all OSG instances:


Code:

int ViewerCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;

RECT rect;
GetWindowRect(&rect);

WCHAR str[255];
swprintf(str, L"(%d,%d) - (%d,%d)", rect.left,rect.top,rect.right,rect.bottom);

// Set window traits and gc

// Set root node of the scene
_root = new osg::Group;
_viewer->setSceneData( _root.get() );
_viewer->realize();

WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(01, 1, 50, 50), this, 1000);

// Create a thread for the simulation loop
_beginthread(&RenderThread, 0, _viewer);
return 0;
}


and this is how do I take screenshot:


Code:

VARIANT_BOOL ViewerCtrl::SaveScreenshot(LPCTSTR name)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

CW2A tmpStr(name);


osg::ref_ptr<osg::Image> shot = new osg::Image();
shot->allocateImage(640, 480, 24, GL_RGB, GL_UNSIGNED_BYTE);
osg::ref_ptr<osg::Camera> camera = _viewer->getCamera();
_viewer->frame();
camera->attach(osg::Camera::COLOR_BUFFER, shot.get());
osgDB::writeImageFile(*shot, "test.png" );

//osgViewer::ScreenCaptureHandler* scrn = new osgViewer::ScreenCaptureHandler();
//osgViewer::ScreenCaptureHandler::WriteToFile* captureOper = new osgViewer::ScreenCaptureHandler::WriteToFile(tmpStr.m_szBuffer, "png");
//scrn->setCaptureOperation(capt()ureOper);
//scrn->captureNextFrame(*_viewer);
//_viewer->frame();
return VARIANT_TRUE;
}


so any clue or suggestion is welcomed.
Thank you!

Cheers,
Danny

------------------
Read this topic online here:

http://forum.openscenegraph.org/viewtopic.php?p=23053#23053

Ralph Kern

unread,
Jan 22, 2010, 7:47:50 AM1/22/10
to osg-...@lists.openscenegraph.org
Hi Danny,

you have to attach the image first and then draw the frame:

> osg::ref_ptr<osg::Image> shot = new osg::Image();
> shot->allocateImage(640, 480, 24, GL_RGB, GL_UNSIGNED_BYTE);
> osg::ref_ptr<osg::Camera> camera = _viewer->getCamera();

> camera->attach(osg::Camera::COLOR_BUFFER, shot.get());

> _viewer->frame();
> osgDB::writeImageFile(*shot, "test.png" );
> camera->attach(osg::Camera::COLOR_BUFFER, NULL);

The image is copied from the frame buffer at the end of the frame() call.

regards Ralph

Danny Lesnik

unread,
Jan 22, 2010, 12:05:12 PM1/22/10
to osg-...@lists.openscenegraph.org
Hi,

camera->attach(osg::Camera::COLOR_BUFFER, NULL);

Gives me
Error C2668: 'osg::Camera::attach' : ambiguous call to overloaded function

If I replace it with:


camera->attach(osg::Camera::COLOR_BUFFER, shot.get());

I still get black image.

What might be the problem?

Thank you!

Cheers,
Danny

------------------
Read this topic online here:

http://forum.openscenegraph.org/viewtopic.php?p=23064#23064

Reply all
Reply to author
Forward
0 new messages