Cleanup QtOpenGL examples

Cleanup the OpenGL examples
 - use nullptr (clang-tidy)
 - use member-initialization
 - avoid redundant checks for != nullptr when deleting a pointer

Change-Id: I3e4702690ed79e71c3e952d51ceef83b907b45b7
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-09-13 20:43:09 +02:00
parent fbda189e08
commit b0042601ed
26 changed files with 132 additions and 225 deletions

View File

@ -50,36 +50,30 @@
#include "openglwindow.h"
#include <QtGui/QGuiApplication>
#include <QtGui/QMatrix4x4>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QScreen>
#include <QGuiApplication>
#include <QMatrix4x4>
#include <QOpenGLShaderProgram>
#include <QScreen>
#include <QtMath>
#include <QtCore/qmath.h>
//! [1]
class TriangleWindow : public OpenGLWindow
{
public:
TriangleWindow();
using OpenGLWindow::OpenGLWindow;
void initialize() override;
void render() override;
private:
GLuint m_posAttr;
GLuint m_colAttr;
GLuint m_matrixUniform;
GLuint m_posAttr = 0;
GLuint m_colAttr = 0;
GLuint m_matrixUniform = 0;
QOpenGLShaderProgram *m_program;
int m_frame;
QOpenGLShaderProgram *m_program = nullptr;
int m_frame = 0;
};
TriangleWindow::TriangleWindow()
: m_program(0)
, m_frame(0)
{
}
//! [1]
//! [2]
@ -144,7 +138,7 @@ void TriangleWindow::render()
m_program->bind();
QMatrix4x4 matrix;
matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
matrix.translate(0, 0, -2);
matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0);

View File

@ -50,18 +50,13 @@
#include "openglwindow.h"
#include <QtCore/QCoreApplication>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLPaintDevice>
#include <QtGui/QPainter>
#include <QOpenGLContext>
#include <QOpenGLPaintDevice>
#include <QPainter>
//! [1]
OpenGLWindow::OpenGLWindow(QWindow *parent)
: QWindow(parent)
, m_animating(false)
, m_context(0)
, m_device(0)
{
setSurfaceType(QWindow::OpenGLSurface);
}

View File

@ -48,8 +48,8 @@
**
****************************************************************************/
#include <QtGui/QWindow>
#include <QtGui/QOpenGLFunctions>
#include <QWindow>
#include <QOpenGLFunctions>
QT_BEGIN_NAMESPACE
class QPainter;
@ -62,7 +62,7 @@ class OpenGLWindow : public QWindow, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit OpenGLWindow(QWindow *parent = 0);
explicit OpenGLWindow(QWindow *parent = nullptr);
~OpenGLWindow();
virtual void render(QPainter *painter);
@ -82,10 +82,10 @@ protected:
void exposeEvent(QExposeEvent *event) override;
private:
bool m_animating;
bool m_animating = false;
QOpenGLContext *m_context;
QOpenGLPaintDevice *m_device;
QOpenGLContext *m_context = nullptr;
QOpenGLPaintDevice *m_device = nullptr;
};
//! [1]

View File

