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博客

相关推荐
精彩极了吧13 小时前
C语言基本语法-自定义类型:结构体&联合体&枚举
c语言·开发语言·枚举·结构体·内存对齐·位段·联合
南极星100514 小时前
蓝桥杯JAVA--启蒙之路(十)class版本 模块
java·开发语言
baidu_2474386114 小时前
Android ViewModel定时任务
android·开发语言·javascript
Dev7z14 小时前
基于 MATLAB 的铣削切削力建模与仿真
开发语言·matlab
不能隔夜的咖喱14 小时前
牛客网刷题(2)
java·开发语言·算法
小天源15 小时前
Error 1053 Error 1067 服务“启动后立即停止” Java / Python 程序无法后台运行 windows nssm注册器下载与报错处理
开发语言·windows·python·nssm·error 1053·error 1067
肉包_51115 小时前
两个数据库互锁,用全局变量互锁会偶发软件卡死
开发语言·数据库·c++
大空大地202615 小时前
流程控制语句--if语句
开发语言
毕设源码-邱学长16 小时前
【开题答辩全过程】以 基于PHP的发热病人管理平台的设计与实现为例,包含答辩的问题和答案
开发语言·php
HellowAmy16 小时前
我的C++规范 - 线程池
开发语言·c++·代码规范