- 显示文本;
- 显示纹理;
#include <Windows.h>
#include <osg/Node>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Camera>
#include <osg/Texture2D>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgText/Text>
osg::Camera* createHUDCamera(double left, double right, double bottom, double top)
{
osg::ref_ptr<osg::Camera> camera = new osg::Camera();
camera->setProjectionMatrix(osg::Matrix::ortho2D(left, right, bottom, top));
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setRenderOrder(osg::Camera::POST_RENDER);
camera->setAllowEventFocus(false);
camera->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
return camera.release();
}
osgText::Text* createText(const std::string& str)
{
osg::ref_ptr<osgText::Text> text = new osgText::Text();
text->getOrCreateStateSet()->setRenderBinDetails(0, "DepthSortedBin");
text->setPosition(osg::Vec3(10.0, 10.0, 0.0));
text->setCharacterSize(32);
text->setText(str);
return text.release();
}
osg::Geode* createTexture(const std::string& path)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->getOrCreateStateSet()->setRenderBinDetails(1, "DepthSortedBin");
osg::ref_ptr<osg::Geometry> geometry = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(200.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 200.0f, 0.0f));
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D();
texture->setImage(osgDB::readImageFile(path));
geometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture);
geode->addDrawable(geometry);
return geode.release();
}
int main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> group = new osg::Group();
group->addChild(osgDB::readNodeFile("glider.osg"));
osg::ref_ptr<osg::Camera> camera = createHUDCamera(0, 800, 0, 600);
camera->addChild(createText("glider"));
camera->addChild(createTexture("Images/blueFlowers.png"));
group->addChild(camera);
viewer.setSceneData(group);
viewer.setUpViewInWindow(100, 100, 500, 400);
return viewer.run();
}