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>
This commit is contained in:
Laszlo Agocs 2023-04-20 13:10:08 +02:00
parent 11209cfde6
commit 5ebb9a8bf3
2 changed files with 13 additions and 4 deletions

View File

@ -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);

View File

@ -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().