osgEarth实现三维场景中HUD的显示

在osgearth的三维场景中,为了实现添加项目名称或者其他固定在屏幕某一位置的内容,可以采用HUD技术进行开发。本文以添加项目名称“三维仿真系统”为例,实现了osgearth三维场景中HUD的显示,实现效果如图1所示。在开发的过程中,由于涉及到文字的添加,还需要利用OSG提供的osgText类进行文字的相关设置。

osgEarth实现三维场景中HUD的显示_第1张图片

 

osgEarth实现文字的HUD显示关键代码如下:

osg::ref_ptr OSGEarthSystem::showHUD()
{ 
	setlocale(LC_ALL,"chs");
	osg::ref_ptr geode = new osg::Geode();
	osg::ref_ptr 
    fontHei=osgText::readFontFile("C:\\Windows\\Fonts\\simkai.ttf");
	osg::Vec3 position(850.0f,30.0f,0.0f); 
	mytext = new osgText::Text; 
	geode->addDrawable(mytext);  
	mytext->setFont(fontHei); 
	mytext->setPosition(position);
	mytext->setCharacterSize(20.0f);
	mytext->setColor(osg::Vec4(1,1,1,1));
	mytext->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX);
	char string[100]="三维仿真系统";
	int requiredSize=mbstowcs(NULL,string,0);
	wchar_t* wText=new wchar_t[requiredSize+1];
	mbstowcs(wText,string,requiredSize+1);
	mytext->setText(wText); 
	delete wText;

	osg::ref_ptr camera = new osg::Camera; 
	camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1024,0,768));       
	camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); 
	camera->setViewMatrix(osg::Matrix::identity()); 
	camera->setClearMask(GL_DEPTH_BUFFER_BIT); 
	camera->setRenderOrder(osg::Camera::POST_RENDER);  
	camera->addChild(geode); 
	return camera;

	osg::ref_ptr texttran = new osg::MatrixTransform;
	texttran->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(90.0), 1, 0, 0));
	texttran->addChild(geode);
	return texttran;
}

 

你可能感兴趣的:(osgEarth,osg)