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
60 #include <OpenFlipper/common/GlobalOptions.hh>
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
78 void glViewer::swapBuffers() {
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 if (msg.severity() & QOpenGLDebugMessage::HighSeverity)
115 std::cerr << msg.message().toStdString() << std::endl;
116
117 // also catch deprecated function calls in core profile
118 else if (!glstate_->compatibilityProfile() && msg.type() == QOpenGLDebugMessage::DeprecatedBehaviorType)
119 std::cerr << msg.message().toStdString() << std::endl;
120 }
121
122
123 //-----------------------------------------------------------------------------
124
125 void
126 glViewer::copyToImage( QImage& _image,
127 unsigned int _l, unsigned int _t,
128 unsigned int _w, unsigned int _h,
129 GLenum /* _buffer */ )
130 {
131
132 _image = glWidget_->grabFramebuffer().copy(_l, _t, _w, _h).convertToFormat(QImage::Format_RGB32);
133 }
134
135 //-----------------------------------------------------------------------------
136
137 void glViewer::makeWidgetCurrent()
138 {
139 glWidget_->makeCurrent();
140 }
141
142 //-----------------------------------------------------------------------------
143
144 bool glViewer::createQFBO(QOpenGLFramebufferObject*& ptr, GLuint* _handle, int _width, int _height, int* _samples)
145 {
146 QFramebufferObjectFormat format;
147 format.setInternalTextureFormat(GL_RGBA);
148 format.setTextureTarget(GL_TEXTURE_2D);
149 if(*_samples > -1) //use -1 to indicate, that sample count is bit set
150 {
151 // set the attachments as in the standard rendering
152 format.setAttachment(QFramebufferObject::CombinedDepthStencil);
153 // 16 samples per pixel as we want a nice snapshot. If this is not supported
154 // it will fall back to the maximal supported number of samples
155 format.setSamples(*_samples);
156 }
157
158 QFramebufferObject* fb;
159 fb = new QFramebufferObject(_width, _height, format);
160 ptr = fb;
161 if(fb->isValid())
162 {
163 *_handle = fb->handle();
164 *_samples = fb->format().samples(); // store the actual samples qt uses
165 return true;
166 }
167 else
168 {
169 *_handle = 0;
170 return false;
171 }
172 }
173
174 //-----------------------------------------------------------------------------
175
176 void glViewer::blitQFBO(QOpenGLFramebufferObject* _ptr1, const QRect& _size1, QOpenGLFramebufferObject* _ptr2, const QRect& _size2)
177 {
178 QFramebufferObject::blitFramebuffer(_ptr1, _size1, _ptr2, _size2);
179 }
180
181 //-----------------------------------------------------------------------------
182
183 bool glViewer::bindQFBO(QOpenGLFramebufferObject* _ptr)
184 {
185 return _ptr->bind();
186 }
187
188 //-----------------------------------------------------------------------------
189
190 bool glViewer::QFBOResized(QOpenGLFramebufferObject* _ptr1)
191 {
192 return _ptr1->size() != QSize(glWidth(), glHeight());
193 }
194
195 //-----------------------------------------------------------------------------
196
197 void glViewer::deleteQFBO(QOpenGLFramebufferObject* _ptr)
198 {
199 delete _ptr;
200 }
201
202 //=============================================================================
203 //=============================================================================
204
glViewer::QFramebufferObjectFormat
QOpenGLFramebufferObjectFormat QFramebufferObjectFormat
Framebuffer object that holds the pick cache.
Definition: QtBaseViewer.hh:873
glViewer::copyToImage
void copyToImage(QImage &_image, GLenum _buffer=GL_BACK)
copy current framebuffer to an QImage
Definition: QtBaseViewer.hh:246
glViewer::QFramebufferObject
QOpenGLFramebufferObject QFramebufferObject
Framebuffer object that holds the pick cache.
Definition: QtBaseViewer.hh:874
glViewer::glstate_
ACG::GLState * glstate_
Gl State.
Definition: QtBaseViewer.hh:984
GlobalOptions.hh
glViewer::glHeight
unsigned int glHeight() const
get height of QGLWidget
Definition: QtBaseViewer.cc:1454
glViewer::makeCurrent
virtual void makeCurrent()
Makes this widget the current widget for OpenGL operations.
Definition: QtBaseViewer.cc:228
glViewer::processGLDebugMessage
void processGLDebugMessage(const QOpenGLDebugMessage &msg)
process opengl debug messages
Definition: QtBaseViewer_qt.cc:112
glViewer::glWidth
unsigned int glWidth() const
get width of QGLWidget
Definition: QtBaseViewer.cc:1451
glViewer::swapBuffers
virtual void swapBuffers()
Swaps the screen contents with the off-screen buffer.
Definition: QtBaseViewer_qt.cc:78