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

相关推荐
编程零零七1 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286112 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py2 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy2 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
C-SDN花园GGbond4 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处5 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ5 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
leon6255 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
我是苏苏6 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言