Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickopenglutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QOpenGLContext>
8#include <QOpenGLFunctions>
9#include <QOpenGLFramebufferObject>
10#include <private/qopenglvertexarrayobject_p.h>
11
13
14/*!
15 \namespace QQuickOpenGLUtils
16 \inmodule QtQuick
17 \since 6.0
18
19 \brief The QQuickOpenGLUtils namespace contains utilities for Qt
20 Quick when used with an OpenGL backend.
21*/
22
23/*!
24 Call this function to reset the current OpenGL context its default state.
25
26 The scene graph uses the OpenGL context and will both rely on and
27 clobber its state. When mixing raw OpenGL commands with scene
28 graph rendering, this function provides a convenient way of
29 resetting the OpenGL context state back to its default values.
30
31 This function does not touch state in the fixed-function pipeline.
32
33 \warning This function will only reset the OpenGL context in
34 relation to what may be changed internally as part of the OpenGL
35 scene graph. It does not reset anything that has been changed
36 externally such as direct OpenGL calls done inside the application
37 code if those same calls are not used internally (for example,
38 various OpenGL 3.x or 4.x specific state).
39
40 \since 6.0
41*/
42void QQuickOpenGLUtils::resetOpenGLState()
43{
44 QOpenGLContext *ctx = QOpenGLContext::currentContext();
45 if (!ctx)
46 return;
47
48 QOpenGLFunctions *gl = ctx->functions();
49
50 gl->glBindBuffer(GL_ARRAY_BUFFER, 0);
51 gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
52
53 QOpenGLVertexArrayObjectHelper *vaoHelper = QOpenGLVertexArrayObjectHelper::vertexArrayObjectHelperForContext(ctx);
54 if (vaoHelper->isValid())
55 vaoHelper->glBindVertexArray(0);
56
57 if (ctx->isOpenGLES() || (gl->openGLFeatures() & QOpenGLFunctions::FixedFunctionPipeline)) {
58 int maxAttribs;
59 gl->glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxAttribs);
60 for (int i=0; i<maxAttribs; ++i) {
61 gl->glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
62 gl->glDisableVertexAttribArray(i);
63 }
64 }
65
66 gl->glActiveTexture(GL_TEXTURE0);
67 gl->glBindTexture(GL_TEXTURE_2D, 0);
68
69 gl->glDisable(GL_DEPTH_TEST);
70 gl->glDisable(GL_STENCIL_TEST);
71 gl->glDisable(GL_SCISSOR_TEST);
72
73 gl->glColorMask(true, true, true, true);
74 gl->glClearColor(0, 0, 0, 0);
75
76 gl->glDepthMask(true);
77 gl->glDepthFunc(GL_LESS);
78 gl->glClearDepthf(1);
79
80 gl->glStencilMask(0xff);
81 gl->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
82 gl->glStencilFunc(GL_ALWAYS, 0, 0xff);
83
84 gl->glDisable(GL_BLEND);
85 gl->glBlendFunc(GL_ONE, GL_ZERO);
86
87 gl->glUseProgram(0);
88
89 QOpenGLFramebufferObject::bindDefault();
90}
91
92QT_END_NAMESPACE
Q_QUICK_EXPORT void resetOpenGLState()
Call this function to reset the current OpenGL context its default state.