diff options
author | Laszlo Agocs <laszlo.agocs@digia.com> | 2014-08-02 19:42:15 +0200 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@digia.com> | 2014-08-12 07:47:23 +0200 |
commit | 611558d877a9ee4448b30e5443e3025c6235daaf (patch) | |
tree | 2a5d5a6da4a9994ccef4a49dec235f661a1e57f1 /examples/opengl/textures/glwidget.cpp | |
parent | 338f9c4f7c42d2a7bcd2c5b37137b3cc7cd9775d (diff) |
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/textures/glwidget.cpp')
-rw-r--r-- | examples/opengl/textures/glwidget.cpp | 78 |
1 files changed, 42 insertions, 36 deletions
diff --git a/examples/opengl/textures/glwidget.cpp b/examples/opengl/textures/glwidget.cpp index 305ca1aa4c..96c732f8a6 100644 --- a/examples/opengl/textures/glwidget.cpp +++ b/examples/opengl/textures/glwidget.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. @@ -38,23 +38,30 @@ ** ****************************************************************************/ -#include <QtWidgets> -#include <QtOpenGL> - #include "glwidget.h" - -GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget) - : QGLWidget(parent, shareWidget) +#include <QOpenGLShaderProgram> +#include <QOpenGLTexture> +#include <QMouseEvent> + +GLWidget::GLWidget(QWidget *parent) + : QOpenGLWidget(parent), + clearColor(Qt::black), + xRot(0), + yRot(0), + zRot(0), + program(0) { - clearColor = Qt::black; - xRot = 0; - yRot = 0; - zRot = 0; - program = 0; + memset(textures, 0, sizeof(textures)); } GLWidget::~GLWidget() { + makeCurrent(); + vbo.destroy(); + for (int i = 0; i < 6; ++i) + delete textures[i]; + delete program; + doneCurrent(); } QSize GLWidget::minimumSizeHint() const @@ -72,13 +79,13 @@ void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle) xRot += xAngle; yRot += yAngle; zRot += zAngle; - updateGL(); + update(); } void GLWidget::setClearColor(const QColor &color) { clearColor = color; - updateGL(); + update(); } void GLWidget::initializeGL() @@ -89,14 +96,11 @@ void GLWidget::initializeGL() glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); -#ifdef GL_TEXTURE_2D - glEnable(GL_TEXTURE_2D); -#endif #define PROGRAM_VERTEX_ATTRIBUTE 0 #define PROGRAM_TEXCOORD_ATTRIBUTE 1 - QGLShader *vshader = new QGLShader(QGLShader::Vertex, this); + QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); const char *vsrc = "attribute highp vec4 vertex;\n" "attribute mediump vec4 texCoord;\n" @@ -109,7 +113,7 @@ void GLWidget::initializeGL() "}\n"; vshader->compileSourceCode(vsrc); - QGLShader *fshader = new QGLShader(QGLShader::Fragment, this); + QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); const char *fsrc = "uniform sampler2D texture;\n" "varying mediump vec4 texc;\n" @@ -119,7 +123,7 @@ void GLWidget::initializeGL() "}\n"; fshader->compileSourceCode(fsrc); - program = new QGLShaderProgram(this); + program = new QOpenGLShaderProgram; program->addShader(vshader); program->addShader(fshader); program->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE); @@ -132,7 +136,7 @@ void GLWidget::initializeGL() void GLWidget::paintGL() { - qglClearColor(clearColor); + glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), clearColor.alphaF()); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 m; @@ -145,17 +149,14 @@ void GLWidget::paintGL() program->setUniformValue("matrix", m); program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE); program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE); - program->setAttributeArray - (PROGRAM_VERTEX_ATTRIBUTE, vertices.constData()); - program->setAttributeArray - (PROGRAM_TEXCOORD_ATTRIBUTE, texCoords.constData()); + program->setAttributeBuffer(PROGRAM_VERTEX_ATTRIBUTE, GL_FLOAT, 0, 3, 5 * sizeof(GLfloat)); + program->setAttributeBuffer(PROGRAM_TEXCOORD_ATTRIBUTE, GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat)); for (int i = 0; i < 6; ++i) { - glBindTexture(GL_TEXTURE_2D, textures[i]); + textures[i]->bind(); glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4); } } - void GLWidget::resizeGL(int width, int height) { int side = qMin(width, height); @@ -196,18 +197,23 @@ void GLWidget::makeObject() { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } }; - for (int j=0; j < 6; ++j) { - textures[j] = bindTexture - (QPixmap(QString(":/images/side%1.png").arg(j + 1)), GL_TEXTURE_2D); - } + for (int j = 0; j < 6; ++j) + textures[j] = new QOpenGLTexture(QImage(QString(":/images/side%1.png").arg(j + 1)).mirrored()); + QVector<GLfloat> vertData; for (int i = 0; i < 6; ++i) { for (int j = 0; j < 4; ++j) { - texCoords.append - (QVector2D(j == 0 || j == 3, j == 0 || j == 1)); - vertices.append - (QVector3D(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], - 0.2 * coords[i][j][2])); + // vertex position + vertData.append(0.2 * coords[i][j][0]); + vertData.append(0.2 * coords[i][j][1]); + vertData.append(0.2 * coords[i][j][2]); + // texture coordinate + vertData.append(j == 0 || j == 3); + vertData.append(j == 0 || j == 1); } } + + vbo.create(); + vbo.bind(); + vbo.allocate(vertData.constData(), vertData.count() * sizeof(GLfloat)); } |