OpenSceneGraph实现的NeHe OpenGL教程 - 第四十三课

  • 简介

这节课NeHe教我们怎么使用FreeType字体库来创建字体,在OSG中通过FreeType的插件已经支持FreeType字体了,前面很多课程中的字体就是使用FreeType字体库创建的。

  • 实现

本课的实现过程十分的简单,创建字体,然后再回调中修改字体显示:

创建字体代码如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. osg::Node*  createFont1()  
  2. {  
  3.     osg::MatrixTransform *zoomMT = new osg::MatrixTransform;  
  4.     zoomMT->setMatrix(osg::Matrix::translate(-3.0f,3.0f,-10.0f));  
  5.   
  6.     osgText::Text *textWords = new osgText::Text;  
  7.     textWords->setColor(osg::Vec4(0,0,1,1));  
  8.     textWords->setText("Active WGL Bitmap Text With NeHe");  
  9.     textWords->setFont("Fonts/Arial.ttf");  
  10.     textWords->setCharacterSize(0.3f);  
  11.     textWords->setUpdateCallback(new Font1UpdateCallback);  
  12.   
  13.     osg::Geode *fontGeode = new osg::Geode;  
  14.     fontGeode->addDrawable(textWords);  
  15.       
  16.     zoomMT->addChild(fontGeode);  
  17.   
  18.     return zoomMT;  
  19. }  

在字体回调中不断修改字体的显示内容:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class Font1UpdateCallback : public osg::Drawable::UpdateCallback  
  2. {  
  3. public:  
  4.   
  5.     virtual void update(osg::NodeVisitor*, osg::Drawable* draw)  
  6.     {  
  7.         static float cnt1 = 0.0;  
  8.   
  9.         osgText::Text *text = dynamic_cast<osgText::Text*>(draw);  
  10.         if (!text)  
  11.             return;  
  12.   
  13.         std::stringstream os;  
  14.         std::string str;  
  15.         os.precision(2);  
  16.         os << std::fixed << "Active WGL Bitmap Text With NeHe - " << cnt1;  
  17.         str = os.str();  
  18.         text->setText(str);  
  19.   
  20.         cnt1 += 0.01;  
  21.     }  
  22.   
  23. };  

将两部分添加到场景根节点中:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. osg::Group *root = new osg::Group;  
  2. root->addChild(createFont1());  
  3. root->addChild(createFont2());  
  4. return root;  

编译运行程序:

