From 6e4de176c7c2b4aeb4547fa5dc48d29d92b9d1ca Mon Sep 17 00:00:00 2001
From: "Mr.Huang" <hsq2013@163.com>
Date: Wed, 24 Jan 2018 11:18:24 +0800
Subject: [PATCH] fix bug: using multithread render model with Qt5

If I hadn't call makeCurrent function before swapBuffers by using ThreadingModel::CullThreadPerCameraDrawThreadPerContext with Qt5.9, I will get this warning: qopenglcontext::swapbuffers() called without corresponding makecurrent().
---
 src/osgQt/GraphicsWindowQt.cpp | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/osgQt/GraphicsWindowQt.cpp b/src/osgQt/GraphicsWindowQt.cpp
index 2df1dc3..2fd3855 100644
--- a/src/osgQt/GraphicsWindowQt.cpp
+++ b/src/osgQt/GraphicsWindowQt.cpp
@@ -285,8 +285,9 @@ void GLWidget::setKeyboardModifiers( QInputEvent* event )
 
 void GLWidget::resizeEvent( QResizeEvent* event )
 {
+    if (_gw == NULL || !_gw->valid())
+        return;
     const QSize& size = event->size();
-
     int scaled_width = static_cast<int>(size.width()*_devicePixelRatio);
     int scaled_height = static_cast<int>(size.height()*_devicePixelRatio);
     _gw->resized( x(), y(), scaled_width,  scaled_height);
@@ -298,6 +298,8 @@ void GLWidget::resizeEvent( QResizeEvent* event )
 
 void GLWidget::moveEvent( QMoveEvent* event )
 {
+    if (_gw == NULL || !_gw->valid())
+        return;
     const QPoint& pos = event->pos();
     int scaled_width = static_cast<int>(width()*_devicePixelRatio);
     int scaled_height = static_cast<int>(height()*_devicePixelRatio);
@@ -851,8 +851,6 @@ bool GraphicsWindowQt::releaseContextImplementation()
 
 void GraphicsWindowQt::swapBuffersImplementation()
 {
-    _widget->swapBuffers();
-
     // FIXME: the processDeferredEvents should really be executed in a GUI (main) thread context but
     // I couln't find any reliable way to do this. For now, lets hope non of *GUI thread only operations* will
     // be executed in a QGLWidget::event handler. On the other hand, calling GUI only operations in the
@@ -862,8 +860,8 @@ void GraphicsWindowQt::swapBuffersImplementation()
 
     // We need to call makeCurrent here to restore our previously current context
     // which may be changed by the processDeferredEvents function.
-    if (QGLContext::currentContext() != _widget->context())
-        _widget->makeCurrent();
+    _widget->makeCurrent();
+    _widget->swapBuffers();
 }
 
 void GraphicsWindowQt::requestWarpPointer( float x, float y )
From 2c8382421edec353d47f2d91cad454992ae0e199 Mon Sep 17 00:00:00 2001
From: Robert Osfield <robert@openscenegraph.com>
Date: Wed, 7 Mar 2018 10:30:47 +0000
Subject: [PATCH] Added missing Camera::setDrawBuffer()/setReadBuffer()

---
 examples/osgqfont/osgqfont.cpp       | 4 ++++
 examples/osgviewerQt/osgviewerQt.cpp | 5 +++++
 2 files changed, 9 insertions(+)

diff --git a/examples/osgqfont/osgqfont.cpp b/examples/osgqfont/osgqfont.cpp
index c30820e..8b4b60c 100644
--- a/examples/osgqfont/osgqfont.cpp
+++ b/examples/osgqfont/osgqfont.cpp
@@ -503,6 +503,10 @@ class MainWindow : public QWidget {
         camera->setGraphicsContext(graphicsWindow);
         camera->setViewport(new osg::Viewport(0, 0, width(), 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);
+
         startTimer(10);
     }
 
diff --git a/examples/osgviewerQt/osgviewerQt.cpp b/examples/osgviewerQt/osgviewerQt.cpp
index 1a2eac4..5f6b1ad 100644
--- a/examples/osgviewerQt/osgviewerQt.cpp
+++ b/examples/osgviewerQt/osgviewerQt.cpp
@@ -53,6 +53,11 @@ class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
 
         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 );
