NeHe这节课主要讨论如何使用OpenGL显示2D文字。OpenGL显示2D文字是通过wglUseFontBitmaps函数创建了一系列的显示列表来实现的。通过阅读NeHe教程,读者应该能感觉到在OpenGL中显示文字还是比较麻烦的。在OSG中显示2D文字相对来说比较简单,OSG提供了专门用来处理文字的库osgText(包括二维和三维字体)
首先创建文字节点,由于osgText::Text继承于osg::Drawable可绘制物体,因此创建文本之后直接将其添加到叶节点中
osg::Group *root = new osg::Group; osg::MatrixTransform *moveMT = new osg::MatrixTransform; moveMT->setMatrix(osg::Matrix::translate(0, 0, -10)); root->addChild(moveMT); osg::Geode *fontGeode = new osg::Geode; osgText::Text *text = new osgText::Text; text->setCharacterSize(0.4f); text->setUpdateCallback(new FontColorCallback); text->setText("OpenGL NeHe"); text->setFont("fonts/arial.ttf"); fontGeode->addDrawable(text); moveMT->addChild(fontGeode); return root;对于OSG中的Text类,它提供了丰富的成员函数允许用户自由地调整排版格式、采用不同的位置和姿态、使用不同的颜色等操作。
由于场景中需要不断修改字体的颜色,我们使用Drawable的UpdateCallback来完成
class FontColorCallback : public osg::Drawable::UpdateCallback { public: virtual void update(osg::NodeVisitor*, osg::Drawable* drawable) { if (dynamic_cast<osgText::Text*>(drawable)) { osgText::Text *text = dynamic_cast<osgText::Text*>(drawable); text->setColor(osg::Vec4(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2)), 1)); std::stringstream os; std::string str; os.precision(2); os << std::fixed << "Active OpenGL Text With NeHe - " << cnt1; str = os.str(); text->setText(str); text->setPosition(osg::Vec3(-4.5f+0.5f*float(cos(cnt1)), 1.92f*float(sin(cnt2)),0)); cnt1+=0.051f; cnt2+=0.005f; } } };在每帧中修改字体的颜色和位置,与NeHe教程中的模式一样
最后编译运行程序:
附:本课源码(源码中可能存在不足和错误,仅供参考)
#include "../osgNeHe.h" #include <QtCore/QTimer> #include <QtGui/QApplication> #include <QtGui/QVBoxLayout> #include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osgQt/GraphicsWindowQt> #include <osg/MatrixTransform> #include <osgDB/ReadFile> #include <osgText/Text> #include <iostream> #include <sstream> float cnt1, cnt2; class FontColorCallback : public osg::Drawable::UpdateCallback { public: virtual void update(osg::NodeVisitor*, osg::Drawable* drawable) { if (dynamic_cast<osgText::Text*>(drawable)) { osgText::Text *text = dynamic_cast<osgText::Text*>(drawable); text->setColor(osg::Vec4(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2)), 1)); std::stringstream os; std::string str; os.precision(2); os << std::fixed << "Active OpenGL Text With NeHe - " << cnt1; str = os.str(); text->setText(str); text->setPosition(osg::Vec3(-4.5f+0.5f*float(cos(cnt1)), 1.92f*float(sin(cnt2)),0)); cnt1+=0.051f; cnt2+=0.005f; } } }; class ViewerWidget : public QWidget, public osgViewer::Viewer { public: ViewerWidget(osg::Node *scene = NULL) { QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(renderWidget); layout->setContentsMargins(0, 0, 0, 1); setLayout( layout ); connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) ); _timer.start( 10 ); } QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene ) { osg::Camera* camera = this->getCamera(); camera->setGraphicsContext( gw ); const osg::GraphicsContext::Traits* traits = gw->getTraits(); camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) ); camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) ); camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f ); camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0)); this->setSceneData( scene ); return gw->getGLWidget(); } osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false ) { osg::DisplaySettings* ds = osg::DisplaySettings::instance().get(); osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; traits->windowName = name; traits->windowDecoration = windowDecoration; traits->x = x; traits->y = y; traits->width = w; traits->height = h; traits->doubleBuffer = true; traits->alpha = ds->getMinimumNumAlphaBits(); traits->stencil = ds->getMinimumNumStencilBits(); traits->sampleBuffers = ds->getMultiSamples(); traits->samples = ds->getNumMultiSamples(); return new osgQt::GraphicsWindowQt(traits.get()); } virtual void paintEvent( QPaintEvent* event ) { frame(); } protected: QTimer _timer; }; osg::Node* buildScene() { osg::Group *root = new osg::Group; osg::MatrixTransform *moveMT = new osg::MatrixTransform; moveMT->setMatrix(osg::Matrix::translate(0, 0, -10)); root->addChild(moveMT); osg::Geode *fontGeode = new osg::Geode; osgText::Text *text = new osgText::Text; text->setCharacterSize(0.4f); text->setUpdateCallback(new FontColorCallback); text->setText("OpenGL NeHe"); text->setFont("fonts/arial.ttf"); fontGeode->addDrawable(text); moveMT->addChild(fontGeode); return root; } int main( int argc, char** argv ) { QApplication app(argc, argv); ViewerWidget* viewWidget = new ViewerWidget(buildScene()); viewWidget->setGeometry( 100, 100, 640, 480 ); viewWidget->show(); return app.exec(); }