QT+OSG+OSG-earth如何在窗口显示一个地球

1、环境配置

系统:windows10系统

QT:版本5.15.2 编译器:MSVC2019_64bit 编辑器:QT Creator

OSG版本:3.7.0 64位 为MSVC环境下编译

osgQt:为第三方编译的库,OSG因为版本不同已经不提供osgQt的封装。

osg-earth版本: 3.6.0

2、如何导入相关的库

衔接:QT+OSG+osg-earth显示一个球-CSDN博客

QT的pro文件:

cpp 复制代码
QT       += core gui
QT      +=opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \ 
    osgshowwidget.cpp

HEADERS += \  
    osgshowwidget.h

FORMS += \
    osgshowwidget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target



win32:CONFIG(release, debug|release): LIBS += -L$$PWD/osg370MSVC2019_64bit/OSG/lib/ -losg  -losgDB -losgUtil  -losgViewer  -losgGA   -losgWidget   -lOpenThreads
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/osg370MSVC2019_64bit/OSG/lib/ -losgd  -losgDBd -losgUtild  -losgViewerd  -losgGAd  -losgWidgetd -lOpenThreadsd
else:unix: LIBS += -L$$PWD/osg370MSVC2019_64bit/OSG/lib/ -losg  -losgDB -losgUtil  -losgViewer  -losgGA  -losgWidget   -lOpenThreads

INCLUDEPATH += $$PWD/osg370MSVC2019_64bit/OSG/include
DEPENDPATH += $$PWD/osg370MSVC2019_64bit/OSG/include



win32:CONFIG(release, debug|release): LIBS += -L$$PWD/osgQtVC2019_64bit/vs2019_x64_osgQt/osgQt/lib/ -losgQOpenGL
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/osgQtVC2019_64bit/vs2019_x64_osgQt/osgQt/lib/ -losgQOpenGLd
else:unix: LIBS += -L$$PWD/osgQtVC2019_64bit/vs2019_x64_osgQt/osgQt/lib/ -losgQOpenGL

INCLUDEPATH += $$PWD/osgQtVC2019_64bit/vs2019_x64_osgQt/osgQt/include
DEPENDPATH += $$PWD/osgQtVC2019_64bit/vs2019_x64_osgQt/osgQt/include



win32:CONFIG(release, debug|release): LIBS += -L$$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/lib/ -losgEarth
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/lib/ -losgEarthd
else:unix: LIBS += -L$$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/lib/ -losgEarth

INCLUDEPATH += $$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/include
DEPENDPATH += $$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/include



win32:CONFIG(release, debug|release): LIBS += -L$$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/lib/ -losgEarth
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/lib/ -losgEarthd
else:unix: LIBS += -L$$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/lib/ -losgEarth

INCLUDEPATH += $$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/include
DEPENDPATH += $$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/include

3、代码部分

.h

cpp 复制代码
#ifndef OSGSHOWWIDGET_H
#define OSGSHOWWIDGET_H

#include <QWidget>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include<osgGA/FlightManipulator>
#include <osgViewer/View>
#include <osg/ArgumentParser>
#include <osgGA/CameraManipulator>
#include <osgEarth/EarthManipulator>
#include <osgEarth/Map>
#include <osgEarth/MapNode>
#include <osgEarth/TMS>
#include <osgEarth/EarthManipulator>
#include <osg/ArgumentParser>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include<osgGA/FlightManipulator>
#include <osgViewer/View>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osg/Timer>
#include <osg/ArgumentParser>
#include <osgGA/CameraManipulator>

namespace Ui {
class osgShowWidget;
}

class osgShowWidget : public QWidget
{
    Q_OBJECT

public:
    explicit osgShowWidget(QWidget *parent = nullptr);
    ~osgShowWidget();
protected slots:
    void initWindow();

protected:
    osgQOpenGLWidget* _pOsgQOpenGLWidget;
    osg::ref_ptr<osgViewer::Viewer> pViewer;

private:
    Ui::osgShowWidget *ui;
};

#endif // OSGSHOWWIDGET_H

.cpp

cpp 复制代码
#include "osgshowwidget.h"
#include "ui_osgshowwidget.h"
#include <QCoreApplication>
#include <QApplication>
#include <QDir>
#include <QString>
#include <QDebug>
osgShowWidget::osgShowWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::osgShowWidget)
{
    ui->setupUi(this);
    this->move(0,0);
    ui->widget->move(0,0);
    _pOsgQOpenGLWidget = new osgQOpenGLWidget(ui->widget);
    _pOsgQOpenGLWidget->setGeometry(ui->widget->geometry());

    connect(_pOsgQOpenGLWidget, SIGNAL(initialized()), this, SLOT(initWindow()));



}

