|
MFC框架程序中,在画面上动态更新当前时刻。采用动态更新回调方法,创建HUD代码如下:
osg::ref_ptr<osg::Node> cOSG::createHUD()
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
//设置字体,必须是汉字字体,这里是华文彩云
std::string heiti("fonts/SIMHEI.TTF");
//设置状态,关闭灯光
osg::ref_ptr<osg::StateSet> stateset=geode->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
osg::Vec3 position(5.0f,10.0f,0.0f);
//设置字体属性
text = new osgText::Text;
text->setDataVariance(osg::Object:YNAMIC);
geode->addDrawable(text);
//设置字体
text->setFont(heiti);
//设置字体颜色
text->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
//设置字体大小
text->setCharacterSize(20.0f);
text->setAutoRotateToScreen(true);
//设置位置
text->setPosition(position);
CTime tm = CTime::GetCurrentTime();
CString time = tm.Format("%d %B %Y %H:%M:%S");
text->setText(time);
//设置相机
osg::ref_ptr<osg::Camera> camera=new osg::CameraNode;
//设置透视矩阵
camera->setProjectionMatrix(osg::Matrix:rtho2D(0,1024,0,768));
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
//得到默认设置
camera->setViewMatrix(osg::Matrix::identity());
//设置背景为透明,否则的话可以设置ClearColor
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
//设置渲染顺序,必须在最后渲染
camera->setRenderOrder(osg::CameraNode:OST_RENDER);
camera->addChild(geode);
return camera;
}
回调函数:
class TextCallBack : public osg::NodeCallback
{
public:
TextCallBack(osg::ref_ptr<osgText::Text> text):
Text(text)
{
}
virtual void operator()(osg::Node* node,osg::NodeVisitor* nv)
{
CTime t;
t=CTime::GetCurrentTime();//得到当前时间
CString time = t.Format("%d %B %Y %H:%M:%S");
Text->setText(time);
}
private:
osg::ref_ptr<osgText::Text> Text;
};
在MFC中启用定时器,1秒钟调用一次更新回调函数。在程序运行若干秒后就会中断,停在回调函数的Text->setText(time);这句,并弹出对话框,信息如下:
Unhandled exception at 0x014dded4 in CREAT_EARTH.exe: 0xC0000005: Access violation reading location 0xfeeeffe2.
请问问题出在什么地方? |
|