From ef5b4a98911b6f43acf15d06cbbf84df53f749e9 Mon Sep 17 00:00:00 2001 From: Paul Wicking Date: Tue, 25 Aug 2020 16:32:24 +0200 Subject: Doc: Compile OpenGL snippets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix minor issues (e.g. whitespace, missing semi-colon) in passing. Change-Id: I022ac0382ae1068a75246964c37532e7e15417b0 Done-with: Nico Vertriest Task-number: QTBUG-81486 Reviewed-by: Topi Reiniƶ --- src/gui/doc/snippets/code/code.pro | 5 +- .../snippets/code/src_gui_opengl_qopenglbuffer.cpp | 16 ++- .../snippets/code/src_gui_opengl_qopengldebug.cpp | 89 +++++++++++----- .../code/src_gui_opengl_qopenglfunctions.cpp | 117 ++++++++++++--------- 4 files changed, 146 insertions(+), 81 deletions(-) (limited to 'src/gui/doc') diff --git a/src/gui/doc/snippets/code/code.pro b/src/gui/doc/snippets/code/code.pro index 2e5e58f7ee..a5498f38a3 100644 --- a/src/gui/doc/snippets/code/code.pro +++ b/src/gui/doc/snippets/code/code.pro @@ -21,4 +21,7 @@ SOURCES = \ src_gui_kernel_qguiapplication_x11.cpp \ src_gui_kernel_qkeysequence.cpp \ src_gui_kernel_qshortcutmap.cpp \ - src_gui_math3d_qquaternion.cpp + src_gui_math3d_qquaternion.cpp \ + src_gui_opengl_qopenglbuffer.cpp \ + src_gui_opengl_qopengldebug.cpp \ + src_gui_opengl_qopenglfunctions.cpp diff --git a/src/gui/doc/snippets/code/src_gui_opengl_qopenglbuffer.cpp b/src/gui/doc/snippets/code/src_gui_opengl_qopenglbuffer.cpp index 57dc909598..bd768b1ed7 100644 --- a/src/gui/doc/snippets/code/src_gui_opengl_qopenglbuffer.cpp +++ b/src/gui/doc/snippets/code/src_gui_opengl_qopenglbuffer.cpp @@ -47,14 +47,22 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include + +namespace src_gui_opengl_qopenglbuffer { +void wrapper() { //! [0] - QOpenGLBuffer buffer1(QOpenGLBuffer::IndexBuffer); - buffer1.create(); +QOpenGLBuffer buffer1(QOpenGLBuffer::IndexBuffer); +buffer1.create(); - QOpenGLBuffer buffer2 = buffer1; +QOpenGLBuffer buffer2 = buffer1; //! [0] + //! [1] - QOpenGLBuffer::release(QOpenGLBuffer::VertexBuffer); +QOpenGLBuffer::release(QOpenGLBuffer::VertexBuffer); //! [1] + +} // wrapper +} // src_gui_opengl_qopenglbuffer diff --git a/src/gui/doc/snippets/code/src_gui_opengl_qopengldebug.cpp b/src/gui/doc/snippets/code/src_gui_opengl_qopengldebug.cpp index e82447a174..241450c23a 100644 --- a/src/gui/doc/snippets/code/src_gui_opengl_qopengldebug.cpp +++ b/src/gui/doc/snippets/code/src_gui_opengl_qopengldebug.cpp @@ -47,54 +47,87 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include +#include +#include +#include + +namespace src_gui_opengl_qopengldebug { +struct LogHandler : public QObject +{ + Q_OBJECT +public slots: + static bool handleLoggedMessage() { return true; }; +}; +struct SnippetWrapper : public QObject +{ + Q_OBJECT + void wrapper1(LogHandler *receiver); + const QWidget *receiver; +}; + +void wrapper0() { //! [0] - GLenum error = GL_NO_ERROR; - do { - error = glGetError(); - if (error != GL_NO_ERROR) - // handle the error - } while (error != GL_NO_ERROR); +GLenum error = GL_NO_ERROR; +do { + error = glGetError(); + if (error != GL_NO_ERROR) { + // handle the error + } +} while (error != GL_NO_ERROR); //! [0] + //! [1] - QSurfaceFormat format; - // asks for a OpenGL 3.2 debug context using the Core profile - format.setMajorVersion(3); - format.setMinorVersion(2); - format.setProfile(QSurfaceFormat::CoreProfile); - format.setOption(QSurfaceFormat::DebugContext); - - QOpenGLContext *context = new QOpenGLContext; - context->setFormat(format); - context->create(); +QSurfaceFormat format; +// asks for a OpenGL 3.2 debug context using the Core profile +format.setMajorVersion(3); +format.setMinorVersion(2); +format.setProfile(QSurfaceFormat::CoreProfile); +format.setOption(QSurfaceFormat::DebugContext); + +QOpenGLContext *context = new QOpenGLContext; +context->setFormat(format); +context->create(); //! [1] +} // wrapper0 + + +void SnippetWrapper::wrapper1(LogHandler *receiver) { //! [2] - QOpenGLContext *ctx = QOpenGLContext::currentContext(); - QOpenGLDebugLogger *logger = new QOpenGLDebugLogger(this); +QOpenGLContext *ctx = QOpenGLContext::currentContext(); +QOpenGLDebugLogger *logger = new QOpenGLDebugLogger(this); - logger->initialize(); // initializes in the current context, i.e. ctx +logger->initialize(); // initializes in the current context, i.e. ctx //! [2] + //! [3] - ctx->hasExtension(QByteArrayLiteral("GL_KHR_debug")) +ctx->hasExtension(QByteArrayLiteral("GL_KHR_debug")); //! [3] + //! [4] - const QList messages = logger->loggedMessages(); - for (const QOpenGLDebugMessage &message : messages) - qDebug() << message; +const QList messages = logger->loggedMessages(); +for (const QOpenGLDebugMessage &message : messages) + qDebug() << message; //! [4] + //! [5] - connect(logger, &QOpenGLDebugLogger::messageLogged, receiver, &LogHandler::handleLoggedMessage); - logger->startLogging(); +connect(logger, &QOpenGLDebugLogger::messageLogged, receiver, &LogHandler::handleLoggedMessage); +logger->startLogging(); //! [5] + //! [6] - QOpenGLDebugMessage message = - QOpenGLDebugMessage::createApplicationMessage(QStringLiteral("Custom message")); +QOpenGLDebugMessage message = + QOpenGLDebugMessage::createApplicationMessage(QStringLiteral("Custom message")); - logger->logMessage(message); +logger->logMessage(message); //! [6] + +} // SnippetWrapper::wrapper1 +} // src_gui_opengl_qopengldebug diff --git a/src/gui/doc/snippets/code/src_gui_opengl_qopenglfunctions.cpp b/src/gui/doc/snippets/code/src_gui_opengl_qopenglfunctions.cpp index e4255107d3..946e3caef5 100644 --- a/src/gui/doc/snippets/code/src_gui_opengl_qopenglfunctions.cpp +++ b/src/gui/doc/snippets/code/src_gui_opengl_qopenglfunctions.cpp @@ -47,66 +47,87 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include +#include + +#include +#include +#include + +namespace src_gui_opengl_qopenglfunctions { //! [0] - class MyGLWindow : public QWindow, protected QOpenGLFunctions - { - Q_OBJECT - public: - explicit MyGLWindow(QScreen *screen = nullptr); - - protected: - void initializeGL(); - void paintGL(); - - QOpenGLContext *m_context; - }; - - MyGLWindow(QScreen *screen) - : QWindow(screen), QOpenGLWidget(parent) - { - setSurfaceType(OpenGLSurface); - create(); - - // Create an OpenGL context - m_context = new QOpenGLContext; - m_context->create(); - - // Setup scene and render it - initializeGL(); - paintGL(); - } - - void MyGLWindow::initializeGL() - { - m_context->makeCurrent(this); - initializeOpenGLFunctions(); - } +class MyGLWindow : public QWindow, protected QOpenGLFunctions +{ + Q_OBJECT +public: + explicit MyGLWindow(QScreen *screen = nullptr); + +protected: + void initializeGL(); + void paintGL(); + + QOpenGLContext *m_context; +}; + +MyGLWindow::MyGLWindow(QScreen *screen) + : QWindow(screen) +{ + setSurfaceType(OpenGLSurface); + create(); + + // Create an OpenGL context + m_context = new QOpenGLContext; + m_context->create(); + + // Setup scene and render it + initializeGL(); + paintGL(); +}; + +void MyGLWindow::initializeGL() +{ + m_context->makeCurrent(this); + initializeOpenGLFunctions(); +} //! [0] + +int textureId = 0; + //! [1] - void MyGLWindow::paintGL() - { - m_context->makeCurrent(this); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, textureId); - ... - m_context->swapBuffers(this); - m_context->doneCurrent(); - } +void MyGLWindow::paintGL() +{ + m_context->makeCurrent(this); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, textureId); + // ... + m_context->swapBuffers(this); + m_context->doneCurrent(); +} //! [1] + +void wrapper0() { //! [2] - QOpenGLFunctions glFuncs(QOpenGLContext::currentContext()); - glFuncs.glActiveTexture(GL_TEXTURE1); +QOpenGLFunctions glFuncs(QOpenGLContext::currentContext()); +glFuncs.glActiveTexture(GL_TEXTURE1); //! [2] +} // wrapper0 + +void wrapper1() { //! [3] - QOpenGLFunctions *glFuncs = QOpenGLContext::currentContext()->functions(); - glFuncs->glActiveTexture(GL_TEXTURE1); +QOpenGLFunctions *glFuncs = QOpenGLContext::currentContext()->functions(); +glFuncs->glActiveTexture(GL_TEXTURE1); //! [3] + //! [4] - QOpenGLFunctions funcs(QOpenGLContext::currentContext()); - bool npot = funcs.hasOpenGLFeature(QOpenGLFunctions::NPOTTextures); +QOpenGLFunctions funcs(QOpenGLContext::currentContext()); +bool npot = funcs.hasOpenGLFeature(QOpenGLFunctions::NPOTTextures); //! [4] + +Q_UNUSED(npot); +} // wrapper1 +} // src_gui_opengl_qopenglfunctions -- cgit v1.2.3