summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@qt.io>2023-04-20 13:10:08 +0200
committerLaszlo Agocs <laszlo.agocs@qt.io>2023-04-25 18:10:44 +0200
commit5ebb9a8bf357b435a9d9cf56ae018f552c96d03c (patch)
treeb789f1ab456c25a79314e3f1e03f70d541f47cbb
parent11209cfde68e9b8b8722859c690e57c7d6e8a338 (diff)
Tune textures example to work with wasm and update docs
Old examples inherited from Qt 4 tend to set some state, such as enabling the depth test or culling, in initializeGL(). Newer examples tend not to do this; they rather set the necessary state in paintGL(). This mattered little (or not at all) in the past, but with WebAssembly and WebGL there are limitations in the GL context management in the wasm platform plugin. Under certain conditions, esp. when QOffscreenSurface is involved, it looks like the same native context gets reused, which means there is a chance of unexpected changes to the current state between calls to initializeGL() and paintGL(). (and also between paintGL() calls) See QWasmOpenGLContext for details. Update the textures example the same way we did for the cube one. Add a note to the QOpenGLWidget docs about this problem. Task-number: QTBUG-111304 Pick-to: 6.5 6.4 Change-Id: I29d2b2cdeb07bcecc5dc915d79c12b4323ca9ab3 Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io> Reviewed-by: Inho Lee <inho.lee@qt.io>
-rw-r--r--examples/opengl/textures/glwidget.cpp8
-rw-r--r--src/openglwidgets/qopenglwidget.cpp9
2 files changed, 13 insertions, 4 deletions
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/src/openglwidgets/qopenglwidget.cpp b/src/openglwidgets/qopenglwidget.cpp
index 19230aeedd..25c4dcf902 100644
--- a/src/openglwidgets/qopenglwidget.cpp
+++ b/src/openglwidgets/qopenglwidget.cpp
@@ -1391,7 +1391,7 @@ GLuint QOpenGLWidget::defaultFramebufferObject(TargetBuffer targetBuffer) const
This virtual function is called once before the first call to
paintGL() or resizeGL(). Reimplement it in a subclass.
- This function should set up any required OpenGL resources and state.
+ This function should set up any required OpenGL resources.
There is no need to call makeCurrent() because this has already been
done when this function is called. Note however that the framebuffer
@@ -1433,6 +1433,13 @@ void QOpenGLWidget::resizeGL(int w, int h)
other state is set and no clearing or drawing is performed by the
framework.
+ \note To ensure portability, do not expect that state set in initializeGL()
+ persists. Rather, set all necessary state, for example, by calling
+ glEnable(), in paintGL(). This is because some platforms, such as WebAssembly
+ with WebGL, may have limitations on OpenGL contexts in some situations, which
+ can lead to using the context used with the QOpenGLWidget for other purposes
+ as well.
+
When \l QSurfaceFormat::StereoBuffers is enabled, this function
will be called twice - once for each buffer. Query what buffer is
currently bound by calling currentTargetBuffer().