osgPresentation.HUDTransform 是 OpenSceneGraph 中的一个类,用于将 2D 的 HUD(头顶显示器)元素添加到场景中,实现可视化的效果。
osg::Transform -> osgPresentation::HUDTransform
#include <osgPresentation/HUDTransform>
HUDTransform()
HUDTransform(const osgPresentation::HUDTransform &ht, const osg::CopyOp ©op = osg::CopyOp::SHALLOW_COPY)
bool addChild(osg::Node *child)
加入一个子节点(用于装载HUD元素)bool removeChild(osg::Node *child)
移除一个子节点(用于移除HUD元素)void setProjectionMatrix(const osg::Matrixd &matrix)
设置传入矩阵作为HUD节点的投影矩阵osg::Matrixd getProjectionMatrix() const
返回当前的投影矩阵osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3d( 0, 0, 0));
vertices->push_back(osg::Vec3d( 0, 0, 10));
vertices->push_back(osg::Vec3d(10, 0, 10));
vertices->push_back(osg::Vec3d(10, 0, 0));
geometry->setVertexArray(vertices.get());
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
geode->addDrawable(geometry.get());
root->addChild(geode);
osgPresentation::View* view = new osgPresentation::View;
// 创建一个HUDTexture
osg::Image* image = osgDB::readImageFile("image.png");
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D(image);
texture->setResizeNonPowerOfTwoHint(false);
texture->setDataVariance(osg::Object::DYNAMIC);
texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
osg::Geometry* quad = osg::createTexturedQuadGeometry(osg::Vec3(-1.f, 0.f, -1.f), osg::Vec3(2.f, 0.f, 0.f), osg::Vec3(0.f, 0.f, 2.f));
osgPresentation::HUDTexture* hudtex = new osgPresentation::HUDTexture();
hudtex->setTexture(texture);
hudtex->addChild(quad);
osgPresentation::HUDTransform* hudTrans = new osgPresentation::HUDTransform();
hudTrans->setProjectionMatrix(osg::Matrix::ortho2D(0, image->s(), 0, image->t()));
hudTrans->addChild(hudtex);
view->getCamera()->addChild(hudTrans);