基于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
(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);