@ -73,15 +73,6 @@
#endif
GLWindow::GLWindow()
: m_texImageInput(0),
m_texImageTmp(0),
m_texImageProcessed(0),
m_shaderDisplay(0),
m_shaderComputeV(0),
m_shaderComputeH(0),
m_blurRadius(0.0f),
m_animate(true),
m_vao(0)
{
const float animationStart = 0.0;
const float animationEnd = 10.0;
@ -324,27 +315,18 @@ void GLWindow::initializeGL()
<< ((ctx->format().renderableType() == QSurfaceFormat::OpenGLES) ? (" GLES") : (" GL"))
<< " context";
if (m_texImageInput) {
delete m_texImageInput;
m_texImageInput = 0;
}
QImage img(":/Qt-logo-medium.png");
Q_ASSERT(!img.isNull());
delete m_texImageInput;
m_texImageInput = new QOpenGLTexture(img.convertToFormat(QImage::Format_RGBA8888).mirrored());
if (m_texImageTmp) {
delete m_texImageTmp;
m_texImageTmp = 0;
}
delete m_texImageTmp;
m_texImageTmp = new QOpenGLTexture(QOpenGLTexture::Target2D);
m_texImageTmp->setFormat(m_texImageInput->format());
m_texImageTmp->setSize(m_texImageInput->width(),m_texImageInput->height());
m_texImageTmp->allocateStorage(QOpenGLTexture::RGBA,QOpenGLTexture::UInt8); // WTF?
if (m_texImageProcessed) {
delete m_texImageProcessed;
m_texImageProcessed = 0;
}
delete m_texImageProcessed;
m_texImageProcessed = new QOpenGLTexture(QOpenGLTexture::Target2D);
m_texImageProcessed->setFormat(m_texImageInput->format());
m_texImageProcessed->setSize(m_texImageInput->width(),m_texImageInput->height());
@ -354,10 +336,7 @@ void GLWindow::initializeGL()
m_texImageProcessed->setMinificationFilter(QOpenGLTexture::Linear);
m_texImageProcessed->setWrapMode(QOpenGLTexture::ClampToEdge);
if (m_shaderDisplay) {
delete m_shaderDisplay;
m_shaderDisplay = 0;
}
delete m_shaderDisplay;
m_shaderDisplay = new QOpenGLShaderProgram;
// Prepend the correct version directive to the sources. The rest is the
// same, thanks to the common GLSL syntax.
@ -365,18 +344,12 @@ void GLWindow::initializeGL()
m_shaderDisplay->addShaderFromSourceCode(QOpenGLShader::Fragment, versionedShaderCode(fsDisplaySource));
m_shaderDisplay->link();
if (m_shaderComputeV) {
delete m_shaderComputeV;
m_shaderComputeV = 0;
}
delete m_shaderComputeV;
m_shaderComputeV = new QOpenGLShaderProgram;
m_shaderComputeV->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceV));
m_shaderComputeV->link();
if (m_shaderComputeH) {
delete m_shaderComputeH;
m_shaderComputeH = 0;
}
delete m_shaderComputeH;
m_shaderComputeH = new QOpenGLShaderProgram;
m_shaderComputeH->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceH));
m_shaderComputeH->link();

View File

@ -90,21 +90,21 @@ protected:
void setAnimating(bool animate);
private:
QPropertyAnimation *m_animationForward;
QPropertyAnimation *m_animationBackward;
QPropertyAnimation *m_animationForward = nullptr;
QPropertyAnimation *m_animationBackward = nullptr;
QSequentialAnimationGroup *m_animationGroup;
QOpenGLTexture *m_texImageInput;
QOpenGLTexture *m_texImageTmp;
QOpenGLTexture *m_texImageProcessed;
QOpenGLShaderProgram *m_shaderDisplay;
QOpenGLShaderProgram *m_shaderComputeV;
QOpenGLShaderProgram *m_shaderComputeH;
QOpenGLTexture *m_texImageInput = nullptr;
QOpenGLTexture *m_texImageTmp = nullptr;
QOpenGLTexture *m_texImageProcessed = nullptr;
QOpenGLShaderProgram *m_shaderDisplay = nullptr;
QOpenGLShaderProgram *m_shaderComputeV = nullptr;
QOpenGLShaderProgram *m_shaderComputeH = nullptr;
QMatrix4x4 m_proj;
QSizeF m_quadSize;
int m_blurRadius;
bool m_animate;
QOpenGLVertexArrayObject *m_vao;
int m_blurRadius = 0;
bool m_animate = true;
QOpenGLVertexArrayObject *m_vao = nullptr;
};
#endif

View File

@ -56,7 +56,7 @@
#include <QOpenGLFunctions>
RenderWindow::RenderWindow(const QSurfaceFormat &format)
: m_context(0),
: m_context(nullptr),
m_initialized(false),
m_forceGLSL110(false),
m_angle(0.0f)
@ -67,7 +67,7 @@ RenderWindow::RenderWindow(const QSurfaceFormat &format)
m_context->setFormat(requestedFormat());
if (!m_context->create()) {
delete m_context;
m_context = 0;
m_context = nullptr;
}
}

View File

@ -64,7 +64,7 @@ class Widget : public QWidget
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
explicit Widget(QWidget *parent = nullptr);
private slots:
void start();

View File

@ -174,6 +174,6 @@ void GeometryEngine::drawCubeGeometry(QOpenGLShaderProgram *program)
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, 0);
glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, nullptr);
}
//! [2]

View File

