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
|
2012-06-05 11:21:55 +00:00
|
|
|
|
|
|
|
#include "openglwindow.h"
|
|
|
|
|
2019-09-13 18:43:09 +00:00
|
|
|
#include <QOpenGLContext>
|
|
|
|
#include <QOpenGLPaintDevice>
|
|
|
|
#include <QPainter>
|
2012-06-05 11:21:55 +00:00
|
|
|
|
|
|
|
//! [1]
|
|
|
|
OpenGLWindow::OpenGLWindow(QWindow *parent)
|
|
|
|
: QWindow(parent)
|
|
|
|
{
|
|
|
|
setSurfaceType(QWindow::OpenGLSurface);
|
|
|
|
}
|
|
|
|
//! [1]
|
|
|
|
|
|
|
|
OpenGLWindow::~OpenGLWindow()
|
|
|
|
{
|
|
|
|
delete m_device;
|
|
|
|
}
|
|
|
|
//! [2]
|
|
|
|
void OpenGLWindow::render(QPainter *painter)
|
|
|
|
{
|
|
|
|
Q_UNUSED(painter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLWindow::initialize()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLWindow::render()
|
|
|
|
{
|
|
|
|
if (!m_device)
|
|
|
|
m_device = new QOpenGLPaintDevice;
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
|
2018-11-16 14:06:18 +00:00
|
|
|
m_device->setSize(size() * devicePixelRatio());
|
|
|
|
m_device->setDevicePixelRatio(devicePixelRatio());
|
2012-06-05 11:21:55 +00:00
|
|
|
|
|
|
|
QPainter painter(m_device);
|
|
|
|
render(&painter);
|
|
|
|
}
|
|
|
|
//! [2]
|
|
|
|
|
|
|
|
//! [3]
|
|
|
|
void OpenGLWindow::renderLater()
|
|
|
|
{
|
2016-09-14 13:01:13 +00:00
|
|
|
requestUpdate();
|
2012-06-05 11:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenGLWindow::event(QEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::UpdateRequest:
|
|
|
|
renderNow();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return QWindow::event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLWindow::exposeEvent(QExposeEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
if (isExposed())
|
|
|
|
renderNow();
|
|
|
|
}
|
|
|
|
//! [3]
|
|
|
|
|
|
|
|
//! [4]
|
|
|
|
void OpenGLWindow::renderNow()
|
|
|
|
{
|
|
|
|
if (!isExposed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool needsInitialize = false;
|
|
|
|
|
|
|
|
if (!m_context) {
|
|
|
|
m_context = new QOpenGLContext(this);
|
|
|
|
m_context->setFormat(requestedFormat());
|
|
|
|
m_context->create();
|
|
|
|
|
|
|
|
needsInitialize = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_context->makeCurrent(this);
|
|
|
|
|
|
|
|
if (needsInitialize) {
|
|
|
|
initializeOpenGLFunctions();
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
render();
|
|
|
|
|
|
|
|
m_context->swapBuffers(this);
|
|
|
|
|
|
|
|
if (m_animating)
|
|
|
|
renderLater();
|
|
|
|
}
|
|
|
|
//! [4]
|
|
|
|
|
|
|
|
//! [5]
|
|
|
|
void OpenGLWindow::setAnimating(bool animating)
|
|
|
|
{
|
|
|
|
m_animating = animating;
|
|
|
|
|
|
|
|
if (animating)
|
|
|
|
renderLater();
|
|
|
|
}
|
|
|
|
//! [5]
|
|
|
|
|