summaryrefslogtreecommitdiffstats
path: root/examples/opengl
diff options
context:
space:
mode:
Diffstat (limited to 'examples/opengl')
-rw-r--r--examples/opengl/2dpainting/CMakeLists.txt21
-rw-r--r--examples/opengl/CMakeLists.txt4
-rw-r--r--examples/opengl/contextinfo/CMakeLists.txt39
-rw-r--r--examples/opengl/contextinfo/contextinfo.pro14
-rw-r--r--examples/opengl/contextinfo/main.cpp25
-rw-r--r--examples/opengl/contextinfo/renderwindow.cpp191
-rw-r--r--examples/opengl/contextinfo/renderwindow.h48
-rw-r--r--examples/opengl/contextinfo/widget.cpp360
-rw-r--r--examples/opengl/contextinfo/widget.h46
-rw-r--r--examples/opengl/cube/CMakeLists.txt21
-rw-r--r--examples/opengl/cube/cube.pngbin30341 -> 88913 bytes
-rw-r--r--examples/opengl/cube/fshader.glsl2
-rw-r--r--examples/opengl/cube/mainwidget.cpp17
-rw-r--r--examples/opengl/cube/vshader.glsl2
-rw-r--r--examples/opengl/doc/src/2dpainting.qdoc1
-rw-r--r--examples/opengl/doc/src/cube.qdoc11
-rw-r--r--examples/opengl/doc/src/hellogl2.qdoc1
-rw-r--r--examples/opengl/doc/src/hellogles3.qdoc141
-rw-r--r--examples/opengl/doc/src/openglwindow.qdoc2
-rw-r--r--examples/opengl/doc/src/stereoqopenglwidget.qdoc15
-rw-r--r--examples/opengl/hellogl2/CMakeLists.txt21
-rw-r--r--examples/opengl/hellogl2/mainwindow.cpp18
-rw-r--r--examples/opengl/hellogl2/window.cpp80
-rw-r--r--examples/opengl/hellogl2/window.h12
-rw-r--r--examples/opengl/hellogles3/CMakeLists.txt21
-rw-r--r--examples/opengl/hellogles3/glwindow.h12
-rw-r--r--examples/opengl/opengl.pro4
-rw-r--r--examples/opengl/openglwindow/CMakeLists.txt21
-rw-r--r--examples/opengl/openglwindow/main.cpp81
-rw-r--r--examples/opengl/openglwindow/openglwindow.h8
-rw-r--r--examples/opengl/qopenglwidget/CMakeLists.txt53
-rw-r--r--examples/opengl/qopenglwidget/bubble.cpp99
-rw-r--r--examples/opengl/qopenglwidget/bubble.h39
-rw-r--r--examples/opengl/qopenglwidget/glwidget.cpp540
-rw-r--r--examples/opengl/qopenglwidget/glwidget.h85
-rw-r--r--examples/opengl/qopenglwidget/main.cpp43
-rw-r--r--examples/opengl/qopenglwidget/mainwindow.cpp188
-rw-r--r--examples/opengl/qopenglwidget/mainwindow.h39
-rw-r--r--examples/opengl/qopenglwidget/qopenglwidget.pro15
-rw-r--r--examples/opengl/qopenglwidget/qt.pngbin2991 -> 0 bytes
-rw-r--r--examples/opengl/qopenglwidget/texture.qrc5
-rw-r--r--examples/opengl/stereoqopenglwidget/CMakeLists.txt31
-rw-r--r--examples/opengl/textures/CMakeLists.txt21
-rw-r--r--examples/opengl/textures/glwidget.cpp8
-rw-r--r--examples/opengl/textures/main.cpp2
45 files changed, 361 insertions, 2046 deletions
diff --git a/examples/opengl/2dpainting/CMakeLists.txt b/examples/opengl/2dpainting/CMakeLists.txt
index eeaa9cb726..b4ae3aa313 100644
--- a/examples/opengl/2dpainting/CMakeLists.txt
+++ b/examples/opengl/2dpainting/CMakeLists.txt
@@ -1,15 +1,9 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(2dpainting LANGUAGES CXX)
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/2dpainting")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets)
qt_standard_project_setup()
@@ -36,7 +30,14 @@ target_link_libraries(2dpainting PRIVATE
)
install(TARGETS 2dpainting
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET 2dpainting
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/opengl/CMakeLists.txt b/examples/opengl/CMakeLists.txt
index 2b075de2f7..09e31defe5 100644
--- a/examples/opengl/CMakeLists.txt
+++ b/examples/opengl/CMakeLists.txt
@@ -1,13 +1,11 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
qt_internal_add_example(openglwindow)
qt_internal_add_example(hellogles3)
if(TARGET Qt6::Widgets)
- qt_internal_add_example(contextinfo)
qt_internal_add_example(2dpainting)
qt_internal_add_example(hellogl2)
- qt_internal_add_example(qopenglwidget)
qt_internal_add_example(cube)
qt_internal_add_example(textures)
qt_internal_add_example(stereoqopenglwidget)
diff --git a/examples/opengl/contextinfo/CMakeLists.txt b/examples/opengl/contextinfo/CMakeLists.txt
deleted file mode 100644
index 8dfd412408..0000000000
--- a/examples/opengl/contextinfo/CMakeLists.txt
+++ /dev/null
@@ -1,39 +0,0 @@
-# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
-
-cmake_minimum_required(VERSION 3.16)
-project(contextinfo LANGUAGES CXX)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/contextinfo")
-
-find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL Widgets)
-
-qt_standard_project_setup()
-
-qt_add_executable(contextinfo
- main.cpp
- renderwindow.cpp renderwindow.h
- widget.cpp widget.h
-)
-
-set_target_properties(contextinfo PROPERTIES
- WIN32_EXECUTABLE TRUE
- MACOSX_BUNDLE TRUE
-)
-
-target_link_libraries(contextinfo PRIVATE
- Qt6::Core
- Qt6::Gui
- Qt6::OpenGL
- Qt6::Widgets
-)
-
-install(TARGETS contextinfo
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
-)
diff --git a/examples/opengl/contextinfo/contextinfo.pro b/examples/opengl/contextinfo/contextinfo.pro
deleted file mode 100644
index ae8f4067b6..0000000000
--- a/examples/opengl/contextinfo/contextinfo.pro
+++ /dev/null
@@ -1,14 +0,0 @@
-TEMPLATE = app
-QT += widgets opengl
-requires(qtConfig(filedialog))
-
-SOURCES += main.cpp \
- widget.cpp \
- renderwindow.cpp
-
-HEADERS += widget.h \
- renderwindow.h
-
-# install
-target.path = $$[QT_INSTALL_EXAMPLES]/opengl/contextinfo
-INSTALLS += target
diff --git a/examples/opengl/contextinfo/main.cpp b/examples/opengl/contextinfo/main.cpp
deleted file mode 100644
index 253425ded9..0000000000
--- a/examples/opengl/contextinfo/main.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include <QApplication>
-#include "widget.h"
-
-int main(int argc, char **argv)
-{
- for (int i = 1; i < argc; ++i) {
- if (!qstrcmp(argv[i], "-g"))
- QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
- else if (!qstrcmp(argv[i], "-s"))
- QCoreApplication::setAttribute(Qt::AA_UseSoftwareOpenGL);
- else if (!qstrcmp(argv[i], "-d"))
- QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
- }
-
- QApplication app(argc, argv);
-
- Widget w;
- w.resize(700, 800);
- w.show();
-
- return app.exec();
-}
diff --git a/examples/opengl/contextinfo/renderwindow.cpp b/examples/opengl/contextinfo/renderwindow.cpp
deleted file mode 100644
index 64c0f47db9..0000000000
--- a/examples/opengl/contextinfo/renderwindow.cpp
+++ /dev/null
@@ -1,191 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "renderwindow.h"
-#include <QTimer>
-#include <QMatrix4x4>
-#include <QOpenGLContext>
-#include <QtOpenGL/QOpenGLShaderProgram>
-#include <QOpenGLFunctions>
-
-RenderWindow::RenderWindow(const QSurfaceFormat &format)
- : m_context(nullptr),
- m_initialized(false),
- m_forceGLSL110(false),
- m_angle(0.0f)
-{
- setSurfaceType(QWindow::OpenGLSurface);
- setFormat(format);
- m_context = new QOpenGLContext(this);
- m_context->setFormat(requestedFormat());
- if (!m_context->create()) {
- delete m_context;
- m_context = nullptr;
- }
-}
-
-void RenderWindow::exposeEvent(QExposeEvent *)
-{
- if (isExposed())
- render();
-}
-
-// ES needs the precision qualifiers.
-// On desktop GL QOpenGLShaderProgram inserts dummy defines for highp/mediump/lowp.
-static const char *vertexShaderSource110 =
- "attribute highp vec4 posAttr;\n"
- "attribute lowp vec4 colAttr;\n"
- "varying lowp vec4 col;\n"
- "uniform highp mat4 matrix;\n"
- "void main() {\n"
- " col = colAttr;\n"
- " gl_Position = matrix * posAttr;\n"
- "}\n";
-
-static const char *fragmentShaderSource110 =
- "varying lowp vec4 col;\n"
- "void main() {\n"
- " gl_FragColor = col;\n"
- "}\n";
-
-static const char *vertexShaderSource =
- "#version 150\n"
- "in vec4 posAttr;\n"
- "in vec4 colAttr;\n"
- "out vec4 col;\n"
- "uniform mat4 matrix;\n"
- "void main() {\n"
- " col = colAttr;\n"
- " gl_Position = matrix * posAttr;\n"
- "}\n";
-
-static const char *fragmentShaderSource =
- "#version 150\n"
- "in vec4 col;\n"
- "out vec4 fragColor;\n"
- "void main() {\n"
- " fragColor = col;\n"
- "}\n";
-
-static GLfloat vertices[] = {
- 0.0f, 0.707f,
- -0.5f, -0.5f,
- 0.5f, -0.5f
-};
-
-static GLfloat colors[] = {
- 1.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 1.0f
-};
-
-void RenderWindow::init()
-{
- m_program = new QOpenGLShaderProgram(this);
-
- QSurfaceFormat format = m_context->format();
- bool useNewStyleShader = format.profile() == QSurfaceFormat::CoreProfile;
- // Try to handle 3.0 & 3.1 that do not have the core/compatibility profile concept 3.2+ has.
- // This may still fail since version 150 (3.2) is specified in the sources but it's worth a try.
- if (format.renderableType() == QSurfaceFormat::OpenGL && format.majorVersion() == 3 && format.minorVersion() <= 1)
- useNewStyleShader = !format.testOption(QSurfaceFormat::DeprecatedFunctions);
- if (m_forceGLSL110)
- useNewStyleShader = false;
-
- const char *vsrc = useNewStyleShader ? vertexShaderSource : vertexShaderSource110;
- const char *fsrc = useNewStyleShader ? fragmentShaderSource : fragmentShaderSource110;
- qDebug("Using version %s shader", useNewStyleShader ? "150" : "110");
-
- if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vsrc)) {
- emit error(m_program->log());
- return;
- }
- if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fsrc)) {
- emit error(m_program->log());
- return;
- }
- if (!m_program->link()) {
- emit error(m_program->log());
- return;
- }
-
- m_posAttr = m_program->attributeLocation("posAttr");
- m_colAttr = m_program->attributeLocation("colAttr");
- m_matrixUniform = m_program->uniformLocation("matrix");
-
- m_vbo.create();
- m_vbo.bind();
- m_vbo.allocate(vertices, sizeof(vertices) + sizeof(colors));
- m_vbo.write(sizeof(vertices), colors, sizeof(colors));
- m_vbo.release();
-
- QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
- if (m_vao.isCreated()) // have VAO support, use it
- setupVertexAttribs();
-}
-
-void RenderWindow::setupVertexAttribs()
-{
- m_vbo.bind();
- m_program->setAttributeBuffer(m_posAttr, GL_FLOAT, 0, 2);
- m_program->setAttributeBuffer(m_colAttr, GL_FLOAT, sizeof(vertices), 3);
- m_program->enableAttributeArray(m_posAttr);
- m_program->enableAttributeArray(m_colAttr);
- m_vbo.release();
-}
-
-bool RenderWindow::event(QEvent *ev)
-{
- if (ev->type() == QEvent::UpdateRequest)
- render();
- return QWindow::event(ev);
-}
-
-void RenderWindow::render()
-{
- if (!m_context->makeCurrent(this)) {
- emit error(tr("makeCurrent() failed"));
- return;
- }
-
- QOpenGLFunctions *f = m_context->functions();
- if (!m_initialized) {
- m_initialized = true;
- f->glEnable(GL_DEPTH_TEST);
- f->glClearColor(0, 0, 0, 1);
- init();
- emit ready();
- }
-
- if (!m_vbo.isCreated()) // init() failed, don't bother with trying to render
- return;
-
- const qreal retinaScale = devicePixelRatio();
- f->glViewport(0, 0, width() * retinaScale, height() * retinaScale);
- f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- m_program->bind();
- QMatrix4x4 matrix;
- matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
- matrix.translate(0.0f, 0.0f, -2.0f);
- matrix.rotate(m_angle, 0.0f, 1.0f, 0.0f);
- m_program->setUniformValue(m_matrixUniform, matrix);
-
- if (m_vao.isCreated())
- m_vao.bind();
- else // no VAO support, set the vertex attribute arrays now
- setupVertexAttribs();
-
- f->glDrawArrays(GL_TRIANGLES, 0, 3);
-
- m_vao.release();
- m_program->release();
-
- // swapInterval is 1 by default which means that swapBuffers() will (hopefully) block
- // and wait for vsync.
- m_context->swapBuffers(this);
-
- m_angle += 1.0f;
-
- requestUpdate();
-}
diff --git a/examples/opengl/contextinfo/renderwindow.h b/examples/opengl/contextinfo/renderwindow.h
deleted file mode 100644
index 964dd601a6..0000000000
--- a/examples/opengl/contextinfo/renderwindow.h
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef RENDERWINDOW_H
-#define RENDERWINDOW_H
-
-#include <QWindow>
-#include <QOpenGLVertexArrayObject>
-#include <QOpenGLBuffer>
-
-QT_FORWARD_DECLARE_CLASS(QOpenGLContext)
-QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
-
-class RenderWindow : public QWindow
-{
- Q_OBJECT
-
-public:
- RenderWindow(const QSurfaceFormat &format);
- QOpenGLContext *context() { return m_context; }
- void exposeEvent(QExposeEvent *) override;
- void setForceGLSL110(bool enable) { m_forceGLSL110 = enable; }
-
-signals:
- void ready();
- void error(const QString &msg);
-
-protected:
- bool event(QEvent *ev) override;
-
-private slots:
- void render();
-
-private:
- void init();
- void setupVertexAttribs();
-
- QOpenGLContext *m_context;
- bool m_initialized;
- bool m_forceGLSL110;
- QOpenGLShaderProgram *m_program;
- int m_posAttr, m_colAttr, m_matrixUniform;
- QOpenGLVertexArrayObject m_vao;
- QOpenGLBuffer m_vbo;
- float m_angle;
-};
-
-#endif // RENDERWINDOW_H
diff --git a/examples/opengl/contextinfo/widget.cpp b/examples/opengl/contextinfo/widget.cpp
deleted file mode 100644
index bc31497039..0000000000
--- a/examples/opengl/contextinfo/widget.cpp
+++ /dev/null
@@ -1,360 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "widget.h"
-#include "renderwindow.h"
-#include <QVBoxLayout>
-#include <QComboBox>
-#include <QGroupBox>
-#include <QRadioButton>
-#include <QCheckBox>
-#include <QHBoxLayout>
-#include <QLabel>
-#include <QList>
-#include <QByteArray>
-#include <QPushButton>
-#include <QTextEdit>
-#include <QSplitter>
-#include <QGuiApplication>
-#include <QSurfaceFormat>
-#include <QOpenGLContext>
-#include <QOpenGLFunctions>
-#include <QDebug>
-#include <QTextStream>
-
-struct Version {
- const char *str;
- int major;
- int minor;
-};
-
-static struct Version versions[] = {
- { "1.0", 1, 0 },
- { "1.1", 1, 1 },
- { "1.2", 1, 2 },
- { "1.3", 1, 3 },
- { "1.4", 1, 4 },
- { "1.5", 1, 5 },
- { "2.0", 2, 0 },
- { "2.1", 2, 1 },
- { "3.0", 3, 0 },
- { "3.1", 3, 1 },
- { "3.2", 3, 2 },
- { "3.3", 3, 3 },
- { "4.0", 4, 0 },
- { "4.1", 4, 1 },
- { "4.2", 4, 2 },
- { "4.3", 4, 3 },
- { "4.4", 4, 4 },
- { "4.5", 4, 5 }
-};
-
-struct Profile {
- const char *str;
- QSurfaceFormat::OpenGLContextProfile profile;
-};
-
-static struct Profile profiles[] = {
- { "none", QSurfaceFormat::NoProfile },
- { "core", QSurfaceFormat::CoreProfile },
- { "compatibility", QSurfaceFormat::CompatibilityProfile }
-};
-
-struct Option {
- const char *str;
- QSurfaceFormat::FormatOption option;
-};
-
-static struct Option options[] = {
- { "deprecated functions (not forward compatible)", QSurfaceFormat::DeprecatedFunctions },
- { "debug context", QSurfaceFormat::DebugContext },
- { "stereo buffers", QSurfaceFormat::StereoBuffers },
- // This is not a QSurfaceFormat option but is helpful to determine if the driver
- // allows compiling old-style shaders with core profile.
- { "force version 110 shaders", QSurfaceFormat::FormatOption(0) }
-};
-
-struct Renderable {
- const char *str;
- QSurfaceFormat::RenderableType renderable;
-};
-
-static struct Renderable renderables[] = {
- { "default", QSurfaceFormat::DefaultRenderableType },
-#ifndef Q_OS_ANDROID
- { "OpenGL", QSurfaceFormat::OpenGL },
-#endif
- { "OpenGL ES", QSurfaceFormat::OpenGLES }
-};
-
-void Widget::addVersions(QLayout *layout)
-{
- QHBoxLayout *hbox = new QHBoxLayout;
- hbox->setSpacing(20);
- QLabel *label = new QLabel(tr("Context &version: "));
- hbox->addWidget(label);
- m_version = new QComboBox;
- m_version->setMinimumWidth(60);
- label->setBuddy(m_version);
- hbox->addWidget(m_version);
- for (size_t i = 0; i < sizeof(versions) / sizeof(Version); ++i) {
- m_version->addItem(QString::fromLatin1(versions[i].str));
- if (versions[i].major == 2 && versions[i].minor == 0)
- m_version->setCurrentIndex(m_version->count() - 1);
- }
-
- QPushButton *btn = new QPushButton(tr("Create context"));
- connect(btn, &QPushButton::clicked, this, &Widget::start);
- btn->setMinimumSize(120, 40);
- hbox->addWidget(btn);
-
- layout->addItem(hbox);
-}
-
-void Widget::addProfiles(QLayout *layout)
-{
- QGroupBox *groupBox = new QGroupBox(tr("Profile"));
- QVBoxLayout *vbox = new QVBoxLayout;
- for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
- vbox->addWidget(new QRadioButton(QString::fromLatin1(profiles[i].str)));
- static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true);
- groupBox->setLayout(vbox);
- layout->addWidget(groupBox);
- m_profiles = vbox;
-}
-
-void Widget::addOptions(QLayout *layout)
-{
- QGroupBox *groupBox = new QGroupBox(tr("Options"));
- QVBoxLayout *vbox = new QVBoxLayout;
- for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
- vbox->addWidget(new QCheckBox(QString::fromLatin1(options[i].str)));
- groupBox->setLayout(vbox);
- layout->addWidget(groupBox);
- m_options = vbox;
-}
-
-void Widget::addRenderableTypes(QLayout *layout)
-{
- QGroupBox *groupBox = new QGroupBox(tr("Renderable type"));
- QVBoxLayout *vbox = new QVBoxLayout;
- for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
- vbox->addWidget(new QRadioButton(QString::fromLatin1(renderables[i].str)));
- static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true);
- groupBox->setLayout(vbox);
- layout->addWidget(groupBox);
- m_renderables = vbox;
-}
-
-void Widget::addRenderWindow()
-{
- m_renderWindowLayout->addWidget(m_renderWindowContainer);
-}
-
-static QWidget *widgetWithLayout(QLayout *layout)
-{
- QWidget *w = new QWidget;
- w->setLayout(layout);
- return w;
-}
-
-Widget::Widget(QWidget *parent)
- : QWidget(parent)
-{
- QVBoxLayout *layout = new QVBoxLayout;
- QSplitter *vsplit = new QSplitter(Qt::Vertical);
- layout->addWidget(vsplit);
-
- QSplitter *hsplit = new QSplitter;
-
- QVBoxLayout *settingsLayout = new QVBoxLayout;
- addVersions(settingsLayout);
- addProfiles(settingsLayout);
- addOptions(settingsLayout);
- addRenderableTypes(settingsLayout);
- hsplit->addWidget(widgetWithLayout(settingsLayout));
-
- QVBoxLayout *outputLayout = new QVBoxLayout;
- m_output = new QTextEdit;
- m_output->setReadOnly(true);
- outputLayout->addWidget(m_output);
- m_extensions = new QTextEdit;
- m_extensions->setReadOnly(true);
- outputLayout->addWidget(m_extensions);
- hsplit->addWidget(widgetWithLayout(outputLayout));
-
- hsplit->setStretchFactor(0, 4);
- hsplit->setStretchFactor(1, 6);
- vsplit->addWidget(hsplit);
-
- m_renderWindowLayout = new QVBoxLayout;
- vsplit->addWidget(widgetWithLayout(m_renderWindowLayout));
- vsplit->setStretchFactor(1, 5);
-
- m_renderWindowContainer = new QWidget;
- addRenderWindow();
-
- QString description;
- QTextStream str(&description);
- str << "Qt " << QT_VERSION_STR << ' ' << QGuiApplication::platformName();
- const char *openGlVariables[] =
- {"QT_ANGLE_PLATFORM", "QT_OPENGL", "QT_OPENGL_BUGLIST", "QT_OPENGL_DLL"};
- const size_t variableCount = sizeof(openGlVariables) / sizeof(openGlVariables[0]);
- for (size_t v = 0; v < variableCount; ++v) {
- if (qEnvironmentVariableIsSet(openGlVariables[v]))
- str << ' ' << openGlVariables[v] << '=' << qgetenv(openGlVariables[v]);
- }
- if (QCoreApplication::testAttribute(Qt::AA_UseOpenGLES))
- str << " Qt::AA_UseOpenGLES";
- if (QCoreApplication::testAttribute(Qt::AA_UseSoftwareOpenGL))
- str << " Qt::AA_UseSoftwareOpenGL";
- if (QCoreApplication::testAttribute(Qt::AA_UseDesktopOpenGL))
- str << " Qt::AA_UseDesktopOpenGL";
- layout->addWidget(new QLabel(description));
-
- setLayout(layout);
-}
-
-void Widget::start()
-{
- QSurfaceFormat fmt;
-
- int idx = m_version->currentIndex();
- if (idx < 0)
- return;
- fmt.setVersion(versions[idx].major, versions[idx].minor);
-
- for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
- if (static_cast<QRadioButton *>(m_profiles->itemAt(int(i))->widget())->isChecked()) {
- fmt.setProfile(profiles[i].profile);
- break;
- }
-
- bool forceGLSL110 = false;
- for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
- if (static_cast<QCheckBox *>(m_options->itemAt(int(i))->widget())->isChecked()) {
- if (options[i].option)
- fmt.setOption(options[i].option);
- else if (i == 3)
- forceGLSL110 = true;
- }
-
- for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
- if (static_cast<QRadioButton *>(m_renderables->itemAt(int(i))->widget())->isChecked()) {
- fmt.setRenderableType(renderables[i].renderable);
- break;
- }
-
- // The example rendering will need a depth buffer.
- fmt.setDepthBufferSize(16);
-
- m_output->clear();
- m_extensions->clear();
- qDebug() << "Requesting surface format" << fmt;
-
- m_renderWindowLayout->removeWidget(m_renderWindowContainer);
- delete m_renderWindowContainer;
-
- RenderWindow *renderWindow = new RenderWindow(fmt);
- if (!renderWindow->context()) {
- m_output->append(tr("Failed to create context"));
- delete renderWindow;
- m_renderWindowContainer = new QWidget;
- addRenderWindow();
- return;
- }
- m_surface = renderWindow;
-
- renderWindow->setForceGLSL110(forceGLSL110);
- connect(renderWindow, &RenderWindow::ready, this, &Widget::renderWindowReady);
- connect(renderWindow, &RenderWindow::error, this, &Widget::renderWindowError);
-
- m_renderWindowContainer = QWidget::createWindowContainer(renderWindow);
- addRenderWindow();
-}
-
-void Widget::printFormat(const QSurfaceFormat &format)
-{
- m_output->append(tr("OpenGL version: %1.%2").arg(format.majorVersion()).arg(format.minorVersion()));
-
- for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
- if (profiles[i].profile == format.profile()) {
- m_output->append(tr("Profile: %1").arg(QString::fromLatin1(profiles[i].str)));
- break;
- }
-
- QString opts;
- for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
- if (format.testOption(options[i].option))
- opts += QString::fromLatin1(options[i].str) + QLatin1Char(' ');
- m_output->append(tr("Options: %1").arg(opts));
-
- for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
- if (renderables[i].renderable == format.renderableType()) {
- m_output->append(tr("Renderable type: %1").arg(QString::fromLatin1(renderables[i].str)));
- break;
- }
-
- m_output->append(tr("Depth buffer size: %1").arg(QString::number(format.depthBufferSize())));
- m_output->append(tr("Stencil buffer size: %1").arg(QString::number(format.stencilBufferSize())));
- m_output->append(tr("Samples: %1").arg(QString::number(format.samples())));
- m_output->append(tr("Red buffer size: %1").arg(QString::number(format.redBufferSize())));
- m_output->append(tr("Green buffer size: %1").arg(QString::number(format.greenBufferSize())));
- m_output->append(tr("Blue buffer size: %1").arg(QString::number(format.blueBufferSize())));
- m_output->append(tr("Alpha buffer size: %1").arg(QString::number(format.alphaBufferSize())));
- m_output->append(tr("Swap interval: %1").arg(QString::number(format.swapInterval())));
-}
-
-void Widget::renderWindowReady()
-{
- QOpenGLContext *context = QOpenGLContext::currentContext();
- Q_ASSERT(context);
-
- QString vendor, renderer, version, glslVersion;
- const GLubyte *p;
- QOpenGLFunctions *f = context->functions();
- if ((p = f->glGetString(GL_VENDOR)))
- vendor = QString::fromLatin1(reinterpret_cast<const char *>(p));
- if ((p = f->glGetString(GL_RENDERER)))
- renderer = QString::fromLatin1(reinterpret_cast<const char *>(p));
- if ((p = f->glGetString(GL_VERSION)))
- version = QString::fromLatin1(reinterpret_cast<const char *>(p));
- if ((p = f->glGetString(GL_SHADING_LANGUAGE_VERSION)))
- glslVersion = QString::fromLatin1(reinterpret_cast<const char *>(p));
-
- m_output->append(tr("*** Context information ***"));
- m_output->append(tr("Vendor: %1").arg(vendor));
- m_output->append(tr("Renderer: %1").arg(renderer));
- m_output->append(tr("OpenGL version: %1").arg(version));
- m_output->append(tr("GLSL version: %1").arg(glslVersion));
-
- m_output->append(tr("\n*** QSurfaceFormat from context ***"));
- printFormat(context->format());
-
- m_output->append(tr("\n*** QSurfaceFormat from window surface ***"));
- printFormat(m_surface->format());
-
- m_output->append(tr("\n*** Qt build information ***"));
- const char *gltype[] = { "Desktop", "GLES 2", "GLES 1" };
- m_output->append(tr("Qt OpenGL configuration: %1")
- .arg(QString::fromLatin1(gltype[QOpenGLContext::openGLModuleType()])));
-#if defined(Q_OS_WIN)
- using namespace QNativeInterface;
- m_output->append(tr("Qt OpenGL library handle: %1")
- .arg(QString::number(qintptr(QWGLContext::openGLModuleHandle()), 16)));
-#endif
-
- QList<QByteArray> extensionList = context->extensions().values();
- std::sort(extensionList.begin(), extensionList.end());
- m_extensions->append(tr("Found %1 extensions:").arg(extensionList.count()));
- for (const QByteArray &ext : std::as_const(extensionList))
- m_extensions->append(QString::fromLatin1(ext));
-
- m_output->moveCursor(QTextCursor::Start);
- m_extensions->moveCursor(QTextCursor::Start);
-}
-
-void Widget::renderWindowError(const QString &msg)
-{
- m_output->append(tr("An error has occurred:\n%1").arg(msg));
-}
diff --git a/examples/opengl/contextinfo/widget.h b/examples/opengl/contextinfo/widget.h
deleted file mode 100644
index e5c31fcaa7..0000000000
--- a/examples/opengl/contextinfo/widget.h
+++ /dev/null
@@ -1,46 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef WIDGET_H
-#define WIDGET_H
-
-#include <QWidget>
-
-QT_FORWARD_DECLARE_CLASS(QComboBox)
-QT_FORWARD_DECLARE_CLASS(QTextEdit)
-QT_FORWARD_DECLARE_CLASS(QVBoxLayout)
-QT_FORWARD_DECLARE_CLASS(QSurfaceFormat)
-QT_FORWARD_DECLARE_CLASS(QSurface)
-
-class Widget : public QWidget
-{
- Q_OBJECT
-
-public:
- explicit Widget(QWidget *parent = nullptr);
-
-private slots:
- void start();
- void renderWindowReady();
- void renderWindowError(const QString &msg);
-
-private:
- void addVersions(QLayout *layout);
- void addProfiles(QLayout *layout);
- void addOptions(QLayout *layout);
- void addRenderableTypes(QLayout *layout);
- void addRenderWindow();
- void printFormat(const QSurfaceFormat &format);
-
- QComboBox *m_version;
- QLayout *m_profiles;
- QLayout *m_options;
- QLayout *m_renderables;
- QTextEdit *m_output;
- QTextEdit *m_extensions;
- QVBoxLayout *m_renderWindowLayout;
- QWidget *m_renderWindowContainer;
- QSurface *m_surface;
-};
-
-#endif // WIDGET_H
diff --git a/examples/opengl/cube/CMakeLists.txt b/examples/opengl/cube/CMakeLists.txt
index 0446a5d52e..7486d058a4 100644
--- a/examples/opengl/cube/CMakeLists.txt
+++ b/examples/opengl/cube/CMakeLists.txt
@@ -1,15 +1,9 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(cube LANGUAGES CXX)
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/cube")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets)
qt_standard_project_setup()
@@ -58,7 +52,14 @@ qt6_add_resources(cube "textures"
)
install(TARGETS cube
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET cube
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/opengl/cube/cube.png b/examples/opengl/cube/cube.png
index 42c8c51b3a..c3abbfe84d 100644
--- a/examples/opengl/cube/cube.png
+++ b/examples/opengl/cube/cube.png
Binary files differ
diff --git a/examples/opengl/cube/fshader.glsl b/examples/opengl/cube/fshader.glsl
index 18068cf0e6..0c056b6165 100644
--- a/examples/opengl/cube/fshader.glsl
+++ b/examples/opengl/cube/fshader.glsl
@@ -1,3 +1,5 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
diff --git a/examples/opengl/cube/mainwidget.cpp b/examples/opengl/cube/mainwidget.cpp
index 485bca54f4..e67c93da5a 100644
--- a/examples/opengl/cube/mainwidget.cpp
+++ b/examples/opengl/cube/mainwidget.cpp
@@ -72,14 +72,6 @@ void MainWidget::initializeGL()
initShaders();
initTextures();
-//! [2]
- // Enable depth buffer
- glEnable(GL_DEPTH_TEST);
-
- // Enable back face culling
- glEnable(GL_CULL_FACE);
-//! [2]
-
geometries = new GeometryEngine;
// Use QBasicTimer because its faster than QTimer
@@ -147,7 +139,16 @@ void MainWidget::paintGL()
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+//! [2]
+ // Enable depth buffer
+ glEnable(GL_DEPTH_TEST);
+
+ // Enable back face culling
+ glEnable(GL_CULL_FACE);
+//! [2]
+
texture->bind();
+ program.bind();
//! [6]
// Calculate model view transformation
diff --git a/examples/opengl/cube/vshader.glsl b/examples/opengl/cube/vshader.glsl
index cfdc061852..ac57d96285 100644
--- a/examples/opengl/cube/vshader.glsl
+++ b/examples/opengl/cube/vshader.glsl
@@ -1,3 +1,5 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
diff --git a/examples/opengl/doc/src/2dpainting.qdoc b/examples/opengl/doc/src/2dpainting.qdoc
index 934097f38e..5196c78937 100644
--- a/examples/opengl/doc/src/2dpainting.qdoc
+++ b/examples/opengl/doc/src/2dpainting.qdoc
@@ -5,6 +5,7 @@
\example 2dpainting
\title 2D Painting Example
\ingroup examples-widgets-opengl
+ \examplecategory {Graphics}
\brief The 2D Painting example shows how QPainter and QOpenGLWidget can be
used together to display accelerated 2D graphics on supported hardware.
diff --git a/examples/opengl/doc/src/cube.qdoc b/examples/opengl/doc/src/cube.qdoc
index e0a1870606..d01985701c 100644
--- a/examples/opengl/doc/src/cube.qdoc
+++ b/examples/opengl/doc/src/cube.qdoc
@@ -5,11 +5,14 @@
\example cube
\ingroup examples-widgets-opengl
\title Cube OpenGL ES 2.0 example
+ \examplecategory {3D}
- \brief The Cube OpenGL ES 2.0 example shows how to write mouse rotatable
- textured 3D cube using OpenGL ES 2.0 with Qt. It shows how to handle
- polygon geometries efficiently and how to write simple vertex and
- fragment shader for programmable graphics pipeline. In addition it
+ \brief Shows how to manually rotate a textured 3D cube with user input.
+
+ The Cube OpenGL ES 2.0 example shows how to manually rotate a textured 3D
+ cube with user input, using OpenGL ES 2.0 with Qt. It shows how to
+ handle polygon geometries efficiently and how to write a simple vertex and
+ fragment shader for a programmable graphics pipeline. In addition it
shows how to use quaternions for representing 3D object orientation.
This example has been written for OpenGL ES 2.0 but it works also on
diff --git a/examples/opengl/doc/src/hellogl2.qdoc b/examples/opengl/doc/src/hellogl2.qdoc
index 3a81b0dfad..29e8ef85cd 100644
--- a/examples/opengl/doc/src/hellogl2.qdoc
+++ b/examples/opengl/doc/src/hellogl2.qdoc
@@ -5,6 +5,7 @@
\example hellogl2
\title Hello GL2 Example
\ingroup examples-widgets-opengl
+ \examplecategory {3D}
\brief The Hello GL2 example demonstrates the basic use of the OpenGL-related classes
provided with Qt.
diff --git a/examples/opengl/doc/src/hellogles3.qdoc b/examples/opengl/doc/src/hellogles3.qdoc
index e76536d9d1..d85f40a29f 100644
--- a/examples/opengl/doc/src/hellogles3.qdoc
+++ b/examples/opengl/doc/src/hellogles3.qdoc
@@ -5,24 +5,145 @@
\example hellogles3
\title Hello GLES3 Example
\ingroup examples-widgets-opengl
+ \examplecategory {Graphics}
+ \examplecategory {Mobile}
- \brief The Hello GLES3 example demonstrates easy, cross-platform usage of
- OpenGL ES 3.0 functions via QOpenGLExtraFunctions in an application that
+ \brief Demonstrates OpenGL ES 3.0 functions via QOpenGLExtraFunctions.
+
+ \image hellogles3-example.png
+ \section1 Overview
+
+ This example demonstrates easy, cross-platform usage of OpenGL ES 3.0
+ functions via QOpenGLExtraFunctions in an application that
works identically on desktop platforms with OpenGL 3.3 and mobile/embedded
devices with OpenGL ES 3.0.
- The code is always the same, with the exception of two places:
+ This example has no QWidget dependencies, it uses QOpenGLWindow, a
+ convenience subclass of QWindow that allows easy implementation of windows
+ that contain OpenGL-rendered content. In this sense it complements the
+ \l{OpenGL Window Example}, which shows the implementation of an OpenGL-based
+ QWindow without using the convenience subclass.
+
+ The Qt logo shape implementation is included from the \l{Hello GL2 Example}.
+
+ In other aspects pertaining to using OpenGL there are the following
+ differences.
+
\list
\li The OpenGL context creation has to have a sufficiently high version
number for the features that are in use.
- \li The shader code's version directive is different.
+ \li The shader's version directive is different.
\endlist
- This example has no QWidget dependencies. Instead, it uses QOpenGLWindow, a
- convenience subclass of QWindow that allows easy implementation of windows
- that contain OpenGL-rendered content. In this sense it complements the
- \l{OpenGL Window Example}, which shows the implementation of an OpenGL-based
- QWindow without using the convenience subclass.
+ \section1 Setting up in main.cpp
- \image hellogles3-example.png
+ Here we instantiate our QGuiApplication, QSurfaceformat and set its
+ \l{QSurfaceFormat::depthBufferSize()}{depth buffer size}:
+
+ \quotefromfile hellogles3/main.cpp
+ \skipto int main(int argc, char *argv[])
+ \printuntil fmt.setDepthBufferSize(24);
+
+ We request an OpenGL 3.3 core or OpenGL ES 3.0 context, depending on
+ QOpenGLContext::openGLModuleType():
+
+ \skipto if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
+ \printuntil QSurfaceFormat::setDefaultFormat(fmt);
+
+ We set the default surface format and instantiate our GLWindow \c glWindow.
+
+ \section1 Implementing GLWindow
+
+ This class delivers the features of the example application.
+
+ To start, \c GLWindow is declared by implementing a subclass of
+ QOpenGLWindow:
+
+ \quotefromfile hellogles3/glwindow.h
+ \skipto class GLWindow : public QOpenGLWindow
+ \printto {
+
+ The following properties are declared using Q_PROPERTY:
+
+ \printto public:
+
+ The following public functions are declared:
+
+ \printto private slots:
+
+ The following private objects are declared:
+
+ \printto };
+
+ On the implementation side, those functions that are not declared inline are
+ implemented (or re-implemented) in \c{glwindow.cpp}. The following selections
+ will cover implementation particulars pertaining to the use of OpenGL ES 3.0.
+
+ \section2 Animations
+
+ The following code pertains to the animations, and won't be explored here:
+
+ \quotefromfile hellogles3/glwindow.cpp
+ \skipto GLWindow::GLWindow()
+ \printto static const char *vertexShaderSource =
+
+ For more information see the documentation for \l QPropertyAnimation,
+ \l QSequentialAnimationGroup.
+
+ \section2 Shaders
+ The shaders are defined like so:
+
+ \printto QByteArray versionedShaderCode(const char *src)
+
+ \note These are OpenGL version agnostic. We take this and append
+ the version like so:
+
+ \printto void GLWindow::initializeGL()
+
+ \section2 Initializing OpenGL
+
+ Initializing the shader program in handled by \c initializeGL():
+
+ \printuntil m_program = new QOpenGLShaderProgram;
+ \skipto m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, versionedShaderCode(vertexShaderSource));
+
+ Now the OpenGL version is prepended and the various matrices and light
+ position is set:
+
+ \printuntil m_lightPosLoc = m_program->uniformLocation("lightPos");
+
+ While not strictly required for ES 3, a vertex array object is created.
+
+ \skipto delete m_vao;
+ \printuntil f->glEnable(GL_CULL_FACE);
+
+ \section2 Resizing the window
+
+ The perspective needs to be aligned with the new window size as so:
+
+ \skipto void GLWindow::resizeGL(int w, int h)
+ \printto void GLWindow::paintGL()
+
+ \section2 Painting
+
+ We use QOpenGLExtraFunctions instead of QOpenGLFunctions as we want to
+ do more than what GL(ES) 2.0 offers:
+
+ \printuntil QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions();
+
+ We clear the screen and buffers and bind our shader program and texture:
+
+ \printuntil m_texture->bind();
+
+ Logic for handling an initial \c paintGL() call or a call after a
+ \c resizeGL() call is implemented like so:
+
+ \printuntil }
+
+ Last, we demonstrate a function introduced in OpenGL 3.1 or OpenGL ES 3.0:
+
+ \skipto f->glDrawArraysInstanced(GL_TRIANGLES, 0, m_logo.vertexCount(), 32 * 36);
+ \printuntil }
+
+ This works because we earlier requested 3.3 or 3.0 context.
*/
diff --git a/examples/opengl/doc/src/openglwindow.qdoc b/examples/opengl/doc/src/openglwindow.qdoc
index 505ff20fbc..cade2f009a 100644
--- a/examples/opengl/doc/src/openglwindow.qdoc
+++ b/examples/opengl/doc/src/openglwindow.qdoc
@@ -4,6 +4,8 @@
/*!
\example openglwindow
\title OpenGL Window Example
+ \ingroup examples-widgets-opengl
+ \examplecategory {Graphics}
\brief This example shows how to create a minimal QWindow based application
for the purpose of using OpenGL.
diff --git a/examples/opengl/doc/src/stereoqopenglwidget.qdoc b/examples/opengl/doc/src/stereoqopenglwidget.qdoc
index c4e51f9cee..0cb45b9808 100644
--- a/examples/opengl/doc/src/stereoqopenglwidget.qdoc
+++ b/examples/opengl/doc/src/stereoqopenglwidget.qdoc
@@ -4,9 +4,17 @@
/*!
\example stereoqopenglwidget
\title QOpenGLWidget Stereoscopic Rendering Example
+ \examplecategory {Graphics}
+ \ingroup examples-widgets-opengl
+ \brief This example shows how to create a minimal QOpenGLWidget based
+ application with stereoscopic rendering support.
- \brief This example shows how to create a minimal QOpenGLWidget based application
- with stereoscopic rendering support.
+ \note Support for stereoscopic rendering has certain hardware requirements,
+ such as, a graphics card with stereo support, 3D glasses and specific
+ monitors.
+
+ \note This example renders two images to two separate buffers. When you
+ view the images through 3D glasses, they give a 3D holographic effect.
\image stereoexample-leftbuffer.png
@@ -16,9 +24,6 @@
The above image is what will be rendered to the right buffer.
- \note Support for stereoscopic rendering has certain hardware requirements, like
- your graphics card needs stereo support.
-
\section1 Setting the correct surface flag
To enable stereoscopic rendering you need to set the flag
QSurfaceFormat::StereoBuffers globally. Just doing it on the widget is not enough
diff --git a/examples/opengl/hellogl2/CMakeLists.txt b/examples/opengl/hellogl2/CMakeLists.txt
index 8846fcc23e..74ef4a8c24 100644
--- a/examples/opengl/hellogl2/CMakeLists.txt
+++ b/examples/opengl/hellogl2/CMakeLists.txt
@@ -1,15 +1,9 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(hellogl2 LANGUAGES CXX)
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/hellogl2")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets)
qt_standard_project_setup()
@@ -36,7 +30,14 @@ target_link_libraries(hellogl2 PRIVATE
)
install(TARGETS hellogl2
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET hellogl2
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/opengl/hellogl2/mainwindow.cpp b/examples/opengl/hellogl2/mainwindow.cpp
index 2416fd6057..5ee2b738c2 100644
--- a/examples/opengl/hellogl2/mainwindow.cpp
+++ b/examples/opengl/hellogl2/mainwindow.cpp
@@ -3,19 +3,19 @@
#include "mainwindow.h"
#include "window.h"
+#include <QApplication>
+#include <QKeySequence>
#include <QMenuBar>
#include <QMenu>
#include <QMessageBox>
MainWindow::MainWindow()
{
- QMenuBar *menuBar = new QMenuBar;
- QMenu *menuWindow = menuBar->addMenu(tr("&Window"));
- QAction *addNew = new QAction(menuWindow);
- addNew->setText(tr("Add new"));
- menuWindow->addAction(addNew);
- connect(addNew, &QAction::triggered, this, &MainWindow::onAddNew);
- setMenuBar(menuBar);
+ QMenu *menuWindow = menuBar()->addMenu(tr("&Window"));
+ menuWindow->addAction(tr("Add new"), QKeySequence(Qt::CTRL | Qt::Key_N),
+ this, &MainWindow::onAddNew);
+ menuWindow->addAction(tr("Quit"), QKeySequence(Qt::CTRL | Qt::Key_Q),
+ qApp, QApplication::closeAllWindows);
onAddNew();
}
@@ -23,8 +23,8 @@ MainWindow::MainWindow()
void MainWindow::onAddNew()
{
if (!centralWidget())
- setCentralWidget(new Window(this));
+ setCentralWidget(new Window);
else
- QMessageBox::information(nullptr, tr("Cannot add new window"),
+ QMessageBox::information(this, tr("Cannot Add New Window"),
tr("Already occupied. Undock first."));
}
diff --git a/examples/opengl/hellogl2/window.cpp b/examples/opengl/hellogl2/window.cpp
index 055dd5b5e3..5a7ddf826f 100644
--- a/examples/opengl/hellogl2/window.cpp
+++ b/examples/opengl/hellogl2/window.cpp
@@ -1,9 +1,8 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include "glwidget.h"
#include "window.h"
-#include "mainwindow.h"
+#include "glwidget.h"
#include <QSlider>
#include <QVBoxLayout>
#include <QHBoxLayout>
@@ -11,9 +10,18 @@
#include <QPushButton>
#include <QApplication>
#include <QMessageBox>
+#include <QMainWindow>
-Window::Window(MainWindow *mw)
- : mainWindow(mw)
+static QMainWindow *findMainWindow()
+{
+ for (auto *w : QApplication::topLevelWidgets()) {
+ if (auto *mw = qobject_cast<QMainWindow *>(w))
+ return mw;
+ }
+ return nullptr;
+}
+
+Window::Window()
{
glWidget = new GLWidget;
@@ -28,22 +36,19 @@ Window::Window(MainWindow *mw)
connect(zSlider, &QSlider::valueChanged, glWidget, &GLWidget::setZRotation);
connect(glWidget, &GLWidget::zRotationChanged, zSlider, &QSlider::setValue);
- QVBoxLayout *mainLayout = new QVBoxLayout;
- QHBoxLayout *container = new QHBoxLayout;
+ QVBoxLayout *mainLayout = new QVBoxLayout(this);
+ QWidget *w = new QWidget;
+ QHBoxLayout *container = new QHBoxLayout(w);
container->addWidget(glWidget);
container->addWidget(xSlider);
container->addWidget(ySlider);
container->addWidget(zSlider);
- QWidget *w = new QWidget;
- w->setLayout(container);
mainLayout->addWidget(w);
dockBtn = new QPushButton(tr("Undock"), this);
connect(dockBtn, &QPushButton::clicked, this, &Window::dockUndock);
mainLayout->addWidget(dockBtn);
- setLayout(mainLayout);
-
xSlider->setValue(15 * 16);
ySlider->setValue(345 * 16);
zSlider->setValue(0 * 16);
@@ -64,7 +69,7 @@ QSlider *Window::createSlider()
void Window::keyPressEvent(QKeyEvent *e)
{
- if (e->key() == Qt::Key_Escape)
+ if (isWindow() && e->key() == Qt::Key_Escape)
close();
else
QWidget::keyPressEvent(e);
@@ -72,26 +77,37 @@ void Window::keyPressEvent(QKeyEvent *e)
void Window::dockUndock()
{
- if (parent()) {
- setParent(nullptr);
- setAttribute(Qt::WA_DeleteOnClose);
- move(QGuiApplication::primaryScreen()->size().width() / 2 - width() / 2,
- QGuiApplication::primaryScreen()->size().height() / 2 - height() / 2);
- dockBtn->setText(tr("Dock"));
- show();
- } else {
- if (!mainWindow->centralWidget()) {
- if (mainWindow->isVisible()) {
- setAttribute(Qt::WA_DeleteOnClose, false);
- dockBtn->setText(tr("Undock"));
- mainWindow->setCentralWidget(this);
- } else {
- QMessageBox::information(nullptr, tr("Cannot dock"),
- tr("Main window already closed"));
- }
- } else {
- QMessageBox::information(nullptr, tr("Cannot dock"),
- tr("Main window already occupied"));
- }
+ if (parent())
+ undock();
+ else
+ dock();
+}
+
+void Window::dock()
+{
+ auto *mainWindow = findMainWindow();
+ if (mainWindow == nullptr || !mainWindow->isVisible()) {
+ QMessageBox::information(this, tr("Cannot Dock"),
+ tr("Main window already closed"));
+ return;
}
+ if (mainWindow->centralWidget()) {
+ QMessageBox::information(this, tr("Cannot Dock"),
+ tr("Main window already occupied"));
+ return;
+ }
+ setAttribute(Qt::WA_DeleteOnClose, false);
+ dockBtn->setText(tr("Undock"));
+ mainWindow->setCentralWidget(this);
+}
+
+void Window::undock()
+{
+ setParent(nullptr);
+ setAttribute(Qt::WA_DeleteOnClose);
+ const auto geometry = screen()->availableGeometry();
+ move(geometry.x() + (geometry.width() - width()) / 2,
+ geometry.y() + (geometry.height() - height()) / 2);
+ dockBtn->setText(tr("Dock"));
+ show();
}
diff --git a/examples/opengl/hellogl2/window.h b/examples/opengl/hellogl2/window.h
index f9e0607903..edbd643ae2 100644
--- a/examples/opengl/hellogl2/window.h
+++ b/examples/opengl/hellogl2/window.h
@@ -6,20 +6,17 @@
#include <QWidget>
-QT_BEGIN_NAMESPACE
-class QSlider;
-class QPushButton;
-QT_END_NAMESPACE
+QT_FORWARD_DECLARE_CLASS(QSlider)
+QT_FORWARD_DECLARE_CLASS(QPushButton)
class GLWidget;
-class MainWindow;
class Window : public QWidget
{
Q_OBJECT
public:
- Window(MainWindow *mw);
+ Window();
protected:
void keyPressEvent(QKeyEvent *event) override;
@@ -28,6 +25,8 @@ private slots:
void dockUndock();
private:
+ void dock();
+ void undock();
QSlider *createSlider();
GLWidget *glWidget;
@@ -35,7 +34,6 @@ private:
QSlider *ySlider;
QSlider *zSlider;
QPushButton *dockBtn;
- MainWindow *mainWindow;
};
#endif
diff --git a/examples/opengl/hellogles3/CMakeLists.txt b/examples/opengl/hellogles3/CMakeLists.txt
index 917f64568c..07df173a47 100644
--- a/examples/opengl/hellogles3/CMakeLists.txt
+++ b/examples/opengl/hellogles3/CMakeLists.txt
@@ -1,15 +1,9 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(hellogles3 LANGUAGES CXX)
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/hellogles3")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL)
qt_standard_project_setup()
@@ -44,7 +38,14 @@ qt_add_resources(hellogles3 "hellogles3"
)
install(TARGETS hellogles3
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET hellogles3
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/opengl/hellogles3/glwindow.h b/examples/opengl/hellogles3/glwindow.h
index fc2a0e487e..54bc531af8 100644
--- a/examples/opengl/hellogles3/glwindow.h
+++ b/examples/opengl/hellogles3/glwindow.h
@@ -9,14 +9,10 @@
#include <QVector3D>
#include "../hellogl2/logo.h"
-QT_BEGIN_NAMESPACE
-
-class QOpenGLTexture;
-class QOpenGLShaderProgram;
-class QOpenGLBuffer;
-class QOpenGLVertexArrayObject;
-
-QT_END_NAMESPACE
+QT_FORWARD_DECLARE_CLASS(QOpenGLTexture)
+QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
+QT_FORWARD_DECLARE_CLASS(QOpenGLBuffer)
+QT_FORWARD_DECLARE_CLASS(QOpenGLVertexArrayObject)
class GLWindow : public QOpenGLWindow
{
diff --git a/examples/opengl/opengl.pro b/examples/opengl/opengl.pro
index b0eac5ee9c..6ee57c7018 100644
--- a/examples/opengl/opengl.pro
+++ b/examples/opengl/opengl.pro
@@ -4,10 +4,8 @@ SUBDIRS = openglwindow \
hellogles3
qtHaveModule(widgets) {
- SUBDIRS += contextinfo \
- 2dpainting \
+ SUBDIRS += 2dpainting \
hellogl2 \
- qopenglwidget \
cube \
textures \
stereoqopenglwidget
diff --git a/examples/opengl/openglwindow/CMakeLists.txt b/examples/opengl/openglwindow/CMakeLists.txt
index 3558ce549d..0258a42822 100644
--- a/examples/opengl/openglwindow/CMakeLists.txt
+++ b/examples/opengl/openglwindow/CMakeLists.txt
@@ -1,15 +1,9 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(openglwindow LANGUAGES CXX)
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/openglwindow")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL)
qt_standard_project_setup()
@@ -31,7 +25,14 @@ target_link_libraries(openglwindow PRIVATE
)
install(TARGETS openglwindow
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET openglwindow
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/opengl/openglwindow/main.cpp b/examples/opengl/openglwindow/main.cpp
index 6813dc9efd..89933fa379 100644
--- a/examples/opengl/openglwindow/main.cpp
+++ b/examples/opengl/openglwindow/main.cpp
@@ -6,10 +6,10 @@
#include <QGuiApplication>
#include <QMatrix4x4>
#include <QOpenGLShaderProgram>
+#include <QOpenGLBuffer>
#include <QScreen>
#include <QtMath>
-
//! [1]
class TriangleWindow : public OpenGLWindow
{
@@ -20,10 +20,8 @@ public:
void render() override;
private:
- GLint m_posAttr = 0;
- GLint m_colAttr = 0;
GLint m_matrixUniform = 0;
-
+ QOpenGLBuffer m_vbo;
QOpenGLShaderProgram *m_program = nullptr;
int m_frame = 0;
};
@@ -48,36 +46,48 @@ int main(int argc, char **argv)
}
//! [2]
-
//! [3]
-static const char *vertexShaderSource =
- "attribute highp vec4 posAttr;\n"
- "attribute lowp vec4 colAttr;\n"
- "varying lowp vec4 col;\n"
- "uniform highp mat4 matrix;\n"
- "void main() {\n"
- " col = colAttr;\n"
- " gl_Position = matrix * posAttr;\n"
- "}\n";
-
-static const char *fragmentShaderSource =
- "varying lowp vec4 col;\n"
- "void main() {\n"
- " gl_FragColor = col;\n"
- "}\n";
+static const char *vertexShaderSource = "attribute highp vec4 posAttr;\n"
+ "attribute lowp vec4 colAttr;\n"
+ "varying lowp vec4 col;\n"
+ "uniform highp mat4 matrix;\n"
+ "void main() {\n"
+ " col = colAttr;\n"
+ " gl_Position = matrix * posAttr;\n"
+ "}\n";
+
+static const char *fragmentShaderSource = "varying lowp vec4 col;\n"
+ "void main() {\n"
+ " gl_FragColor = col;\n"
+ "}\n";
//! [3]
//! [4]
void TriangleWindow::initialize()
{
+ static const GLfloat vertices_colors[] = { +0.0f, +0.707f, 1.0f, 0.0f, 0.0f,
+ -0.5f, -0.500f, 0.0f, 1.0f, 0.0f,
+ +0.5f, -0.500f, 0.0f, 0.0f, 1.0f };
+
+ m_vbo.create();
+ m_vbo.bind();
+ m_vbo.allocate(vertices_colors, sizeof(vertices_colors));
+
+ glEnableVertexAttribArray(0);
+ glEnableVertexAttribArray(1);
+
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), nullptr);
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
+ reinterpret_cast<void *>(2 * sizeof(GLfloat)));
+
m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
+ m_program->bindAttributeLocation("posAttr", 0);
+ m_program->bindAttributeLocation("colAttr", 1);
m_program->link();
- m_posAttr = m_program->attributeLocation("posAttr");
- Q_ASSERT(m_posAttr != -1);
- m_colAttr = m_program->attributeLocation("colAttr");
- Q_ASSERT(m_colAttr != -1);
+ m_program->bind();
+
m_matrixUniform = m_program->uniformLocation("matrix");
Q_ASSERT(m_matrixUniform != -1);
}
@@ -100,28 +110,13 @@ void TriangleWindow::render()
m_program->setUniformValue(m_matrixUniform, matrix);
- static const GLfloat vertices[] = {
- 0.0f, 0.707f,
- -0.5f, -0.5f,
- 0.5f, -0.5f
- };
-
- static const GLfloat colors[] = {
- 1.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 1.0f
- };
-
- glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
- glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
-
- glEnableVertexAttribArray(m_posAttr);
- glEnableVertexAttribArray(m_colAttr);
+ glEnableVertexAttribArray(0);
+ glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 3);
- glDisableVertexAttribArray(m_colAttr);
- glDisableVertexAttribArray(m_posAttr);
+ glDisableVertexAttribArray(0);
+ glDisableVertexAttribArray(1);
m_program->release();
diff --git a/examples/opengl/openglwindow/openglwindow.h b/examples/opengl/openglwindow/openglwindow.h
index 579f9eede4..41bcbab9c4 100644
--- a/examples/opengl/openglwindow/openglwindow.h
+++ b/examples/opengl/openglwindow/openglwindow.h
@@ -7,11 +7,9 @@
#include <QWindow>
#include <QOpenGLFunctions>
-QT_BEGIN_NAMESPACE
-class QPainter;
-class QOpenGLContext;
-class QOpenGLPaintDevice;
-QT_END_NAMESPACE
+QT_FORWARD_DECLARE_CLASS(QPainter)
+QT_FORWARD_DECLARE_CLASS(QOpenGLContext)
+QT_FORWARD_DECLARE_CLASS(QOpenGLPaintDevice)
//! [1]
class OpenGLWindow : public QWindow, protected QOpenGLFunctions
diff --git a/examples/opengl/qopenglwidget/CMakeLists.txt b/examples/opengl/qopenglwidget/CMakeLists.txt
deleted file mode 100644
index 4a0da689cd..0000000000
--- a/examples/opengl/qopenglwidget/CMakeLists.txt
+++ /dev/null
@@ -1,53 +0,0 @@
-# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
-
-cmake_minimum_required(VERSION 3.16)
-project(qopenglwidget LANGUAGES CXX)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/qopenglwidget")
-
-find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets)
-
-qt_standard_project_setup()
-
-qt_add_executable(qopenglwidget
- bubble.cpp bubble.h
- glwidget.cpp glwidget.h
- main.cpp
- mainwindow.cpp mainwindow.h
-)
-
-set_target_properties(qopenglwidget PROPERTIES
- WIN32_EXECUTABLE TRUE
- MACOSX_BUNDLE TRUE
-)
-
-target_link_libraries(qopenglwidget PRIVATE
- Qt6::Core
- Qt6::Gui
- Qt6::OpenGL
- Qt6::OpenGLWidgets
- Qt6::Widgets
-)
-
-# Resources:
-set(texture_resource_files
- "qt.png"
-)
-
-qt6_add_resources(qopenglwidget "texture"
- PREFIX
- "/"
- FILES
- ${texture_resource_files}
-)
-
-install(TARGETS qopenglwidget
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
-)
diff --git a/examples/opengl/qopenglwidget/bubble.cpp b/examples/opengl/qopenglwidget/bubble.cpp
deleted file mode 100644
index 3c32b62113..0000000000
--- a/examples/opengl/qopenglwidget/bubble.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include <QtWidgets>
-
-#include "bubble.h"
-
-Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity)
- : position(position), vel(velocity), radius(radius)
-{
- innerColor = randomColor();
- outerColor = randomColor();
- updateBrush();
-}
-
-//! [0]
-void Bubble::updateCache()
-{
- delete cache;
- cache = new QImage(qRound(radius * 2 + 2), qRound(radius * 2 + 2), QImage::Format_ARGB32_Premultiplied);
- cache->fill(0x00000000);
- QPainter p(cache);
- p.setRenderHint(QPainter::Antialiasing);
- QPen pen(Qt::white);
- pen.setWidth(2);
- p.setPen(pen);
- p.setBrush(brush);
- p.drawEllipse(1, 1, int(2*radius), int(2*radius));
-}
-//! [0]
-
-Bubble::~Bubble()
-{
- delete cache;
-}
-
-void Bubble::updateBrush()
-{
- QRadialGradient gradient(QPointF(radius, radius), radius,
- QPointF(radius*0.5, radius*0.5));
-
- gradient.setColorAt(0, QColor(255, 255, 255, 255));
- gradient.setColorAt(0.25, innerColor);
- gradient.setColorAt(1, outerColor);
- brush = QBrush(gradient);
- updateCache();
-}
-
-//! [1]
-void Bubble::drawBubble(QPainter *painter)
-{
- painter->save();
- painter->translate(position.x() - radius, position.y() - radius);
- painter->setOpacity(0.8);
- painter->drawImage(0, 0, *cache);
- painter->restore();
-}
-//! [1]
-
-QColor Bubble::randomColor()
-{
- int red = int(185 + QRandomGenerator::global()->bounded(70));
- int green = int(185 + QRandomGenerator::global()->bounded(70));
- int blue = int(205 + QRandomGenerator::global()->bounded(50));
- int alpha = int(91 + QRandomGenerator::global()->bounded(100));
-
- return QColor(red, green, blue, alpha);
-}
-
-void Bubble::move(const QRect &bbox)
-{
- position += vel;
- qreal leftOverflow = position.x() - radius - bbox.left();
- qreal rightOverflow = position.x() + radius - bbox.right();
- qreal topOverflow = position.y() - radius - bbox.top();
- qreal bottomOverflow = position.y() + radius - bbox.bottom();
-
- if (leftOverflow < 0.0) {
- position.setX(position.x() - 2 * leftOverflow);
- vel.setX(-vel.x());
- } else if (rightOverflow > 0.0) {
- position.setX(position.x() - 2 * rightOverflow);
- vel.setX(-vel.x());
- }
-
- if (topOverflow < 0.0) {
- position.setY(position.y() - 2 * topOverflow);
- vel.setY(-vel.y());
- } else if (bottomOverflow > 0.0) {
- position.setY(position.y() - 2 * bottomOverflow);
- vel.setY(-vel.y());
- }
-}
-
-QRectF Bubble::rect()
-{
- return QRectF(position.x() - radius, position.y() - radius,
- 2 * radius, 2 * radius);
-}
diff --git a/examples/opengl/qopenglwidget/bubble.h b/examples/opengl/qopenglwidget/bubble.h
deleted file mode 100644
index 47355ead41..0000000000
--- a/examples/opengl/qopenglwidget/bubble.h
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef BUBBLE_H
-#define BUBBLE_H
-
-#include <QBrush>
-#include <QColor>
-#include <QPointF>
-#include <QRect>
-#include <QRectF>
-
-QT_FORWARD_DECLARE_CLASS(QPainter)
-
-class Bubble
-{
-public:
- Bubble(const QPointF &position, qreal radius, const QPointF &velocity);
- ~Bubble();
-
- void drawBubble(QPainter *painter);
- void updateBrush();
- void move(const QRect &bbox);
- void updateCache();
- QRectF rect();
-
-private:
- QColor randomColor();
-
- QBrush brush;
- QPointF position;
- QPointF vel;
- qreal radius;
- QColor innerColor;
- QColor outerColor;
- QImage *cache = nullptr;
-};
-
-#endif
diff --git a/examples/opengl/qopenglwidget/glwidget.cpp b/examples/opengl/qopenglwidget/glwidget.cpp
deleted file mode 100644
index e2a3ea4fc5..0000000000
--- a/examples/opengl/qopenglwidget/glwidget.cpp
+++ /dev/null
@@ -1,540 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "glwidget.h"
-#include <QPainter>
-#include <QPaintEngine>
-#include <QOpenGLShaderProgram>
-#include <QOpenGLTexture>
-#include <QRandomGenerator>
-#include <QCoreApplication>
-#include <qmath.h>
-
-#include "mainwindow.h"
-#include "bubble.h"
-
-const int bubbleNum = 8;
-
-#ifndef GL_SRGB8_ALPHA8
-#define GL_SRGB8_ALPHA8 0x8C43
-#endif
-
-GLWidget::GLWidget(MainWindow *maybeMainWindow, const QColor &background)
- : m_mainWindow(maybeMainWindow),
- m_background(background)
-{
- setMinimumSize(300, 250);
- if (QCoreApplication::arguments().contains(QStringLiteral("--srgb")))
- setTextureFormat(GL_SRGB8_ALPHA8);
-}
-
-GLWidget::~GLWidget()
-{
- reset();
-}
-
-void GLWidget::reset()
-{
- qDeleteAll(m_bubbles);
- // Leave everything in a state suitable for a subsequent call to
- // initialize(). This matters when coming from the context's
- // aboutToBeDestroyed signal, would not matter when invoked from the
- // destructor.
- m_bubbles.clear();
-
- // And now release all OpenGL resources.
- makeCurrent();
- delete m_texture;
- m_texture = nullptr;
- delete m_program1;
- m_program1 = nullptr;
- delete m_program2;
- m_program2 = nullptr;
- delete m_vshader1;
- m_vshader1 = nullptr;
- delete m_fshader1;
- m_fshader1 = nullptr;
- delete m_vshader2;
- m_vshader2 = nullptr;
- delete m_fshader2;
- m_fshader2 = nullptr;
- m_vbo1.destroy();
- m_vbo2.destroy();
- doneCurrent();
-
- // We are done with the current QOpenGLContext, forget it. If there is a
- // subsequent initialize(), that will then connect to the new context.
- QObject::disconnect(m_contextWatchConnection);
-}
-
-void GLWidget::setScaling(int scale)
-{
- if (scale > 30)
- m_fScale = 1 + qreal(scale - 30) / 30 * 0.25;
- else if (scale < 30)
- m_fScale = 1 - (qreal(30 - scale) / 30 * 0.25);
- else
- m_fScale = 1;
-}
-
-void GLWidget::setLogo()
-{
- m_qtLogo = true;
-}
-
-void GLWidget::setTexture()
-{
- m_qtLogo = false;
-}
-
-void GLWidget::setShowBubbles(bool bubbles)
-{
- m_showBubbles = bubbles;
-}
-
-void GLWidget::paintQtLogo()
-{
- m_program1->enableAttributeArray(m_vertexAttr1);
- m_program1->enableAttributeArray(m_normalAttr1);
-
- m_vbo1.bind();
- // The data in the buffer is placed like this:
- // vertex1.x, vertex1.y, vertex1.z, normal1.x, normal1.y, normal1.z, vertex2.x, ...
- m_program1->setAttributeBuffer(m_vertexAttr1, GL_FLOAT, 0, 3, 6 * sizeof(GLfloat));
- m_program1->setAttributeBuffer(m_normalAttr1, GL_FLOAT, 3 * sizeof(GLfloat), 3, 6 * sizeof(GLfloat));
- m_vbo1.release();
-
- glDrawArrays(GL_TRIANGLES, 0, m_vertices.size());
-
- m_program1->disableAttributeArray(m_normalAttr1);
- m_program1->disableAttributeArray(m_vertexAttr1);
-}
-
-void GLWidget::paintTexturedCube()
-{
- m_texture->bind();
-
- if (!m_vbo2.isCreated()) {
- static GLfloat afVertices[] = {
- -0.5, 0.5, 0.5, 0.5,-0.5,0.5,-0.5,-0.5,0.5,
- 0.5, -0.5, 0.5, -0.5,0.5,0.5,0.5,0.5,0.5,
- -0.5, -0.5, -0.5, 0.5,-0.5,-0.5,-0.5,0.5,-0.5,
- 0.5, 0.5, -0.5, -0.5,0.5,-0.5,0.5,-0.5,-0.5,
-
- 0.5, -0.5, -0.5, 0.5,-0.5,0.5,0.5,0.5,-0.5,
- 0.5, 0.5, 0.5, 0.5,0.5,-0.5,0.5,-0.5,0.5,
- -0.5, 0.5, -0.5, -0.5,-0.5,0.5,-0.5,-0.5,-0.5,
- -0.5, -0.5, 0.5, -0.5,0.5,-0.5,-0.5,0.5,0.5,
-
- 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5,
- -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5,
- -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5,
- 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5
- };
-
- static GLfloat afTexCoord[] = {
- 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f,
- 1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f,
- 1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
- 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f,
-
- 1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
- 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f,
- 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f,
- 1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f,
-
- 0.0f,1.0f, 1.0f,0.0f, 1.0f,1.0f,
- 1.0f,0.0f, 0.0f,1.0f, 0.0f,0.0f,
- 1.0f,0.0f, 1.0f,1.0f, 0.0f,0.0f,
- 0.0f,1.0f, 0.0f,0.0f, 1.0f,1.0f
- };
-
- GLfloat afNormals[] = {
-
- 0,0,-1, 0,0,-1, 0,0,-1,
- 0,0,-1, 0,0,-1, 0,0,-1,
- 0,0,1, 0,0,1, 0,0,1,
- 0,0,1, 0,0,1, 0,0,1,
-
- -1,0,0, -1,0,0, -1,0,0,
- -1,0,0, -1,0,0, -1,0,0,
- 1,0,0, 1,0,0, 1,0,0,
- 1,0,0, 1,0,0, 1,0,0,
-
- 0,-1,0, 0,-1,0, 0,-1,0,
- 0,-1,0, 0,-1,0, 0,-1,0,
- 0,1,0, 0,1,0, 0,1,0,
- 0,1,0, 0,1,0, 0,1,0
- };
-
- m_vbo2.create();
- m_vbo2.bind();
- m_vbo2.allocate(36 * 8 * sizeof(GLfloat));
- m_vbo2.write(0, afVertices, sizeof(afVertices));
- m_vbo2.write(sizeof(afVertices), afTexCoord, sizeof(afTexCoord));
- m_vbo2.write(sizeof(afVertices) + sizeof(afTexCoord), afNormals, sizeof(afNormals));
- m_vbo2.release();
- }
-
- m_program2->setUniformValue(m_textureUniform2, 0); // use texture unit 0
-
- m_program2->enableAttributeArray(m_vertexAttr2);
- m_program2->enableAttributeArray(m_normalAttr2);
- m_program2->enableAttributeArray(m_texCoordAttr2);
-
- m_vbo2.bind();
- // In the buffer we first have 36 vertices (3 floats for each), then 36 texture
- // coordinates (2 floats for each), then 36 normals (3 floats for each).
- m_program2->setAttributeBuffer(m_vertexAttr2, GL_FLOAT, 0, 3);
- m_program2->setAttributeBuffer(m_texCoordAttr2, GL_FLOAT, 36 * 3 * sizeof(GLfloat), 2);
- m_program2->setAttributeBuffer(m_normalAttr2, GL_FLOAT, 36 * 5 * sizeof(GLfloat), 3);
- m_vbo2.release();
-
- glDrawArrays(GL_TRIANGLES, 0, 36);
-
- m_program2->disableAttributeArray(m_vertexAttr2);
- m_program2->disableAttributeArray(m_normalAttr2);
- m_program2->disableAttributeArray(m_texCoordAttr2);
-}
-
-void GLWidget::initializeGL()
-{
- initializeOpenGLFunctions();
-
- m_texture = new QOpenGLTexture(QImage(":/qt.png"));
-
- m_vshader1 = new QOpenGLShader(QOpenGLShader::Vertex);
- const char *vsrc1 =
- "attribute highp vec4 vertex;\n"
- "attribute mediump vec3 normal;\n"
- "uniform mediump mat4 matrix;\n"
- "varying mediump vec4 color;\n"
- "void main(void)\n"
- "{\n"
- " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
- " float angle = max(dot(normal, toLight), 0.0);\n"
- " vec3 col = vec3(0.40, 1.0, 0.0);\n"
- " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n"
- " color = clamp(color, 0.0, 1.0);\n"
- " gl_Position = matrix * vertex;\n"
- "}\n";
- m_vshader1->compileSourceCode(vsrc1);
-
- m_fshader1 = new QOpenGLShader(QOpenGLShader::Fragment);
- const char *fsrc1 =
- "varying mediump vec4 color;\n"
- "void main(void)\n"
- "{\n"
- " gl_FragColor = color;\n"
- "}\n";
- m_fshader1->compileSourceCode(fsrc1);
-
- m_program1 = new QOpenGLShaderProgram;
- m_program1->addShader(m_vshader1);
- m_program1->addShader(m_fshader1);
- m_program1->link();
-
- m_vertexAttr1 = m_program1->attributeLocation("vertex");
- m_normalAttr1 = m_program1->attributeLocation("normal");
- m_matrixUniform1 = m_program1->uniformLocation("matrix");
-
- m_vshader2 = new QOpenGLShader(QOpenGLShader::Vertex);
- const char *vsrc2 =
- "attribute highp vec4 vertex;\n"
- "attribute highp vec4 texCoord;\n"
- "attribute mediump vec3 normal;\n"
- "uniform mediump mat4 matrix;\n"
- "varying highp vec4 texc;\n"
- "varying mediump float angle;\n"
- "void main(void)\n"
- "{\n"
- " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
- " angle = max(dot(normal, toLight), 0.0);\n"
- " gl_Position = matrix * vertex;\n"
- " texc = texCoord;\n"
- "}\n";
- m_vshader2->compileSourceCode(vsrc2);
-
- m_fshader2 = new QOpenGLShader(QOpenGLShader::Fragment);
- const char *fsrc2 =
- "varying highp vec4 texc;\n"
- "uniform sampler2D tex;\n"
- "varying mediump float angle;\n"
- "void main(void)\n"
- "{\n"
- " highp vec3 color = texture2D(tex, texc.st).rgb;\n"
- " color = color * 0.2 + color * 0.8 * angle;\n"
- " gl_FragColor = vec4(clamp(color, 0.0, 1.0), 1.0);\n"
- "}\n";
- m_fshader2->compileSourceCode(fsrc2);
-
- m_program2 = new QOpenGLShaderProgram;
- m_program2->addShader(m_vshader2);
- m_program2->addShader(m_fshader2);
- m_program2->link();
-
- m_vertexAttr2 = m_program2->attributeLocation("vertex");
- m_normalAttr2 = m_program2->attributeLocation("normal");
- m_texCoordAttr2 = m_program2->attributeLocation("texCoord");
- m_matrixUniform2 = m_program2->uniformLocation("matrix");
- m_textureUniform2 = m_program2->uniformLocation("tex");
-
- m_fAngle = 0;
- m_fScale = 1;
-
- createGeometry();
-
- // Use a vertex buffer object. Client-side pointers are old-school and should be avoided.
- m_vbo1.create();
- m_vbo1.bind();
- // For the cube all the data belonging to the texture coordinates and
- // normals is placed separately, after the vertices. Here, for the Qt logo,
- // let's do something different and potentially more efficient: create a
- // properly interleaved data set.
- const int vertexCount = m_vertices.count();
- QList<GLfloat> buf;
- buf.resize(vertexCount * 3 * 2);
- GLfloat *p = buf.data();
- for (int i = 0; i < vertexCount; ++i) {
- *p++ = m_vertices[i].x();
- *p++ = m_vertices[i].y();
- *p++ = m_vertices[i].z();
- *p++ = m_normals[i].x();
- *p++ = m_normals[i].y();
- *p++ = m_normals[i].z();
- }
- m_vbo1.allocate(buf.constData(), buf.count() * sizeof(GLfloat));
- m_vbo1.release();
-
- createBubbles(bubbleNum - m_bubbles.count());
-
- // A well-behaved QOpenGLWidget releases OpenGL resources not only upon
- // destruction, but also when the associated OpenGL context disappears. If
- // the widget continues to exist, the context's destruction will be
- // followed by a call to initialize(). This is not strictly mandatory in
- // widgets that never change their parents.
- m_contextWatchConnection = QObject::connect(context(), &QOpenGLContext::aboutToBeDestroyed, context(), [this] { reset(); });
-}
-
-void GLWidget::paintGL()
-{
- createBubbles(bubbleNum - m_bubbles.count());
-
- QPainter painter;
- painter.begin(this);
-
- painter.beginNativePainting();
-
- glClearColor(m_background.redF(), m_background.greenF(), m_background.blueF(), m_transparent ? 0.0f : 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- glFrontFace(GL_CW);
- glCullFace(GL_FRONT);
- glEnable(GL_CULL_FACE);
- glEnable(GL_DEPTH_TEST);
-
- QMatrix4x4 modelview;
- modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f);
- modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f);
- modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f);
- modelview.scale(m_fScale);
- modelview.translate(0.0f, -0.2f, 0.0f);
-
- if (m_qtLogo) {
- m_program1->bind();
- m_program1->setUniformValue(m_matrixUniform1, modelview);
- paintQtLogo();
- m_program1->release();
- } else {
- m_program2->bind();
- m_program2->setUniformValue(m_matrixUniform2, modelview);
- paintTexturedCube();
- m_program2->release();
- }
-
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_CULL_FACE);
-
- painter.endNativePainting();
-
- if (m_showBubbles) {
- for (Bubble *bubble : std::as_const(m_bubbles))
- bubble->drawBubble(&painter);
- }
-
- if (const int elapsed = m_time.elapsed()) {
- QString framesPerSecond;
- framesPerSecond.setNum(m_frames /(elapsed / 1000.0), 'f', 2);
- painter.setPen(m_transparent ? Qt::black : Qt::white);
- painter.drawText(20, 40, framesPerSecond + " paintGL calls / s");
- }
-
- painter.end();
-
- for (Bubble *bubble : std::as_const(m_bubbles))
- bubble->move(rect());
-
- if (!(m_frames % 100)) {
- m_time.start();
- m_frames = 0;
- }
- m_fAngle += 1.0f;
- ++m_frames;
-
- // When requested, follow the ideal way to animate: Rely on
- // blocking swap and just schedule updates continuously.
- if (!m_mainWindow || !m_mainWindow->timerEnabled())
- update();
-}
-
-void GLWidget::createBubbles(int number)
-{
- for (int i = 0; i < number; ++i) {
- QPointF position(width()*(0.1 + QRandomGenerator::global()->bounded(0.8)),
- height()*(0.1 + QRandomGenerator::global()->bounded(0.8)));
- qreal radius = qMin(width(), height())*(0.0175 + QRandomGenerator::global()->bounded(0.0875));
- QPointF velocity(width()*0.0175*(-0.5 + QRandomGenerator::global()->bounded(1.0)),
- height()*0.0175*(-0.5 + QRandomGenerator::global()->bounded(1.0)));
-
- m_bubbles.append(new Bubble(position, radius, velocity));
- }
-}
-
-void GLWidget::createGeometry()
-{
- m_vertices.clear();
- m_normals.clear();
-
- qreal x1 = +0.06f;
- qreal y1 = -0.14f;
- qreal x2 = +0.14f;
- qreal y2 = -0.06f;
- qreal x3 = +0.08f;
- qreal y3 = +0.00f;
- qreal x4 = +0.30f;
- qreal y4 = +0.22f;
-
- quad(x1, y1, x2, y2, y2, x2, y1, x1);
- quad(x3, y3, x4, y4, y4, x4, y3, x3);
-
- extrude(x1, y1, x2, y2);
- extrude(x2, y2, y2, x2);
- extrude(y2, x2, y1, x1);
- extrude(y1, x1, x1, y1);
- extrude(x3, y3, x4, y4);
- extrude(x4, y4, y4, x4);
- extrude(y4, x4, y3, x3);
-
- const int NumSectors = 100;
- const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors;
-
- for (int i = 0; i < NumSectors; ++i) {
- qreal angle = i * sectorAngle;
- qreal x5 = 0.30 * sin(angle);
- qreal y5 = 0.30 * cos(angle);
- qreal x6 = 0.20 * sin(angle);
- qreal y6 = 0.20 * cos(angle);
-
- angle += sectorAngle;
- qreal x7 = 0.20 * sin(angle);
- qreal y7 = 0.20 * cos(angle);
- qreal x8 = 0.30 * sin(angle);
- qreal y8 = 0.30 * cos(angle);
-
- quad(x5, y5, x6, y6, x7, y7, x8, y8);
-
- extrude(x6, y6, x7, y7);
- extrude(x8, y8, x5, y5);
- }
-
- for (int i = 0;i < m_vertices.size();i++)
- m_vertices[i] *= 2.0f;
-}
-
-void GLWidget::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
-{
- m_vertices << QVector3D(x1, y1, -0.05f);
- m_vertices << QVector3D(x2, y2, -0.05f);
- m_vertices << QVector3D(x4, y4, -0.05f);
-
- m_vertices << QVector3D(x3, y3, -0.05f);
- m_vertices << QVector3D(x4, y4, -0.05f);
- m_vertices << QVector3D(x2, y2, -0.05f);
-
- QVector3D n = QVector3D::normal
- (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));
-
- m_normals << n;
- m_normals << n;
- m_normals << n;
-
- m_normals << n;
- m_normals << n;
- m_normals << n;
-
- m_vertices << QVector3D(x4, y4, 0.05f);
- m_vertices << QVector3D(x2, y2, 0.05f);
- m_vertices << QVector3D(x1, y1, 0.05f);
-
- m_vertices << QVector3D(x2, y2, 0.05f);
- m_vertices << QVector3D(x4, y4, 0.05f);
- m_vertices << QVector3D(x3, y3, 0.05f);
-
- n = QVector3D::normal
- (QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));
-
- m_normals << n;
- m_normals << n;
- m_normals << n;
-
- m_normals << n;
- m_normals << n;
- m_normals << n;
-}
-
-void GLWidget::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
-{
- m_vertices << QVector3D(x1, y1, +0.05f);
- m_vertices << QVector3D(x2, y2, +0.05f);
- m_vertices << QVector3D(x1, y1, -0.05f);
-
- m_vertices << QVector3D(x2, y2, -0.05f);
- m_vertices << QVector3D(x1, y1, -0.05f);
- m_vertices << QVector3D(x2, y2, +0.05f);
-
- QVector3D n = QVector3D::normal
- (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f));
-
- m_normals << n;
- m_normals << n;
- m_normals << n;
-
- m_normals << n;
- m_normals << n;
- m_normals << n;
-}
-
-void GLWidget::setTransparent(bool transparent)
-{
- setAttribute(Qt::WA_AlwaysStackOnTop, transparent);
- m_transparent = transparent;
- // Call update() on the top-level window after toggling AlwayStackOnTop to make sure
- // the entire backingstore is updated accordingly.
- window()->update();
-}
-
-void GLWidget::resizeGL(int, int)
-{
- if (m_mainWindow) {
- if (!m_btn) {
- m_btn = new QPushButton("\nAdd widget\n", this);
- connect(m_btn, &QPushButton::clicked, this, [this] { m_mainWindow->addNew(); });
- }
- m_btn->move(20, 80);
- if (!m_btn2) {
- m_btn2 = new QPushButton("\nI prefer tabbed widgets\n", this);
- connect(m_btn2, &QPushButton::clicked, this, [this] { m_mainWindow->showNewWindow(); });
- }
- m_btn2->move(20, 160);
- }
-}
diff --git a/examples/opengl/qopenglwidget/glwidget.h b/examples/opengl/qopenglwidget/glwidget.h
deleted file mode 100644
index de9d11b94c..0000000000
--- a/examples/opengl/qopenglwidget/glwidget.h
+++ /dev/null
@@ -1,85 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef GLWIDGET_H
-#define GLWIDGET_H
-
-#include <QOpenGLWidget>
-#include <QOpenGLFunctions>
-#include <QOpenGLBuffer>
-#include <QVector3D>
-#include <QMatrix4x4>
-#include <QElapsedTimer>
-#include <QList>
-#include <QPushButton>
-
-class Bubble;
-class MainWindow;
-
-QT_FORWARD_DECLARE_CLASS(QOpenGLTexture)
-QT_FORWARD_DECLARE_CLASS(QOpenGLShader)
-QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
-
-class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
-{
- Q_OBJECT
-public:
- GLWidget(MainWindow *maybeMainWindow, const QColor &background);
- ~GLWidget();
-
-public slots:
- void setScaling(int scale);
- void setLogo();
- void setTexture();
- void setShowBubbles(bool);
- void setTransparent(bool transparent);
-
-protected:
- void resizeGL(int w, int h) override;
- void paintGL() override;
- void initializeGL() override;
-
-private:
- void paintTexturedCube();
- void paintQtLogo();
- void createGeometry();
- void createBubbles(int number);
- void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4);
- void extrude(qreal x1, qreal y1, qreal x2, qreal y2);
- void reset();
-
- MainWindow *m_mainWindow;
- qreal m_fAngle = 0;
- qreal m_fScale = 1;
- bool m_showBubbles = true;
- QList<QVector3D> m_vertices;
- QList<QVector3D> m_normals;
- bool m_qtLogo = true;
- QList<Bubble *> m_bubbles;
- int m_frames = 0;
- QElapsedTimer m_time;
- QOpenGLShader *m_vshader1 = nullptr;
- QOpenGLShader *m_fshader1 = nullptr;
- QOpenGLShader *m_vshader2 = nullptr;
- QOpenGLShader *m_fshader2 = nullptr;
- QOpenGLShaderProgram *m_program1 = nullptr;
- QOpenGLShaderProgram *m_program2 = nullptr;
- QOpenGLTexture *m_texture = nullptr;
- QOpenGLBuffer m_vbo1;
- QOpenGLBuffer m_vbo2;
- int m_vertexAttr1 = 0;
- int m_normalAttr1 = 0;
- int m_matrixUniform1 = 0;
- int m_vertexAttr2 = 0;
- int m_normalAttr2 = 0;
- int m_texCoordAttr2 = 0;
- int m_matrixUniform2 = 0;
- int m_textureUniform2 = 0;
- bool m_transparent = false;
- QPushButton *m_btn = nullptr;
- QPushButton *m_btn2 = nullptr;
- QColor m_background;
- QMetaObject::Connection m_contextWatchConnection;
-};
-
-#endif
diff --git a/examples/opengl/qopenglwidget/main.cpp b/examples/opengl/qopenglwidget/main.cpp
deleted file mode 100644
index 04f1c57f08..0000000000
--- a/examples/opengl/qopenglwidget/main.cpp
+++ /dev/null
@@ -1,43 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include <QApplication>
-#include <QMainWindow>
-#include <QColorSpace>
-#include <QSurfaceFormat>
-#include <QCommandLineParser>
-#include <QCommandLineOption>
-#include "mainwindow.h"
-
-int main( int argc, char ** argv )
-{
- Q_INIT_RESOURCE(texture);
- QApplication a( argc, argv );
-
- QCoreApplication::setApplicationName("Qt QOpenGLWidget Example");
- QCoreApplication::setOrganizationName("QtProject");
- QCoreApplication::setApplicationVersion(QT_VERSION_STR);
- QCommandLineParser parser;
- parser.setApplicationDescription(QCoreApplication::applicationName());
- parser.addHelpOption();
- parser.addVersionOption();
- QCommandLineOption multipleSampleOption("multisample", "Multisampling");
- parser.addOption(multipleSampleOption);
- QCommandLineOption srgbOption("srgb", "Use sRGB Color Space");
- parser.addOption(srgbOption);
- parser.process(a);
-
- QSurfaceFormat format;
- format.setDepthBufferSize(24);
- format.setStencilBufferSize(8);
- if (parser.isSet(srgbOption))
- format.setColorSpace(QColorSpace::SRgb);
- if (parser.isSet(multipleSampleOption))
- format.setSamples(4);
- QSurfaceFormat::setDefaultFormat(format);
-
- MainWindow mw;
- mw.resize(1280, 720);
- mw.show();
- return a.exec();
-}
diff --git a/examples/opengl/qopenglwidget/mainwindow.cpp b/examples/opengl/qopenglwidget/mainwindow.cpp
deleted file mode 100644
index c383a692d5..0000000000
--- a/examples/opengl/qopenglwidget/mainwindow.cpp
+++ /dev/null
@@ -1,188 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "mainwindow.h"
-
-#include <QApplication>
-#include <QMenuBar>
-#include <QGroupBox>
-#include <QSlider>
-#include <QLabel>
-#include <QCheckBox>
-#include <QRandomGenerator>
-#include <QSpinBox>
-#include <QScrollArea>
-#include <QTabWidget>
-#include <QTabBar>
-#include <QToolButton>
-
-#include "glwidget.h"
-
-MainWindow::MainWindow()
- : m_nextX(1), m_nextY(1)
-{
- GLWidget *glwidget = new GLWidget(this, qRgb(20, 20, 50));
- m_glWidgets << glwidget;
- QLabel *label = new QLabel(this);
- m_timer = new QTimer(this);
- QSlider *slider = new QSlider(this);
- slider->setOrientation(Qt::Horizontal);
-
- QLabel *updateLabel = new QLabel("Update interval");
- QSpinBox *updateInterval = new QSpinBox(this);
- updateInterval->setSuffix(" ms");
- updateInterval->setValue(10);
- updateInterval->setToolTip("Interval for the timer that calls update().\n"
- "Note that on most systems the swap will block to wait for vsync\n"
- "and therefore an interval < 16 ms will likely lead to a 60 FPS update rate.");
- QGroupBox *updateGroupBox = new QGroupBox(this);
- QCheckBox *timerBased = new QCheckBox("Use timer", this);
- timerBased->setChecked(false);
- timerBased->setToolTip("Toggles using a timer to trigger update().\n"
- "When not set, each paintGL() schedules the next update immediately,\n"
- "expecting the blocking swap to throttle the thread.\n"
- "This shows how unnecessary the timer is in most cases.");
- QCheckBox *transparent = new QCheckBox("Transparent background", this);
- transparent->setToolTip("Toggles Qt::WA_AlwaysStackOnTop and transparent clear color for glClear().\n"
- "Note how the button on top stacks incorrectly when enabling this.");
- QHBoxLayout *updateLayout = new QHBoxLayout;
- updateLayout->addWidget(updateLabel);
- updateLayout->addWidget(updateInterval);
- updateLayout->addWidget(timerBased);
- updateLayout->addWidget(transparent);
- updateGroupBox->setLayout(updateLayout);
-
- slider->setRange(0, 50);
- slider->setSliderPosition(30);
- m_timer->setInterval(10);
- label->setText("A scrollable QOpenGLWidget");
- label->setAlignment(Qt::AlignHCenter);
-
- QGroupBox * groupBox = new QGroupBox(this);
- setCentralWidget(groupBox);
- groupBox->setTitle("QOpenGLWidget Example");
-
- m_layout = new QGridLayout(groupBox);
-
- QScrollArea *scrollArea = new QScrollArea;
- scrollArea->setWidget(glwidget);
-
- m_layout->addWidget(scrollArea,1,0,8,1);
- m_layout->addWidget(label,9,0,1,1);
- m_layout->addWidget(updateGroupBox, 10, 0, 1, 1);
- m_layout->addWidget(slider, 11,0,1,1);
-
- groupBox->setLayout(m_layout);
-
-
- QMenu *fileMenu = menuBar()->addMenu("&File");
- fileMenu->addAction("E&xit", this, &QWidget::close);
- QMenu *showMenu = menuBar()->addMenu("&Show");
- showMenu->addAction("Show 3D Logo", glwidget, &GLWidget::setLogo);
- showMenu->addAction("Show 2D Texture", glwidget, &GLWidget::setTexture);
- QAction *showBubbles = showMenu->addAction("Show bubbles", glwidget, &GLWidget::setShowBubbles);
- showBubbles->setCheckable(true);
- showBubbles->setChecked(true);
- showMenu->addAction("Open tab window", this, &MainWindow::showNewWindow);
- QMenu *helpMenu = menuBar()->addMenu("&Help");
- helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt);
-
- connect(m_timer, &QTimer::timeout, glwidget, QOverload<>::of(&QWidget::update));
-
- connect(slider, &QAbstractSlider::valueChanged, glwidget, &GLWidget::setScaling);
- connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent);
- connect(updateInterval, &QSpinBox::valueChanged,
- this, &MainWindow::updateIntervalChanged);
- connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged);
- connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled);
-
- if (timerBased->isChecked())
- m_timer->start();
- else
- updateInterval->setEnabled(false);
-}
-
-void MainWindow::updateIntervalChanged(int value)
-{
- m_timer->setInterval(value);
- if (m_timer->isActive())
- m_timer->start();
-}
-
-void MainWindow::addNew()
-{
- if (m_nextY == 4)
- return;
- GLWidget *w = new GLWidget(nullptr, qRgb(QRandomGenerator::global()->bounded(256),
- QRandomGenerator::global()->bounded(256),
- QRandomGenerator::global()->bounded(256)));
- m_glWidgets << w;
- connect(m_timer, &QTimer::timeout, w, QOverload<>::of(&QWidget::update));
- m_layout->addWidget(w, m_nextY, m_nextX, 1, 1);
- if (m_nextX == 3) {
- m_nextX = 1;
- ++m_nextY;
- } else {
- ++m_nextX;
- }
-}
-
-void MainWindow::timerUsageChanged(bool enabled)
-{
- if (enabled) {
- m_timer->start();
- } else {
- m_timer->stop();
- for (QOpenGLWidget *w : std::as_const(m_glWidgets))
- w->update();
- }
-}
-
-void MainWindow::resizeEvent(QResizeEvent *)
-{
- m_glWidgets[0]->setMinimumSize(size() + QSize(128, 128));
-}
-
-void MainWindow::showNewWindow()
-{
- QTabWidget *tabs = new QTabWidget;
- tabs->resize(800, 600);
-
- QToolButton *tb = new QToolButton;
- tb->setText(QLatin1String("+"));
- tabs->addTab(new QLabel(QLatin1String("Add OpenGL widgets with +")), QString());
- tabs->setTabEnabled(0, false);
- tabs->tabBar()->setTabButton(0, QTabBar::RightSide, tb);
- tabs->tabBar()->setTabsClosable(true);
- QObject::connect(tabs->tabBar(), &QTabBar::tabCloseRequested, tabs, [tabs](int index) {
- tabs->widget(index)->deleteLater();
- });
-
- const QString msgToTopLevel = QLatin1String("Break out to top-level window");
- const QString msgFromTopLevel = QLatin1String("Move back under tab widget");
-
- QObject::connect(tb, &QAbstractButton::clicked, tabs, [=] {
- GLWidget *glwidget = new GLWidget(nullptr, Qt::blue);
- glwidget->resize(tabs->size());
- glwidget->setWindowTitle(QString::asprintf("QOpenGLWidget %p", glwidget));
-
- QPushButton *btn = new QPushButton(msgToTopLevel, glwidget);
- connect(btn, &QPushButton::clicked, glwidget, [=] {
- if (glwidget->parent()) {
- glwidget->setAttribute(Qt::WA_DeleteOnClose, true);
- glwidget->setParent(nullptr);
- glwidget->show();
- btn->setText(msgFromTopLevel);
- } else {
- glwidget->setAttribute(Qt::WA_DeleteOnClose, false);
- tabs->addTab(glwidget, glwidget->windowTitle());
- btn->setText(msgToTopLevel);
- }
- });
-
- tabs->setCurrentIndex(tabs->addTab(glwidget, glwidget->windowTitle()));
- });
-
- tabs->setAttribute(Qt::WA_DeleteOnClose);
- tabs->show();
-}
diff --git a/examples/opengl/qopenglwidget/mainwindow.h b/examples/opengl/qopenglwidget/mainwindow.h
deleted file mode 100644
index 85f11ad764..0000000000
--- a/examples/opengl/qopenglwidget/mainwindow.h
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef MAINWINDOW_H
-#define MAINWINDOW_H
-
-#include <QMainWindow>
-#include <QTimer>
-#include <QGridLayout>
-
-QT_FORWARD_DECLARE_CLASS(QOpenGLWidget)
-
-class MainWindow : public QMainWindow
-{
- Q_OBJECT
-
-public:
- MainWindow();
- void addNew();
- bool timerEnabled() const { return m_timer->isActive(); }
-
- void resizeEvent(QResizeEvent *);
-
-public slots:
- void showNewWindow();
-
-private slots:
- void updateIntervalChanged(int value);
- void timerUsageChanged(bool enabled);
-
-private:
- QTimer *m_timer;
- QGridLayout *m_layout;
- int m_nextX;
- int m_nextY;
- QList<QOpenGLWidget *> m_glWidgets;
-};
-
-#endif
diff --git a/examples/opengl/qopenglwidget/qopenglwidget.pro b/examples/opengl/qopenglwidget/qopenglwidget.pro
deleted file mode 100644
index 7ac546d7ce..0000000000
--- a/examples/opengl/qopenglwidget/qopenglwidget.pro
+++ /dev/null
@@ -1,15 +0,0 @@
-QT += widgets opengl openglwidgets
-
-SOURCES += main.cpp \
- glwidget.cpp \
- mainwindow.cpp \
- bubble.cpp
-
-HEADERS += glwidget.h \
- mainwindow.h \
- bubble.h
-
-RESOURCES += texture.qrc
-
-target.path = $$[QT_INSTALL_EXAMPLES]/opengl/qopenglwidget
-INSTALLS += target
diff --git a/examples/opengl/qopenglwidget/qt.png b/examples/opengl/qopenglwidget/qt.png
deleted file mode 100644
index 4f68e162de..0000000000
--- a/examples/opengl/qopenglwidget/qt.png
+++ /dev/null
Binary files differ
diff --git a/examples/opengl/qopenglwidget/texture.qrc b/examples/opengl/qopenglwidget/texture.qrc
deleted file mode 100644
index ff1d0e535f..0000000000
--- a/examples/opengl/qopenglwidget/texture.qrc
+++ /dev/null
@@ -1,5 +0,0 @@
-<!DOCTYPE RCC><RCC version="1.0">
-<qresource>
- <file>qt.png</file>
-</qresource>
-</RCC>
diff --git a/examples/opengl/stereoqopenglwidget/CMakeLists.txt b/examples/opengl/stereoqopenglwidget/CMakeLists.txt
index 69fd304bb2..d0fb7a0345 100644
--- a/examples/opengl/stereoqopenglwidget/CMakeLists.txt
+++ b/examples/opengl/stereoqopenglwidget/CMakeLists.txt
@@ -1,17 +1,11 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(stereoqopenglwidget LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/stereoqopenglwidget")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets)
qt_add_executable(stereoqopenglwidget
@@ -26,16 +20,23 @@ set_target_properties(stereoqopenglwidget PROPERTIES
)
target_link_libraries(stereoqopenglwidget PUBLIC
- Qt::Core
- Qt::Gui
- Qt::OpenGL
- Qt::OpenGLWidgets
- Qt::Widgets
+ Qt6::Core
+ Qt6::Gui
+ Qt6::OpenGL
+ Qt6::OpenGLWidgets
+ Qt6::Widgets
)
install(TARGETS stereoqopenglwidget
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET stereoqopenglwidget
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/opengl/textures/CMakeLists.txt b/examples/opengl/textures/CMakeLists.txt
index 5103782381..2765355c5a 100644
--- a/examples/opengl/textures/CMakeLists.txt
+++ b/examples/opengl/textures/CMakeLists.txt
@@ -1,15 +1,9 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(textures LANGUAGES CXX)
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/textures")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets)
qt_standard_project_setup()
@@ -51,7 +45,14 @@ qt6_add_resources(textures "textures"
)
install(TARGETS textures
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET textures
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/opengl/textures/glwidget.cpp b/examples/opengl/textures/glwidget.cpp
index 9c6828b902..cca08f4c91 100644
--- a/examples/opengl/textures/glwidget.cpp
+++ b/examples/opengl/textures/glwidget.cpp
@@ -46,9 +46,6 @@ void GLWidget::initializeGL()
makeObject();
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
-
#define PROGRAM_VERTEX_ATTRIBUTE 0
#define PROGRAM_TEXCOORD_ATTRIBUTE 1
@@ -91,6 +88,9 @@ void GLWidget::paintGL()
glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), clearColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_CULL_FACE);
+
QMatrix4x4 m;
m.ortho(-0.5f, +0.5f, +0.5f, -0.5f, 4.0f, 15.0f);
m.translate(0.0f, 0.0f, -10.0f);
@@ -98,6 +98,8 @@ void GLWidget::paintGL()
m.rotate(yRot / 16.0f, 0.0f, 1.0f, 0.0f);
m.rotate(zRot / 16.0f, 0.0f, 0.0f, 1.0f);
+ vbo.bind();
+ program->bind();
program->setUniformValue("matrix", m);
program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
diff --git a/examples/opengl/textures/main.cpp b/examples/opengl/textures/main.cpp
index c1e91e6257..7e5f0646cb 100644
--- a/examples/opengl/textures/main.cpp
+++ b/examples/opengl/textures/main.cpp
@@ -8,8 +8,6 @@
int main(int argc, char *argv[])
{
- Q_INIT_RESOURCE(textures);
-
QApplication app(argc, argv);
QSurfaceFormat format;