@ -52,15 +52,7 @@
#include <QMouseEvent>
#include <math.h>
MainWidget::MainWidget(QWidget *parent) :
QOpenGLWidget(parent),
geometries(0),
texture(0),
angularSpeed(0)
{
}
#include <cmath>
MainWidget::~MainWidget()
{

View File

@ -69,7 +69,7 @@ class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
using QOpenGLWidget::QOpenGLWidget;
~MainWidget();
protected:
@ -87,15 +87,15 @@ protected:
private:
QBasicTimer timer;
QOpenGLShaderProgram program;
GeometryEngine *geometries;
GeometryEngine *geometries = nullptr;
QOpenGLTexture *texture;
QOpenGLTexture *texture = nullptr;
QMatrix4x4 projection;
QVector2D mousePressPosition;
QVector3D rotationAxis;
qreal angularSpeed;
qreal angularSpeed = 0;
QQuaternion rotation;
};

View File

@ -57,11 +57,7 @@
bool GLWidget::m_transparent = false;
GLWidget::GLWidget(QWidget *parent)
: QOpenGLWidget(parent),
m_xRot(0),
m_yRot(0),
m_zRot(0),
m_program(0)
: QOpenGLWidget(parent)
{
m_core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile;
// --transparent causes the clear color to be transparent. Therefore, on systems that
@ -133,7 +129,7 @@ void GLWidget::cleanup()
makeCurrent();
m_logoVbo.destroy();
delete m_program;
m_program = 0;
m_program = nullptr;
doneCurrent();
}
@ -250,8 +246,10 @@ void GLWidget::setupVertexAttribs()
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glEnableVertexAttribArray(0);
f->glEnableVertexAttribArray(1);
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0);
f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), reinterpret_cast<void *>(3 * sizeof(GLfloat)));
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
nullptr);
f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
reinterpret_cast<void *>(3 * sizeof(GLfloat)));
m_logoVbo.release();
}

View File

@ -65,7 +65,7 @@ class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
Q_OBJECT
public:
GLWidget(QWidget *parent = 0);
GLWidget(QWidget *parent = nullptr);
~GLWidget();
static bool isTransparent() { return m_transparent; }
@ -96,18 +96,18 @@ private:
void setupVertexAttribs();
bool m_core;
int m_xRot;
int m_yRot;
int m_zRot;
int m_xRot = 0;
int m_yRot = 0;
int m_zRot = 0;
QPoint m_lastPos;
Logo m_logo;
QOpenGLVertexArrayObject m_vao;
QOpenGLBuffer m_logoVbo;
QOpenGLShaderProgram *m_program;
int m_projMatrixLoc;
int m_mvMatrixLoc;
int m_normalMatrixLoc;
int m_lightPosLoc;
QOpenGLShaderProgram *m_program = nullptr;
int m_projMatrixLoc = 0;
int m_mvMatrixLoc = 0;
int m_normalMatrixLoc = 0;
int m_lightPosLoc = 0;
QMatrix4x4 m_proj;
QMatrix4x4 m_camera;
QMatrix4x4 m_world;

View File

@ -52,7 +52,6 @@
#include <qmath.h>
Logo::Logo()
: m_count(0)
{
m_data.resize(2500 * 6);

View File

@ -69,7 +69,7 @@ private:
void add(const QVector3D &v, const QVector3D &n);
QVector<GLfloat> m_data;
int m_count;
int m_count = 0;
};
#endif // LOGO_H

View File

@ -72,5 +72,6 @@ void MainWindow::onAddNew()
if (!centralWidget())
setCentralWidget(new Window(this));
else
QMessageBox::information(0, tr("Cannot add new window"), tr("Already occupied. Undock first."));
QMessageBox::information(nullptr, tr("Cannot add new window"),
tr("Already occupied. Undock first."));
}

View File

@ -121,7 +121,7 @@ void Window::keyPressEvent(QKeyEvent *e)
void Window::dockUndock()
{
if (parent()) {
setParent(0);
setParent(nullptr);
setAttribute(Qt::WA_DeleteOnClose);
move(QApplication::desktop()->width() / 2 - width() / 2,
QApplication::desktop()->height() / 2 - height() / 2);
@ -134,10 +134,12 @@ void Window::dockUndock()
dockBtn->setText(tr("Undock"));
mainWindow->setCentralWidget(this);
} else {
QMessageBox::information(0, tr("Cannot dock"), tr("Main window already closed"));
QMessageBox::information(nullptr, tr("Cannot dock"),
tr("Main window already closed"));
}
} else {
QMessageBox::information(0, tr("Cannot dock"), tr("Main window already occupied"));
QMessageBox::information(nullptr, tr("Cannot dock"),
tr("Main window already occupied"));
}
}
}

View File

