当前位置:   article > 正文

在QT中搭建OSG开发环境_qt osg 集成

qt osg 集成

一、概述

        OpenSceneGraph(简称OSG)使用OpenGL技术开发,是一套基于C++平台的应用程序接口(API),它让程序员能够更加快速、便捷地创建高性能、跨平台的交互式图形程序。它作为中间件(middleware)为应用软件提供了各种高级渲染特性,IO,以及空间结构组织函数;而更低层次的OpenGL 硬件抽象层(HAL)实现了底层硬件显示的驱动。

        网上关于编译osg的资料太多,关于OSG的编译在此不过多赘述,本人使用的是vs2015+osg365,本篇将详细讲解一下如何在QT中搭建OSG开发环境。

        无论是集成到QT还是MFC还是其他开发环境,归根到底是显示方式的不同,就好比看视频,可以投屏、可以投影,只要找到显示的窗体类,借助窗体类完成显示即可,这是问题的思路。

        OSG集成到QT中有两种方式:

  • osgQt。在较老版本的osg中自带osgQt源码包,主要适配的是QT4,网址:osgQt
  • osgQOpenGL。需要自行编译适配的osg版本,主要适配QT5。

        本文采用的是第一种方式,采用osgQt的方式,源码集成。为什么采用源码集成而不采用原有的lib文件,是因为方便做二次开发,这在后面用到会逐渐讲到。


二、准备 GraphicsWindowQt

        首先是文件GraphicsWindowQt源码,这里拿到源码需要修改一下,修改项目如下:

  • GraphicsWindowQt.h:
    • #include <QMutex>改为#include <QtCore/QMutex>;
    • 删除 #include <osgQt/Export>;
    • 删除 OSGQT_EXPORT 关键字。
  •  GraphicsWindowQt.cpp:
    • #include <QMutex>改为#include <QtCore/QMutex>;
    • 删除 OSGQT_EXPORT 关键字;
    • 578行:
      getEventQueue()->syncWindowRectangleWithGraphicsContext();

      改为:

      getEventQueue()->getCurrentEventState()->getGraphicsContext();

         如果不想改,我改好的地址:osgQt修改版,提取码:0501。


