2014-02-26 14:21:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2015-01-28 08:44:43 +00:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://www.qt.io/licensing/
|
2014-02-26 14:21:18 +00:00
|
|
|
**
|
|
|
|
** 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.
|
2015-02-13 11:15:33 +00:00
|
|
|
** * 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.
|
2014-02-26 14:21:18 +00:00
|
|
|
**
|
|
|
|
**
|
|
|
|
** 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 "renderwindow.h"
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QMatrix4x4>
|
|
|
|
#include <QOpenGLContext>
|
|
|
|
#include <QOpenGLShaderProgram>
|
2014-03-03 13:47:27 +00:00
|
|
|
#include <QOpenGLFunctions>
|
2014-02-26 14:21:18 +00:00
|
|
|
|
|
|
|
RenderWindow::RenderWindow(const QSurfaceFormat &format)
|
|
|
|
: m_context(0),
|
|
|
|
m_initialized(false),
|
|
|
|
m_forceGLSL110(false),
|
|
|
|
m_angle(0.0f)
|
|
|
|
{
|
|
|
|
setSurfaceType(QWindow::OpenGLSurface);
|
|
|
|
setFormat(format);
|
|
|
|
m_context = new QOpenGLContext(this);
|
|
|
|
m_context->setFormat(requestedFormat());
|
|
|
|
if (!m_context->create()) {
|
|
|
|
delete m_context;
|
|
|
|
m_context = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderWindow::exposeEvent(QExposeEvent *)
|
|
|
|
{
|
|
|
|
if (isExposed())
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ES needs the precision qualifiers.
|
|
|
|
// On desktop GL QOpenGLShaderProgram inserts dummy defines for highp/mediump/lowp.
|
|
|
|
static const char *vertexShaderSource110 =
|
|
|
|
"attribute highp vec4 posAttr;\n"
|
|
|
|
"attribute lowp vec4 colAttr;\n"
|
|
|
|
"varying lowp vec4 col;\n"
|
|
|
|
"uniform highp mat4 matrix;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" col = colAttr;\n"
|
|
|
|
" gl_Position = matrix * posAttr;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const char *fragmentShaderSource110 =
|
|
|
|
"varying lowp vec4 col;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_FragColor = col;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const char *vertexShaderSource =
|
|
|
|
"#version 150\n"
|
|
|
|
"in vec4 posAttr;\n"
|
|
|
|
"in vec4 colAttr;\n"
|
|
|
|
"out vec4 col;\n"
|
|
|
|
"uniform mat4 matrix;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" col = colAttr;\n"
|
|
|
|
" gl_Position = matrix * posAttr;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const char *fragmentShaderSource =
|
|
|
|
"#version 150\n"
|
|
|
|
"in vec4 col;\n"
|
|
|
|
"out vec4 fragColor;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" fragColor = col;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static GLfloat vertices[] = {
|
|
|
|
0.0f, 0.707f,
|
|
|
|
-0.5f, -0.5f,
|
|
|
|
0.5f, -0.5f
|
|
|
|
};
|
|
|
|
|
|
|
|
static GLfloat colors[] = {
|
|
|
|
1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f
|
|
|
|
};
|
|
|
|
|
|
|
|
void RenderWindow::init()
|
|
|
|
{
|
|
|
|
m_program = new QOpenGLShaderProgram(this);
|
|
|
|
|
|
|
|
QSurfaceFormat format = m_context->format();
|
|
|
|
bool useNewStyleShader = format.profile() == QSurfaceFormat::CoreProfile;
|
|
|
|
// Try to handle 3.0 & 3.1 that do not have the core/compatibility profile concept 3.2+ has.
|
|
|
|
// This may still fail since version 150 (3.2) is specified in the sources but it's worth a try.
|
|
|
|
if (format.renderableType() == QSurfaceFormat::OpenGL && format.majorVersion() == 3 && format.minorVersion() <= 1)
|
|
|
|
useNewStyleShader = !format.testOption(QSurfaceFormat::DeprecatedFunctions);
|
|
|
|
if (m_forceGLSL110)
|
|
|
|
useNewStyleShader = false;
|
|
|
|
|
|
|
|
const char *vsrc = useNewStyleShader ? vertexShaderSource : vertexShaderSource110;
|
|
|
|
const char *fsrc = useNewStyleShader ? fragmentShaderSource : fragmentShaderSource110;
|
|
|
|
qDebug("Using version %s shader", useNewStyleShader ? "150" : "110");
|
|
|
|
|
|
|
|
if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vsrc)) {
|
|
|
|
emit error(m_program->log());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fsrc)) {
|
|
|
|
emit error(m_program->log());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!m_program->link()) {
|
|
|
|
emit error(m_program->log());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_posAttr = m_program->attributeLocation("posAttr");
|
|
|
|
m_colAttr = m_program->attributeLocation("colAttr");
|
|
|
|
m_matrixUniform = m_program->uniformLocation("matrix");
|
|
|
|
|
|
|
|
m_vbo.create();
|
|
|
|
m_vbo.bind();
|
|
|
|
m_vbo.allocate(vertices, sizeof(vertices) + sizeof(colors));
|
|
|
|
m_vbo.write(sizeof(vertices), colors, sizeof(colors));
|
|
|
|
m_vbo.release();
|
|
|
|
|
|
|
|
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
|
|
|
|
if (m_vao.isCreated()) // have VAO support, use it
|
|
|
|
setupVertexAttribs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderWindow::setupVertexAttribs()
|
|
|
|
{
|
|
|
|
m_vbo.bind();
|
|
|
|
m_program->setAttributeBuffer(m_posAttr, GL_FLOAT, 0, 2);
|
|
|
|
m_program->setAttributeBuffer(m_colAttr, GL_FLOAT, sizeof(vertices), 3);
|
|
|
|
m_program->enableAttributeArray(m_posAttr);
|
|
|
|
m_program->enableAttributeArray(m_colAttr);
|
|
|
|
m_vbo.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderWindow::render()
|
|
|
|
{
|
|
|
|
if (!m_context->makeCurrent(this)) {
|
2014-03-07 11:56:13 +00:00
|
|
|
emit error(tr("makeCurrent() failed"));
|
2014-02-26 14:21:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-03 13:47:27 +00:00
|
|
|
QOpenGLFunctions *f = m_context->functions();
|
2014-02-26 14:21:18 +00:00
|
|
|
if (!m_initialized) {
|
|
|
|
m_initialized = true;
|
2014-03-03 13:47:27 +00:00
|
|
|
f->glEnable(GL_DEPTH_TEST);
|
|
|
|
f->glClearColor(0, 0, 0, 1);
|
2014-02-26 14:21:18 +00:00
|
|
|
init();
|
|
|
|
emit ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_vbo.isCreated()) // init() failed, don't bother with trying to render
|
|
|
|
return;
|
|
|
|
|
|
|
|
const qreal retinaScale = devicePixelRatio();
|
2014-03-03 13:47:27 +00:00
|
|
|
f->glViewport(0, 0, width() * retinaScale, height() * retinaScale);
|
|
|
|
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2014-02-26 14:21:18 +00:00
|
|
|
|
|
|
|
m_program->bind();
|
|
|
|
QMatrix4x4 matrix;
|
|
|
|
matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
|
|
|
|
matrix.translate(0.0f, 0.0f, -2.0f);
|
|
|
|
matrix.rotate(m_angle, 0.0f, 1.0f, 0.0f);
|
|
|
|
m_program->setUniformValue(m_matrixUniform, matrix);
|
|
|
|
|
|
|
|
if (m_vao.isCreated())
|
|
|
|
m_vao.bind();
|
|
|
|
else // no VAO support, set the vertex attribute arrays now
|
|
|
|
setupVertexAttribs();
|
|
|
|
|
2014-03-03 13:47:27 +00:00
|
|
|
f->glDrawArrays(GL_TRIANGLES, 0, 3);
|
2014-02-26 14:21:18 +00:00
|
|
|
|
|
|
|
m_vao.release();
|
|
|
|
m_program->release();
|
|
|
|
|
|
|
|
// swapInterval is 1 by default which means that swapBuffers() will (hopefully) block
|
|
|
|
// and wait for vsync.
|
|
|
|
m_context->swapBuffers(this);
|
|
|
|
|
|
|
|
m_angle += 1.0f;
|
2014-03-07 11:56:13 +00:00
|
|
|
|
|
|
|
// Instead of 0 wait a few more milliseconds before rendering again. This is
|
|
|
|
// only here to make the UI widgets more responsive on slower machines. We
|
|
|
|
// can afford it since our rendering is so lightweight.
|
|
|
|
const int interval = 5;
|
2015-08-28 13:16:57 +00:00
|
|
|
QTimer::singleShot(interval, this, &RenderWindow::render);
|
2014-02-26 14:21:18 +00:00
|
|
|
}
|