2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2014-03-20 11:58:22 +00:00
|
|
|
|
|
|
|
#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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|