QtBase  v6.3.1
doc_gui_widgets_qopenglwidget.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
52 class MyGLWidget : public QOpenGLWidget
53 {
54 public:
56 
57 protected:
58  void initializeGL() override
59  {
60  // Set up the rendering context, load shaders and other resources, etc.:
62  f->glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
63  ...
64  }
65 
66  void resizeGL(int w, int h) override
67  {
68  // Update projection matrix and other size related settings:
69  m_projection.setToIdentity();
70  m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f);
71  ...
72  }
73 
74  void paintGL() override
75  {
76  // Draw the scene:
78  f->glClear(GL_COLOR_BUFFER_BIT);
79  ...
80  }
81 
82 };
84 
86 class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
87 {
88  ...
89  void initializeGL() override
90  {
92  glClearColor(...);
93  ...
94  }
95  ...
96 };
98 
103 format.setStencilBufferSize(8);
104 format.setVersion(3, 2);
106 widget->setFormat(format); // must be called before the widget or its parent window gets shown
108 
110  ...
111  void paintGL() override
112  {
114  ...
115  f->glDrawArraysInstanced(...);
116  ...
117  }
118  ...
120 
122 class MyGLWidget : public QOpenGLWidget
123 {
124  ...
125 
126 private:
128  QOpenGLBuffer m_vbo;
129  QOpenGLShaderProgram *m_program;
130  QOpenGLShader *m_shader;
131  QOpenGLTexture *m_texture;
132 };
133 
135  : m_program(0), m_shader(0), m_texture(0)
136 {
137  // No OpenGL resource initialization is done here.
138 }
139 
140 MyGLWidget::~MyGLWidget()
141 {
142  // Make sure the context is current and then explicitly
143  // destroy all underlying OpenGL resources.
144  makeCurrent();
145 
146  delete m_texture;
147  delete m_shader;
148  delete m_program;
149 
150  m_vbo.destroy();
151  m_vao.destroy();
152 
153  doneCurrent();
154 }
155 
157 {
158  m_vao.create();
159  if (m_vao.isCreated())
160  m_vao.bind();
161 
162  m_vbo.create();
163  m_vbo.bind();
164  m_vbo.allocate(...);
165 
166  m_texture = new QOpenGLTexture(QImage(...));
167 
168  m_shader = new QOpenGLShader(...);
169  m_program = new QOpenGLShaderProgram(...);
170 
171  ...
172 }
174 
177 {
178  // context() and QOpenGLContext::currentContext() are equivalent when called from initializeGL or paintGL.
180 }
181 
182 void MyGLWidget::cleanup()
183 {
184  makeCurrent();
185  delete m_texture;
186  m_texture = 0;
187  ...
188  doneCurrent();
189 }
191 
193 int main(int argc, char **argv)
194 {
195  QApplication app(argc, argv);
196 
199  format.setStencilBufferSize(8);
200  format.setVersion(3, 2);
203 
205  widget.show();
206 
207  return app.exec();
208 }
void initializeGL() override
[4]
MyGLWidget(QWidget *parent)
void resizeGL(int w, int h) override
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
static int exec()
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:73
QObject * parent() const
Definition: qobject.h:409
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QOpenGLBuffer class provides functions for creating and managing OpenGL buffer objects.
Definition: qopenglbuffer.h:56
void allocate(const void *data, int count)
void aboutToBeDestroyed()
static QOpenGLContext * currentContext()
QOpenGLFunctions * functions() const
The QOpenGLFunctions_3_2_Core class provides all functions for OpenGL 3.2 core profile.
void glDrawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei instancecount)
The QOpenGLFunctions class provides cross-platform access to the OpenGL ES 2.0 API.
void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
void glClear(GLbitfield mask)
The QOpenGLShader class allows OpenGL shaders to be compiled.
The QOpenGLShaderProgram class allows OpenGL shader programs to be linked and used.
The QOpenGLTexture class encapsulates an OpenGL texture object.
The QOpenGLVertexArrayObject class wraps an OpenGL Vertex Array Object. \inmodule QtOpenGL.
The QOpenGLWidget class is a widget for rendering OpenGL graphics.
Definition: qopenglwidget.h:54
QOpenGLContext * context() const
void setFormat(const QSurfaceFormat &format)
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
void setDepthBufferSize(int size)
static void setDefaultFormat(const QSurfaceFormat &format)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void show()
Definition: qwidget.cpp:7825
int main(int argc, char **argv)
[5]
QOpenGLWidget * widget
[1]
void paintGL() override
[2]
QSurfaceFormat format
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld cleanup[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1 beq endif SRC MASK if dst_r_bpp DST_R else add endif PF add sub src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head pixblock_size cache_preload_simple process_pixblock_tail pixinterleave dst_w_basereg irp beq endif process_pixblock_tail_head tst beq irp if pixblock_size chunk_size tst beq pixld SRC pixld MASK if DST_R else pixld DST_R endif if src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head if pixblock_size cache_preload_simple endif process_pixblock_tail pixinterleave dst_w_basereg irp if pixblock_size chunk_size tst beq if DST_W else pixst DST_W else mov ORIG_W endif add lsl if lsl endif if lsl endif lsl endif lsl endif lsl endif subs mov DST_W if regs_shortage str endif bge start_of_loop_label endm macro generate_composite_function
GLfloat GLfloat GLfloat w
[0]
GLfloat GLfloat f
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat GLfloat GLfloat h
QApplication app(argc, argv)
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent