Skip to content

Instantly share code, notes, and snippets.

@ViteFalcon
Last active September 9, 2019 07:13
Show Gist options
  • Save ViteFalcon/90f9266bf9750e2d0142 to your computer and use it in GitHub Desktop.
Save ViteFalcon/90f9266bf9750e2d0142 to your computer and use it in GitHub Desktop.
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 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());
@ViteFalcon
Copy link
Author

Second revision of this solved my problem

@bobykhani
Copy link

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