三、代码集成

  • 新建空白Qt Widgets  Application 项目,新建osgwidget QT设计师类,pro文件中引入opengl:
    QT       += core gui opengl

  • 引入osg相关外部依赖: 
    1. win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losg
    2. else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgd
    3. else:unix: LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losg
    4. INCLUDEPATH += $$PWD/../../osg/denpendency/osg/include
    5. DEPENDPATH += $$PWD/../../osg/denpendency/osg/include
    6. win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -lOpenThreads
    7. else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -lOpenThreadsd
    8. else:unix: LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -lOpenThreads
    9. INCLUDEPATH += $$PWD/../../osg/denpendency/osg/include
    10. DEPENDPATH += $$PWD/../../osg/denpendency/osg/include
    11. win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgGA
    12. else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgGAd
    13. else:unix: LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgGA
    14. INCLUDEPATH += $$PWD/../../osg/denpendency/osg/include
    15. DEPENDPATH += $$PWD/../../osg/denpendency/osg/include
    16. win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgViewer
    17. else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgViewerd
    18. else:unix: LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgViewer
    19. INCLUDEPATH += $$PWD/../../osg/denpendency/osg/include
    20. DEPENDPATH += $$PWD/../../osg/denpendency/osg/include
    21. win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgDB
    22. else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgDBd
    23. else:unix: LIBS += -L$$PWD/../../osg/denpendency/osg/lib/ -losgDB
    24. INCLUDEPATH += $$PWD/../../osg/denpendency/osg/include
    25. DEPENDPATH += $$PWD/../../osg/denpendency/osg/include

    这里文件路径替换成你自己的osg文件路径。

  • main.h:

    1. #include "osgwidget.h"
    2. #include <QApplication>
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. OSGWidget w;
    7. w.show();
    8. return a.exec();
    9. }

  • osgwidget.h: 
    1. #ifndef OSGWIDGET_H
    2. #define OSGWIDGET_H
    3. #include <QWidget>
    4. #include <QTimer>
    5. #include <osgDB/ReadFile>
    6. #include <osg/Group>
    7. #include <osg/Node>
    8. #include <osg/Camera>
    9. #include <osgGA/TrackballManipulator>
    10. #include <osgGA/StateSetManipulator>
    11. #include <osgViewer/ViewerEventHandlers>
    12. #include "GraphicsWindowQt.h" // 这个是本地自己建的。所以用双引号
    13. #include <QDebug>
    14. namespace Ui {
    15. class OSGWidget;
    16. }
    17. class OSGWidget : public QWidget
    18. {
    19. Q_OBJECT
    20. public:
    21. explicit OSGWidget(QWidget *parent = nullptr);
    22. ~OSGWidget();
    23. private:
    24. Ui::OSGWidget *ui;
    25. public:
    26. QTimer* m_timer;
    27. osgViewer::Viewer* viewer;
    28. public:
    29. void initEarth();
    30. public slots:
    31. void updateFrame();
    32. };
    33. #endif // OSGWIDGET_H

  • osgwidget.cpp:
    1. #include "osgwidget.h"
    2. #include "ui_osgwidget.h"
    3. OSGWidget::OSGWidget(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::OSGWidget)
    6. {
    7. ui->setupUi(this);
    8. m_timer = new QTimer;
    9. viewer = new osgViewer::Viewer;
    10. initEarth();
    11. }
    12. OSGWidget::~OSGWidget()
    13. {
    14. delete ui;
    15. // delete m_timer;
    16. }
    17. void OSGWidget::initEarth(){
    18. // 设置模型
    19. osg::ref_ptr<osg::Group> root = new osg::Group;
    20. // 显示cow.osg 模型
    21. root->addChild(osgDB::readNodeFile("cow.osg"));
    22. // 显示 .earth 文件中的地球模型
    23. // root->addChild(osgDB::readNodeFile("china-simple-3d.earth"));
    24. osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    25. traits->windowDecoration = false;//声明是否显示窗口的描述
    26. traits->x = 50;
    27. traits->y = 50;
    28. traits->width = 800;
    29. traits->height = 600;
    30. traits->doubleBuffer = true;//创建图形窗口是否使用双缓存
    31. osg::ref_ptr<osg::Camera> camera = new osg::Camera;
    32. camera->setGraphicsContext(new osgQt::GraphicsWindowQt(traits.get()));
    33. camera->setClearColor(osg::Vec4(0.2f, 0.2f, 0.6f, 1.0));
    34. camera->setViewport(new osg::Viewport(0, 0, 800, 600));
    35. camera->setProjectionMatrixAsPerspective(30.0, 800.0/600.0, 1.0, 10000.0);
    36. //设置渲染器
    37. viewer->setCamera(camera);
    38. viewer->setSceneData(root);
    39. viewer->setCameraManipulator(new osgGA::TrackballManipulator);
    40. viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);//创建为单线程
    41. viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
    42. viewer->addEventHandler(new osgViewer::WindowSizeHandler);
    43. viewer->addEventHandler(new osgViewer::StatsHandler);
    44. osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>(camera->getGraphicsContext());
    45. // 这里是直接在ui文件中添加了一个布局,重命名成了horizontalLayoutOSG
    46. ui->horizontalLayoutOSG->addWidget((QWidget*)(gw->getGLWidget()));
    47. QObject::connect(m_timer, SIGNAL(timeout()), this, SLOT(updateFrame()));
    48. m_timer->start(5); // 每5ms触发一次帧的刷新
    49. }
    50. void OSGWidget::updateFrame(){
    51. viewer->frame();
    52. }
     
  • 将之前修改好的GraphicsWindowQt.h,GraphicsWindowQt.cpp文件引入到项目中即可,如图:

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/112744?site
推荐阅读
相关标签
  

闽ICP备14008679号