当前位置: 首页 > news >正文

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文件:

QT       += core gui
QT      +=opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += 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.0SOURCES += \main.cpp \ osgshowwidget.cppHEADERS += \  osgshowwidget.hFORMS += \osgshowwidget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetwin32: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   -lOpenThreadsINCLUDEPATH += $$PWD/osg370MSVC2019_64bit/OSG/include
DEPENDPATH += $$PWD/osg370MSVC2019_64bit/OSG/includewin32: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/ -losgQOpenGLINCLUDEPATH += $$PWD/osgQtVC2019_64bit/vs2019_x64_osgQt/osgQt/include
DEPENDPATH += $$PWD/osgQtVC2019_64bit/vs2019_x64_osgQt/osgQt/includewin32: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/ -losgEarthINCLUDEPATH += $$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/include
DEPENDPATH += $$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/includewin32: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/ -losgEarthINCLUDEPATH += $$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/include
DEPENDPATH += $$PWD/vs2019_x64_osgearth_3.6.0/OSGEARTH/include

3、代码部分

.h

#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_OBJECTpublic: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

#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

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【在Linux世界中追寻伟大的One Piece】五种IO模型和阻塞IO
  • play-with-docker使用指南
  • redis集群创建问题处理
  • 数据结构-3.3.栈的链式存储实现
  • PCL 计算点云距离
  • Python 入门教程(4)数据类型 | 4.6、列表
  • Oracle从入门到放弃
  • Halo 开发者指南——项目运行、构建
  • AI绘画:科技赋能艺术的崭新时代
  • CAPL_构建基于UDS的刷写学习—01 Hex文件的解析
  • 详细介绍 Redis 列表的应用场景
  • Java语言程序设计基础篇_编程练习题*18.29(某个目录下的文件数目)
  • xml重点笔记(尚学堂 3h)
  • zookeeper向管控平台上报状态
  • java序列化对象后读取数据错误的问题
  • 【译】JS基础算法脚本:字符串结尾
  • 《Java编程思想》读书笔记-对象导论
  • 2017 前端面试准备 - 收藏集 - 掘金
  • Apache Pulsar 2.1 重磅发布
  • axios请求、和返回数据拦截,统一请求报错提示_012
  • ES6--对象的扩展
  • gulp 教程
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • Javascript编码规范
  • JavaScript异步流程控制的前世今生
  • Java基本数据类型之Number
  • JS字符串转数字方法总结
  • Promise初体验
  • Rancher-k8s加速安装文档
  • STAR法则
  • 编写符合Python风格的对象
  • 后端_ThinkPHP5
  • 基于游标的分页接口实现
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 判断客户端类型,Android,iOS,PC
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 微信公众号开发小记——5.python微信红包
  • 携程小程序初体验
  • 函数计算新功能-----支持C#函数
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • #git 撤消对文件的更改
  • #include<初见C语言之指针(5)>
  • #laravel部署安装报错loadFactoriesFrom是undefined method #
  • #NOIP 2014# day.2 T2 寻找道路
  • #如何使用 Qt 5.6 在 Android 上启用 NFC
  • (20)docke容器
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (C#)获取字符编码的类
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第2节(共同的基类)
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (Oracle)SQL优化技巧(一):分页查询
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (二)正点原子I.MX6ULL u-boot移植
  • (六)vue-router+UI组件库