Developer Documentation
QtBaseViewer_qt.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 // QOpenGL headers and glew are in conflict,
46 // so implement functions that make use of QOpenGL classes in separate file
47 
48 
49 //=============================================================================
50 //
51 // CLASS glViewer - IMPLEMENTATION
52 //
53 //=============================================================================
54 
55 
56 //== INCLUDES =================================================================
57 
58 #include "QtBaseViewer.hh"
59 
61 #include <QOffscreenSurface>
62 
63 #include <QOpenGLContext>
64 #include <QOpenGLDebugLogger>
65 
66 
67 #include <QOpenGLWidget>
68 #include <QOpenGLFramebufferObject>
69 
70 //== NAMESPACES ===============================================================
71 
72 
73 
74 //== IMPLEMENTATION ==========================================================
75 
76 
77 
79 
80  glWidget_->context()->swapBuffers(glWidget_->context()->surface());
81 
82 }
83 
84 //-----------------------------------------------------------------------------
85 
86 void glViewer::startGLDebugLogger()
87 {
88 
89  if (OpenFlipper::Options::debug())
90  {
91  delete glDebugLogger_;
92  glDebugLogger_ = new QOpenGLDebugLogger(this);
93  if (glDebugLogger_->initialize())
94  {
95  connect(glDebugLogger_, SIGNAL(messageLogged(QOpenGLDebugMessage)), this, SLOT(processGLDebugMessage(QOpenGLDebugMessage)));
96  glDebugLogger_->startLogging(QOpenGLDebugLogger::SynchronousLogging);
97  }
98  }
99 
100 }
101 
102 //-----------------------------------------------------------------------------
103 
104 void glViewer::deleteGLDebugLogger()
105 {
106  makeCurrent();
107  delete glDebugLogger_;
108 }
109 
110 //-----------------------------------------------------------------------------
111 
112 void glViewer::processGLDebugMessage(const QOpenGLDebugMessage& msg)
113 {
114  QString error;
115 
116  // Format based on severity
117  switch (msg.severity())
118  {
119  case QOpenGLDebugMessage::NotificationSeverity:
120  error += "--";
121  break;
122  case QOpenGLDebugMessage::HighSeverity:
123  error += "!!";
124  break;
125  case QOpenGLDebugMessage::MediumSeverity:
126  error += "!~";
127  break;
128  case QOpenGLDebugMessage::LowSeverity:
129  error += "~~";
130  break;
131  default:
132  error += "__";
133  }
134 
135  error += " (";
136 
137  // Format based on source
138  #define CASE(c) case QOpenGLDebugMessage::c: error += #c; break
139  switch (msg.source())
140  {
141  CASE(APISource);
142  CASE(WindowSystemSource);
143  CASE(ShaderCompilerSource);
144  CASE(ThirdPartySource);
145  CASE(ApplicationSource);
146  CASE(OtherSource);
147  CASE(InvalidSource);
148  default:
149  error += " " ; break;
150  }
151  #undef CASE
152 
153  error += " : ";
154 
155  // Format based on type
156  #define CASE(c) case QOpenGLDebugMessage::c: error += #c; break
157  switch (msg.type())
158  {
159  CASE(ErrorType);
160  CASE(DeprecatedBehaviorType);
161  CASE(UndefinedBehaviorType);
162  CASE(PortabilityType);
163  CASE(PerformanceType);
164  //CASE(OtherType);
165  CASE(MarkerType);
166  CASE(GroupPushType);
167  CASE(GroupPopType);
168  default:
169  return;
170  }
171  #undef CASE
172 
173  error += ")";
174  qDebug() << qPrintable(error) << " " << qPrintable(msg.message()) << "\n";
175 
176 
177 // if (msg.severity() & QOpenGLDebugMessage::HighSeverity)
178 // std::cerr << msg.message().toStdString() << std::endl;
179 
180 // // also catch deprecated function calls in core profile
181 // else if (!glstate_->compatibilityProfile() && msg.type() == QOpenGLDebugMessage::DeprecatedBehaviorType)
182 // std::cerr << msg.message().toStdString() << std::endl;
183 }
184 
185 
186 //-----------------------------------------------------------------------------
187 
188 void
189 glViewer::copyToImage( QImage& _image,
190  unsigned int _l, unsigned int _t,
191  unsigned int _w, unsigned int _h,
192  GLenum /* _buffer */ )
193 {
194 
195  _image = glWidget_->grabFramebuffer().copy(_l, _t, _w, _h).convertToFormat(QImage::Format_RGB32);
196 }
197 
198 //-----------------------------------------------------------------------------
199 
200 void glViewer::makeWidgetCurrent()
201 {
202  glWidget_->makeCurrent();
203 }
204 
205 //-----------------------------------------------------------------------------
206 
207 bool glViewer::createQFBO(QOpenGLFramebufferObject*& ptr, GLuint* _handle, int _width, int _height, int* _samples)
208 {
210  format.setInternalTextureFormat(GL_RGBA);
211  format.setTextureTarget(GL_TEXTURE_2D);
212  if(*_samples > -1) //use -1 to indicate, that sample count is bit set
213  {
214  // set the attachments as in the standard rendering
215  format.setAttachment(QFramebufferObject::CombinedDepthStencil);
216  // 16 samples per pixel as we want a nice snapshot. If this is not supported
217  // it will fall back to the maximal supported number of samples
218  format.setSamples(*_samples);
219  }
220 
221  QFramebufferObject* fb;
222  fb = new QFramebufferObject(_width, _height, format);
223  ptr = fb;
224  if(fb->isValid())
225  {
226  *_handle = fb->handle();
227  *_samples = fb->format().samples(); // store the actual samples qt uses
228  return true;
229  }
230  else
231  {
232  *_handle = 0;
233  return false;
234  }
235 }
236 
237 //-----------------------------------------------------------------------------
238 
239 void glViewer::blitQFBO(QOpenGLFramebufferObject* _ptr1, const QRect& _size1, QOpenGLFramebufferObject* _ptr2, const QRect& _size2)
240 {
241  QFramebufferObject::blitFramebuffer(_ptr1, _size1, _ptr2, _size2);
242 }
243 
244 //-----------------------------------------------------------------------------
245 
246 bool glViewer::bindQFBO(QOpenGLFramebufferObject* _ptr)
247 {
248  return _ptr->bind();
249 }
250 
251 //-----------------------------------------------------------------------------
252 
253 bool glViewer::QFBOResized(QOpenGLFramebufferObject* _ptr1)
254 {
255  return _ptr1->size() != QSize(glWidth(), glHeight());
256 }
257 
258 //-----------------------------------------------------------------------------
259 
260 void glViewer::deleteQFBO(QOpenGLFramebufferObject* _ptr)
261 {
262  delete _ptr;
263 }
264 
265 //=============================================================================
266 //=============================================================================
267 
unsigned int glWidth() const
get width of QGLWidget
void copyToImage(QImage &_image, GLenum _buffer=GL_BACK)
copy current framebuffer to an QImage
virtual void makeCurrent()
Makes this widget the current widget for OpenGL operations.
unsigned int glHeight() const
get height of QGLWidget
void processGLDebugMessage(const QOpenGLDebugMessage &msg)
process opengl debug messages
QOpenGLFramebufferObjectFormat QFramebufferObjectFormat
Framebuffer object that holds the pick cache.
virtual void swapBuffers()
Swaps the screen contents with the off-screen buffer.
QOpenGLFramebufferObject QFramebufferObject
Framebuffer object that holds the pick cache.