Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenSceneGraph: Creating background image
// 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;
}
@ViteFalcon

This comment has been minimized.

Copy link
Owner Author

ViteFalcon commented Nov 14, 2015

Second revision of this solved my problem

@bobykhani

This comment has been minimized.

Copy link

bobykhani commented Jul 26, 2017

Hi. I have a Problem With This Code.After Define "windows"(line 57) Without any initializing For That Check "(windows.empty())"(line 60) and The program return And Exit.Where is My Mistake?Please help Me.THX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.