基於ViewerWidget類,通過加載earth文件方式創建三維地球,實現Osgearth在Qt5.13中QWidget顯示
(1)初始化節點
m_Root = new osg::Group();
std::string url = m_EarthFile;
m_EarthNode = osgDB::readNodeFile(url);
m_Root->addChild(m_EarthNode);
m_MapNode = osgEarth::MapNode::findMapNode(m_EarthNode);
m_ViewerWidget = new ViewerWidget(this, m_Root);
m_MainViewer = dynamic_cast<osgViewer::Viewer*>(m_ViewerWidget->getViewer());
(2)設置漫遊器
m_EarthManipulator = new osgEarth::Util::EarthManipulator();
m_EarthManipulator->getSettings()->setMinMaxPitch(-90, 0);//設置最大最小傾斜角度
m_EarthManipulator->getSettings()->setMinMaxDistance(100.0, 4e7);//設置最近最遠距離
m_MainViewer->setCameraManipulator(m_EarthManipulator);
(3)設置Camera
osg::Camera* camera = m_MainViewer->getCamera();
osg::GraphicsContext* pGC = camera->getGraphicsContext();
if (!camera->getViewport())
{
camera->setViewport(new osg::Viewport(0, 0, pGC->getTraits()->width, pGC->getTraits()->height));
}
camera->setProjectionMatrixAsPerspective(30.0f, camera->getViewport()->width() / camera->getViewport()->height(), 1.0f, 10000.0f);
camera->setNearFarRatio(0.00001);
camera->setSmallFeatureCullingPixelSize(-1.0f);
osgEarth::GLUtils::setGlobalDefaults(camera->getOrCreateStateSet());//osgearth2.10.1 添加後纔可顯示feature
(4)添加到佈局
QGridLayout* pLayout = new QGridLayout(this);
pLayout->addWidget(m_ViewerWidget,0,0,1,1);
setLayout(pLayout);