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
|
2011-09-14 02:03:34 +00:00
|
|
|
|
2011-04-27 15:49:30 +00:00
|
|
|
#include "hellowindow.h"
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
#include <QOpenGLContext>
|
2014-03-03 13:47:27 +00:00
|
|
|
#include <QOpenGLFunctions>
|
2017-04-14 04:13:52 +00:00
|
|
|
#include <QRandomGenerator>
|
2011-04-27 15:49:30 +00:00
|
|
|
#include <qmath.h>
|
2020-05-20 09:05:17 +00:00
|
|
|
#include <QElapsedTimer>
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2011-12-06 11:54:16 +00:00
|
|
|
Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
|
2011-06-07 15:25:22 +00:00
|
|
|
: m_initialized(false)
|
2011-08-18 08:50:18 +00:00
|
|
|
, m_format(format)
|
2011-06-07 15:25:22 +00:00
|
|
|
{
|
2011-08-31 07:14:00 +00:00
|
|
|
m_context = new QOpenGLContext(this);
|
2011-12-06 11:54:16 +00:00
|
|
|
if (screen)
|
|
|
|
m_context->setScreen(screen);
|
2011-08-18 08:50:18 +00:00
|
|
|
m_context->setFormat(format);
|
|
|
|
if (share)
|
|
|
|
m_context->setShareContext(share->m_context);
|
2011-07-21 11:50:28 +00:00
|
|
|
m_context->create();
|
2014-10-16 11:35:33 +00:00
|
|
|
|
|
|
|
m_backgroundColor = QColor::fromRgbF(0.1f, 0.1f, 0.2f, 1.0f);
|
2017-04-14 04:13:52 +00:00
|
|
|
m_backgroundColor.setRed(QRandomGenerator::global()->bounded(64));
|
|
|
|
m_backgroundColor.setGreen(QRandomGenerator::global()->bounded(128));
|
|
|
|
m_backgroundColor.setBlue(QRandomGenerator::global()->bounded(256));
|
2011-06-07 15:25:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:33 +00:00
|
|
|
HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer, QScreen *screen)
|
2013-04-08 06:32:43 +00:00
|
|
|
: m_colorIndex(0), m_renderer(renderer)
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
2011-06-22 11:33:27 +00:00
|
|
|
setSurfaceType(QWindow::OpenGLSurface);
|
Rename all QWindow properties that have "window" in them
windowTitle, windowModality, windowIcon and so on are named that way
to be similar to the ones in QWidget. However QQuickWindow inherits
all of the declared properties, and we would like to have shorter
property names in QML. If you are working with a Window then it's
obvious the title property is the window title. Unfortunately,
there must be patches in many other modules which depend on this one.
In order to avoid the need to merge them all at the same time,
there is also patch https://codereview.qt-project.org/#change,39001
which temporarily adds backwards-compatible accessors, which can be
removed after the other modules are able to build without them.
We should not rename windowState to state, because in QML, state
usually drives the state machine for animation transitions etc.
(although QWindow is not an Item, a user might get confused about it).
Related patches are
https://codereview.qt-project.org/#change,39001
https://codereview.qt-project.org/#change,37764
https://codereview.qt-project.org/#change,37765
https://codereview.qt-project.org/#change,37766
https://codereview.qt-project.org/#change,37762
Change-Id: Ie4424ec15fbdef6b29b137f90a2ae33f173edd21
Reviewed-by: Samuel Rødal <samuel.rodal@digia.com>
2012-10-22 10:47:34 +00:00
|
|
|
setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
setGeometry(QRect(10, 10, 640, 480));
|
|
|
|
|
2011-08-18 08:50:18 +00:00
|
|
|
setFormat(renderer->format());
|
2014-10-16 11:35:33 +00:00
|
|
|
if (screen)
|
|
|
|
setScreen(screen);
|
2011-08-18 08:50:18 +00:00
|
|
|
|
2011-04-27 15:49:30 +00:00
|
|
|
create();
|
|
|
|
|
|
|
|
updateColor();
|
2020-05-20 09:05:17 +00:00
|
|
|
|
|
|
|
connect(renderer.data(), &Renderer::requestUpdate, this, &QWindow::requestUpdate);
|
2011-04-27 15:49:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 10:27:10 +00:00
|
|
|
void HelloWindow::exposeEvent(QExposeEvent *)
|
2012-04-19 07:41:48 +00:00
|
|
|
{
|
2016-08-23 06:02:16 +00:00
|
|
|
if (isExposed())
|
2020-05-20 09:05:17 +00:00
|
|
|
render();
|
2012-04-19 07:41:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 13:21:10 +00:00
|
|
|
bool HelloWindow::event(QEvent *ev)
|
|
|
|
{
|
2020-05-20 09:05:17 +00:00
|
|
|
if (ev->type() == QEvent::UpdateRequest && isExposed())
|
|
|
|
render();
|
2019-01-14 13:21:10 +00:00
|
|
|
return QWindow::event(ev);
|
|
|
|
}
|
|
|
|
|
2020-05-20 09:05:17 +00:00
|
|
|
void HelloWindow::render()
|
|
|
|
{
|
|
|
|
static QElapsedTimer timer;
|
|
|
|
if (!timer.isValid())
|
|
|
|
timer.start();
|
|
|
|
qreal a = (qreal)(((timer.elapsed() * 3) % 36000) / 100.0);
|
|
|
|
auto call = [this, r = m_renderer.data(), a, c = color()]() { r->render(this, a, c); };
|
|
|
|
QMetaObject::invokeMethod(m_renderer.data(), call);
|
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void HelloWindow::mousePressEvent(QMouseEvent *)
|
2011-04-28 11:50:07 +00:00
|
|
|
{
|
2011-06-07 15:25:22 +00:00
|
|
|
updateColor();
|
2011-04-28 11:50:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 06:32:43 +00:00
|
|
|
QColor HelloWindow::color() const
|
2011-08-18 08:50:18 +00:00
|
|
|
{
|
2013-04-08 06:32:43 +00:00
|
|
|
return m_color;
|
2011-08-18 08:50:18 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 15:49:30 +00:00
|
|
|
void HelloWindow::updateColor()
|
|
|
|
{
|
2011-06-07 15:25:22 +00:00
|
|
|
QColor colors[] =
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
2011-06-07 15:25:22 +00:00
|
|
|
QColor(100, 255, 0),
|
|
|
|
QColor(0, 100, 255)
|
2011-04-27 15:49:30 +00:00
|
|
|
};
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
m_color = colors[m_colorIndex];
|
2012-12-19 10:27:10 +00:00
|
|
|
m_colorIndex = 1 - m_colorIndex;
|
2011-04-27 15:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 09:05:17 +00:00
|
|
|
void Renderer::render(HelloWindow *surface, qreal angle, const QColor &color)
|
2011-06-07 15:25:22 +00:00
|
|
|
{
|
2011-06-21 11:39:26 +00:00
|
|
|
if (!m_context->makeCurrent(surface))
|
|
|
|
return;
|
2011-06-07 15:25:22 +00:00
|
|
|
|
2013-04-08 06:32:43 +00:00
|
|
|
QSize viewSize = surface->size();
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
if (!m_initialized) {
|
|
|
|
initialize();
|
|
|
|
m_initialized = true;
|
|
|
|
}
|
2011-05-02 10:00:00 +00:00
|
|
|
|
2014-03-03 13:47:27 +00:00
|
|
|
QOpenGLFunctions *f = m_context->functions();
|
|
|
|
f->glViewport(0, 0, viewSize.width() * surface->devicePixelRatio(), viewSize.height() * surface->devicePixelRatio());
|
|
|
|
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2014-10-16 11:35:33 +00:00
|
|
|
f->glClearColor(m_backgroundColor.redF(), m_backgroundColor.greenF(), m_backgroundColor.blueF(), m_backgroundColor.alphaF());
|
2014-09-01 14:25:14 +00:00
|
|
|
f->glFrontFace(GL_CW);
|
|
|
|
f->glCullFace(GL_FRONT);
|
|
|
|
f->glEnable(GL_CULL_FACE);
|
|
|
|
f->glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
m_program->bind();
|
|
|
|
m_vbo.bind();
|
|
|
|
|
|
|
|
m_program->enableAttributeArray(vertexAttr);
|
|
|
|
m_program->enableAttributeArray(normalAttr);
|
|
|
|
m_program->setAttributeBuffer(vertexAttr, GL_FLOAT, 0, 3);
|
|
|
|
const int verticesSize = vertices.count() * 3 * sizeof(GLfloat);
|
|
|
|
m_program->setAttributeBuffer(normalAttr, GL_FLOAT, verticesSize, 3);
|
|
|
|
|
2011-04-27 15:49:30 +00:00
|
|
|
QMatrix4x4 modelview;
|
2020-05-20 09:05:17 +00:00
|
|
|
modelview.rotate(angle, 0.0f, 1.0f, 0.0f);
|
|
|
|
modelview.rotate(angle, 1.0f, 0.0f, 0.0f);
|
|
|
|
modelview.rotate(angle, 0.0f, 0.0f, 1.0f);
|
2011-04-27 15:49:30 +00:00
|
|
|
modelview.translate(0.0f, -0.2f, 0.0f);
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
m_program->setUniformValue(matrixUniform, modelview);
|
|
|
|
m_program->setUniformValue(colorUniform, color);
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2014-08-02 17:42:15 +00:00
|
|
|
m_context->functions()->glDrawArrays(GL_TRIANGLES, 0, vertices.size());
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
m_context->swapBuffers(surface);
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2020-05-20 09:05:17 +00:00
|
|
|
emit requestUpdate();
|
2011-04-27 15:49:30 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:33 +00:00
|
|
|
Q_GLOBAL_STATIC(QMutex, initMutex)
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::initialize()
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
2014-10-16 11:35:33 +00:00
|
|
|
// Threaded shader compilation can confuse some drivers. Avoid it.
|
|
|
|
QMutexLocker lock(initMutex());
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
|
2012-12-19 10:27:10 +00:00
|
|
|
vshader->compileSourceCode(
|
|
|
|
"attribute highp vec4 vertex;"
|
|
|
|
"attribute mediump vec3 normal;"
|
|
|
|
"uniform mediump mat4 matrix;"
|
|
|
|
"uniform lowp vec4 sourceColor;"
|
|
|
|
"varying mediump vec4 color;"
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
|
|
|
" vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));"
|
|
|
|
" float angle = max(dot(normal, toLight), 0.0);"
|
|
|
|
" vec3 col = sourceColor.rgb;"
|
|
|
|
" color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);"
|
|
|
|
" color = clamp(color, 0.0, 1.0);"
|
|
|
|
" gl_Position = matrix * vertex;"
|
|
|
|
"}");
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
|
2012-12-19 10:27:10 +00:00
|
|
|
fshader->compileSourceCode(
|
|
|
|
"varying mediump vec4 color;"
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
|
|
|
" gl_FragColor = color;"
|
|
|
|
"}");
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2012-05-25 10:50:21 +00:00
|
|
|
m_program = new QOpenGLShaderProgram(this);
|
2011-08-22 08:49:28 +00:00
|
|
|
m_program->addShader(vshader);
|
|
|
|
m_program->addShader(fshader);
|
|
|
|
m_program->link();
|
2014-08-02 17:42:15 +00:00
|
|
|
m_program->bind();
|
2011-04-27 15:49:30 +00:00
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
vertexAttr = m_program->attributeLocation("vertex");
|
|
|
|
normalAttr = m_program->attributeLocation("normal");
|
|
|
|
matrixUniform = m_program->uniformLocation("matrix");
|
|
|
|
colorUniform = m_program->uniformLocation("sourceColor");
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
createGeometry();
|
2014-08-02 17:42:15 +00:00
|
|
|
|
|
|
|
m_vbo.create();
|
|
|
|
m_vbo.bind();
|
|
|
|
const int verticesSize = vertices.count() * 3 * sizeof(GLfloat);
|
|
|
|
m_vbo.allocate(verticesSize * 2);
|
|
|
|
m_vbo.write(0, vertices.constData(), verticesSize);
|
|
|
|
m_vbo.write(verticesSize, normals.constData(), verticesSize);
|
2011-04-27 15:49:30 +00:00
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::createGeometry()
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
|
|
|
vertices.clear();
|
|
|
|
normals.clear();
|
|
|
|
|
|
|
|
qreal x1 = +0.06f;
|
|
|
|
qreal y1 = -0.14f;
|
|
|
|
qreal x2 = +0.14f;
|
|
|
|
qreal y2 = -0.06f;
|
|
|
|
qreal x3 = +0.08f;
|
|
|
|
qreal y3 = +0.00f;
|
|
|
|
qreal x4 = +0.30f;
|
|
|
|
qreal y4 = +0.22f;
|
|
|
|
|
|
|
|
quad(x1, y1, x2, y2, y2, x2, y1, x1);
|
|
|
|
quad(x3, y3, x4, y4, y4, x4, y3, x3);
|
|
|
|
|
|
|
|
extrude(x1, y1, x2, y2);
|
|
|
|
extrude(x2, y2, y2, x2);
|
|
|
|
extrude(y2, x2, y1, x1);
|
|
|
|
extrude(y1, x1, x1, y1);
|
|
|
|
extrude(x3, y3, x4, y4);
|
|
|
|
extrude(x4, y4, y4, x4);
|
|
|
|
extrude(y4, x4, y3, x3);
|
|
|
|
|
|
|
|
const int NumSectors = 100;
|
2017-03-22 12:18:52 +00:00
|
|
|
const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors;
|
2011-04-27 15:49:30 +00:00
|
|
|
for (int i = 0; i < NumSectors; ++i) {
|
2017-03-22 12:18:52 +00:00
|
|
|
qreal angle = i * sectorAngle;
|
|
|
|
qreal x5 = 0.30 * qSin(angle);
|
|
|
|
qreal y5 = 0.30 * qCos(angle);
|
|
|
|
qreal x6 = 0.20 * qSin(angle);
|
|
|
|
qreal y6 = 0.20 * qCos(angle);
|
|
|
|
|
|
|
|
angle += sectorAngle;
|
|
|
|
qreal x7 = 0.20 * qSin(angle);
|
|
|
|
qreal y7 = 0.20 * qCos(angle);
|
|
|
|
qreal x8 = 0.30 * qSin(angle);
|
|
|
|
qreal y8 = 0.30 * qCos(angle);
|
2011-04-27 15:49:30 +00:00
|
|
|
|
|
|
|
quad(x5, y5, x6, y6, x7, y7, x8, y8);
|
|
|
|
|
|
|
|
extrude(x6, y6, x7, y7);
|
|
|
|
extrude(x8, y8, x5, y5);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0;i < vertices.size();i++)
|
|
|
|
vertices[i] *= 2.0f;
|
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
|
|
|
vertices << QVector3D(x1, y1, -0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, -0.05f);
|
|
|
|
vertices << QVector3D(x4, y4, -0.05f);
|
|
|
|
|
|
|
|
vertices << QVector3D(x3, y3, -0.05f);
|
|
|
|
vertices << QVector3D(x4, y4, -0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, -0.05f);
|
|
|
|
|
|
|
|
QVector3D n = QVector3D::normal
|
|
|
|
(QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
|
|
|
|
vertices << QVector3D(x4, y4, 0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, 0.05f);
|
|
|
|
vertices << QVector3D(x1, y1, 0.05f);
|
|
|
|
|
|
|
|
vertices << QVector3D(x2, y2, 0.05f);
|
|
|
|
vertices << QVector3D(x4, y4, 0.05f);
|
|
|
|
vertices << QVector3D(x3, y3, 0.05f);
|
|
|
|
|
|
|
|
n = QVector3D::normal
|
|
|
|
(QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
}
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void Renderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
|
2011-04-27 15:49:30 +00:00
|
|
|
{
|
|
|
|
vertices << QVector3D(x1, y1, +0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, +0.05f);
|
|
|
|
vertices << QVector3D(x1, y1, -0.05f);
|
|
|
|
|
|
|
|
vertices << QVector3D(x2, y2, -0.05f);
|
|
|
|
vertices << QVector3D(x1, y1, -0.05f);
|
|
|
|
vertices << QVector3D(x2, y2, +0.05f);
|
|
|
|
|
|
|
|
QVector3D n = QVector3D::normal
|
|
|
|
(QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f));
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
normals << n;
|
|
|
|
}
|