2014-03-20 11:58:22 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-22 12:24:00 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-03-20 11:58:22 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2016-01-22 12:24:00 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2014-03-20 11:58:22 +00:00
|
|
|
**
|
|
|
|
** "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-03-20 11:58:22 +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 "background_renderer.h"
|
|
|
|
|
2014-08-06 08:25:36 +00:00
|
|
|
#include <qmath.h>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QTime>
|
2014-03-20 11:58:22 +00:00
|
|
|
|
2014-08-06 08:25:36 +00:00
|
|
|
#include <QOpenGLShaderProgram>
|
|
|
|
#include <QOpenGLContext>
|
|
|
|
#include <QOpenGLFunctions>
|
2014-03-20 11:58:22 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
static const char vertex_shader[] =
|
|
|
|
"attribute highp vec3 vertexCoord;"
|
|
|
|
"void main() {"
|
|
|
|
" gl_Position = vec4(vertexCoord,1.0);"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
static const char fragment_shader[] =
|
|
|
|
"void main() {"
|
|
|
|
" gl_FragColor = vec4(0.0,1.0,0.0,1.0);"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
static const float vertices[] = { -1, -1, 0,
|
|
|
|
-1, 1, 0,
|
|
|
|
1, -1, 0,
|
|
|
|
1, 1, 0 };
|
|
|
|
|
|
|
|
FragmentToy::FragmentToy(const QString &fragmentSource, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_recompile_shaders(true)
|
|
|
|
{
|
|
|
|
if (QFile::exists(fragmentSource)) {
|
|
|
|
QFileInfo info(fragmentSource);
|
|
|
|
m_fragment_file_last_modified = info.lastModified();
|
|
|
|
m_fragment_file = fragmentSource;
|
2018-09-26 06:56:36 +00:00
|
|
|
#if QT_CONFIG(filesystemwatcher)
|
2015-01-06 13:55:30 +00:00
|
|
|
m_watcher.addPath(info.canonicalPath());
|
|
|
|
QObject::connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FragmentToy::fileChanged);
|
2014-03-20 11:58:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:42:01 +00:00
|
|
|
FragmentToy::~FragmentToy()
|
|
|
|
= default;
|
|
|
|
|
2014-03-20 11:58:22 +00:00
|
|
|
void FragmentToy::draw(const QSize &windowSize)
|
|
|
|
{
|
|
|
|
if (!m_program)
|
|
|
|
initializeOpenGLFunctions();
|
|
|
|
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
|
2014-08-12 09:45:50 +00:00
|
|
|
glClearColor(0, 0, 0, 1);
|
2014-03-20 11:58:22 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
if (!m_vao.isCreated())
|
|
|
|
m_vao.create();
|
|
|
|
|
2015-01-06 13:55:30 +00:00
|
|
|
QOpenGLVertexArrayObject::Binder binder(&m_vao);
|
2014-03-20 11:58:22 +00:00
|
|
|
|
|
|
|
if (!m_vertex_buffer.isCreated()) {
|
|
|
|
m_vertex_buffer.create();
|
|
|
|
m_vertex_buffer.bind();
|
|
|
|
m_vertex_buffer.allocate(vertices, sizeof(vertices));
|
|
|
|
m_vertex_buffer.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_program) {
|
|
|
|
m_program.reset(new QOpenGLShaderProgram);
|
|
|
|
m_program->create();
|
|
|
|
m_vertex_shader.reset(new QOpenGLShader(QOpenGLShader::Vertex));
|
|
|
|
if (!m_vertex_shader->compileSourceCode(vertex_shader)) {
|
|
|
|
qWarning() << "Failed to compile the vertex shader:" << m_vertex_shader->log();
|
|
|
|
}
|
2019-05-14 18:42:01 +00:00
|
|
|
if (!m_program->addShader(m_vertex_shader.get())) {
|
2014-03-20 11:58:22 +00:00
|
|
|
qWarning() << "Failed to add vertex shader to program:" << m_program->log();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 13:55:30 +00:00
|
|
|
if (!m_fragment_shader && m_recompile_shaders) {
|
2014-03-20 11:58:22 +00:00
|
|
|
QByteArray data;
|
|
|
|
if (m_fragment_file.size()) {
|
|
|
|
QFile file(m_fragment_file);
|
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
|
|
data = file.readAll();
|
|
|
|
} else {
|
|
|
|
qWarning() << "Failed to load input file, falling back to default";
|
|
|
|
data = QByteArray::fromRawData(fragment_shader, sizeof(fragment_shader));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QFile qrcFile(":/background.frag");
|
|
|
|
if (qrcFile.open(QIODevice::ReadOnly))
|
|
|
|
data = qrcFile.readAll();
|
|
|
|
else
|
|
|
|
data = QByteArray::fromRawData(fragment_shader, sizeof(fragment_shader));
|
|
|
|
}
|
|
|
|
if (data.size()) {
|
|
|
|
m_fragment_shader.reset(new QOpenGLShader(QOpenGLShader::Fragment));
|
|
|
|
if (!m_fragment_shader->compileSourceCode(data)) {
|
|
|
|
qWarning() << "Failed to compile fragment shader:" << m_fragment_shader->log();
|
2017-09-18 09:49:52 +00:00
|
|
|
m_fragment_shader.reset(nullptr);
|
2014-03-20 11:58:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << "Unknown error, no fragment shader";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_fragment_shader) {
|
2019-05-14 18:42:01 +00:00
|
|
|
if (!m_program->addShader(m_fragment_shader.get())) {
|
2014-03-20 11:58:22 +00:00
|
|
|
qWarning() << "Failed to add fragment shader to program:" << m_program->log();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_recompile_shaders) {
|
|
|
|
m_recompile_shaders = false;
|
|
|
|
|
|
|
|
if (m_program->link()) {
|
|
|
|
m_vertex_coord_pos = m_program->attributeLocation("vertexCoord");
|
|
|
|
} else {
|
|
|
|
qWarning() << "Failed to link shader program" << m_program->log();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-06 13:55:30 +00:00
|
|
|
if (!m_program->isLinked())
|
|
|
|
return;
|
|
|
|
|
2014-03-20 11:58:22 +00:00
|
|
|
m_program->bind();
|
|
|
|
|
|
|
|
m_vertex_buffer.bind();
|
|
|
|
m_program->setAttributeBuffer("vertexCoord", GL_FLOAT, 0, 3, 0);
|
|
|
|
m_program->enableAttributeArray("vertexCoord");
|
|
|
|
m_vertex_buffer.release();
|
|
|
|
|
2016-06-21 21:30:25 +00:00
|
|
|
m_program->setUniformValue("currentTime", (uint) QDateTime::currentMSecsSinceEpoch());
|
2014-03-20 11:58:22 +00:00
|
|
|
m_program->setUniformValue("windowSize", windowSize);
|
|
|
|
|
|
|
|
QOpenGLContext::currentContext()->functions()->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
m_program->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FragmentToy::fileChanged(const QString &path)
|
|
|
|
{
|
|
|
|
Q_UNUSED(path);
|
|
|
|
if (QFile::exists(m_fragment_file)) {
|
|
|
|
QFileInfo fragment_source(m_fragment_file);
|
|
|
|
if (fragment_source.lastModified() > m_fragment_file_last_modified) {
|
|
|
|
m_fragment_file_last_modified = fragment_source.lastModified();
|
|
|
|
m_recompile_shaders = true;
|
|
|
|
if (m_program) {
|
2019-05-14 18:42:01 +00:00
|
|
|
m_program->removeShader(m_fragment_shader.get());
|
2017-09-18 09:49:52 +00:00
|
|
|
m_fragment_shader.reset(nullptr);
|
2014-03-20 11:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_recompile_shaders = true;
|
|
|
|
if (m_program) {
|
2019-05-14 18:42:01 +00:00
|
|
|
m_program->removeShader(m_fragment_shader.get());
|
2017-09-18 09:49:52 +00:00
|
|
|
m_fragment_shader.reset(nullptr);
|
2014-03-20 11:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|