OSG的HUD抬头文字显示

原文:http://blog.csdn.net/tmljs1988/article/details/7562926

可以运行

1.       HUD流程图:

完整源代码如下:

/*OSG中的HUD,文字总是显示在最前面*/

#include <osgDB/ReadFile>

#include <osgViewer/Viewer>

#include <osg/Geode>

#include <osg/Depth>

#include <osg/CameraNode>

#include <osgText/Text>

#pragma comment( lib, "osgd.lib"); //.在Debug版本下的库名都加d,如"osgd.lib"

#pragma comment( lib, "osgDBd.lib")

#pragma comment( lib, "osgViewerd.lib");

#pragma comment( lib, "osgTextd.lib");

osg::Node* createHUD()

{

    //文字

    osgText::Text* text = new osgText::Text;

    //设置字体

    std::string caiyun("fonts /STCAIYUN.TTF");//此处设置的是汉字字体

    text->setFont(caiyun); 

    //设置文字显示的位置

    osg::Vec3 position(150.0f,500.0f,0.0f);

    text->setPosition(position);   

    text->setColor( osg::Vec4( 1, 1, 0, 1));

    text->setText(L"osg中国官网网站www.osgChina.org");//设置显示的文字

    //几何体节点

    osg::Geode* geode = new osg::Geode();

    geode->addDrawable( text );//将文字Text作这drawable加入到Geode节点中

    //设置状态

    osg::StateSet* stateset = geode->getOrCreateStateSet(); 

    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);//关闭灯光

    stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);//关闭深度测试

    //打开GL_BLEND混合模式(以保证Alpha纹理正确)

    stateset->setMode(GL_BLEND,osg::StateAttribute::ON);

    //相机

    osg::Camera* camera = new osg::Camera;

    //设置透视矩阵

    camera->setProjectionMatrix(osg::Matrix::ortho2D(0,600,0,600));//正交投影   

    //设置绝对参考坐标系,确保视图矩阵不会被上级节点的变换矩阵影响

    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);

    //视图矩阵为默认的

    camera->setViewMatrix(osg::Matrix::identity());

    //设置背景为透明,否则的话可以设置ClearColor 

    camera->setClearMask(GL_DEPTH_BUFFER_BIT);

    camera->setAllowEventFocus( false);//不响应事件,始终得不到焦点

    //设置渲染顺序,必须在最后渲染

    camera->setRenderOrder(osg::CameraNode::POST_RENDER); 

    camera->addChild(geode);//将要显示的Geode节点加入到相机

    return camera; 

};

 

int main( int argc, char **argv )

{

    osgViewer::Viewer viewer; 

    osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("fountain.osg");

    osg::ref_ptr<osg::Group> root= new osg::Group; 

    root->addChild( model.get());//加入某个模型

    root->addChild(createHUD());//把HUD文字的相机加入到根节点下

    viewer.setSceneData( root.get());

    viewer.realize();

    viewer.run() ;  

 

    return 0;

}

原文地址:https://www.cnblogs.com/zhizhan/p/4957430.html