From 1e39eb5f701977ac3ea4b6d66f2ce304931a1085 Mon Sep 17 00:00:00 2001 From: Gunnar Sletta Date: Fri, 13 Jun 2014 13:59:57 +0200 Subject: Separate renderer out in "OpenGL under QML" example. The example was promoting very bad practice. Change-Id: Ibb83780ec33e59ee5aabf65a775705dd0da681e6 Reviewed-by: Laszlo Agocs --- .../quick/scenegraph/openglunderqml/squircle.cpp | 74 +++++++++++----------- 1 file changed, 36 insertions(+), 38 deletions(-) (limited to 'examples/quick/scenegraph/openglunderqml/squircle.cpp') diff --git a/examples/quick/scenegraph/openglunderqml/squircle.cpp b/examples/quick/scenegraph/openglunderqml/squircle.cpp index 91d69c90a4..4b892e3761 100644 --- a/examples/quick/scenegraph/openglunderqml/squircle.cpp +++ b/examples/quick/scenegraph/openglunderqml/squircle.cpp @@ -47,9 +47,8 @@ //! [7] Squircle::Squircle() - : m_program(0) - , m_t(0) - , m_thread_t(0) + : m_t(0) + , m_renderer(0) { connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*))); } @@ -71,24 +70,46 @@ void Squircle::setT(qreal t) void Squircle::handleWindowChanged(QQuickWindow *win) { if (win) { -//! [1] - // Connect the beforeRendering signal to our paint function. - // Since this call is executed on the rendering thread it must be - // a Qt::DirectConnection -//! [2] - connect(win, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection); connect(win, SIGNAL(beforeSynchronizing()), this, SLOT(sync()), Qt::DirectConnection); -//! [2] - + connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(cleanup()), Qt::DirectConnection); +//! [1] // If we allow QML to do the clearing, they would clear what we paint // and nothing would show. //! [3] win->setClearBeforeRendering(false); } } +//! [3] + +//! [6] +void Squircle::cleanup() +{ + if (m_renderer) { + delete m_renderer; + m_renderer = 0; + } +} -//! [3] //! [4] -void Squircle::paint() +SquircleRenderer::~SquircleRenderer() +{ + delete m_program; +} +//! [6] + +//! [9] +void Squircle::sync() +{ + if (!m_renderer) { + m_renderer = new SquircleRenderer(); + connect(window(), SIGNAL(beforeRendering()), m_renderer, SLOT(paint()), Qt::DirectConnection); + } + m_renderer->setViewportSize(window()->size() * window()->devicePixelRatio()); + m_renderer->setT(m_t); +} +//! [9] + +//! [4] +void SquircleRenderer::paint() { if (!m_program) { m_program = new QOpenGLShaderProgram(); @@ -112,8 +133,6 @@ void Squircle::paint() m_program->bindAttributeLocation("vertices", 0); m_program->link(); - connect(window()->openglContext(), SIGNAL(aboutToBeDestroyed()), - this, SLOT(cleanup()), Qt::DirectConnection); } //! [4] //! [5] m_program->bind(); @@ -127,12 +146,9 @@ void Squircle::paint() 1, 1 }; m_program->setAttributeArray(0, GL_FLOAT, values, 2); - m_program->setUniformValue("t", (float) m_thread_t); + m_program->setUniformValue("t", (float) m_t); - qreal ratio = window()->devicePixelRatio(); - int w = int(ratio * window()->width()); - int h = int(ratio * window()->height()); - glViewport(0, 0, w, h); + glViewport(0, 0, m_viewportSize.width(), m_viewportSize.height()); glDisable(GL_DEPTH_TEST); @@ -148,21 +164,3 @@ void Squircle::paint() m_program->release(); } //! [5] - -//! [6] -void Squircle::cleanup() -{ - if (m_program) { - delete m_program; - m_program = 0; - } -} -//! [6] - -//! [9] -void Squircle::sync() -{ - m_thread_t = m_t; -} -//! [9] - -- cgit v1.2.3