Make the gles3 example more impressive

Add some extra rotations and offsets, to show off the power of
what you can do with a single draw call. Also use QGLWindow instead
of QGLWidget. This improves performance from 23 FPS to 30 FPS on a
4k monitor with integrated Intel graphics.

Change-Id: Iedac1c14cc6057959aa15faaacdf29da91f36ab1
Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
This commit is contained in:
Paul Olav Tvete 2015-07-22 13:38:26 +02:00
parent 906ad92fbf
commit 80dbdf0de3
6 changed files with 84 additions and 144 deletions

View File

@ -38,7 +38,7 @@
**
****************************************************************************/
#include "glwidget.h"
#include "glwindow.h"
#include <QImage>
#include <QOpenGLTexture>
#include <QOpenGLShaderProgram>
@ -49,14 +49,17 @@
#include <QPropertyAnimation>
#include <QPauseAnimation>
#include <QSequentialAnimationGroup>
#include <QTimer>
GLWidget::GLWidget()
GLWindow::GLWindow()
: m_texture(0),
m_program(0),
m_vbo(0),
m_vao(0),
m_target(0, 0, -1),
m_uniformsDirty(true)
m_uniformsDirty(true),
m_r(0),
m_r2(0)
{
m_world.setToIdentity();
m_world.translate(0, 0, -1);
@ -65,25 +68,34 @@ GLWidget::GLWidget()
QSequentialAnimationGroup *animGroup = new QSequentialAnimationGroup(this);
animGroup->setLoopCount(-1);
QPropertyAnimation *zAnim0 = new QPropertyAnimation(this, QByteArrayLiteral("z"));
zAnim0->setStartValue(0.0f);
zAnim0->setEndValue(1.0f);
zAnim0->setStartValue(1.5f);
zAnim0->setEndValue(10.0f);
zAnim0->setDuration(2000);
animGroup->addAnimation(zAnim0);
QPropertyAnimation *zAnim1 = new QPropertyAnimation(this, QByteArrayLiteral("z"));
zAnim1->setStartValue(0.0f);
zAnim1->setEndValue(70.0f);
zAnim1->setStartValue(10.0f);
zAnim1->setEndValue(50.0f);
zAnim1->setDuration(4000);
zAnim1->setEasingCurve(QEasingCurve::OutElastic);
animGroup->addAnimation(zAnim1);
QPropertyAnimation *zAnim2 = new QPropertyAnimation(this, QByteArrayLiteral("z"));
zAnim2->setStartValue(70.0f);
zAnim2->setEndValue(0.0f);
zAnim2->setStartValue(50.0f);
zAnim2->setEndValue(1.5f);
zAnim2->setDuration(2000);
animGroup->addAnimation(zAnim2);
animGroup->start();
QPropertyAnimation* rAnim = new QPropertyAnimation(this, QByteArrayLiteral("r"));
rAnim->setStartValue(0.0f);
rAnim->setEndValue(360.0f);
rAnim->setDuration(2000);
rAnim->setLoopCount(-1);
rAnim->start();
QTimer::singleShot(4000, this, SLOT(startSecondStage()));
}
GLWidget::~GLWidget()
GLWindow::~GLWindow()
{
makeCurrent();
delete m_texture;
@ -92,13 +104,37 @@ GLWidget::~GLWidget()
delete m_vao;
}
void GLWidget::setZ(float v)
void GLWindow::startSecondStage()
{
QPropertyAnimation* r2Anim = new QPropertyAnimation(this, QByteArrayLiteral("r2"));
r2Anim->setStartValue(0.0f);
r2Anim->setEndValue(360.0f);
r2Anim->setDuration(20000);
r2Anim->setLoopCount(-1);
r2Anim->start();
}
void GLWindow::setZ(float v)
{
m_eye.setZ(v);
m_uniformsDirty = true;
update();
}
void GLWindow::setR(float v)
{
m_r = v;
m_uniformsDirty = true;
update();
}
void GLWindow::setR2(float v)
{
m_r2 = v;
m_uniformsDirty = true;
update();
}
static const char *vertexShaderSource =
"layout(location = 0) in vec4 vertex;\n"
"layout(location = 1) in vec3 normal;\n"
@ -108,11 +144,13 @@ static const char *vertexShaderSource =
"uniform mat4 projMatrix;\n"
"uniform mat4 camMatrix;\n"
"uniform mat4 worldMatrix;\n"
"uniform mat4 myMatrix;\n"
"uniform sampler2D sampler;\n"
"void main() {\n"
" ivec2 pos = ivec2(gl_InstanceID % 32, gl_InstanceID / 32);\n"
" vec2 t = vec2(-16 + pos.x, -18 + pos.y);\n"
" mat4 wm = mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, t.x, t.y, 0, 1) * worldMatrix;\n"
" vec2 t = vec2(float(-16 + pos.x) * 0.8, float(-18 + pos.y) * 0.6);\n"
" float val = 2.0 * length(texelFetch(sampler, pos, 0).rgb);\n"
" mat4 wm = myMatrix * mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, t.x, t.y, val, 1) * worldMatrix;\n"
" color = texelFetch(sampler, pos, 0).rgb * vec3(0.4, 1.0, 0.0);\n"
" vert = vec3(wm * vertex);\n"
" vertNormal = mat3(transpose(inverse(wm))) * normal;\n"
@ -145,7 +183,7 @@ QByteArray versionedShaderCode(const char *src)
return versionedSrc;
}
void GLWidget::initializeGL()
void GLWindow::initializeGL()
{
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
@ -171,6 +209,7 @@ void GLWidget::initializeGL()
m_projMatrixLoc = m_program->uniformLocation("projMatrix");
m_camMatrixLoc = m_program->uniformLocation("camMatrix");
m_worldMatrixLoc = m_program->uniformLocation("worldMatrix");
m_myMatrixLoc = m_program->uniformLocation("myMatrix");
m_lightPosLoc = m_program->uniformLocation("lightPos");
// Create a VAO. Not strictly required for ES 3, but it is for plain OpenGL.
@ -201,14 +240,14 @@ void GLWidget::initializeGL()
f->glEnable(GL_CULL_FACE);
}
void GLWidget::resizeGL(int w, int h)
void GLWindow::resizeGL(int w, int h)
{
m_proj.setToIdentity();
m_proj.perspective(45.0f, GLfloat(w) / h, 0.01f, 100.0f);
m_uniformsDirty = true;
}
void GLWidget::paintGL()
void GLWindow::paintGL()
{
// Now use QOpenGLExtraFunctions instead of QOpenGLFunctions as we want to
// do more than what GL(ES) 2.0 offers.
@ -226,7 +265,13 @@ void GLWidget::paintGL()
camera.lookAt(m_eye, m_eye + m_target, QVector3D(0, 1, 0));
m_program->setUniformValue(m_projMatrixLoc, m_proj);
m_program->setUniformValue(m_camMatrixLoc, camera);
m_program->setUniformValue(m_worldMatrixLoc, m_world);
QMatrix4x4 wm = m_world;
wm.rotate(m_r, 1, 1, 0);
m_program->setUniformValue(m_worldMatrixLoc, wm);
QMatrix4x4 mm;
mm.setToIdentity();
mm.rotate(-m_r2, 1, 0, 0);
m_program->setUniformValue(m_myMatrixLoc, mm);
m_program->setUniformValue(m_lightPosLoc, QVector3D(0, 0, 70));
}

