Hi Oliver,
I'm trying to render scenes into a buffer (e.g. an osg::Image). There
are multiple demos and examples about this (e.g.
example_osgscreencapture) but they all require some sort of visible
viewer to work. I'd like a console application that does not display
any windows.

Is there a class/working example that allows this?
Probably there's a simpler way, but you can create an off-screen viewer with this code:

int witdth = ...;
int height = ...;
osgViewer::Viewer viewer;
osg::Camera *camera = viewer.getCamera();
osg::ref_ptr<osg::GraphicsContext::Traits> traits =
    new osg::GraphicsContext::Traits;
traits->x = 0;
traits->y = 0;
traits->width = width;
traits->height = height;
traits->doubleBuffer = false;
traits->sharedContext = 0;
traits->pbuffer = true;
traits->readDISPLAY();
osg::GraphicsContext *gc =
    osg::GraphicsContext::createGraphicsContext(traits.get());
camera->setGraphicsContext(gc);
camera->setDrawBuffer(GL_FRONT);
camera->setReadBuffer(GL_FRONT);
camera->setViewport(new osg::Viewport(0, 0, width, height));
double fovy, aspectRatio, near, far;
camera->getProjectionMatrixAsPerspective(fovy, aspectRatio, near, far);
double newAspectRatio = double(traits->width) / double(traits->height);
double aspectRatioChange = newAspectRatio / aspectRatio;
if (aspectRatioChange != 1.0)
    camera->getProjectionMatrix() *=
        osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0);

// Viewer stuff like handlers, setting scene data, etc. Don't call any
// of the setUpXYZ functions.
// And don't forget to attach the image to the camera.

viewer.realize();
viewer.run();

In windows I don't know how it works but in *NIX, note that you still need access to the display server.

Cheers,
Juan
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to