[osg-users] How to create a background image?

Vite Falcon osgforum at tevs.eu
Mon Nov 16 08:59:18 PST 2015


I managed to solve this issue using the following code:


Code:

// create a camera to set up the projection and model view matrices, and the subgraph to draw in the HUD
osg::ref_ptr<osg::Camera> camera = new osg::Camera();

// set the view matrix
camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
// use identity view matrix so that children do not get (view) transformed
camera->setViewMatrix(osg::Matrix::identity());
// set the projection matrix to be of width and height of 1
camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1.0f, 0, 1.0f));
// set resize policy to fixed
camera->setProjectionResizePolicy(osg::Camera::ProjectionResizePolicy::FIXED);

// only clear the depth buffer
camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

// draw subgraph before main camera view.
camera->setRenderOrder(osg::Camera::NESTED_RENDER);

// we don't want the camera to grab event focus from the viewers main camera(s).
camera->setAllowEventFocus(false);

osg::StateSet* cameraStateSet = camera->getOrCreateStateSet();
cameraStateSet->setRenderBinDetails(1, "RenderBin");
cameraStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);

// add to this camera a subgraph to render
{

    osg::ref_ptr<osg::Geode> geode{ new osg::Geode() };

    auto stateset = geode->getOrCreateStateSet();
    stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    {
        auto backgroundImage = osgDB::readImageFile("data/images/bg_ragnarok_01.bmp");
        auto texturedQuad = osg::createTexturedQuadGeometry(
            osg::Vec3(0.f, 0.f, 0.f),
            osg::Vec3(1.0f, 0.f, 0.f),
            osg::Vec3(0.f, 1.0f, 0.f),
            0.f,
            0.f,
            backgroundImage->s(),
            backgroundImage->t());
        auto textureRect = new osg::TextureRectangle(backgroundImage); textureRect->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
        textureRect->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
        textureRect->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
        textureRect->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
        texturedQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, textureRect, osg::StateAttribute::ON);
        texturedQuad->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
        geode->addDrawable(texturedQuad);
    }

    if (!camera->addChild(geode))
    {
        osg::notify(osg::NotifySeverity::WARN) << "Failed to add geode" << std::endl;
    }
}
osgViewer::Viewer::Windows windows;
viewer.getWindows(windows);

if (windows.empty())
{
    return;
}

// set up cameras to render on the first window available.
auto window = windows[0];
camera->setGraphicsContext(window);
auto windowTraits = window->getTraits();
camera->setViewport(0, 0, windowTraits->width, windowTraits->height);

auto group = dynamic_cast<osg::Group*>(viewer.getSceneData());
if (nullptr == group)
{
    osg::notify(osg::NotifySeverity::WARN) << "Failed to add camera" << std::endl;
    return;
}
group->addChild(camera.get());




The gist of the code is:

1. Create a camera
2. Set the view matrix of the camera to identity so that there are no view transforms made to the children
3. Set the projection matrix of the camera to be of an orthogonal matrix with width and height equal to 1
4. Set resize policy on projection matrix to be fixed
5. Set render order to be NESTED_RENDER (I previously had this as PRE_RENDER and that didn't work. I still have to dig into why).
6. Create a textured quad with width and height of 1 unit
7. Add the textured quad to an instance of osg::Geode and add that geode to the camera
8. Extract the first window from the camera. Set the graphics-context and viewport of the camera
9. Add the camera to the scene and you have a background image


This answer can also be found in gamedev section of stackexchange. Since I cannot add links to my posts yet, I'm skipping that. But it should be easy to find if you search my username.

Thanks to everyone who took a look at this question.

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




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20151116/273117fc/attachment-0003.htm>


More information about the osg-users mailing list