上一篇中,我们已经使用傻瓜化的 pacman -S 安装了 openSceneGraph 与 osgEarth 。为了在Qt中导入osgEarth 的窗口,需要安装 osgQt 模块。
注意,这个包分32位、64位,同时,也分 release, debug,下载下来还是不小的,需要好几个GB的空间
pacman -S --needed mingw-w64-i686-osgQt-debug mingw-w64-x86_64-osgQt-debug mingw-w64-i686-osgQt mingw-w64-x86_64-osgQt
pacman -S --needed mingw-w64-i686-osgearth mingw-w64-x86_64-osgearth mingw-w64-i686-osgearth-debug mingw-w64-x86_64-osgearth-debug
安装完毕后,即可启动 qtcreator,或者直接命令行qmake例子了。例子在
https://github.com/openscenegraph/osgQt/blob/master/examples/osgviewerQt/osgviewerQt.cpp
为其准备一个pro文件
QT += core gui opengl
debug:LIBS+= -losgViewerd -losgDBd -losgd -lOpenThreadsd -losgGAd -losgQtd
release:LIBS+= -losgViewer -losgDB -losg -lOpenThreadsd -losgGA -losgQt
TARGET = osgviewerQt
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += osgviewerQt.cpp
而后把本地OSM数据源改写到 cpp 中:
#include
#include
#include
#include
#include
#include
#include
#include
#include
class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
{
public:
ViewerWidget(QWidget* parent = 0, Qt::WindowFlags f = 0, osgViewer::ViewerBase::ThreadingModel threadingModel=osgViewer::CompositeViewer::SingleThreaded) : QWidget(parent, f)
{
setThreadingModel(threadingModel);
// disable the default setting of viewer.done() by pressing Escape.
setKeyEventSetsDone(0);
QWidget* widget1 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("openstreetmap.earth") );
QWidget* widget2 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("openstreetmap.earth") );
QWidget* widget3 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("openstreetmap.earth") );
QWidget* widget4 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("openstreetmap.earth") );
QWidget* popupWidget = addViewWidget( createGraphicsWindow(900,100,320,240,"Popup window",true), osgDB::readRefNodeFile("openstreetmap.earth") );
popupWidget->show();
QGridLayout* grid = new QGridLayout;
grid->addWidget( widget1, 0, 0 );
grid->addWidget( widget2, 0, 1 );
grid->addWidget( widget3, 1, 0 );
grid->addWidget( widget4, 1, 1 );
setLayout( grid );
connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
_timer.start( 10 );
}
QWidget* addViewWidget( osgQt::GraphicsWindowQt* gw, osg::ref_ptr scene )
{
osgViewer::View* view = new osgViewer::View;
addView( view );
osg::Camera* camera = view->getCamera();
camera->setGraphicsContext( gw );
const osg::GraphicsContext::Traits* traits = gw->getTraits();
camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
view->setSceneData( scene );
view->addEventHandler( new osgViewer::StatsHandler );
view->setCameraManipulator( new osgGA::MultiTouchTrackballManipulator );
gw->setTouchEventsEnabled( true );
return gw->getGLWidget();
}
osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
{
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr traits = new osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();
return new osgQt::GraphicsWindowQt(traits.get());
}
virtual void paintEvent( QPaintEvent* /*event*/ )
{ frame(); }
protected:
QTimer _timer;
};
int main( int argc, char** argv )
{
QApplication app(argc, argv);
osg::ArgumentParser arguments(&argc, argv);
#if QT_VERSION >= 0x050000
// Qt5 is currently crashing and reporting "Cannot make QOpenGLContext current in a different thread" when the viewer is run multi-threaded, this is regression from Qt4
osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::ViewerBase::SingleThreaded;
#else
osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
#endif
while (arguments.read("--SingleThreaded")) threadingModel = osgViewer::ViewerBase::SingleThreaded;
while (arguments.read("--CullDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
while (arguments.read("--DrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::DrawThreadPerContext;
while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext;
#if QT_VERSION >= 0x040800
// Required for multithreaded QGLWidget on Linux/X11, see http://blog.qt.io/blog/2011/06/03/threaded-opengl-in-4-8/
if (threadingModel != osgViewer::ViewerBase::SingleThreaded)
QApplication::setAttribute(Qt::AA_X11InitThreads);
#endif
ViewerWidget* viewWidget = new ViewerWidget(0, Qt::Widget, threadingModel);
viewWidget->setGeometry( 100, 100, 800, 600 );
viewWidget->show();
return app.exec();
}
编译运行,即可见到效果了。
要注意的是,这里连接的是release库,因此必须在 release 配置下运行。debug需要链接 debug库,否则虽然编译通过了,运行却报错的。如果报错,要检查一下,osg相关的debug库是不是都安装了,运行
pacman -Ss osg
检查。
最后,编译、运行效果:
当然,能够这样顺利,还是站在伟大、辛勤、无私、可敬的MSYS2工具链维护者的肩膀上。到底怎样嵌入自己的场景呢?估计要从 OpenSceneGraph 而不是 osgEarth 开始入手了。毕竟 osgEarth 是 OpenSceneGraph 的一个扩展包,场景渲染、添加自己的模型(比如玩具飞机)应该是OpenSceneGraph 的工作。目前姑且这样理解吧!