osgShowWidget::~osgShowWidget()
{
    delete ui;
   if(_pOsgQOpenGLWidget != NULL)
        delete _pOsgQOpenGLWidget;
}


void osgShowWidget::initWindow()
{
//    osgViewer::Viewer* pViewer = _pOsgQOpenGLWidget->getOsgViewer();
//    pViewer->setCameraManipulator(new osgGA::TrackballManipulator);
//    osg::Node* node = osgDB::readNodeFile("model/F-22.ive");
//    pViewer->setSceneData(node);
   QString exeDir = QCoreApplication::applicationDirPath();
   QDir::setCurrent(exeDir);

    osgEarth::initialize();
    osgViewer::Viewer* pViewer = _pOsgQOpenGLWidget->getOsgViewer();
    //初始化View
    //pViewer = getOsgViewer();
    //设置相机
    pViewer->setCameraManipulator(new osgEarth::Util::EarthManipulator);
    osg::ref_ptr<osg::Group> root;		//根节点
    osg::ref_ptr<osg::Node>	earth;		//地球节点
    root = new osg::Group;

    QString relativeEarthFilePath = "Maps/ocean.earth";  // 请替换为你实际的.earth文件相对路径
    QString earthFilePath = exeDir + "/" + relativeEarthFilePath;
    earth = osgDB::readNodeFile(earthFilePath.toStdString());
    if (!earth.valid())

        qDebug() << "File not found";
    root->addChild(earth.get());
    pViewer->setSceneData(root.get());
    pViewer->realize();





}

main.cpp

cpp 复制代码
#include <QCoreApplication>
#include <QApplication>
#include <QDir>
#include "osgshowwidget.h"
#include <osgEarth/Map>
#include <osgEarth/MapNode>
#include <osgEarth/EarthManipulator>
#include <osgEarth/MapNode>
#include <osgEarth/TMS>
#include <osgEarth/EarthManipulator>
#include <osg/ArgumentParser>
#include <osgViewer/Viewer>

#include <QWidget>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include<osgGA/FlightManipulator>
#include <osgViewer/View>
#include <osgQOpenGL/osgQOpenGLWidget>

#include <osg/Timer>
#include <osg/ArgumentParser>
#include <osgGA/CameraManipulator>

int main(int argc, char *argv[])
{
        QApplication a(argc, argv);

//    // 获取可执行文件的目录

   osgShowWidget w;
   w.show();


    // 初始化osgViewer
    // 初始化osgEarth
//    osgEarth::initialize();
//    osg::ArgumentParser args(&argc, argv);

//    // 创建Viewer实例
//    osgViewer::Viewer viewer(args);

//    // 使用相对于exe的相对路径来构建.earth文件的完整路径
//    QString relativeEarthFilePath = "Maps/ocean.earth";  // 请替换为你实际的.earth文件相对路径
//    QString earthFilePath = exeDir + "/" + relativeEarthFilePath;

//    // 请替换为你实际的.earth文件路径
//    osg::ref_ptr<osg::Node> earthNode = osgDB::readNodeFile(earthFilePath.toStdString());

//    if (!earthNode) {
//        std::cerr << "Failed to load .earth file: " << earthFilePath.toStdString() << std::endl;
//        return -1;  // 如果加载失败,返回错误码
//    }

//    // 设置场景数据为加载的.earth文件
//    viewer.setSceneData(earthNode.get());

//    // 设置地球操控器
//    viewer.setCameraManipulator(new osgEarth::Util::EarthManipulator(args));

//    // 运行Viewer
//    return viewer.run();



    return a.exec();
}

4、运行结果

5、出现的问题

不知道还qt的bug还是什么问题,我的窗口直接show出来会发生偏移,窗口出现的位置默认就不是在0.0处,必须写代码,move移动到0.0才行,这应该是qt兼容性问题,不是我代码的问题,所以我的代码有一部分对于正常显示qt界面也就不需要。

6、相关资料参考

QT+OSG+osg-earth显示一个球-CSDN博客

QT+OSG显示一个三维模型_qt+osg加载obj模型-CSDN博客

相关推荐
-银雾鸢尾-9 小时前
C#中的StringBuilder相关方法
开发语言·c#
-银雾鸢尾-9 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
大模型码小白10 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
段一凡-华北理工大学12 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe12 小时前
C++——多态
开发语言·c++
心平气和量大福大13 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
从零开始的代码生活_14 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸14 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
Quz14 小时前
QML 信号与槽:直接绑定与跨文件通信
qt·编程语言·设计
GIS阵地15 小时前
QgsSingleBandPseudoColorRenderer 完整详解(QGIS 3.40.13 C++)
开发语言·前端·c++·qt·qgis