参考Ogre Snippets上的文章:
http://www.ogre3d.org/tikiwiki/MovableText
实现功能:在SceneNode节点位置上显示文本
开发环境:VS2013 x64
STEP 1:
C/C++附加目录中添加OgreSDK\include\OGRE\Overlay
链接器->输入中添加OgreOverlay_d.lib和OgreOverlay.lib
STEP 2:
在OgreSDK\media文件夹中添加fonts文件夹,把要调用的TTF字体文件放入其中
同时注意在resource_d.cfg和resource.cfg文件中添加fonts文件夹所在路径
STEP 3:
修改MovableText类的构造函数
MovableText::MovableText(const String &name, const String &caption, const String &fontName , Real charHeight , const ColourValue &color )
:mpCam(NULL), mpWin(NULL), mName(name), mCaption(caption), mFontName(fontName),
mCharHeight(charHeight), mColor(color), mType("MovableText"),
mTimeUntilNextToggle(0), mSpaceWidth(0), mUpdateColors(true),
mOnTop(false), mHorizontalAlignment(H_LEFT), mVerticalAlignment(V_BELOW),
mAdditionalHeight(0.0), m_RelativeX(0.0), m_RelativeY(0.0)
{
if (name == "")
throw Exception(Exception::ERR_INVALIDPARAMS, "Trying to create MovableText without name", "MovableText::MovableText");
if (caption == "")
throw Exception(Exception::ERR_INVALIDPARAMS, "Trying to create MovableText without caption", "MovableText::MovableText");
// 修改:加载字体
mpFont = Ogre::FontManager::getSingleton().create("sample", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
mpFont->setSource(mFontName); // 设置字体
mpFont->setType(FT_TRUETYPE); // 设置字体类型
mpFont->setTrueTypeSize(18); // 设置字体纹理大小
mpFont->setTrueTypeResolution(96 * 2); // 设置打印分辨率,一般为96
mpFont->addCodePointRange(Font::CodePointRange(33, 166)); // 设置英文UNICODE的INT值范围
// mpFont->addCodePointRange(Font::CodePointRange(19968, 40869)); // 设置汉子UNICODE的INT值范围
mpFont->load();
mRenderOp.vertexData = NULL;
this->setFontName(mFontName);
this->_setupGeometry();
}
其中,参数caption为要显示的文本,fontName为字体名称,charHeight为字体大小,color为颜色
STEP 4:
调用MovableText类的方法
// Ogre 1.9 使用Overlay前先初始化OverlaySystem
Ogre::OverlaySystem* mOverlaySystem = new Ogre::OverlaySystem();
mSceneMgr->addRenderQueueListener(mOverlaySystem);
// 调用MovalbeText类,参数2为文字内容,参数3为字体类型,参数4为字体大小,参数5为字体颜色
// 参数3必须是fonts文件夹中的字体
MovableText* msg = new MovableText("MSG_0", "NO.23003688", "solo5.ttf", 0.8f, ColourValue::Blue);
// 文字与节点的相对位置(H_CENTER/H_LFET V_ABOVE/V_BELOW)
msg->setTextAlignment(MovableText::H_CENTER, MovableText::V_ABOVE);
SceneNode* nodeText = mSceneMgr->getRootSceneNode()->createChildSceneNode("nodeText", Vector3(0, 3, 0));
nodeText->attachObject(msg);
// nodeText->pitch(Degree(-90.0f)); // 文字的默认方向是朝向Z轴正方向,若要朝向Y轴正方向则pitch(-90度)