• OSG文字-HUD显示汉字示例(3)


            显示文字是一种非常实用的技术,可以用来把一些重要的文字始终显示在屏幕上。HUD的全称是HeadsUpDisplay,即抬头显示,这种技术最早应用在军事战斗机上。

            创建HUD显示的基本步骤如下:

            <1> 创建一个osg::Camera对象,设置视图、投影矩阵及渲染顺序,以确保在场景图形全部渲染完后才进行渲染。

            <2> 创建一个osg::Geode 对象和一个osgText::Text 对象,并设置相应的文字属性。把osgText::Text对象添加到 Geode 叶节点。

            <3> 把叶节点Geode关联到步骤(1)所创建的相机

            在创建HUD显示文字显示时,需要注意的有如下几点:

    • 渲染顺序设置为 POST,否则可能会被场景中的其他图形所覆盖。
    • 注意关闭光照和深度
    • 投影矩阵通常设置为屏幕尺寸大小。

            代码如程序清单9-3所示。

    1. 1. /* HUD 显示汉字 */
    2. 2. osg::ref_ptr createHUDText(const string strFontPath)
    3. 3. {
    4. 4. osg::ref_ptr camera = new osg::Camera();
    5. 5.
    6. 6. // 设置投影矩阵
    7. 7. camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1280, 0, 800));
    8. 8.
    9. 9. // 设置视图矩阵,同事确保不被场景中其它图形位置变换影响,使用绝对帧引用
    10. 10. camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    11. 11. camera->setViewMatrix(osg::Matrix::identity());
    12. 12.
    13. 13. // 清除深度缓存
    14. 14. camera->setClearMask(GL_DEPTH_BUFFER_BIT);
    15. 15.
    16. 16. // 设置渲染顺序为POST
    17. 17. camera->setRenderOrder(osg::Camera::POST_RENDER);
    18. 18.
    19. 19. // 设置为不接收时间,始终不得到焦点
    20. 20. camera->setAllowEventFocus(false);
    21. 21.
    22. 22. osg::ref_ptr geode = new osg::Geode();
    23. 23. osg::ref_ptr stateset = geode->getOrCreateStateSet();
    24. 24.
    25. 25. // 关闭光照
    26. 26. stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    27. 27.
    28. 28. // 关闭深度测试
    29. 29. stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
    30. 30.
    31. 31. // 设置文字
    32. 32. osg::ref_ptr text = new osgText::Text();
    33. 33. osg::ref_ptr font = new osgText::Font();
    34. 34. font = osgText::readFontFile(strFontPath);
    35. 35. text->setFont(font.get());
    36. 36. text->setText(L"http://www.OsgChina.osg-OpenScenseGraph 中国官方");
    37. 37. text->setPosition(osg::Vec3(100.0, 600.0, 0));
    38. 38. text->setCharacterSize(40.0);
    39. 39. text->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
    40. 40. text->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX);
    41. 41.
    42. 42. geode->addDrawable(text.get());
    43. 43. camera->addChild(geode.get());
    44. 44.
    45. 45. return camera.get();
    46. 46. }
    47. 47.
    48. 48. void osgText_HUD_Text_9_3(const string &strDataFolder)
    49. 49. {
    50. 50. osg::ref_ptr viewer = new osgViewer::Viewer();
    51. 51. osg::ref_ptr traits = new osg::GraphicsContext::Traits;
    52. 52. traits->x = 40;
    53. 53. traits->y = 40;
    54. 54. traits->width = 600;
    55. 55. traits->height = 480;
    56. 56. traits->windowDecoration = true;
    57. 57. traits->doubleBuffer = true;
    58. 58. traits->sharedContext = 0;
    59. 59.
    60. 60. osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    61. 61.
    62. 62. osg::ref_ptr camera = viewer->getCamera();
    63. 63. camera->setGraphicsContext(gc.get());
    64. 64. camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
    65. 65. GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
    66. 66. camera->setDrawBuffer(buffer);
    67. 67. camera->setReadBuffer(buffer);
    68. 68.
    69. 69. osg::ref_ptr root = new osg::Group();
    70. 70.
    71. 71. // 读取模型
    72. 72. string strDataPath = strDataFolder + "logo.ive";
    73. 73. osg::ref_ptr node = osgDB::readNodeFile(strDataPath);
    74. 74.
    75. 75. root->addChild(node.get());
    76. 76.
    77. 77. // 添加HUD文字
    78. 78. string strFontPath = strDataFolder + "font\\simhei.ttf";
    79. 79. root->addChild(createHUDText(strFontPath));
    80. 80.
    81. 81. // 优化场景数据
    82. 82. osgUtil::Optimizer optimizer;
    83. 83. optimizer.optimize(root.get());
    84. 84.
    85. 85. viewer->setSceneData(root.get());
    86. 86. viewer->realize();
    87. 87. viewer->run();
    88. 88. }

            运行程序,截图如图9-5 所示。

    图9-5 HUD显示汉字示例截图

  • 相关阅读:
    分布式锁之Redis实现
    Redis哨兵机制配置实战-实测(Redis6.2.5版本)
    全面重构存储系统,释放AI时代全新数据价值
    Postgresql支持的浮点类型和区别案例
    ES6
    RocketMQ生产环境常见问题分析与总结
    逆向分析:基于 JS 字节码的保护技术
    Python实现聚类分析和数据降维
    Apache顶级项目Ranger和Alluxio的最佳实践(附教程)
    ModbusTCP 转 Profinet 主站网关控制汇川伺服驱动器配置案例
  • 原文地址:https://blog.csdn.net/liangfei868/article/details/134561529