| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
//! [0] |
| 5 |
class MyGLWidget : public QOpenGLWidget |
| 6 |
{ |
| 7 |
public: |
| 8 |
MyGLWidget(QWidget *parent) : QOpenGLWidget(parent) { } |
| 9 |
|
| 10 |
protected: |
| 11 |
void initializeGL() override |
| 12 |
{ |
| 13 |
// Set up the rendering context, load shaders and other resources, etc.: |
| 14 |
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); |
| 15 |
f->glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
| 16 |
... |
| 17 |
} |
| 18 |
|
| 19 |
void resizeGL(int w, int h) override |
| 20 |
{ |
| 21 |
// Update projection matrix and other size related settings: |
| 22 |
m_projection.setToIdentity(); |
| 23 |
m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f); |
| 24 |
... |
| 25 |
} |
| 26 |
|
| 27 |
void paintGL() override |
| 28 |
{ |
| 29 |
// Draw the scene: |
| 30 |
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); |
| 31 |
f->glClear(GL_COLOR_BUFFER_BIT); |
| 32 |
... |
| 33 |
} |
| 34 |
|
| 35 |
}; |
| 36 |
//! [0] |
| 37 |
|
| 38 |
//! [1] |
| 39 |
class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions |
| 40 |
{ |
| 41 |
... |
| 42 |
void initializeGL() override |
| 43 |
{ |
| 44 |
initializeOpenGLFunctions(); |
| 45 |
glClearColor(...); |
| 46 |
... |
| 47 |
} |
| 48 |
... |
| 49 |
}; |
| 50 |
//! [1] |
| 51 |
|
| 52 |
//! [2] |
| 53 |
QOpenGLWidget *widget = new QOpenGLWidget(parent); |
| 54 |
QSurfaceFormat format; |
| 55 |
format.setDepthBufferSize(24); |
| 56 |
format.setStencilBufferSize(8); |
| 57 |
format.setVersion(3, 2); |
| 58 |
format.setProfile(QSurfaceFormat::CoreProfile); |
| 59 |
widget->setFormat(format); // must be called before the widget or its parent window gets shown |
| 60 |
//! [2] |
| 61 |
|
| 62 |
//! [3] |
| 63 |
... |
| 64 |
void paintGL() override |
| 65 |
{ |
| 66 |
QOpenGLFunctions_3_2_Core *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_2_Core>(); |
| 67 |
... |
| 68 |
f->glDrawArraysInstanced(...); |
| 69 |
... |
| 70 |
} |
| 71 |
... |
| 72 |
//! [3] |
| 73 |
|
| 74 |
//! [4] |
| 75 |
class MyGLWidget : public QOpenGLWidget |
| 76 |
{ |
| 77 |
... |
| 78 |
|
| 79 |
private: |
| 80 |
QOpenGLVertexArrayObject m_vao; |
| 81 |
QOpenGLBuffer m_vbo; |
| 82 |
QOpenGLShaderProgram *m_program; |
| 83 |
QOpenGLShader *m_shader; |
| 84 |
QOpenGLTexture *m_texture; |
| 85 |
}; |
| 86 |
|
| 87 |
MyGLWidget::MyGLWidget() |
| 88 |
: m_program(0), m_shader(0), m_texture(0) |
| 89 |
{ |
| 90 |
// No OpenGL resource initialization is done here. |
| 91 |
} |
| 92 |
|
| 93 |
MyGLWidget::~MyGLWidget() |
| 94 |
{ |
| 95 |
// Make sure the context is current and then explicitly |
| 96 |
// destroy all underlying OpenGL resources. |
| 97 |
makeCurrent(); |
| 98 |
|
| 99 |
delete m_texture; |
| 100 |
delete m_shader; |
| 101 |
delete m_program; |
| 102 |
|
| 103 |
m_vbo.destroy(); |
| 104 |
m_vao.destroy(); |
| 105 |
|
| 106 |
doneCurrent(); |
| 107 |
} |
| 108 |
|
| 109 |
void MyGLWidget::initializeGL() |
| 110 |
{ |
| 111 |
m_vao.create(); |
| 112 |
if (m_vao.isCreated()) |
| 113 |
m_vao.bind(); |
| 114 |
|
| 115 |
m_vbo.create(); |
| 116 |
m_vbo.bind(); |
| 117 |
m_vbo.allocate(...); |
| 118 |
|
| 119 |
m_texture = new QOpenGLTexture(QImage(...)); |
| 120 |
|
| 121 |
m_shader = new QOpenGLShader(...); |
| 122 |
m_program = new QOpenGLShaderProgram(...); |
| 123 |
|
| 124 |
... |
| 125 |
} |
| 126 |
//! [4] |
| 127 |
|
| 128 |
//! [5] |
| 129 |
MyGLWidget::~MyGLWidget() |
| 130 |
{ |
| 131 |
cleanup(); |
| 132 |
} |
| 133 |
|
| 134 |
void MyGLWidget::initializeGL() |
| 135 |
{ |
| 136 |
... |
| 137 |
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &MyGLWidget::cleanup); |
| 138 |
} |
| 139 |
|
| 140 |
void MyGLWidget::cleanup() |
| 141 |
{ |
| 142 |
makeCurrent(); |
| 143 |
delete m_texture; |
| 144 |
m_texture = 0; |
| 145 |
... |
| 146 |
doneCurrent(); |
| 147 |
disconnect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &MyGLWidget::cleanup); |
| 148 |
} |
| 149 |
//! [5] |
| 150 |
|
| 151 |
//! [6] |
| 152 |
int main(int argc, char **argv) |
| 153 |
{ |
| 154 |
QApplication app(argc, argv); |
| 155 |
|
| 156 |
QSurfaceFormat format; |
| 157 |
format.setDepthBufferSize(24); |
| 158 |
format.setStencilBufferSize(8); |
| 159 |
format.setVersion(3, 2); |
| 160 |
format.setProfile(QSurfaceFormat::CoreProfile); |
| 161 |
QSurfaceFormat::setDefaultFormat(format); |
| 162 |
|
| 163 |
MyWidget widget; |
| 164 |
widget.show(); |
| 165 |
|
| 166 |
return app.exec(); |
| 167 |
} |
| 168 |
//! [6] |