【QT + OsgEarth】(二)-- OsgEarth + osgQOpenGLWidget 加载地球

编译环境

osg: 3.6.5
osgEarth: 3.2.0
MSVC2019
osg 和 osgearth 的库文件配置参考这篇文章

编译生成osgQOpenGLWidget

修改CMakeLists 文件

  1. 添加qt版本和编译器目录
    在这里插入图片描述
SET(DESIRED_QT_VERSION "5.15.2" CACHE STRING "")
SET(CMAKE_PREFIX_PATH "D:/Solfware/Qt/5.15.2/msvc2019_64" CACHE PATH "")

2.修改osg版本
与安装的osg版本一致在这里插入图片描述
其余内容按照这个文章内容进行即可

将osgEarth嵌入Qt

头文件


#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgEarth/EarthManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/View>

#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgGA/GUIEventHandler>
#include <osgGA/NodeTrackerManipulator>
#include <osgGA/FirstPersonManipulator>
#include <osgGA/KeySwitchMatrixManipulator>

#include <osg/GraphicsContext>
#include <osg/Math>
#include <osg/BlendFunc>
#include <osg/BlendColor>

#include <osgEarth/ElevationQuery>
#include <osgEarth/Config>
#include <osgEarth/GeoTransform>
#include <osgEarth/MapNode>
#include <osgEarth/Terrain>

#include <osgDB/ReadFile>
#include <osgUtil/LineSegmentIntersector>
#include <osgUtil/Optimizer>
#include <osgUtil/IntersectVisitor>

using namespace osgEarth::Util;
using namespace osgEarth;
using namespace osgGA;

DigtalEarth.h

class DigtalEarth : public QMainWindow
{
    Q_OBJECT

public:
    DigtalEarth(QWidget *parent = nullptr);
    ~DigtalEarth();

private slots:
    void initOsg();

private:
    Ui::DigtalEarth *ui;

    osgQOpenGLWidget *pOsgW;
    osgViewer::Viewer *mViewer;         //场景
    osg::ref_ptr<osg::Group> mRoot;     // 根节点
    osg::ref_ptr<osg::Node> mEarth;					//地球节点
    osg::ref_ptr<osgEarth::Util::EarthManipulator>	mCamera;	//相机操作器
};
DigtalEarth::DigtalEarth(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::DigtalEarth)
{
    ui->setupUi(this);
    resize(400, 300);
    pOsgW = new osgQOpenGLWidget(this);
    ui->gridLayout_2->addWidget(pOsgW);
    mRoot = new osg::Group;
    mCamera = new osgEarth::Util::EarthManipulator;
    mEarth = osgDB::readNodeFile("simple.earth");
    mRoot->addChild(mEarth.get());
    connect(pOsgW, SIGNAL(initialized()), this, SLOT(initOsg()));
}

DigtalEarth::~DigtalEarth()
{
    delete ui;
}
void DigtalEarth::initOsg()
{
    osgEarth::initialize();
    mViewer = pOsgW->getOsgViewer();
    mViewer->setCameraManipulator(new osgGA::TrackballManipulator());
    mViewer->setCameraManipulator(mCamera);
    mViewer->setSceneData(mRoot.get());
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值