osgEarth学习笔记4-第一个OsgEarth QT程序

原文链接

使用QT Creator新建一个窗口项目。

编辑pro文件

cpp 复制代码
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui
#osg osgQt
INCLUDEPATH += "D:\coding\osgearth\osgearth-vcpkg-installed\installed\x86-windows\include"
INCLUDEPATH += "D:\coding\osgearth\osgQt-master\include"
INCLUDEPATH += "D:\coding\osgearth\osgQt-master\build-win32\src"
INCLUDEPATH += "D:\coding\osgearth\osgQt-master\build-win32\include"
INCLUDEPATH += "D:\coding\osgearth\osgQt-master\build-win32\include"
LIBS += -L"D:\coding\osgearth\osgearth-vcpkg-installed\installed\x86-windows\lib" -losg -losgUtil -losgViewer  -losgGA  -losgDB  -lOpenThreads -lopengl32
LIBS += -L"D:\coding\osgearth\osgQt-master\build-win32\lib" -losgQOpenGL
#osgEarth
INCLUDEPATH += "D:\coding\osgearth\osgearth-vcpkg-installed\installed\x86-windows\include"
LIBS += -L"D:\coding\osgearth\osgearth-vcpkg-installed\installed\x86-windows\lib" -losgEarth
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

编辑main.cpp

cpp 复制代码
#include "mainwindow.h"
#include <QApplication>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>
#include <osgGA/Device>
#include <QSurfaceFormat>
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;

//osgEarth
#include <osgViewer/Viewer>
#include <osgEarth/Notify>
#include <osgEarth/EarthManipulator>
#include <osgEarth/ExampleResources>
#include <osgEarth/MapNode>
#include <osgEarth/Threading>
#include <osgEarth/GDAL>
#include <osgEarth/ImageLayer>
using namespace osgEarth;
using namespace osgEarth::Util;

#include <osgEarth/Metrics>

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

    osgEarth::initialize();
    osg::ArgumentParser arguments(&argc, argv);
        osgQOpenGLWidget widget(&arguments);
        QObject::connect(&widget, &osgQOpenGLWidget::initialized,[&arguments,&widget]
            {
                widget.getOsgViewer()->setCameraManipulator( new EarthManipulator(arguments) );
                widget.getOsgViewer()->getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
                string tifFile = "D:/coding/osgearth/world.tif";
                osgEarth::Map* map = new osgEarth::Map() ;
                osgEarth::GDALImageLayer::Options gdalOpt ;
                gdalOpt.url() = tifFile ;
                osgEarth::GDALImageLayer* imgLayer = new osgEarth::GDALImageLayer(gdalOpt);
                if( imgLayer==0 ){
                    return 20 ;
                }else{
                    cout<<"good gdal layer."<<endl;
                }
                map->addLayer(imgLayer ) ;
                MapNode* mapNode = new MapNode(map);
                if(!mapNode)
                {
                    std::cout << "bad map node." << std::endl;
                    return 1;
                }
                widget.getOsgViewer()->setSceneData(mapNode);
                return 0;
            });
    MainWindow w;
    w.setCentralWidget(&widget);
    w.show();
    return a.exec();
}

以Release模式编译执行,然后参考前一篇笔记,拷贝相关依赖项进入exe目录。再次运行。

此时运行会提示错误

FTH: (7640): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes. ***

Error reading file .osgearth_engine_rex: file not handled

[osgEarth]* WARNING: Failed to load terrain engine driver for "rex"

[osgEarth]* FAILED to create a terrain engine for this map

原因是没有加载到osgEarth的plugins动态库,之前我们是osg的plugins都考过来了,现在到下面目录

把里面dll都拷贝到exe目录下的osgPlugins-3.6.5目录下即可。

运行结果如下:

相关推荐
今天我又学废了5 分钟前
Scala学习记录,List
学习
幸运超级加倍~9 分钟前
软件设计师-上午题-16 算法(4-5分)
笔记·算法
王俊山IT29 分钟前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
Mephisto.java1 小时前
【大数据学习 | kafka高级部分】kafka中的选举机制
大数据·学习·kafka
Yawesh_best1 小时前
思源笔记轻松连接本地Ollama大语言模型,开启AI写作新体验!
笔记·语言模型·ai写作
南宫生2 小时前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
武子康3 小时前
大数据-212 数据挖掘 机器学习理论 - 无监督学习算法 KMeans 基本原理 簇内误差平方和
大数据·人工智能·学习·算法·机器学习·数据挖掘
CXDNW3 小时前
【网络面试篇】HTTP(2)(笔记)——http、https、http1.1、http2.0
网络·笔记·http·面试·https·http2.0
使者大牙3 小时前
【大语言模型学习笔记】第一篇:LLM大规模语言模型介绍
笔记·学习·语言模型
ssf-yasuo3 小时前
SPIRE: Semantic Prompt-Driven Image Restoration 论文阅读笔记
论文阅读·笔记·prompt