summaryrefslogtreecommitdiff
path: root/examples/opengl/hellowindow
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@digia.com>2014-08-02 19:42:15 +0200
committerLaszlo Agocs <laszlo.agocs@digia.com>2014-08-12 07:47:23 +0200
commit611558d877a9ee4448b30e5443e3025c6235daaf (patch)
tree2a5d5a6da4a9994ccef4a49dec235f661a1e57f1 /examples/opengl/hellowindow
parent338f9c4f7c42d2a7bcd2c5b37137b3cc7cd9775d (diff)
downloadqtbase-611558d877a9ee4448b30e5443e3025c6235daaf.tar.gz
Modernize the OpenGL examples
Change them to use QOpenGLWidget and QOpenGLTexture. Advocate also the usage of VBOs. Hopeless examples, that rely on the fixed pipeline and will not compile or work in ES and dynamic builds, are moved to a "legacy" directory. The documentation pages for these are removed. This long due change avoids the confusion newcomers experience when trying to get started with Qt 5 and OpenGL. hellowindow's behavior is changed to open a single window only by default. The old default behavior, that opened three windows on platforms that supported both MultipleWindows & ThreadedOpenGL, can be requested by passing --multiple. --single is removed since it is the default now. This plays much nicer with drivers that have issues with threading. In addition, say hello to hellogl2. This is the old hellogl example updated to use QOpenGLWidget and OpenGL 2. It also has a mainwindow with multiple (un)dockable widgets containing the OpenGL widgets. This helps testing the behavior when the top-level of the QOpenGLWidget changes and provides a very important example of how to do proper resource management in this case. (must use aboutToBeDestroyed() of the context, since the context goes away and is replaced by a new one on every dock/undock) As a bonus, the logo is now real 3D, no more orthographic nonsense. Launch with --multisample to request 4x MSAA. Launch with --coreprofile to request 3.2 Core. In this particular example the shaders are present in both versions and there is a VAO so the application is functional with core profile contexts. Change-Id: Id780a80cb0708ef164cc172450ed74050f065596 Reviewed-by: Gunnar Sletta <gunnar.sletta@jollamobile.com>
Diffstat (limited to 'examples/opengl/hellowindow')
-rw-r--r--examples/opengl/hellowindow/hellowindow.cpp46
-rw-r--r--examples/opengl/hellowindow/hellowindow.h10
-rw-r--r--examples/opengl/hellowindow/main.cpp6
3 files changed, 29 insertions, 33 deletions
diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp
index b4dc4464ba..da31fee920 100644
--- a/examples/opengl/hellowindow/hellowindow.cpp
+++ b/examples/opengl/hellowindow/hellowindow.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
@@ -145,29 +145,18 @@ void Renderer::render()
QOpenGLFunctions *f = m_context->functions();
f->glViewport(0, 0, viewSize.width() * surface->devicePixelRatio(), viewSize.height() * surface->devicePixelRatio());
-
- f->glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- f->glFrontFace(GL_CW);
- f->glCullFace(GL_FRONT);
- f->glEnable(GL_CULL_FACE);
- f->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.translate(0.0f, -0.2f, 0.0f);
- m_program->bind();
m_program->setUniformValue(matrixUniform, modelview);
m_program->setUniformValue(colorUniform, color);
- paintQtLogo();
- m_program->release();
- f->glDisable(GL_DEPTH_TEST);
- f->glDisable(GL_CULL_FACE);
+ m_context->functions()->glDrawArrays(GL_TRIANGLES, 0, vertices.size());
m_context->swapBuffers(surface);
@@ -176,17 +165,6 @@ void Renderer::render()
QTimer::singleShot(0, this, SLOT(render()));
}
-void Renderer::paintQtLogo()
-{
- m_program->enableAttributeArray(normalAttr);
- m_program->enableAttributeArray(vertexAttr);
- m_program->setAttributeArray(vertexAttr, vertices.constData());
- m_program->setAttributeArray(normalAttr, normals.constData());
- m_context->functions()->glDrawArrays(GL_TRIANGLES, 0, vertices.size());
- m_program->disableAttributeArray(normalAttr);
- m_program->disableAttributeArray(vertexAttr);
-}
-
void Renderer::initialize()
{
QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
@@ -218,6 +196,7 @@ void Renderer::initialize()
m_program->addShader(vshader);
m_program->addShader(fshader);
m_program->link();
+ m_program->bind();
vertexAttr = m_program->attributeLocation("vertex");
normalAttr = m_program->attributeLocation("normal");
@@ -226,6 +205,25 @@ void Renderer::initialize()
m_fAngle = 0;
createGeometry();
+
+ m_vbo.create();
+ m_vbo.bind();
+ const int verticesSize = vertices.count() * 3 * sizeof(GLfloat);
+ m_vbo.allocate(verticesSize * 2);
+ m_vbo.write(0, vertices.constData(), verticesSize);
+ m_vbo.write(verticesSize, normals.constData(), verticesSize);
+
+ QOpenGLFunctions *f = m_context->functions();
+ f->glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
+ f->glFrontFace(GL_CW);
+ f->glCullFace(GL_FRONT);
+ f->glEnable(GL_CULL_FACE);
+ f->glEnable(GL_DEPTH_TEST);
+
+ m_program->enableAttributeArray(vertexAttr);
+ m_program->enableAttributeArray(normalAttr);
+ m_program->setAttributeBuffer(vertexAttr, GL_FLOAT, 0, 3);
+ m_program->setAttributeBuffer(normalAttr, GL_FLOAT, verticesSize, 3);
}
void Renderer::createGeometry()
diff --git a/examples/opengl/hellowindow/hellowindow.h b/examples/opengl/hellowindow/hellowindow.h
index 9fb7d109b9..6a6fa275e2 100644
--- a/examples/opengl/hellowindow/hellowindow.h
+++ b/examples/opengl/hellowindow/hellowindow.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
@@ -43,6 +43,7 @@
#include <QColor>
#include <QMutex>
#include <QOpenGLShaderProgram>
+#include <QOpenGLBuffer>
#include <QSharedPointer>
#include <QTimer>
@@ -65,13 +66,13 @@ private slots:
private:
void initialize();
- qreal m_fAngle;
- bool m_showBubbles;
- 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);
+
+ qreal m_fAngle;
+
QVector<QVector3D> vertices;
QVector<QVector3D> normals;
int vertexAttr;
@@ -83,6 +84,7 @@ private:
QSurfaceFormat m_format;
QOpenGLContext *m_context;
QOpenGLShaderProgram *m_program;
+ QOpenGLBuffer m_vbo;
QList<HelloWindow *> m_windows;
int m_currentWindow;
diff --git a/examples/opengl/hellowindow/main.cpp b/examples/opengl/hellowindow/main.cpp
index 5b2552ee3e..62b6bfca3f 100644
--- a/examples/opengl/hellowindow/main.cpp
+++ b/examples/opengl/hellowindow/main.cpp
@@ -41,7 +41,6 @@
#include "hellowindow.h"
#include <qpa/qplatformintegration.h>
-#include <private/qguiapplication_p.h>
#include <QGuiApplication>
#include <QScreen>
@@ -51,10 +50,7 @@ int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
- QPlatformIntegration *integ = QGuiApplicationPrivate::platformIntegration();
- const bool multipleWindows = integ->hasCapability(QPlatformIntegration::ThreadedOpenGL)
- && integ->hasCapability(QPlatformIntegration::WindowManagement)
- && !QGuiApplication::arguments().contains(QStringLiteral("--single"));
+ const bool multipleWindows = QGuiApplication::arguments().contains(QStringLiteral("--multiple"));
QScreen *screen = QGuiApplication::primaryScreen();