[osg-users] Render to texture ONLY?

339 views
Skip to first unread message

Chris Thomas

unread,
Aug 18, 2016, 7:19:08 AM8/18/16
to osg-...@lists.openscenegraph.org
Hi,

I have an existing app I am developing, which itself is based on OpenGL. It uses an API that provides a 3D windowing system, with different media being displayed on planes, within this 3D space. All good...

Except, its API does not offer anything near the flexibility, and ease of use of OSG. So.. how to use OSG within this app.

All of the examples I have seen so far, use a very similar patern, of the ilk....


Code:
osg::ref_ptr<osg::Node> cessna = osgDB::readNodeFile( "cessna.osg" );
viewer.setSceneData( cessna.get() );
return viewer.run();



This is great, in that its very easy to get going, but its thew viewer() that is causing issues for me. Ideally the viewer would be able to render to a texture, rather than to a screen, or window on a screen. I basically need a headless 3D process running, where the OSG output is going to a texture.

Are there any examples of how to do this? Once I have a texture, I can easily copy its contents to my apps planes.

Thank you!

Cheers,
Chris

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





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

Christian Buchner

unread,
Aug 18, 2016, 7:26:07 AM8/18/16
to OpenSceneGraph Users

have a look at the osgprerender example. That one renders to texture first, and then uses the contents of this texture for rendering to screen.

The osgscreencapture and osgposter examples also have options to render off-screen to FBO or pbuffers.


Sebastian Messerschmidt

unread,
Aug 18, 2016, 7:26:51 AM8/18/16
to osg-...@lists.openscenegraph.org
Hi Chris,

Take a look at the osgprerender example. It shows you how to render to
a framebuffer object.
The bound texture can be used to be displayed later on.

Cheers
Sebastian

Nickolai Medvedev

unread,
Aug 18, 2016, 7:49:09 AM8/18/16
to osg-...@lists.openscenegraph.org
Hi,

https://github.com/xarray/osgRecipes

Chapter 6 - it's all what you need.

Cheers,
Nickolai

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

Chris Thomas

unread,
Aug 18, 2016, 10:47:17 AM8/18/16
to osg-...@lists.openscenegraph.org
Hi,

OK, I based my initial integration into my app on osgteapot.cpp. As with all the other examples, it os run via

viewer.run();

And this creates an output window in OSX (and I am assuming any other OS its run on). And thats the issue I have, I need OSG to run "headless", that is to say, producing no visible window in the OS.

If OSG is rendering away, to a non visible buffer, I can then expose this to the user via my UI api (see above). Having this visible viewer, is the issue right now. Is there an option to run viewer with no visible display/window, or is there an alternative to viewer() ?

Thank you!

Cheers,
Chris

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

Christian Buchner

unread,
Aug 18, 2016, 11:07:06 AM8/18/16
to OpenSceneGraph Users

On Windows, create a graphics context with the pbuffer flag set to true
and windowDecoration set to false.

        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
        traits->x = 0;
        traits->y = 0;
        traits->width = 640;
        traits->height = 480;
        traits->red = 8;
        traits->green = 8;
        traits->blue = 8;
        traits->alpha = 8;
        traits->windowDecoration = false;
        traits->pbuffer = true;
        traits->doubleBuffer = false; // or true as needed
        traits->sharedContext = 0;

        m_pbuffer = osg::GraphicsContext::createGraphicsContext(traits.get());
        if (!m_pbuffer.valid())
        {
            osg::notify(osg::NOTICE) << "Pixel buffer has not been created successfully. NOTE: update your dependencies folder if you see this error!" << std::endl;
            exit(1);
        }
        else
        {
            // Create an osgViewer running on top of a pbuffer graphics context
            m_viewer = new osgViewer::Viewer();

            // in my case I use a slave camera with ortho projection
            // to render whatever is needed
            m_camera = new osg::Camera;
            m_camera->setGraphicsContext(m_pbuffer.get());
m_camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR);
            m_camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
            m_camera->setViewMatrix(osg::Matrix());
            m_camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1.0, 0, 1.0));
            m_camera->setViewport(new osg::Viewport(0, 0, 640, 480));
            m_camera->setDrawBuffer(GL_FRONT);
            m_camera->setReadBuffer(GL_FRONT);
            m_viewer->addSlave(m_camera.get(), osg::Matrixd(), osg::Matrixd());
            m_viewer->realize();


I do not know if the same would work on Linux, as pbuffers on Linux are an optional extension that might not be supported.

I get this to render at arbitrary frame rates, entirely decoupled from the screen's VBLANK interval.

Christian


2016-08-18 16:47 GMT+02:00 Chris Thomas <cth...@soasta.com>:
Hi,

OK, I based my initial integration into my app on osgteapot.cpp. As with all the other examples, it os run via

viewer.run();

And this creates an output window in OSX (and I am assuming any other OS its run on). And thats the issue I have, I need OSG to run "headless", that is to say, producing no visible window in the OS.

If OSG is rendering away, to a non visible buffer, I can then expose this to the user via my UI api (see above). Having this visible viewer, is the issue right now. Is there an option to run viewer with no visible display/window, or is there an alternative to viewer() ?

Thank you!

Cheers,
Chris

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





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

Christian Buchner

unread,
Aug 18, 2016, 11:13:00 AM8/18/16
to OpenSceneGraph Users

One more thing: Rendering to a pbuffer does not automatically give you the option to access your rendered content as a texture.

The technique to render to texture with pbuffers is called pbuffer-rtt and implemented in several OSG samples with the "--pbuffer-rtt" command line option.

This may differ a bit from the camera setup I've outlined above.

Christian







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


Reply all
Reply to author
Forward
0 new messages