转自:http://www.cnblogs.com/feixiang-peng/articles/3152754.html
写好了在osg中实时显示中文信息的效果。中间遇到两个问题,一个是
中文显示,一个是动态显示。在这过程中会出现程序崩溃的现象,相信做过的人肯定深有体会。其实解决的
办法很简单,就是再添加两行代码。一行:setlocale(LC_ALL,".936");是配置中文显示地域化信息,二行:
hudText->setDataVariance(osg::Object::DYNAMIC);是为了动态更新时候不出现程序崩溃。
void setupProperties(osgText::Text& textObject,osgText::Font* font, float size, const osg::Vec3& pos ) { textObject.setFont(font); // textObject.setCharacterSize(size); //字体大小 textObject.setPosition(pos); textObject.setColor(osg::Vec4(0.0,1.0,0.0,1.0)); textObject.setAlignment(osgText::Text::CENTER_BOTTOM); //文字显示方向 //textObject.setAxisAlignment(osgText::Text::SCREEN);//获取文字对称成方式正对屏幕方向 //textObject.setCharacterSizeMode(osgText::Text::SCREEN_COORDS);//跟随视角不断变化,离物体越远,文字越大 textObject.setAutoRotateToScreen( false ); //跟随视角不断变化,但离物体越远,文字越小,和现实当中像类似 textObject.setBackdropType(osgText::Text::OUTLINE); //对文字进行描边 textObject.setBackdropColor(osg::Vec4(1.0,1.0,0.0,1.0)); //描边颜色 textObject.setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX); //添加文字边框 textObject.setAxisAlignment(osgText::Text::XZ_PLANE);//获取文字对称成方式 } void createContent(osgText::Text& textObject, const char * string) { int requiredSize= mbstowcs (NULL,string,0); //如果mbstowcs第一参数为NULL那么返回字符串的数目 wchar_t * wText= new wchar_t [requiredSize+1]; mbstowcs (wText,string,requiredSize+1); //由char转换成wchar类型 textObject.setText(wText); delete wText; } osg::ref_ptr<osg::Group> ShowDynamicTexts() { setlocale (LC_ALL, ".936" ); // 配置地域化信息 const char * titleString= "时间" ; osg::Group* rootNode = new osg::Group; osg::Geode* geode = new osg::Geode; rootNode->addChild(geode); { titleTime= new osgText::Text; m_arialFont = osgText::readFontFile( "fonts/simkai.ttf" ); titleTime->setDataVariance(osg::Object::DYNAMIC); setupProperties(*titleTime,m_arialFont,30.0f,osg::Vec3(200,1140,0.0f)); createContent(*titleTime,titleString); geode->addDrawable(titleTime.get()); } return rootNode; } void RefreshTimeText(string textString) { setlocale (LC_ALL, ".936" ); // 配置地域化信息 createContent(*titleTime,textString.data()); } |