1.解決方案
不得不說關於OSG的資料實在太零散了,搜尋了很多關於OSG在QT下的解決方案,都是各有各的說法,有的說的不是很清楚,有的已經過時了。這裡提供一下自己的解決方案吧。
在遠古的OSG裡面,應該是提供對QT的支援的;不過應該是由於QT版本變動比較大,現在的OSG版本應該已經沒有了。但是在GitHub上又有了新的osgQt專案(地址:https://github.com/openscenegraph/osgQt) 用來解決這個問題。
osgQt是個簡單的小專案,其實沒有必要額外的編譯,最核心的是個名為GraphicsWindowQt的類,只需要複製GraphicsWindowQt.h和GraphicsWindowQt.cpp到QT工程裡面就可以使用了。同時osgQt給出了一個名為osgviewerQt的樣例,也僅僅只是個cpp檔案。結合兩者,一個簡單的示例就出來了。
QT工程如下:
編譯執行後如下:
2.存在問題
1) 警告提示
上述專案直接執行,會出現諸如“QOpenGLContext::swapBuffers() called with non-exposed window, behavior is undefined”的警告。查閱網上的英文資料,大意說是因為OpenGL環境未初始化產生的。在osgviewerQt中繫結了一個定時器,每隔10ms就呼叫frame()來繪製一幀,而這個定時器是在建構函式的時候就開始呼叫了,沒有等待QT中OpenGL環境的生成。在這裡我把定時器的部分給改進了一下,等待OSG的環境初始化完成在啟動定時器,這個警告就沒有了。osgviewerQt改進後的程式碼如下:
#include <QTimer>
#include <QApplication>
#include <QGridLayout>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/MultiTouchTrackballManipulator>
#include <osgDB/ReadFile>
#include "GraphicsWindowQt"
#include <iostream>
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("D:/Work/OSGBuild/OpenSceneGraph-Data/cow.osgt") );
QWidget* widget2 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/glider.osgt") );
QWidget* widget3 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/axes.osgt") );
QWidget* widget4 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/fountain.osgt") );
// QWidget* popupWidget = addViewWidget( createGraphicsWindow(900,100,320,240,"Popup window",true), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/dumptruck.osgt") );
// 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<osg::Node> 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) );
// set the draw and read buffers up for a double buffered window with rendering going to back buffer
camera->setDrawBuffer(GL_BACK);
camera->setReadBuffer(GL_BACK);
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<osg::GraphicsContext::Traits> 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(); }
//定時器事件
void timerEvent(QTimerEvent* )
{
frame();
}
//啟動定時器繪製
void show()
{
QWidget::show();
_timerID = startTimer(10);
}
protected:
//QTimer _timer;
int _timerID; //定時器ID
};
int main( int argc, char** 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
QApplication app(argc, argv);
ViewerWidget* viewWidget = new ViewerWidget(0, Qt::Widget, threadingModel);
viewWidget->setGeometry( 100, 100, 800, 600 );
viewWidget->show();
return app.exec();
}
2) 多執行緒問題
在OSG中提供了諸如CullDrawThreadPerContext等多執行緒模式,但是在這裡是沒辦法支援這些多執行緒模式的,只能支援單執行緒。在網上查閱了一些解決方案,但是最後都不是很完美,有空再把其解決方案寫出來。
3) 其他
GraphicsWindowQt最終繼承的還是QT中的QGLWidget類,提供OpenGL功能。而在新版本的QT中,這個類已經被廢棄了,取而代之的是一個叫做QOpenGLWidget的類。所以這裡面問題還是不少的,好在內容相對較少,以後有空可以自己改進。