@ -57,19 +57,10 @@
#include <QOpenGLVertexArrayObject>
#include <QOpenGLExtraFunctions>
#include <QPropertyAnimation>
#include <QPauseAnimation>
#include <QSequentialAnimationGroup>
#include <QTimer>
GLWindow::GLWindow()
: m_texture(0),
m_program(0),
m_vbo(0),
m_vao(0),
m_target(0, 0, -1),
m_uniformsDirty(true),
m_r(0),
m_r2(0)
{
m_world.setToIdentity();
m_world.translate(0, 0, -1);
@ -197,18 +188,12 @@ void GLWindow::initializeGL()
{
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
if (m_texture) {
delete m_texture;
m_texture = 0;
}
QImage img(":/qtlogo.png");
Q_ASSERT(!img.isNull());
delete m_texture;
m_texture = new QOpenGLTexture(img.scaled(32, 36).mirrored());
if (m_program) {
delete m_program;
m_program = 0;
}
delete m_program;
m_program = new QOpenGLShaderProgram;
// Prepend the correct version directive to the sources. The rest is the
// same, thanks to the common GLSL syntax.
@ -223,26 +208,21 @@ void GLWindow::initializeGL()
m_lightPosLoc = m_program->uniformLocation("lightPos");
// Create a VAO. Not strictly required for ES 3, but it is for plain OpenGL.
if (m_vao) {
delete m_vao;
m_vao = 0;
}
delete m_vao;
m_vao = new QOpenGLVertexArrayObject;
if (m_vao->create())
m_vao->bind();
if (m_vbo) {
delete m_vbo;
m_vbo = 0;
}
m_program->bind();
delete m_vbo;
m_vbo = new QOpenGLBuffer;
m_vbo->create();
m_vbo->bind();
m_vbo->allocate(m_logo.constData(), m_logo.count() * sizeof(GLfloat));
f->glEnableVertexAttribArray(0);
f->glEnableVertexAttribArray(1);
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0);
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
nullptr);
f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
reinterpret_cast<void *>(3 * sizeof(GLfloat)));
m_vbo->release();

View File

@ -90,23 +90,23 @@ public:
private slots:
void startSecondStage();
private:
QOpenGLTexture *m_texture;
QOpenGLShaderProgram *m_program;
QOpenGLBuffer *m_vbo;
QOpenGLVertexArrayObject *m_vao;
QOpenGLTexture *m_texture = nullptr;
QOpenGLShaderProgram *m_program = nullptr;
QOpenGLBuffer *m_vbo = nullptr;
QOpenGLVertexArrayObject *m_vao = nullptr;
Logo m_logo;
int m_projMatrixLoc;
int m_camMatrixLoc;
int m_worldMatrixLoc;
int m_myMatrixLoc;
int m_lightPosLoc;
int m_projMatrixLoc = 0;
int m_camMatrixLoc = 0;
int m_worldMatrixLoc = 0;
int m_myMatrixLoc = 0;
int m_lightPosLoc = 0;
QMatrix4x4 m_proj;
QMatrix4x4 m_world;
QVector3D m_eye;
QVector3D m_target;
bool m_uniformsDirty;
float m_r;
float m_r2;
QVector3D m_target = {0, 0, -1};
bool m_uniformsDirty = true;
float m_r = 0;
float m_r2 = 0;
};
#endif

View File

@ -57,15 +57,13 @@ Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity)
{
innerColor = randomColor();
outerColor = randomColor();
cache = 0;
updateBrush();
}
//! [0]
void Bubble::updateCache()
{
if (cache)
delete cache;
delete cache;
cache = new QImage(qRound(radius * 2 + 2), qRound(radius * 2 + 2), QImage::Format_ARGB32_Premultiplied);
cache->fill(0x00000000);
QPainter p(cache);
@ -80,8 +78,7 @@ void Bubble::updateCache()
Bubble::~Bubble()
{
if (cache)
delete cache;
delete cache;
}
void Bubble::updateBrush()

View File

@ -80,7 +80,7 @@ private:
qreal radius;
QColor innerColor;
QColor outerColor;
QImage *cache;
QImage *cache = nullptr;
};
#endif

View File

