| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include <QOpenGLWindow> |
| 5 |
#include <QScreen> |
| 6 |
#include <QPainter> |
| 7 |
#include <QPainterPath> |
| 8 |
#include <QGuiApplication> |
| 9 |
#include <QMatrix4x4> |
| 10 |
#include <QStaticText> |
| 11 |
#include <QKeyEvent> |
| 12 |
|
| 13 |
#include "background_renderer.h" |
| 14 |
|
| 15 |
static QPainterPath painterPathForTriangle() |
| 16 |
{ |
| 17 |
static const QPointF bottomLeft(-1.0, -1.0); |
| 18 |
static const QPointF top(0.0, 1.0); |
| 19 |
static const QPointF bottomRight(1.0, -1.0); |
| 20 |
|
| 21 |
QPainterPath path(bottomLeft); |
| 22 |
path.lineTo(top); |
| 23 |
path.lineTo(bottomRight); |
| 24 |
path.closeSubpath(); |
| 25 |
return path; |
| 26 |
} |
| 27 |
|
| 28 |
class OpenGLWindow : public QOpenGLWindow |
| 29 |
{ |
| 30 |
Q_OBJECT |
| 31 |
|
| 32 |
public: |
| 33 |
OpenGLWindow(); |
| 34 |
|
| 35 |
protected: |
| 36 |
void paintGL() override; |
| 37 |
void resizeGL(int w, int h) override; |
| 38 |
void keyPressEvent(QKeyEvent *e) override; |
| 39 |
|
| 40 |
private: |
| 41 |
void setAnimating(bool enabled); |
| 42 |
|
| 43 |
QMatrix4x4 m_window_normalised_matrix; |
| 44 |
QMatrix4x4 m_window_painter_matrix; |
| 45 |
QMatrix4x4 m_projection; |
| 46 |
QMatrix4x4 m_view; |
| 47 |
QMatrix4x4 m_model_triangle; |
| 48 |
QMatrix4x4 m_model_text; |
| 49 |
|
| 50 |
FragmentToy m_fragment_toy; |
| 51 |
QStaticText m_text_layout; |
| 52 |
bool m_animate; |
| 53 |
}; |
| 54 |
|
| 55 |
// Use NoPartialUpdate. This means that all the rendering goes directly to |
| 56 |
// the window surface, no additional framebuffer object stands in the |
| 57 |
// middle. This is fine since we will clear the entire framebuffer on each |
| 58 |
// paint. Under the hood this means that the behavior is equivalent to the |
| 59 |
// manual makeCurrent - perform OpenGL calls - swapBuffers loop that is |
| 60 |
// typical in pure QWindow-based applications. |
| 61 |
OpenGLWindow::OpenGLWindow() |
| 62 |
: QOpenGLWindow(QOpenGLWindow::NoPartialUpdate) |
| 63 |
, m_fragment_toy("./background.frag") |
| 64 |
, m_text_layout("The triangle and this text is rendered with QPainter") |
| 65 |
, m_animate(true) |
| 66 |
{ |
| 67 |
setGeometry(300, 300, 500, 500); |
| 68 |
|
| 69 |
m_view.lookAt(QVector3D(3,1,1), |
| 70 |
QVector3D(0,0,0), |
| 71 |
QVector3D(0,1,0)); |
| 72 |
|
| 73 |
setAnimating(m_animate); |
| 74 |
} |
| 75 |
|
| 76 |
void OpenGLWindow::paintGL() |
| 77 |
{ |
| 78 |
m_fragment_toy.draw(size()); |
| 79 |
|
| 80 |
QPainter p(this); |
| 81 |
p.setWorldTransform(m_window_normalised_matrix.toTransform()); |
| 82 |
|
| 83 |
QMatrix4x4 mvp = m_projection * m_view * m_model_triangle; |
| 84 |
p.setTransform(mvp.toTransform(), true); |
| 85 |
|
| 86 |
p.fillPath(painterPathForTriangle(), QBrush(QGradient(QGradient::NightFade))); |
| 87 |
|
| 88 |
QTransform text_transform = (m_window_painter_matrix * m_view * m_model_text).toTransform(); |
| 89 |
p.setTransform(text_transform, false); |
| 90 |
p.setPen(QPen(Qt::black)); |
| 91 |
m_text_layout.prepare(text_transform); |
| 92 |
qreal x = - (m_text_layout.size().width() / 2); |
| 93 |
qreal y = 0; |
| 94 |
p.drawStaticText(x, y, m_text_layout); |
| 95 |
|
| 96 |
m_model_triangle.rotate(-1, 0, 1, 0); |
| 97 |
m_model_text.rotate(1, 0, 1, 0); |
| 98 |
} |
| 99 |
|
| 100 |
void OpenGLWindow::resizeGL(int w, int h) |
| 101 |
{ |
| 102 |
m_window_normalised_matrix.setToIdentity(); |
| 103 |
m_window_normalised_matrix.translate(w / 2.0, h / 2.0); |
| 104 |
m_window_normalised_matrix.scale(w / 2.0, -h / 2.0); |
| 105 |
|
| 106 |
m_window_painter_matrix.setToIdentity(); |
| 107 |
m_window_painter_matrix.translate(w / 2.0, h / 2.0); |
| 108 |
|
| 109 |
m_text_layout.setTextWidth(std::max(w * 0.2, 80.0)); |
| 110 |
|
| 111 |
m_projection.setToIdentity(); |
| 112 |
m_projection.perspective(45.f, qreal(w) / qreal(h), 0.1f, 100.f); |
| 113 |
} |
| 114 |
|
| 115 |
void OpenGLWindow::keyPressEvent(QKeyEvent *e) |
| 116 |
{ |
| 117 |
if (e->key() == Qt::Key_P) { // pause |
| 118 |
m_animate = !m_animate; |
| 119 |
setAnimating(m_animate); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void OpenGLWindow::setAnimating(bool enabled) |
| 124 |
{ |
| 125 |
if (enabled) { |
| 126 |
// Animate continuously, throttled by the blocking swapBuffers() call the |
| 127 |
// QOpenGLWindow internally executes after each paint. Once that is done |
| 128 |
// (frameSwapped signal is emitted), we schedule a new update. This |
| 129 |
// obviously assumes that the swap interval (see |
| 130 |
// QSurfaceFormat::setSwapInterval()) is non-zero. |
| 131 |
connect(this, &QOpenGLWindow::frameSwapped, |
| 132 |
this, QOverload<>::of(&QPaintDeviceWindow::update)); |
| 133 |
update(); |
| 134 |
} else { |
| 135 |
disconnect(this, &QOpenGLWindow::frameSwapped, |
| 136 |
this, QOverload<>::of(&QPaintDeviceWindow::update)); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
int main(int argc, char **argv) |
| 141 |
{ |
| 142 |
QGuiApplication app(argc, argv); |
| 143 |
|
| 144 |
OpenGLWindow window; |
| 145 |
QSurfaceFormat fmt; |
| 146 |
fmt.setDepthBufferSize(24); |
| 147 |
fmt.setStencilBufferSize(8); |
| 148 |
window.setFormat(fmt); |
| 149 |
window.show(); |
| 150 |
|
| 151 |
return app.exec(); |
| 152 |
} |
| 153 |
|
| 154 |
#include "main.moc" |