附:本课源码(源码中可能存在错误和不足之处,仅供参考)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "../osgNeHe.h"  
  2.   
  3. #include <QtCore/QTimer>  
  4. #include <QtGui/QApplication>  
  5. #include <QtGui/QVBoxLayout>  
  6.   
  7. #include <osgViewer/Viewer>  
  8. #include <osgDB/ReadFile>  
  9. #include <osgQt/GraphicsWindowQt>  
  10.   
  11. #include <osg/MatrixTransform>  
  12.   
  13. #include <osgText/Text>  
  14. #include <osg/AnimationPath>  
  15.   
  16.   
  17.   
  18. //  
  19.   
  20. class Font1UpdateCallback : public osg::Drawable::UpdateCallback  
  21. {  
  22. public:  
  23.   
  24.     virtual void update(osg::NodeVisitor*, osg::Drawable* draw)  
  25.     {  
  26.         static float cnt1 = 0.0;  
  27.   
  28.         osgText::Text *text = dynamic_cast<osgText::Text*>(draw);  
  29.         if (!text)  
  30.             return;  
  31.   
  32.         std::stringstream os;  
  33.         std::string str;  
  34.         os.precision(2);  
  35.         os << std::fixed << "Active WGL Bitmap Text With NeHe - " << cnt1;  
  36.         str = os.str();  
  37.         text->setText(str);  
  38.   
  39.         cnt1 += 0.01;  
  40.     }  
  41.   
  42. };  
  43.   
  44.   
  45. class Font2UpdateCallback : public osg::Drawable::UpdateCallback  
  46. {  
  47. public:  
  48.   
  49.     virtual void update(osg::NodeVisitor*, osg::Drawable* draw)  
  50.     {  
  51.         static float cnt1 = 0.0;  
  52.   
  53.         osgText::Text *text = dynamic_cast<osgText::Text*>(draw);  
  54.         if (!text)  
  55.             return;  
  56.   
  57.         std::stringstream os;  
  58.         std::string str;  
  59.         os.precision(2);  
  60.         os << std::fixed << "Active FreeType Text - " << cnt1;  
  61.         str = os.str();  
  62.         text->setText(str);  
  63.   
  64.         cnt1 += 0.02;  
  65.     }  
  66. };  
  67.   
  68.   
  69.   
  70.   
  71. //  
  72.   
  73. osg::Node*  createFont1()  
  74. {  
  75.     osg::MatrixTransform *zoomMT = new osg::MatrixTransform;  
  76.     zoomMT->setMatrix(osg::Matrix::translate(-3.0f,3.0f,-10.0f));  
  77.   
  78.     osgText::Text *textWords = new osgText::Text;  
  79.     textWords->setColor(osg::Vec4(0,0,1,1));  
  80.     textWords->setText("Active WGL Bitmap Text With NeHe");  
  81.     textWords->setFont("Fonts/Arial.ttf");  
  82.     textWords->setCharacterSize(0.3f);  
  83.     textWords->setUpdateCallback(new Font1UpdateCallback);  
  84.   
  85.     osg::Geode *fontGeode = new osg::Geode;  
  86.     fontGeode->addDrawable(textWords);  
  87.       
  88.     zoomMT->addChild(fontGeode);  
  89.   
  90.     return zoomMT;  
  91. }  
  92.   
  93.   
  94. osg::Node*  createFont2()  
  95. {  
  96.     osg::MatrixTransform *zoomMT = new osg::MatrixTransform;  
  97.     zoomMT->setMatrix(osg::Matrix::translate(0.0f,-2.0f,-1.0f));  
  98.   
  99.     osg::MatrixTransform *rotZ = new osg::MatrixTransform;  
  100.     rotZ->setMatrix(osg::Matrix::rotate(0, osg::Z_AXIS));  
  101.     rotZ->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::Z_AXIS, 0.5));  
  102.   
  103.     osg::MatrixTransform *transMT = new osg::MatrixTransform;  
  104.     transMT->setMatrix(osg::Matrix::translate(-5, 0, -10));  
  105.   
  106.     zoomMT->addChild(rotZ);  
  107.     rotZ->addChild(transMT);  
  108.       
  109.     osgText::Text *textWords = new osgText::Text;  
  110.     textWords->setColor(osg::Vec4(1,0,0,1));  
  111.     textWords->setText("Active FreeType Text");  
  112.     textWords->setFont("Fonts/Arial.ttf");  
  113.     textWords->setCharacterSize(0.6f);  
  114.     textWords->setUpdateCallback(new Font2UpdateCallback);  
  115.   
  116.     osg::Geode *fontGeode = new osg::Geode;  
  117.     fontGeode->addDrawable(textWords);  
  118.       
  119.     transMT->addChild(fontGeode);  
  120.     return zoomMT;  
  121. }  
  122.   
  123. //  
  124.   
  125.   
  126.   
  127.   
  128. class ViewerWidget : public QWidget, public osgViewer::Viewer  
  129. {  
  130. public:  
  131.     ViewerWidget(osg::Node *scene = NULL)  
  132.     {  
  133.         QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);  
  134.   
  135.         QVBoxLayout* layout = new QVBoxLayout;  
  136.         layout->addWidget(renderWidget);  
  137.         layout->setContentsMargins(0, 0, 0, 1);  
  138.         setLayout( layout );  
  139.   
  140.         connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );  
  141.         _timer.start( 10 );  
  142.     }  
  143.   
  144.     QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )  
  145.     {  
  146.         osg::Camera* camera = this->getCamera();  
  147.         camera->setGraphicsContext( gw );  
  148.   
  149.         const osg::GraphicsContext::Traits* traits = gw->getTraits();  
  150.   
  151.         camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) );  
  152.         camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );  
  153.         camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );  
  154.         camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 0), osg::Vec3d(0, 0, -1), osg::Vec3d(0, 1, 0));  
  155.   
  156.         this->setSceneData( scene );  
  157.   
  158.         return gw->getGLWidget();  
  159.     }  
  160.   
  161.     osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name=""bool windowDecoration=false )  
  162.     {  
  163.         osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();  
  164.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
  165.         traits->windowName = name;  
  166.         traits->windowDecoration = windowDecoration;  
  167.         traits->x = x;  
  168.         traits->y = y;  
  169.         traits->width = w;  
  170.         traits->height = h;  
  171.         traits->doubleBuffer = true;  
  172.         traits->alpha = ds->getMinimumNumAlphaBits();  
  173.         traits->stencil = ds->getMinimumNumStencilBits();  
  174.         traits->sampleBuffers = ds->getMultiSamples();  
  175.         traits->samples = ds->getNumMultiSamples();  
  176.   
  177.         return new osgQt::GraphicsWindowQt(traits.get());  
  178.     }  
  179.   
  180.     virtual void paintEvent( QPaintEvent* event )  
  181.     {   
  182.         frame();   
  183.     }  
  184.   
  185. protected:  
  186.   
  187.     QTimer _timer;  
  188. };  
  189.   
  190.   
  191.   
  192. osg::Node*  buildScene()  
  193. {  
  194.     osg::Group *root = new osg::Group;  
  195.     root->addChild(createFont1());  
  196.     root->addChild(createFont2());  
  197.     return root;  
  198. }  
  199.   
  200.   
  201.   
  202. int main( int argc, char** argv )  
  203. {  
  204.     QApplication app(argc, argv);  
  205.     ViewerWidget* viewWidget = new ViewerWidget(buildScene());  
  206.     viewWidget->setGeometry( 100, 100, 640, 480 );  
  207.     viewWidget->show();  
  208.     return app.exec();  
  209. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值