Original: http://blog.csdn.net/tmljs1988/article/details/7562926
Can run
1. HUD Flowchart:
The complete source code is as follows:
/*OSG in the HUD, the text always appears in the front * *
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/Depth>
#include <osg/CameraNode>
#include <osgText/Text>
#pragma comment (lib, "Osgd.lib"); The library names in the debug version plus D, such as "Osgd.lib"
#pragma comment (lib, "OsgDBd.lib")
#pragma comment (lib, "OsgViewerd.lib");
#pragma comment (lib, "OsgTextd.lib");
osg::node* Createhud ()
{
Text
osgtext::text* Text = new Osgtext::text;
Set font
std::string Caiyun ("Fonts/stcaiyun. TTF ");//The Chinese font is set here
Text->setfont (Caiyun);
Set the position of text display
OSG::VEC3 position (150.0f,500.0f,0.0f);
Text->setposition (position);
Text->setcolor (OSG::VEC4 (1, 1, 0, 1));
Text->settext (L "OSG China website www.osgChina.org");//Set the displayed text
Geometry node
osg::geode* Geode = new Osg::geode ();
Geode->adddrawable (text);//Add text text as this drawable to Geode node
Set status
osg::stateset* Stateset = Geode->getorcreatestateset ();
Stateset->setmode (Gl_lighting,osg::stateattribute::off);//Turn Off the lights
Stateset->setmode (Gl_depth_test,osg::stateattribute::off);//Close depth test
Turn on Gl_blend blending mode (to ensure the alpha texture is correct)
Stateset->setmode (Gl_blend,osg::stateattribute::on);
Camera
osg::camera* Camera = new Osg::camera;
Set Perspective matrix
Camera->setprojectionmatrix (osg::matrix::ortho2d (0,600,0,600));//orthogonal projection
Set the absolute reference coordinate system to ensure that the view matrix is not affected by the transformation matrix of the ancestor node
Camera->setreferenceframe (OSG::TRANSFORM::ABSOLUTE_RF);
The view matrix is the default
Camera->setviewmatrix (Osg::matrix::identity ());
Set the background to transparent, otherwise you can set the Clearcolor
Camera->setclearmask (Gl_depth_buffer_bit);
Camera->setalloweventfocus (false);//do not respond to events, always get the focus
Set the render order, which must be rendered at the end
Camera->setrenderorder (osg::cameranode::P ost_render);
Camera->addchild (Geode)//The Geode node to be displayed is added to the camera
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 ());//Join a model
Root->addchild (Createhud ());//Add the camera of the HUD text to the root node
Viewer.setscenedata (Root.get ());
Viewer.realize ();
Viewer.run ();
return 0;
}
OSG HUD Header Text display