View File

@ -41,7 +41,7 @@
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLWindow>
#include <QMatrix4x4>
#include <QVector3D>
#include "../hellogl2/logo.h"
@ -51,14 +51,16 @@ class QOpenGLShaderProgram;
class QOpenGLBuffer;
class QOpenGLVertexArrayObject;
class GLWidget : public QOpenGLWidget
class GLWindow : public QOpenGLWindow
{
Q_OBJECT
Q_PROPERTY(float z READ z WRITE setZ)
Q_PROPERTY(float r READ r WRITE setR)
Q_PROPERTY(float r2 READ r2 WRITE setR2)
public:
GLWidget();
~GLWidget();
GLWindow();
~GLWindow();
void initializeGL();
void resizeGL(int w, int h);
@ -67,6 +69,12 @@ public:
float z() const { return m_eye.z(); }
void setZ(float v);
float r() const { return m_r; }
void setR(float v);
float r2() const { return m_r2; }
void setR2(float v);
private slots:
void startSecondStage();
private:
QOpenGLTexture *m_texture;
QOpenGLShaderProgram *m_program;
@ -76,12 +84,15 @@ private:
int m_projMatrixLoc;
int m_camMatrixLoc;
int m_worldMatrixLoc;
int m_myMatrixLoc;
int m_lightPosLoc;
QMatrix4x4 m_proj;
QMatrix4x4 m_world;
QVector3D m_eye;
QVector3D m_target;
bool m_uniformsDirty;
float m_r;
float m_r2;
};
#endif

View File

@ -1,14 +1,10 @@
HEADERS = $$PWD/glwidget.h \
$$PWD/mainwindow.h \
HEADERS = $$PWD/glwindow.h \
$$PWD/../hellogl2/logo.h
SOURCES = $$PWD/glwidget.cpp \
SOURCES = $$PWD/glwindow.cpp \
$$PWD/main.cpp \
$$PWD/mainwindow.cpp \
$$PWD/../hellogl2/logo.cpp
QT += widgets
RESOURCES += hellogles3.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellogles3

View File

@ -38,11 +38,11 @@
**
****************************************************************************/
#include <QApplication>
#include <QGuiApplication>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include "mainwindow.h"
#include "glwindow.h"
// This example demonstrates easy, cross-platform usage of OpenGL ES 3.0 functions via
// QOpenGLExtraFunctions in an application that works identically on desktop platforms
@ -69,10 +69,10 @@ int main(int argc, char *argv[])
QSurfaceFormat::setDefaultFormat(fmt);
QApplication app(argc, argv);
QGuiApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.showMaximized();
GLWindow glWindow;
glWindow.showMaximized();
return app.exec();
}

View File

@ -1,58 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "mainwindow.h"
#include "glwidget.h"
#include <QApplication>
#include <QMenuBar>
#include <QMenu>
MainWindow::MainWindow()
{
QMenuBar *menuBar = new QMenuBar;
QMenu *menuWindow = menuBar->addMenu(tr("&File"));
QAction *exitAction = new QAction(menuWindow);
exitAction->setText(tr("E&xit"));
menuWindow->addAction(exitAction);
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
setMenuBar(menuBar);
setCentralWidget(new GLWidget);
}

View File

@ -1,54 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
};
#endif