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

来源:互联网 发布:鹏业软件股份有限公司 编辑:程序博客网 时间:2024/05/16 17:39
  • 简介

本节课实现在场景中绘制3D的轮廓字体,实现方式和第十三课中类似,只不过我们需要使用的是osgText库中的三维字体类osgText::Text3D

  • 实现

首先同样需要绘制osgText,将它加到场景的叶节点之中


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. osg::Group *root = new osg::Group;  
  2.   
  3. osg::MatrixTransform *moveMT = new osg::MatrixTransform;  
  4. moveMT->setMatrix(osg::Matrix::translate(-2, 0, -10));  
  5. root->addChild(moveMT);  
  6.   
  7. osg::MatrixTransform *rotX = new osg::MatrixTransform;  
  8. rotX->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, 0.5));  
  9. osg::MatrixTransform *rotY = new osg::MatrixTransform;  
  10. rotY->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, 0.4));  
  11. osg::MatrixTransform *rotZ = new osg::MatrixTransform;  
  12. rotZ->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Z_AXIS, 0.3));  
  13.   
  14. moveMT->addChild(rotX);  
  15. rotX->addChild(rotY);  
  16. rotY->addChild(rotZ);  
  17.   
  18. osg::Geode *fontGeode = new osg::Geode;  
  19.   
  20. osgText::Text3D *text = new osgText::Text3D;  
  21. text->setCharacterSize(0.8f);  
  22. text->setCharacterDepth(0.2f);  
  23. text->setUpdateCallback(new FontColorCallback);  
  24. text->setText("OpenGL NeHe");  
  25. text->setFont("fonts/arial.ttf");  
  26. fontGeode->addDrawable(text);  
  27.   
  28. rotZ->addChild(fontGeode);  
  29.   
  30. return root;  

唯一不同的是我们需要设置字体的"厚度",通过setCharacterDepth函数实现,为了简便起见,我们使用AnimationPathCallback来实现旋转

为了实现动态的颜色改变效果,我们使用和上一课一样的方式

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. class FontColorCallback : public osg::Drawable::UpdateCallback  
  2. {  
  3. public:  
  4.   
  5.     virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)  
  6.     {  
  7.         if (dynamic_cast<osgText::Text3D*>(drawable))  
  8.         {  
  9.             osgText::Text3D *text = dynamic_cast<osgText::Text3D*>(drawable);  
  10.             text->setColor(osg::Vec4(1.0f*float(cos(rot/20.0f)),1.0f*float(sin(rot/25.0f)),1.0f-0.5f*float(cos(rot/17.0f)), 1));  
  11.   
  12.             std::stringstream os;  
  13.             std::string str;  
  14.             os.precision(2);  
  15.             os << std::fixed << "NeHe - " << rot;  
  16.             str = os.str();  
  17.   
  18.             text->setText(str);  
  19.             rot+=0.5f;  
  20.         }  
  21.     }  
  22. };  

最后编译运行程序

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

