diff options
author | hjk <qthjk@ovi.com> | 2012-12-19 11:27:10 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-12-28 20:44:55 +0100 |
commit | 0136252cb2973824b7f97abad1d4c3a4d9301178 (patch) | |
tree | 2e03b3946253216d6ce3a010d4155c36f3e834b7 /examples/opengl/cube | |
parent | 4eac2c4728da85a5cdf91ec25170b3417f7deb68 (diff) |
Polish code of some opengl examples
Change-Id: If24ae1845176fc525cf6a239a5079f4802f8df3f
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Diffstat (limited to 'examples/opengl/cube')
-rw-r--r-- | examples/opengl/cube/geometryengine.cpp | 3 | ||||
-rw-r--r-- | examples/opengl/cube/geometryengine.h | 8 | ||||
-rw-r--r-- | examples/opengl/cube/main.cpp | 19 | ||||
-rw-r--r-- | examples/opengl/cube/mainwidget.cpp | 65 | ||||
-rw-r--r-- | examples/opengl/cube/mainwidget.h | 24 |
5 files changed, 45 insertions, 74 deletions
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 <QtOpenGL/QGLFunctions> -#include <QtOpenGL/QGLShaderProgram> +#include <QGLFunctions> +#include <QGLShaderProgram> 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 <QtOpenGL/QGLShaderProgram> - -#include <QBasicTimer> #include <QMouseEvent> -#include <QDebug> #include <math.h> #include <locale.h> 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 <QtOpenGL/QGLWidget> -#include <QtOpenGL/QGLFunctions> +#include "geometryengine.h" +#include <QGLWidget> +#include <QGLFunctions> #include <QMatrix4x4> #include <QQuaternion> #include <QVector2D> +#include <QBasicTimer> +#include <QGLShaderProgram> -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; |