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

原文链接
 

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

编辑pro文件

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

#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目录下即可。

 运行结果如下:

相关推荐

  1. 第一Rust程序

    2024-03-19 21:30:03       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-19 21:30:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-19 21:30:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-19 21:30:03       82 阅读
  4. Python语言-面向对象

    2024-03-19 21:30:03       91 阅读

热门阅读

  1. QT 多线程使用以及注意事项

    2024-03-19 21:30:03       43 阅读
  2. react面试题

    2024-03-19 21:30:03       29 阅读
  3. Nacos

    Nacos

    2024-03-19 21:30:03      37 阅读
  4. +-x c++

    2024-03-19 21:30:03       42 阅读
  5. LeetCode --- 2057. Smallest Index With Equal Value 解题报告

    2024-03-19 21:30:03       37 阅读
  6. mysql笔记:21. 演示脏读、不可重复读和幻读现象

    2024-03-19 21:30:03       36 阅读
  7. 代码随想录算法训练营第24天|理论基础|77. 组合

    2024-03-19 21:30:03       41 阅读
  8. Linux之shell条件判断

    2024-03-19 21:30:03       37 阅读
  9. 中文编程入门(Lua5.4.6中文版)第六章 Lua 运算符

    2024-03-19 21:30:03       39 阅读