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

Vite Falcon vitefalcon at gmail.com
Fri Nov 13 13:56:30 PST 2015


Hi Everyone,

This might be a very n00b question to ask here, but alas, I'm stuck! I'm trying to create a background image for a prototype and so far I've been unsuccessful. I tried to search for an answer and none of them have been helpful so far. Here's the end result of what I have:


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 projection matrix
camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1280, 0, 1024));

// set the view matrix
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());

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

// draw subgraph after main camera view.
camera->setRenderOrder(osg::Camera::PRE_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::Geode* geode = new osg::Geode();
    // turn lighting off for the text and disable depth test to ensure it's always ontop.
    osg::StateSet* stateset = geode->getOrCreateStateSet();
    stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    {
        osg::BoundingBox bb;
        for (unsigned int i = 0; i < geode->getNumDrawables(); ++i)
        {
            bb.expandBy(geode->getDrawable(i)->getBound());
        }

        osg::Geometry* geom = new osg::Geometry();

        osg::Vec3Array* vertices = new osg::Vec3Array;
        float depth = bb.zMin() - 0.1;
        vertices->push_back(osg::Vec3(bb.xMin(), bb.yMax(), depth));
        vertices->push_back(osg::Vec3(bb.xMin(), bb.yMin(), depth));
        vertices->push_back(osg::Vec3(bb.xMax(), bb.yMin(), depth));
        vertices->push_back(osg::Vec3(bb.xMax(), bb.yMax(), depth));
        geom->setVertexArray(vertices);

        osg::Vec3Array* normals = new osg::Vec3Array;
        normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
        geom->setNormalArray(normals, osg::Array::BIND_OVERALL);

        osg::Vec4Array* colors = new osg::Vec4Array;
        colors->push_back(osg::Vec4(1.0f, 1.0, 0.8f, 0.2f));
        geom->setColorArray(colors, osg::Array::BIND_OVERALL);

        geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));

        osg::Vec2Array* uvCoords = new osg::Vec2Array();
        uvCoords->push_back(osg::Vec2(0.0f, 1.0f));
        uvCoords->push_back(osg::Vec2(0.0f, 0.0f));
        uvCoords->push_back(osg::Vec2(1.0f, 0.0f));
        uvCoords->push_back(osg::Vec2(1.0f, 1.0f));
        geom->setTexCoordArray(0, uvCoords);

        osg::StateSet* stateset = geom->getOrCreateStateSet();
        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
        //stateset->setAttribute(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON);
        stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

        auto backgroundImage = osgDB::readImageFile("data/images/bg_image_01.bmp");
        auto texture = new osg::Texture2D(backgroundImage);
        stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);

        geode->addDrawable(geom);
    }

    camera->addChild(geode);
}
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);

if (viewer.addSlave(camera.get(), false))
{
    osg::notify(osg::NotifySeverity::INFO) << "Successfully added camera as slave!" << std::endl;
}
else
{
    osg::notify(osg::NotifySeverity::WARN) << "Failed to add camera as slave!" << std::endl;
}




The image gets loaded fine (due to the message I see in the console) and I see that the camera was added as a slave to the viewer. I based my code on the viewer example and on HUD examples.

Any help to get this resolved will be much appreciated!

Many thanks!

Cheers,
Vite Falcon
Code:




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




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20151113/be28f04c/attachment-0002.htm>


More information about the osg-users mailing list