|
本帖最后由 nie 于 2014-4-1 16:23 编辑
我想用更新回调或事件处理的方式将飞机飞行高度信息显示出来,
我用一个每帧加一的数来表示高度;代码如下:
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/Depth>
#include <osg/CameraNode>
#include <osgText/Text>
#include <osgGA/TrackballManipulator>
#include <sstream>
#pragma comment( lib, "osgd.lib");
#pragma comment( lib, "osgDBd.lib")
#pragma comment( lib, "osgViewerd.lib");
#pragma comment( lib, "osgTextd.lib");
#pragma comment( lib, "osgGAd.lib");
using namespace std;
//将string转换成float型
float StringtoFloat(std::string str)
{
std::istringstream iss(str);
float num;
iss>>num;
return num;
}
//将float型转成string型
std::string FloattoString(float x)
{
float y = x;
std::string gdlist = "";
std:stringstream os;
os<<y;
gdlist = os.str();
return gdlist;
}
//事件类
class CHUD_viewPoint: public osgGA::GUIEventHandler
{
public:
/**构造函数*/
CHUD_viewPoint(float* updatepp):
m_pp(updatepp) ,xx(3454.22){
}
~CHUD_viewPoint(){}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
void UpdateText(osgViewer::Viewer* viewer,const osgGA::GUIEventAdapter&);
protected:
float* m_pp;//高度信息
float xx;//当前高度
};
bool CHUD_viewPoint::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::FRAME):
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
UpdateText( viewer, ea);//更新文字信息
//}
xx++;
return true;
}
default:
return false;
}
}
void CHUD_viewPoint::UpdateText(osgViewer::Viewer* viewer,const osgGA::GUIEventAdapter&)
{
*m_pp = xx;
}
osg::Node* createHUD_viewPoint( float* pp)//想从这里将更新的高度值传进来进行绘图
{
std::string gdlist;
gdlist= FloattoString(*pp);
//文字
osgText::Text* text = new osgText::Text;
std::string timesFont("fonts/times.ttf");
text->setFont(timesFont);
osg::Vec3 position(400.0f,100.0f,0.0f);
text->setPosition(position);
text->setColor( osg::Vec4( 1, 1, 0, 1));
text->setText(gdlist);
text->setCharacterSize(15);
text->setDataVariance(osg::Object:YNAMIC);
//几何体节点
osg::Geode* geode = new osg::Geode();
geode->addDrawable( text );
osg::StateSet* stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
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());
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setAllowEventFocus( false);
camera->setRenderOrder(osg::CameraNode:OST_RENDER);
camera->addChild(geode);
return camera;
};
int main( int argc, char **argv )
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> root= new osg::Group;
float* pp = new float;
osg::ref_ptr< CHUD_viewPoint> pHUD= new CHUD_viewPoint(pp);
root->addChild( createHUD_viewPoint(pp));
viewer.addEventHandler( pHUD.get());
viewer.setSceneData( root.get());
viewer.realize();
viewer.run() ;
return 0;
}
但是,显示出来的数是一个随机数,
应该是createHUD_viewPoint()并没有获得实时更新的数据,不知道为什么
求大神帮忙看下我这个程序
|
|