From 0136252cb2973824b7f97abad1d4c3a4d9301178 Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 19 Dec 2012 11:27:10 +0100 Subject: Polish code of some opengl examples Change-Id: If24ae1845176fc525cf6a239a5079f4802f8df3f Reviewed-by: Friedemann Kleint --- examples/opengl/2dpainting/glwidget.cpp | 3 +- examples/opengl/2dpainting/glwidget.h | 4 -- examples/opengl/2dpainting/helper.cpp | 16 ++++--- examples/opengl/2dpainting/helper.h | 6 +-- examples/opengl/2dpainting/main.cpp | 3 +- examples/opengl/2dpainting/widget.cpp | 3 +- examples/opengl/2dpainting/widget.h | 3 -- examples/opengl/2dpainting/window.cpp | 10 +++-- examples/opengl/2dpainting/window.h | 7 +-- examples/opengl/cube/geometryengine.cpp | 3 +- examples/opengl/cube/geometryengine.h | 8 ++-- examples/opengl/cube/main.cpp | 19 ++++---- examples/opengl/cube/mainwidget.cpp | 65 +++++++++------------------ examples/opengl/cube/mainwidget.h | 24 +++++----- examples/opengl/grabber/glwidget.cpp | 63 ++++++++++++-------------- examples/opengl/grabber/glwidget.h | 2 +- examples/opengl/grabber/main.cpp | 8 ++-- examples/opengl/grabber/mainwindow.cpp | 5 +-- examples/opengl/grabber/mainwindow.h | 4 +- examples/opengl/hellowindow/hellowindow.cpp | 56 ++++++++++------------- examples/opengl/hellowindow/hellowindow.h | 13 ++---- examples/opengl/hellowindow/main.cpp | 20 +++++---- examples/opengl/overpainting/bubble.cpp | 2 - examples/opengl/overpainting/bubble.h | 12 +---- examples/opengl/overpainting/glwidget.cpp | 12 ++--- examples/opengl/overpainting/glwidget.h | 10 +---- examples/opengl/overpainting/main.cpp | 3 +- examples/opengl/overpainting/overpainting.pro | 4 +- examples/opengl/shared/qtlogo.cpp | 49 ++++++++++---------- 29 files changed, 182 insertions(+), 255 deletions(-) (limited to 'examples') diff --git a/examples/opengl/2dpainting/glwidget.cpp b/examples/opengl/2dpainting/glwidget.cpp index 23065a0b42..e1e40ea84c 100644 --- a/examples/opengl/2dpainting/glwidget.cpp +++ b/examples/opengl/2dpainting/glwidget.cpp @@ -38,10 +38,11 @@ ** ****************************************************************************/ -#include #include "glwidget.h" #include "helper.h" +#include + //! [0] GLWidget::GLWidget(Helper *helper, QWidget *parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), helper(helper) diff --git a/examples/opengl/2dpainting/glwidget.h b/examples/opengl/2dpainting/glwidget.h index f8e44f8a3e..3540810983 100644 --- a/examples/opengl/2dpainting/glwidget.h +++ b/examples/opengl/2dpainting/glwidget.h @@ -45,10 +45,6 @@ //! [0] class Helper; -QT_BEGIN_NAMESPACE -class QPaintEvent; -class QWidget; -QT_END_NAMESPACE class GLWidget : public QGLWidget { diff --git a/examples/opengl/2dpainting/helper.cpp b/examples/opengl/2dpainting/helper.cpp index b412dc11dc..168df2aff3 100644 --- a/examples/opengl/2dpainting/helper.cpp +++ b/examples/opengl/2dpainting/helper.cpp @@ -38,9 +38,12 @@ ** ****************************************************************************/ -#include #include "helper.h" +#include +#include +#include + //! [0] Helper::Helper() { @@ -70,14 +73,15 @@ void Helper::paint(QPainter *painter, QPaintEvent *event, int elapsed) painter->setPen(circlePen); painter->rotate(elapsed * 0.030); - qreal r = elapsed/1000.0; + qreal r = elapsed / 1000.0; int n = 30; for (int i = 0; i < n; ++i) { painter->rotate(30); - qreal radius = 0 + 120.0*((i+r)/n); - qreal circleRadius = 1 + ((i+r)/n)*20; + qreal factor = (i + r) / n; + qreal radius = 0 + 120.0 * factor; + qreal circleRadius = 1 + factor * 20; painter->drawEllipse(QRectF(radius, -circleRadius, - circleRadius*2, circleRadius*2)); + circleRadius * 2, circleRadius * 2)); } painter->restore(); //! [2] @@ -85,6 +89,6 @@ void Helper::paint(QPainter *painter, QPaintEvent *event, int elapsed) //! [3] painter->setPen(textPen); painter->setFont(textFont); - painter->drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, "Qt"); + painter->drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, QStringLiteral("Qt")); } //! [3] diff --git a/examples/opengl/2dpainting/helper.h b/examples/opengl/2dpainting/helper.h index 10bb2a5a9c..47f4c96639 100644 --- a/examples/opengl/2dpainting/helper.h +++ b/examples/opengl/2dpainting/helper.h @@ -44,11 +44,7 @@ #include #include #include - -QT_BEGIN_NAMESPACE -class QPainter; -class QPaintEvent; -QT_END_NAMESPACE +#include //! [0] class Helper diff --git a/examples/opengl/2dpainting/main.cpp b/examples/opengl/2dpainting/main.cpp index 3374e57964..903795685f 100644 --- a/examples/opengl/2dpainting/main.cpp +++ b/examples/opengl/2dpainting/main.cpp @@ -38,9 +38,10 @@ ** ****************************************************************************/ -#include #include "window.h" +#include + int main(int argc, char *argv[]) { QApplication app(argc, argv); diff --git a/examples/opengl/2dpainting/widget.cpp b/examples/opengl/2dpainting/widget.cpp index 49ce8b1329..832f37eb36 100644 --- a/examples/opengl/2dpainting/widget.cpp +++ b/examples/opengl/2dpainting/widget.cpp @@ -38,10 +38,11 @@ ** ****************************************************************************/ -#include #include "widget.h" #include "helper.h" +#include +#include //! [0] Widget::Widget(Helper *helper, QWidget *parent) diff --git a/examples/opengl/2dpainting/widget.h b/examples/opengl/2dpainting/widget.h index 7d665b3952..ccf424330c 100644 --- a/examples/opengl/2dpainting/widget.h +++ b/examples/opengl/2dpainting/widget.h @@ -45,9 +45,6 @@ //! [0] class Helper; -QT_BEGIN_NAMESPACE -class QPaintEvent; -QT_END_NAMESPACE class Widget : public QWidget { diff --git a/examples/opengl/2dpainting/window.cpp b/examples/opengl/2dpainting/window.cpp index 700d0caaa8..b6aba2a945 100644 --- a/examples/opengl/2dpainting/window.cpp +++ b/examples/opengl/2dpainting/window.cpp @@ -38,15 +38,19 @@ ** ****************************************************************************/ -#include #include "glwidget.h" #include "widget.h" #include "window.h" +#include +#include +#include + //! [0] Window::Window() - : QWidget() { + setWindowTitle(tr("2D Painting on Native and OpenGL Widgets")); + Widget *native = new Widget(&helper, this); GLWidget *openGL = new GLWidget(&helper, this); QLabel *nativeLabel = new QLabel(tr("Native")); @@ -65,7 +69,5 @@ Window::Window() connect(timer, SIGNAL(timeout()), native, SLOT(animate())); connect(timer, SIGNAL(timeout()), openGL, SLOT(animate())); timer->start(50); - - setWindowTitle(tr("2D Painting on Native and OpenGL Widgets")); } //! [0] diff --git a/examples/opengl/2dpainting/window.h b/examples/opengl/2dpainting/window.h index 263572637e..acded9895f 100644 --- a/examples/opengl/2dpainting/window.h +++ b/examples/opengl/2dpainting/window.h @@ -41,14 +41,9 @@ #ifndef WINDOW_H #define WINDOW_H -#include - #include "helper.h" -QT_BEGIN_NAMESPACE -class QLabel; -class QWidget; -QT_END_NAMESPACE +#include //! [0] class Window : public QWidget diff --git a/examples/opengl/cube/geometryengine.cpp b/examples/opengl/cube/geometryengine.cpp index d8dfeab822..277a6e9cfc 100644 --- a/examples/opengl/cube/geometryengine.cpp +++ b/examples/opengl/cube/geometryengine.cpp @@ -49,14 +49,13 @@ struct VertexData QVector2D texCoord; }; -GeometryEngine::GeometryEngine() : vboIds(new GLuint[2]) +GeometryEngine::GeometryEngine() { } GeometryEngine::~GeometryEngine() { glDeleteBuffers(2, vboIds); - delete[] vboIds; } void GeometryEngine::init() diff --git a/examples/opengl/cube/geometryengine.h b/examples/opengl/cube/geometryengine.h index 2716b05c2b..6a15c3ce03 100644 --- a/examples/opengl/cube/geometryengine.h +++ b/examples/opengl/cube/geometryengine.h @@ -41,8 +41,8 @@ #ifndef GEOMETRYENGINE_H #define GEOMETRYENGINE_H -#include -#include +#include +#include class GeometryEngine : protected QGLFunctions { @@ -51,14 +51,12 @@ public: virtual ~GeometryEngine(); void init(); - void drawCubeGeometry(QGLShaderProgram *program); private: void initCubeGeometry(); - GLuint *vboIds; - + GLuint vboIds[2]; }; #endif // GEOMETRYENGINE_H diff --git a/examples/opengl/cube/main.cpp b/examples/opengl/cube/main.cpp index fbc1d93e4a..fdae1caa16 100644 --- a/examples/opengl/cube/main.cpp +++ b/examples/opengl/cube/main.cpp @@ -47,16 +47,15 @@ int main(int argc, char *argv[]) { - QApplication a(argc, argv); - a.setApplicationName("cube"); - a.setApplicationVersion("0.1"); -#ifndef QT_NO_OPENGL - MainWidget w; - w.resize(640, 480); - w.show(); + QApplication app(argc, argv); + app.setApplicationName("cube"); + app.setApplicationVersion("0.1"); +#ifdef QT_NO_OPENGL + MainWidget widget; + widget.show(); #else - QLabel * notifyLabel = new QLabel("OpenGL Support required"); - notifyLabel->show(); + QLabel note("OpenGL Support required"); + note.show(); #endif - return a.exec(); + return app.exec(); } diff --git a/examples/opengl/cube/mainwidget.cpp b/examples/opengl/cube/mainwidget.cpp index 055dcf49cf..4a42eca159 100644 --- a/examples/opengl/cube/mainwidget.cpp +++ b/examples/opengl/cube/mainwidget.cpp @@ -40,39 +40,26 @@ #include "mainwidget.h" -#include "geometryengine.h" - -#include - -#include #include -#include #include #include MainWidget::MainWidget(QWidget *parent) : QGLWidget(parent), - timer(new QBasicTimer), - program(new QGLShaderProgram), - geometries(new GeometryEngine), angularSpeed(0) { } MainWidget::~MainWidget() { - delete timer; timer = 0; - delete program; program = 0; - delete geometries; geometries = 0; - deleteTexture(texture); } //! [0] void MainWidget::mousePressEvent(QMouseEvent *e) { - // Saving mouse press position + // Save mouse press position mousePressPosition = QVector2D(e->localPos()); } @@ -97,17 +84,15 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *e) //! [0] //! [1] -void MainWidget::timerEvent(QTimerEvent *e) +void MainWidget::timerEvent(QTimerEvent *) { - Q_UNUSED(e); - // Decrease angular speed (friction) angularSpeed *= 0.99; // Stop rotation when speed goes below threshold - if (angularSpeed < 0.01) + if (angularSpeed < 0.01) { angularSpeed = 0.0; - else { + } else { // Update rotation rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation; @@ -120,13 +105,8 @@ void MainWidget::timerEvent(QTimerEvent *e) void MainWidget::initializeGL() { initializeGLFunctions(); - qglClearColor(Qt::black); - - qDebug() << "Initializing shaders..."; initShaders(); - - qDebug() << "Initializing textures..."; initTextures(); //! [2] @@ -137,33 +117,32 @@ void MainWidget::initializeGL() glEnable(GL_CULL_FACE); //! [2] - qDebug() << "Initializing geometries..."; - geometries->init(); + geometries.init(); - // using QBasicTimer because its faster that QTimer - timer->start(12, this); + // Use QBasicTimer because its faster than QTimer + timer.start(12, this); } //! [3] void MainWidget::initShaders() { - // Overriding system locale until shaders are compiled + // Override system locale until shaders are compiled setlocale(LC_NUMERIC, "C"); - // Compiling vertex shader - if (!program->addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl")) + // Compile vertex shader + if (!program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl")) close(); - // Compiling fragment shader - if (!program->addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl")) + // Compile fragment shader + if (!program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl")) close(); - // Linking shader pipeline - if (!program->link()) + // Link shader pipeline + if (!program.link()) close(); - // Binding shader pipeline for use - if (!program->bind()) + // Bind shader pipeline for use + if (!program.bind()) close(); // Restore system locale @@ -174,7 +153,7 @@ void MainWidget::initShaders() //! [4] void MainWidget::initTextures() { - // Loading cube.png + // Load cube.png image glEnable(GL_TEXTURE_2D); texture = bindTexture(QImage(":/cube.png")); @@ -198,7 +177,7 @@ void MainWidget::resizeGL(int w, int h) glViewport(0, 0, w, h); // Calculate aspect ratio - qreal aspect = (qreal)w / ((qreal)h?h:1); + qreal aspect = qreal(w) / qreal(h ? h : 1); // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees const qreal zNear = 3.0, zFar = 7.0, fov = 45.0; @@ -223,12 +202,12 @@ void MainWidget::paintGL() matrix.rotate(rotation); // Set modelview-projection matrix - program->setUniformValue("mvp_matrix", projection * matrix); + program.setUniformValue("mvp_matrix", projection * matrix); //! [6] - // Using texture unit 0 which contains cube.png - program->setUniformValue("texture", 0); + // Use texture unit 0 which contains cube.png + program.setUniformValue("texture", 0); // Draw cube geometry - geometries->drawCubeGeometry(program); + geometries.drawCubeGeometry(&program); } diff --git a/examples/opengl/cube/mainwidget.h b/examples/opengl/cube/mainwidget.h index bd30ea93a0..cebfb30999 100644 --- a/examples/opengl/cube/mainwidget.h +++ b/examples/opengl/cube/mainwidget.h @@ -41,30 +41,26 @@ #ifndef MAINWIDGET_H #define MAINWIDGET_H -#include -#include +#include "geometryengine.h" +#include +#include #include #include #include +#include +#include -QT_BEGIN_NAMESPACE -class QBasicTimer; -class QGLShaderProgram; -QT_END_NAMESPACE class GeometryEngine; class MainWidget : public QGLWidget, protected QGLFunctions { Q_OBJECT + public: explicit MainWidget(QWidget *parent = 0); - virtual ~MainWidget(); - -signals: - -public slots: + ~MainWidget(); protected: void mousePressEvent(QMouseEvent *e); @@ -79,9 +75,9 @@ protected: void initTextures(); private: - QBasicTimer *timer; - QGLShaderProgram *program; - GeometryEngine *geometries; + QBasicTimer timer; + QGLShaderProgram program; + GeometryEngine geometries; GLuint texture; diff --git a/examples/opengl/grabber/glwidget.cpp b/examples/opengl/grabber/glwidget.cpp index 6d8ef0049f..00964c1a0f 100644 --- a/examples/opengl/grabber/glwidget.cpp +++ b/examples/opengl/grabber/glwidget.cpp @@ -38,12 +38,12 @@ ** ****************************************************************************/ -#include -#include +#include "glwidget.h" -#include +#include +#include -#include "glwidget.h" +#include GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) @@ -191,57 +191,52 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, GLdouble r2 = outerRadius + toothSize / 2.0; GLdouble delta = (2.0 * Pi / toothCount) / 4.0; GLdouble z = thickness / 2.0; - int i, j; glShadeModel(GL_FLAT); - for (i = 0; i < 2; ++i) { + for (int i = 0; i < 2; ++i) { GLdouble sign = (i == 0) ? +1.0 : -1.0; glNormal3d(0.0, 0.0, sign); glBegin(GL_QUAD_STRIP); - for (j = 0; j <= toothCount; ++j) { + for (int j = 0; j <= toothCount; ++j) { GLdouble angle = 2.0 * Pi * j / toothCount; - glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); - glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); - glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); - glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), - sign * z); + glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); + glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); + glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); + glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), sign * z); } glEnd(); glBegin(GL_QUADS); - for (j = 0; j < toothCount; ++j) { + for (int j = 0; j < toothCount; ++j) { GLdouble angle = 2.0 * Pi * j / toothCount; - glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); - glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), - sign * z); - glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), - sign * z); - glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), - sign * z); + glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); + glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), sign * z); + glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), sign * z); + glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), sign * z); } glEnd(); } glBegin(GL_QUAD_STRIP); - for (i = 0; i < toothCount; ++i) { - for (j = 0; j < 2; ++j) { - GLdouble angle = 2.0 * Pi * (i + (j / 2.0)) / toothCount; + for (int i = 0; i < toothCount; ++i) { + for (int j = 0; j < 2; ++j) { + GLdouble angle = 2.0 * Pi * (i + j / 2.0) / toothCount; GLdouble s1 = r1; GLdouble s2 = r2; if (j == 1) qSwap(s1, s2); - glNormal3d(cos(angle), sin(angle), 0.0); - glVertex3d(s1 * cos(angle), s1 * sin(angle), +z); - glVertex3d(s1 * cos(angle), s1 * sin(angle), -z); + glNormal3d(cos(angle), sin(angle), 0.0); + glVertex3d(s1 * cos(angle), s1 * sin(angle), +z); + glVertex3d(s1 * cos(angle), s1 * sin(angle), -z); - glNormal3d(s2 * sin(angle + delta) - s1 * sin(angle), + glNormal3d(s2 * sin(angle + delta) - s1 * sin(angle), s1 * cos(angle) - s2 * cos(angle + delta), 0.0); - glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z); - glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z); + glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z); + glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z); } } glVertex3d(r1, 0.0, +z); @@ -251,11 +246,11 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, glShadeModel(GL_SMOOTH); glBegin(GL_QUAD_STRIP); - for (i = 0; i <= toothCount; ++i) { - GLdouble angle = i * 2.0 * Pi / toothCount; - glNormal3d(-cos(angle), -sin(angle), 0.0); - glVertex3d(r0 * cos(angle), r0 * sin(angle), +z); - glVertex3d(r0 * cos(angle), r0 * sin(angle), -z); + for (int i = 0; i <= toothCount; ++i) { + GLdouble angle = i * 2.0 * Pi / toothCount; + glNormal3d(-cos(angle), -sin(angle), 0.0); + glVertex3d(r0 * cos(angle), r0 * sin(angle), +z); + glVertex3d(r0 * cos(angle), r0 * sin(angle), -z); } glEnd(); diff --git a/examples/opengl/grabber/glwidget.h b/examples/opengl/grabber/glwidget.h index 88a97ac8b0..6f48102111 100644 --- a/examples/opengl/grabber/glwidget.h +++ b/examples/opengl/grabber/glwidget.h @@ -94,4 +94,4 @@ private: QPoint lastPos; }; -#endif +#endif // GLWIDGET_H diff --git a/examples/opengl/grabber/main.cpp b/examples/opengl/grabber/main.cpp index f03543e449..be0e7b11cc 100644 --- a/examples/opengl/grabber/main.cpp +++ b/examples/opengl/grabber/main.cpp @@ -38,14 +38,14 @@ ** ****************************************************************************/ -#include - #include "mainwindow.h" +#include + int main(int argc, char *argv[]) { QApplication app(argc, argv); - MainWindow mainWin; - mainWin.show(); + MainWindow window; + window.show(); return app.exec(); } diff --git a/examples/opengl/grabber/mainwindow.cpp b/examples/opengl/grabber/mainwindow.cpp index a559649c55..e521b39dab 100644 --- a/examples/opengl/grabber/mainwindow.cpp +++ b/examples/opengl/grabber/mainwindow.cpp @@ -38,12 +38,11 @@ ** ****************************************************************************/ -#include -#include - #include "glwidget.h" #include "mainwindow.h" +#include + MainWindow::MainWindow() { centralWidget = new QWidget; diff --git a/examples/opengl/grabber/mainwindow.h b/examples/opengl/grabber/mainwindow.h index 6f981c5381..552f902c07 100644 --- a/examples/opengl/grabber/mainwindow.h +++ b/examples/opengl/grabber/mainwindow.h @@ -44,12 +44,12 @@ #include QT_BEGIN_NAMESPACE -class QAction; class QLabel; class QMenu; class QScrollArea; class QSlider; QT_END_NAMESPACE + class GLWidget; class MainWindow : public QMainWindow @@ -91,4 +91,4 @@ private: QAction *aboutQtAct; }; -#endif +#endif // MAINWINDOW_H diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp index b5166abe50..b1140c4f1d 100644 --- a/examples/opengl/hellowindow/hellowindow.cpp +++ b/examples/opengl/hellowindow/hellowindow.cpp @@ -41,9 +41,6 @@ #include "hellowindow.h" #include - -#include - #include Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen) @@ -77,10 +74,8 @@ HelloWindow::HelloWindow(const QSharedPointer &renderer) updateColor(); } -void HelloWindow::exposeEvent(QExposeEvent *event) +void HelloWindow::exposeEvent(QExposeEvent *) { - Q_UNUSED(event); - render(); if (!m_timer) { @@ -109,10 +104,7 @@ void HelloWindow::updateColor() }; m_color = colors[m_colorIndex]; - - m_colorIndex++; - if (m_colorIndex >= int(sizeof(colors) / sizeof(colors[0]))) - m_colorIndex = 0; + m_colorIndex = 1 - m_colorIndex; } void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize) @@ -171,31 +163,29 @@ void Renderer::initialize() glClearColor(0.1f, 0.1f, 0.2f, 1.0f); QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); - const char *vsrc = - "attribute highp vec4 vertex;\n" - "attribute mediump vec3 normal;\n" - "uniform mediump mat4 matrix;\n" - "uniform lowp vec4 sourceColor;\n" - "varying mediump vec4 color;\n" - "void main(void)\n" - "{\n" - " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n" - " float angle = max(dot(normal, toLight), 0.0);\n" - " vec3 col = sourceColor.rgb;\n" - " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n" - " color = clamp(color, 0.0, 1.0);\n" - " gl_Position = matrix * vertex;\n" - "}\n"; - vshader->compileSourceCode(vsrc); + 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;" + "}"); QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); - const char *fsrc = - "varying mediump vec4 color;\n" - "void main(void)\n" - "{\n" - " gl_FragColor = color;\n" - "}\n"; - fshader->compileSourceCode(fsrc); + fshader->compileSourceCode( + "varying mediump vec4 color;" + "void main(void)" + "{" + " gl_FragColor = color;" + "}"); m_program = new QOpenGLShaderProgram(this); m_program->addShader(vshader); diff --git a/examples/opengl/hellowindow/hellowindow.h b/examples/opengl/hellowindow/hellowindow.h index adb85c1a6e..27cae4be7d 100644 --- a/examples/opengl/hellowindow/hellowindow.h +++ b/examples/opengl/hellowindow/hellowindow.h @@ -40,21 +40,15 @@ #include -#include -#include - #include -#include +#include #include - -QT_BEGIN_NAMESPACE -class QOpenGLContext; -class QTimer; -QT_END_NAMESPACE +#include class Renderer : public QObject { Q_OBJECT + public: explicit Renderer(const QSurfaceFormat &format, Renderer *share = 0, QScreen *screen = 0); @@ -89,6 +83,7 @@ private: class HelloWindow : public QWindow { Q_OBJECT + public: explicit HelloWindow(const QSharedPointer &renderer); diff --git a/examples/opengl/hellowindow/main.cpp b/examples/opengl/hellowindow/main.cpp index 343160f755..e63b27d093 100644 --- a/examples/opengl/hellowindow/main.cpp +++ b/examples/opengl/hellowindow/main.cpp @@ -38,21 +38,22 @@ ** ****************************************************************************/ -#include +#include "hellowindow.h" + #include #include + +#include #include #include -#include "hellowindow.h" - -int main(int argc, char **argv) +int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); const bool multipleWindows = QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedOpenGL) - && !QGuiApplication::arguments().contains(QLatin1String("--single")); + && !QGuiApplication::arguments().contains(QStringLiteral("--single")); QScreen *screen = QGuiApplication::primaryScreen(); @@ -71,7 +72,7 @@ int main(int argc, char **argv) HelloWindow *windowA = new HelloWindow(rendererA); windowA->setGeometry(QRect(center, windowSize).translated(-windowSize.width() - delta / 2, 0)); - windowA->setTitle(QLatin1String("Thread A - Context A")); + windowA->setTitle(QStringLiteral("Thread A - Context A")); windowA->setVisible(true); windows.prepend(windowA); @@ -85,13 +86,13 @@ int main(int argc, char **argv) HelloWindow *windowB = new HelloWindow(rendererA); windowB->setGeometry(QRect(center, windowSize).translated(delta / 2, 0)); - windowB->setTitle(QLatin1String("Thread A - Context A")); + windowB->setTitle(QStringLiteral("Thread A - Context A")); windowB->setVisible(true); windows.prepend(windowB); HelloWindow *windowC = new HelloWindow(rendererB); windowC->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, windowSize.height() + delta)); - windowC->setTitle(QLatin1String("Thread B - Context B")); + windowC->setTitle(QStringLiteral("Thread B - Context B")); windowC->setVisible(true); windows.prepend(windowC); @@ -113,7 +114,7 @@ int main(int argc, char **argv) window->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, -windowSize.height() / 2)); QChar id = QChar('B' + i); - window->setTitle(QLatin1String("Thread ") + id + QLatin1String(" - Context ") + id); + window->setTitle(QStringLiteral("Thread ") + id + QStringLiteral(" - Context ") + id); window->setVisible(true); windows.prepend(window); } @@ -128,6 +129,7 @@ int main(int argc, char **argv) for (int i = 0; i < renderThreads.size(); ++i) renderThreads.at(i)->wait(); + qDeleteAll(windows); qDeleteAll(renderThreads); diff --git a/examples/opengl/overpainting/bubble.cpp b/examples/opengl/overpainting/bubble.cpp index 5f0e9710ea..0b3e3b8778 100644 --- a/examples/opengl/overpainting/bubble.cpp +++ b/examples/opengl/overpainting/bubble.cpp @@ -38,8 +38,6 @@ ** ****************************************************************************/ -#include - #include "bubble.h" Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity) diff --git a/examples/opengl/overpainting/bubble.h b/examples/opengl/overpainting/bubble.h index 2a3d27480b..42dedc6e8f 100644 --- a/examples/opengl/overpainting/bubble.h +++ b/examples/opengl/overpainting/bubble.h @@ -41,15 +41,7 @@ #ifndef BUBBLE_H #define BUBBLE_H -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE -class QPainter; -QT_END_NAMESPACE +#include class Bubble { @@ -72,4 +64,4 @@ private: QColor outerColor; }; -#endif +#endif // BUBBLE_H diff --git a/examples/opengl/overpainting/glwidget.cpp b/examples/opengl/overpainting/glwidget.cpp index df546a125b..ea29325b6c 100644 --- a/examples/opengl/overpainting/glwidget.cpp +++ b/examples/opengl/overpainting/glwidget.cpp @@ -38,16 +38,16 @@ ** ****************************************************************************/ -#include -#include -#include - -#include - #include "bubble.h" #include "qtlogo.h" #include "glwidget.h" +#include +#include + +#include +#include + #ifndef GL_MULTISAMPLE #define GL_MULTISAMPLE 0x809D #endif diff --git a/examples/opengl/overpainting/glwidget.h b/examples/opengl/overpainting/glwidget.h index ce2b29d7f3..497d5e1ac4 100644 --- a/examples/opengl/overpainting/glwidget.h +++ b/examples/opengl/overpainting/glwidget.h @@ -41,19 +41,11 @@ #ifndef GLWIDGET_H #define GLWIDGET_H -#include -#include -#include -#include #include #include class Bubble; class QtLogo; -QT_BEGIN_NAMESPACE -class QPaintEvent; -class QWidget; -QT_END_NAMESPACE //! [0] class GLWidget : public QGLWidget @@ -108,4 +100,4 @@ private: //! [4] }; -#endif +#endif // GLWIDGET_H diff --git a/examples/opengl/overpainting/main.cpp b/examples/opengl/overpainting/main.cpp index c4758956ba..2d40a7f76b 100644 --- a/examples/opengl/overpainting/main.cpp +++ b/examples/opengl/overpainting/main.cpp @@ -38,9 +38,10 @@ ** ****************************************************************************/ -#include #include "glwidget.h" +#include + int main(int argc, char *argv[]) { QApplication app(argc, argv); diff --git a/examples/opengl/overpainting/overpainting.pro b/examples/opengl/overpainting/overpainting.pro index 141c8cb7ce..aecc14751b 100644 --- a/examples/opengl/overpainting/overpainting.pro +++ b/examples/opengl/overpainting/overpainting.pro @@ -1,10 +1,12 @@ +QT += opengl widgets + VPATH += ../shared INCLUDEPATH += ../shared -QT += opengl widgets HEADERS = bubble.h \ glwidget.h \ qtlogo.h + SOURCES = bubble.cpp \ glwidget.cpp \ main.cpp \ diff --git a/examples/opengl/shared/qtlogo.cpp b/examples/opengl/shared/qtlogo.cpp index fa3d91c840..e49ed7e67c 100644 --- a/examples/opengl/shared/qtlogo.cpp +++ b/examples/opengl/shared/qtlogo.cpp @@ -38,14 +38,14 @@ ** ****************************************************************************/ +#include "qtlogo.h" + #include #include #include #include -#include "qtlogo.h" - static const qreal tee_height = 0.311126; static const qreal cross_width = 0.25; static const qreal bar_thickness = 0.113137; @@ -117,7 +117,7 @@ void Geometry::finalize() void Geometry::appendSmooth(const QVector3D &a, const QVector3D &n, int from) { - // Smooth normals are acheived by averaging the normals for faces meeting + // Smooth normals are achieved by averaging the normals for faces meeting // at a point. First find the point in geometry already generated // (working backwards, since most often the points shared are between faces // recently added). @@ -125,28 +125,27 @@ void Geometry::appendSmooth(const QVector3D &a, const QVector3D &n, int from) for ( ; v >= from; --v) if (qFuzzyCompare(vertices[v], a)) break; - if (v < from) - { - // The vert was not found so add it as a new one, and initialize + + if (v < from) { + // The vertex was not found so add it as a new one, and initialize // its corresponding normal v = vertices.count(); vertices.append(a); normals.append(n); - } - else - { + } else { // Vert found, accumulate normals into corresponding normal slot. // Must call finalize once finished accumulating normals normals[v] += n; } - // In both cases (found or not) reference the vert via its index + + // In both cases (found or not) reference the vertex via its index faces.append(v); } void Geometry::appendFaceted(const QVector3D &a, const QVector3D &n) { - // Faceted normals are achieved by duplicating the vert for every - // normal, so that faces meeting at a vert get a sharp edge. + // Faceted normals are achieved by duplicating the vertex for every + // normal, so that faces meeting at a vertex get a sharp edge. int v = vertices.count(); vertices.append(a); normals.append(n); @@ -189,32 +188,29 @@ void Patch::draw() const void Patch::addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n) { QVector3D norm = n.isNull() ? QVector3D::normal(a, b, c) : n; - if (sm == Smooth) - { + + if (sm == Smooth) { geom->appendSmooth(a, norm, initv); geom->appendSmooth(b, norm, initv); geom->appendSmooth(c, norm, initv); - } - else - { + } else { geom->appendFaceted(a, norm); geom->appendFaceted(b, norm); geom->appendFaceted(c, norm); } + count += 3; } void Patch::addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &d) { QVector3D norm = QVector3D::normal(a, b, c); - if (sm == Smooth) - { + + if (sm == Smooth) { addTri(a, b, c, norm); addTri(a, c, d, norm); - } - else - { - // If faceted share the two common verts + } else { + // If faceted share the two common vertices addTri(a, b, c, norm); int k = geom->vertices.count(); geom->appendSmooth(a, norm, k); @@ -224,9 +220,9 @@ void Patch::addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, } } -static inline QVector extrude(const QVector &verts, qreal depth) +static inline QVector extrude(const QVector &vertices, qreal depth) { - QVector extr = verts; + QVector extr = vertices; for (int v = 0; v < extr.count(); ++v) extr[v].setZ(extr[v].z() - depth); return extr; @@ -240,6 +236,7 @@ public: for (int i = 0; i < parts.count(); ++i) parts[i]->translate(t); } + void rotate(qreal deg, QVector3D axis) { for (int i = 0; i < parts.count(); ++i) @@ -248,7 +245,7 @@ public: // No special Rectoid destructor - the parts are fetched out of this member // variable, and destroyed by the new owner - QList parts; + QList parts; }; class RectPrism : public Rectoid -- cgit v1.2.3