@ -68,14 +68,6 @@ const int bubbleNum = 8;
GLWidget::GLWidget(MainWindow *mw, bool button, const QColor &background)
: m_mainWindow(mw),
m_showBubbles(true),
m_qtLogo(true),
m_frames(0),
m_program1(0),
m_program2(0),
m_texture(0),
m_transparent(false),
m_btn(0),
m_hasButton(button),
m_background(background)
{

View File

@ -98,34 +98,34 @@ private:
void extrude(qreal x1, qreal y1, qreal x2, qreal y2);
MainWindow *m_mainWindow;
qreal m_fAngle;
qreal m_fScale;
bool m_showBubbles;
qreal m_fAngle = 0;
qreal m_fScale = 1;
bool m_showBubbles = true;
QVector<QVector3D> m_vertices;
QVector<QVector3D> m_normals;
bool m_qtLogo;
QList<Bubble *> m_bubbles;
int m_frames;
bool m_qtLogo = true;
QVector<Bubble *> m_bubbles;
int m_frames = 0;
QElapsedTimer m_time;
QOpenGLShader *m_vshader1;
QOpenGLShader *m_fshader1;
QOpenGLShader *m_vshader2;
QOpenGLShader *m_fshader2;
QOpenGLShaderProgram *m_program1;
QOpenGLShaderProgram *m_program2;
QOpenGLTexture *m_texture;
QOpenGLShader *m_vshader1 = nullptr;
QOpenGLShader *m_fshader1 = nullptr;
QOpenGLShader *m_vshader2 = nullptr;
QOpenGLShader *m_fshader2 = nullptr;
QOpenGLShaderProgram *m_program1 = nullptr;
QOpenGLShaderProgram *m_program2 = nullptr;
QOpenGLTexture *m_texture = nullptr;
QOpenGLBuffer m_vbo1;
QOpenGLBuffer m_vbo2;
int m_vertexAttr1;
int m_normalAttr1;
int m_matrixUniform1;
int m_vertexAttr2;
int m_normalAttr2;
int m_texCoordAttr2;
int m_matrixUniform2;
int m_textureUniform2;
bool m_transparent;
QPushButton *m_btn;
int m_vertexAttr1 = 0;
int m_normalAttr1 = 0;
int m_matrixUniform1 = 0;
int m_vertexAttr2 = 0;
int m_normalAttr2 = 0;
int m_texCoordAttr2 = 0;
int m_matrixUniform2 = 0;
int m_textureUniform2 = 0;
bool m_transparent = false;
QPushButton *m_btn = nullptr;
bool m_hasButton;
QColor m_background;
};

View File

@ -53,17 +53,6 @@
#include <QOpenGLTexture>
#include <QMouseEvent>
GLWidget::GLWidget(QWidget *parent)
: QOpenGLWidget(parent),
clearColor(Qt::black),
xRot(0),
yRot(0),
zRot(0),
program(0)
{
memset(textures, 0, sizeof(textures));
}
GLWidget::~GLWidget()
{
makeCurrent();

View File

@ -63,7 +63,7 @@ class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
using QOpenGLWidget::QOpenGLWidget;
~GLWidget();
QSize minimumSizeHint() const override;
@ -85,13 +85,13 @@ protected:
private:
void makeObject();
QColor clearColor;
QColor clearColor = Qt::black;
QPoint lastPos;
int xRot;
int yRot;
int zRot;
QOpenGLTexture *textures[6];
QOpenGLShaderProgram *program;
int xRot = 0;
int yRot = 0;
int zRot = 0;
QOpenGLTexture *textures[6] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
QOpenGLShaderProgram *program = nullptr;
QOpenGLBuffer vbo;
};

View File

@ -115,12 +115,7 @@ void GLWidget::grabContext()
m_renderer->unlockRenderer();
}
Renderer::Renderer(GLWidget *w)
: m_inited(false),
m_glwidget(w),
m_exiting(false)
{
}
Renderer::Renderer(GLWidget *w) : m_glwidget(w) {}
void Renderer::paintQtLogo()
{

View File

@ -88,29 +88,29 @@ private:
void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4);
void extrude(qreal x1, qreal y1, qreal x2, qreal y2);
bool m_inited;
qreal m_fAngle;
qreal m_fScale;
bool m_inited = false;
qreal m_fAngle = 0;
qreal m_fScale = 1;
QVector<QVector3D> vertices;
QVector<QVector3D> normals;
QOpenGLShaderProgram program;
QOpenGLBuffer vbo;
int vertexAttr;
int normalAttr;
int matrixUniform;
GLWidget *m_glwidget;
int vertexAttr = 0;
int normalAttr = 0;
int matrixUniform = 0;
GLWidget *m_glwidget = nullptr;
QMutex m_renderMutex;
QElapsedTimer m_elapsed;
QMutex m_grabMutex;
QWaitCondition m_grabCond;
bool m_exiting;
bool m_exiting = false;
};
class GLWidget : public QOpenGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
explicit GLWidget(QWidget *parent = nullptr);
~GLWidget();
protected: