主要借助osgQOpenGL,将osg的窗口嵌入qt,osgQopenGL库的编译方法,可以参考参考链接。
main.cpp
#include "osgQOpenGL_osgearth.h"
#include <QtWidgets/QApplication>
#pragma execution_character_set("utf-8")
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
osgQOpenGL_osgearth w;
//设置初始窗口大小
w.setGeometry(200, 100, 1200, 800);
//设置窗口标题名称
w.setWindowTitle(QString("osgearth三维可视化"));
w.show();
return a.exec();
}
osgQOpenGL_osg.h
#pragma once
#pragma execution_character_set("utf-8")
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Node>
#include <osgEarth/EarthManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <QtWidgets/QMainWindow>
#include <qDebug>
#include "ui_osgQOpenGL_osgearth.h"
#include"osg365oe32.h"
class osgQOpenGL_osgearth : public QMainWindow
{
Q_OBJECT
public:
osgQOpenGL_osgearth(QWidget *parent = nullptr);
~osgQOpenGL_osgearth();
protected slots:
void initWindow();
private:
Ui::osgQOpenGL_osgearthClass ui;
osgViewer::Viewer* viewer; //场景
osgQOpenGLWidget* _osgQOpenGLWidget; //osg窗体
osg::ref_ptr<osg::Group> root; //根节点
osg::ref_ptr<osg::Node> earth; //地球节点
osg::ref_ptr<osgEarth::Util::EarthManipulator> em; //相机操作器
};
osgQOpenGL_osg.cpp
#include "osgQOpenGL_osgearth.h"
osgQOpenGL_osgearth::osgQOpenGL_osgearth(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
_osgQOpenGLWidget = new osgQOpenGLWidget(this);
//设置显示尺寸
//_osgQOpenGLWidget->setGeometry(100,100,800,600);
_osgQOpenGLWidget->setGeometry(this->geometry());
this->setCentralWidget(_osgQOpenGLWidget);
root = new osg::Group;
em = new osgEarth::Util::EarthManipulator;
earth = osgDB::readNodeFile("simple.earth");
root->addChild(earth.get());
connect(_osgQOpenGLWidget, SIGNAL(initialized()), this, SLOT(initWindow()));
}
osgQOpenGL_osgearth::~osgQOpenGL_osgearth()
{
if (_osgQOpenGLWidget != NULL)
delete _osgQOpenGLWidget;
}
void osgQOpenGL_osgearth::initWindow()
{
osgEarth::initialize();
viewer = _osgQOpenGLWidget->getOsgViewer();
viewer->setCameraManipulator(em);
viewer->setSceneData(root.get());
viewer->addEventHandler(new osgViewer::StatsHandler);//添加s键帧率显示事件
unsigned int windowWidth; //显示器宽度
unsigned int windowHeight; //显示器高度
//获取系统分辨率
osg::GraphicsContext::WindowingSystemInterface *wsInterface = osg::GraphicsContext::getWindowingSystemInterface();
wsInterface->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), windowWidth, windowHeight);
qDebug() << windowWidth << " " << windowHeight;
//放置窗口变扁
viewer->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(1200) / static_cast<double>(800), 1.0f, 10000.0f);
}
osg365oe32.h
#pragma once
#include <Windows.h>
#include <osgGA/CameraManipulator>
#include <osg/LineSegment>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Matrixd>
#include <osg/Matrix>
#include <osg/Material>
#include <osg/MatrixTransform>
#include <osg/Group>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/PositionAttitudeTransform>
#include <memory>
#include <osg/StateAttribute>
#include <osg/Point>
#include <osgDB/Registry>
#include <osgUtil/Optimizer>
#include <osgUtil/Simplifier>
#include <osgUtil/SmoothingVisitor>
#include <osgUtil/IntersectVisitor>
#include <osg/TextureCubeMap>
#include <osg/TexGen>
#include <osg/ShapeDrawable>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <osgDB/WriteFile>
#include <osgDB/ReadFile>
#include <osg/CullFace>
#include <osgParticle/PrecipitationEffect>
#include <osgParticle/ExplosionEffect>
#include <osgParticle/ExplosionDebrisEffect>
#include <osgParticle/FireEffect>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgQOpenGL/osgQOpenGLWidget>
using namespace std;
#ifdef _DEBUG
//osg库一共19个
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgFXd.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgVolumed.lib")
#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgManipulatord.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib, "osgSimd.lib")
#pragma comment(lib, "osgTerraind.lib")
#pragma comment(lib, "osgWidgetd.lib")
#pragma comment(lib, "osgUId.lib")
#pragma comment(lib, "osgShadowd.lib")
#pragma comment(lib, "osgAnimationd.lib")
#pragma comment(lib, "osgParticled.lib")
#pragma comment(lib, "osgPresentationd.lib")
#pragma comment(lib, "osgQOpenGLd.lib")
//osgearth库1个
#pragma comment(lib, "osgEarthd.lib")
//其他库2个
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#else
#pragma comment(lib, "osg.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "osgFX.lib")
#pragma comment(lib, "osgViewer.lib")
#pragma comment(lib, "osgVolume.lib")
#pragma comment(lib, "OpenThreads.lib")
#pragma comment(lib, "osgGA.lib")
#pragma comment(lib, "osgUtil.lib")
#pragma comment(lib, "osgManipulator.lib")
#pragma comment(lib, "osgText.lib")
#pragma comment(lib, "osgSim.lib")
#pragma comment(lib, "osgTerrain.lib")
#pragma comment(lib, "osgWidget.lib")
#pragma comment(lib, "osgUI.lib")
#pragma comment(lib, "osgShadow.lib")
#pragma comment(lib, "osgAnimation.lib")
#pragma comment(lib, "osgParticle.lib")
#pragma comment(lib, "osgPresentation.lib")
#pragma comment(lib, "osgQOpenGL.lib")
#pragma comment(lib, "osgEarth.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#endif;