From c4e2fa3393c9326167a017a0e8a0031c7cf1f110 Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Tue, 5 Feb 2019 21:24:17 +0100
Subject: [PATCH] fix stereo rendering - Directly Register GLCanvas to Scene at
 initialization - Stereo Rendering (at least on our setup) fails when setting
 the alpha buffer   -> disabled for stereo

---
 modules/gui/pymod/export_gl_canvas.cc |  1 +
 modules/gui/src/gl_canvas.cc          | 25 +++++++++++++++++--------
 modules/gui/src/gl_canvas.hh          |  2 +-
 modules/gui/src/gl_win.cc             |  4 +---
 4 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/modules/gui/pymod/export_gl_canvas.cc b/modules/gui/pymod/export_gl_canvas.cc
index 306f5fd67..daf2eec4e 100644
--- a/modules/gui/pymod/export_gl_canvas.cc
+++ b/modules/gui/pymod/export_gl_canvas.cc
@@ -36,6 +36,7 @@ void export_GLCanvas() {
     .def("Show", &GLCanvas::show)
     .def("SetFormat", &WrapSetGLCanvasFormat)
     .def("SetDefaultFormat", &GLCanvas::SetDefaultFormat)
+    .def("SetStereoFormat", &GLCanvas::SetStereoFormat)
     .def("GetQObject",&get_py_qobject<GLCanvas>)
     .add_property("qobject", &get_py_qobject<GLCanvas>)
   ;
diff --git a/modules/gui/src/gl_canvas.cc b/modules/gui/src/gl_canvas.cc
index 41e192786..c6f28c8a5 100644
--- a/modules/gui/src/gl_canvas.cc
+++ b/modules/gui/src/gl_canvas.cc
@@ -36,7 +36,10 @@ namespace ost { namespace gui {
 ost::gui::GLCanvas::GLCanvas(): QOpenGLWindow(),
                                 last_pos_(),
                                 show_beacon_(false),
-                                bench_flag_(false) { }
+                                bench_flag_(false) { 
+  LOG_DEBUG("GLCanvas::registering with scene");
+  gfx::Scene::Instance().Register(this);  
+}
 
 void GLCanvas::StatusMessage(const String& m) {
   // This Window can also be displayed without a full blown GostyApp.
@@ -47,25 +50,31 @@ void GLCanvas::StatusMessage(const String& m) {
   }
 }
 
-QSurfaceFormat GLCanvas::GetDefaultFormat() {
+void GLCanvas::SetDefaultFormat() {
   QSurfaceFormat f = QSurfaceFormat::defaultFormat();
   f.setRedBufferSize(8);
   f.setGreenBufferSize(8);
   f.setBlueBufferSize(8);
   f.setAlphaBufferSize(8);
   f.setDepthBufferSize(24);
-  return f;
+  this->setFormat(f);
 }
 
-void GLCanvas::SetDefaultFormat() {
-  this->setFormat(GLCanvas::GetDefaultFormat());
+void GLCanvas::SetStereoFormat() {
+  QSurfaceFormat f = QSurfaceFormat::defaultFormat();
+  f.setRedBufferSize(8);
+  f.setGreenBufferSize(8);
+  f.setBlueBufferSize(8);
+  // QOpenGLWindow seems to dislike alphabuffer in stereo rendering...
+  //f.setAlphaBufferSize(8);
+  f.setDepthBufferSize(24);
+  f.setStereo(true);
+  this->setFormat(f);
 }
 
 void GLCanvas::initializeGL() {
   LOG_DEBUG("GLCanvas::initializeGL()");
-  gfx::Scene::Instance().InitGL();
-  LOG_DEBUG("GLCanvas::registering with scene");
-  gfx::Scene::Instance().Register(this);
+  gfx::Scene::Instance().InitGL();    
 }
 
 void GLCanvas::paintGL() {
diff --git a/modules/gui/src/gl_canvas.hh b/modules/gui/src/gl_canvas.hh
index 7ec714d18..4d87a259a 100644
--- a/modules/gui/src/gl_canvas.hh
+++ b/modules/gui/src/gl_canvas.hh
@@ -46,8 +46,8 @@ public:
   virtual bool HasMultisample() const {return format().samples() > 1;}
 
   // QSurfaceFormat for GL context setup
-  static QSurfaceFormat GetDefaultFormat();
   void SetDefaultFormat();
+  void SetStereoFormat();
 
   // central point for sending input to the gfx layer
   void OnTransform(gfx::InputCommand, int indx, 
diff --git a/modules/gui/src/gl_win.cc b/modules/gui/src/gl_win.cc
index d0fe7b5cb..ee29b1f71 100644
--- a/modules/gui/src/gl_win.cc
+++ b/modules/gui/src/gl_win.cc
@@ -48,9 +48,7 @@ GLWin::GLWin(QWidget* p, bool try_stereo): Widget(NULL, p) {
   gl_canvas_ = new GLCanvas;
 
   if(try_stereo) {
-    QSurfaceFormat f = GLCanvas::GetDefaultFormat();
-    f.setStereo(true);
-    gl_canvas_->setFormat(f);
+    gl_canvas_->SetStereoFormat();
   } else{
     gl_canvas_->SetDefaultFormat();
   }
-- 
GitLab