[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 <osgDB/ReadFile>  
  14. #include <osgText/Text>  
  15. #include <osgText/Text3D>  
  16.   
  17. #include <osg/AnimationPath>  
  18.   
  19. #include <iostream>  
  20. #include <sstream>  
  21.   
  22. float rot = 0.0;  
  23.   
  24. class FontColorCallback : public osg::Drawable::UpdateCallback  
  25. {  
  26. public:  
  27.   
  28.     virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)  
  29.     {  
  30.         if (dynamic_cast<osgText::Text3D*>(drawable))  
  31.         {  
  32.             osgText::Text3D *text = dynamic_cast<osgText::Text3D*>(drawable);  
  33.             text->setColor(osg::Vec4(1.0f*float(cos(rot/20.0f)),1.0f*float(sin(rot/25.0f)),1.0f-0.5f*float(cos(rot/17.0f)), 1));  
  34.   
  35.             std::stringstream os;  
  36.             std::string str;  
  37.             os.precision(2);  
  38.             os << std::fixed << "NeHe - " << rot;  
  39.             str = os.str();  
  40.   
  41.             text->setText(str);  
  42.             rot+=0.5f;  
  43.         }  
  44.     }  
  45. };  
  46.   
  47.   
  48.   
  49. class ViewerWidget : public QWidget, public osgViewer::Viewer  
  50. {  
  51. public:  
  52.     ViewerWidget(osg::Node *scene = NULL)  
  53.     {  
  54.         QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);  
  55.   
  56.         QVBoxLayout* layout = new QVBoxLayout;  
  57.         layout->addWidget(renderWidget);  
  58.         layout->setContentsMargins(0, 0, 0, 1);  
  59.         setLayout( layout );  
  60.   
  61.         connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );  
  62.         _timer.start( 10 );  
  63.     }  
  64.   
  65.     QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )  
  66.     {  
  67.         osg::Camera* camera = this->getCamera();  
  68.         camera->setGraphicsContext( gw );  
  69.   
  70.         const osg::GraphicsContext::Traits* traits = gw->getTraits();  
  71.   
  72.         camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) );  
  73.         camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );  
  74.         camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );  
  75.         camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0));  
  76.   
  77.         this->setSceneData( scene );  
  78.   
  79.         return gw->getGLWidget();  
  80.     }  
  81.   
  82.     osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name=""bool windowDecoration=false )  
  83.     {  
  84.         osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();  
  85.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
  86.         traits->windowName = name;  
  87.         traits->windowDecoration = windowDecoration;  
  88.         traits->x = x;  
  89.         traits->y = y;  
  90.         traits->width = w;  
  91.         traits->height = h;  
  92.         traits->doubleBuffer = true;  
  93.         traits->alpha = ds->getMinimumNumAlphaBits();  
  94.         traits->stencil = ds->getMinimumNumStencilBits();  
  95.         traits->sampleBuffers = ds->getMultiSamples();  
  96.         traits->samples = ds->getNumMultiSamples();  
  97.   
  98.         return new osgQt::GraphicsWindowQt(traits.get());  
  99.     }  
  100.   
  101.     virtual void paintEvent( QPaintEvent* event )  
  102.     {   
  103.         frame();   
  104.     }  
  105.   
  106. protected:  
  107.   
  108.     QTimer _timer;  
  109. };  
  110.   
  111.   
  112.   
  113. osg::Node*  buildScene()  
  114. {  
  115.     osg::Group *root = new osg::Group;  
  116.   
  117.     osg::MatrixTransform *moveMT = new osg::MatrixTransform;  
  118.     moveMT->setMatrix(osg::Matrix::translate(-2, 0, -10));  
  119.     root->addChild(moveMT);  
  120.   
  121.     osg::MatrixTransform *rotX = new osg::MatrixTransform;  
  122.     rotX->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, 0.5));  
  123.     osg::MatrixTransform *rotY = new osg::MatrixTransform;  
  124.     rotY->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, 0.4));  
  125.     osg::MatrixTransform *rotZ = new osg::MatrixTransform;  
  126.     rotZ->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Z_AXIS, 0.3));  
  127.   
  128.     moveMT->addChild(rotX);  
  129.     rotX->addChild(rotY);  
  130.     rotY->addChild(rotZ);  
  131.       
  132.     osg::Geode *fontGeode = new osg::Geode;  
  133.   
  134.     osgText::Text3D *text = new osgText::Text3D;  
  135.     text->setCharacterSize(0.8f);  
  136.     text->setCharacterDepth(0.2f);  
  137.     text->setUpdateCallback(new FontColorCallback);  
  138.     text->setText("OpenGL NeHe");  
  139.     text->setFont("fonts/arial.ttf");  
  140.     fontGeode->addDrawable(text);  
  141.   
  142.     rotZ->addChild(fontGeode);  
  143.   
  144.     return root;  
  145. }  
  146.   
  147.   
  148.   
  149. int main( int argc, char** argv )  
  150. {  
  151.     QApplication app(argc, argv);  
  152.     ViewerWidget* viewWidget = new ViewerWidget(buildScene());  
  153.     viewWidget->setGeometry( 100, 100, 640, 480 );  
  154.     viewWidget->show();  
  155.     return app.exec();  
0 0
原创粉丝点击