• 2.OsgEarth封装


    环境:Osg3.6.5         OsgEarth3.2        Qt5.15.2

            基于qt将osgEarth封装,在Qt中作为GLWidget进行呈现。

    1.Earth类的封装

            基于地球的初始化顺序进行了封装,并暴露出了一些必要的属性,类似viwer、map、mapNode等。最为重要的是widght属性,它是放置在qt中的重要承载物。

    1. #pragma once
    2. #include "../osgqt/GraphicsWindowQt.h"
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. namespace Cv {
    18. class CvEarth
    19. {
    20. public:
    21. CvEarth();
    22. ~CvEarth();
    23. osg::ref_ptr viewer;
    24. osg::ref_ptr root;
    25. osg::ref_ptr camera;
    26. osg::ref_ptr map;
    27. osg::ref_ptr mapNode;
    28. osg::ref_ptr earthForm;
    29. osg::ref_ptr em;
    30. osgQt::GLWidget* widget;
    31. private:
    32. void InitOSG();// 初始化设置osg
    33. void InitOsgearth();//初始化osgearth
    34. void InitSky();//天空初始化
    35. void InitUI();//界面初始化
    36. time_t now_time;
    37. tm* t_tm;
    38. osg::ref_ptr m_pSkyNode;//天空盒子节点
    39. };
    40. }
    1. #include "CvEarth.h"
    2. Cv::CvEarth::CvEarth()
    3. {
    4. InitOSG();
    5. InitOsgearth();
    6. InitUI();
    7. }
    8. Cv::CvEarth::~CvEarth()
    9. {
    10. }
    11. void Cv::CvEarth::InitOSG()
    12. {
    13. osgDB::Registry::instance()->addMimeTypeExtensionMapping("image/jpg;charset=utf8", "jpg");
    14. osgDB::Registry::instance()->addMimeTypeExtensionMapping("image/jpeg;charset=utf8", "jpg");
    15. osgDB::Registry::instance()->addMimeTypeExtensionMapping("image/png", "png");
    16. viewer = new osgViewer::Viewer;
    17. // 设置模型
    18. root = new osg::Group;
    19. //获取屏幕分辨率 长宽
    20. osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
    21. if (!wsi)
    22. return;
    23. unsigned int width, height;
    24. wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
    25. //设置图形环境特性
    26. osg::ref_ptr traits = new osg::GraphicsContext::Traits;
    27. traits->windowDecoration = false;//声明是否显示窗口的描述
    28. traits->x = 0;
    29. traits->y = 0;
    30. traits->width = width;
    31. traits->height = height;
    32. traits->doubleBuffer = true;//创建图形窗口是否使用双缓存
    33. //设置照相机
    34. camera = new osg::Camera;
    35. camera->setGraphicsContext(new osgQt::GraphicsWindowQt(traits.get()));
    36. camera->setClearColor(osg::Vec4(0.2, 0.2, 0.6, 1.0));
    37. camera->setViewport(new osg::Viewport(0, 0, width, height));
    38. camera->setProjectionMatrixAsPerspective(30.0f, (double(traits->width)) / (double(traits->height)), 1.0f, 10000.0f);
    39. //设置渲染器
    40. viewer->setCamera(camera);
    41. viewer->setSceneData(root);
    42. viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);//创建为单线程
    43. viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
    44. }
    45. void Cv::CvEarth::InitOsgearth()
    46. {
    47. map = new osgEarth::Map();
    48. mapNode = new osgEarth::MapNode(map.get());
    49. earthForm = new osg::MatrixTransform;
    50. //osgearth操作器 用来设置osgearh
    51. em = new osgEarth::Util::EarthManipulator;
    52. if (mapNode.valid())
    53. {
    54. em->setNode(mapNode);
    55. }
    56. em->getSettings()->setArcViewpointTransitions(true);
    57. //设置osg渲染窗口
    58. viewer->setCameraManipulator(em);
    59. //初始化天空
    60. InitSky();
    61. }
    62. void Cv::CvEarth::InitSky()
    63. {
    64. //获取当前时间 初始化天空
    65. now_time = time(0);
    66. t_tm = localtime(&now_time);
    67. osgEarth::DateTime cur_date_time(now_time);
    68. osgEarth::Ephemeris* ephemeris = new osgEarth::Ephemeris;
    69. //设置黑夜明暗程度
    70. osgEarth::Util::SkyOptions skyOptions;
    71. skyOptions.ambient() = 0.3;
    72. m_pSkyNode = osgEarth::SkyNode::create(skyOptions);
    73. m_pSkyNode->setName("SkyNode");
    74. m_pSkyNode->setEphemeris(ephemeris);
    75. m_pSkyNode->setDateTime(cur_date_time);
    76. viewer->setLightingMode(osg::View::SKY_LIGHT);
    77. m_pSkyNode->attach(viewer, 0);
    78. m_pSkyNode->setLighting(true);
    79. m_pSkyNode->addChild(mapNode);
    80. root->addChild(m_pSkyNode);
    81. }
    82. void Cv::CvEarth::InitUI()
    83. {
    84. // ui布局
    85. osgQt::GraphicsWindowQt* gw = dynamic_cast(camera->getGraphicsContext());
    86. widget = (gw->getGLWidget());
    87. }

    2.窗体中的使用

            将封装的CvEarth引用进来,并添加到对应的布局中。

    1. #include "MainForm.h"
    2. MainForm::MainForm(QWidget* parent)
    3. : QMainWindow(parent)
    4. {
    5. ui.setupUi(this);
    6. cvEarth = new Cv::CvEarth();
    7. //ui布局
    8. ui.mainLyt->addWidget(cvEarth->widget);
    9. _timer = new QTimer;
    10. QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(updateFrame()));
    11. _timer->start(10);
    12. //窗口最大化
    13. this->setWindowState(Qt::WindowMaximized);
    14. this->setWindowTitle(QString::fromLocal8Bit("CvEarth"));
    15. }
    16. MainForm::~MainForm()
    17. {}
    18. void MainForm::updateFrame()
    19. {
    20. cvEarth->viewer->frame();
    21. }

    3.效果

    空白地球

            运行后将会出现如图所示的空白地球,为啥空白是因为现状场景中我们未添加任何的数据,所以就只有一个白色的地球。

     

  • 相关阅读:
    [vue]在鼠标点击处,画点,并弹窗显示两个点的距离
    数字货币中短线策略(数据+回测+实盘)
    Go vs Java vs C# 语法对比
    7.0、C语言数据结构——线性表 (2)
    Llama改进之——分组查询注意力
    【vue组件】使用element-ui table 实现嵌套表格 点击展开时获取数据
    2022年前端还好找工作吗?
    使用open3d可视化3d人脸
    C/C++ 恨透了 double free or corruption
    web UI自动化介绍
  • 原文地址:https://blog.csdn.net/weixin_41012454/article/details/134085859