summaryrefslogtreecommitdiffstats
path: root/examples/opengl/hellogl2
diff options
context:
space:
mode:
Diffstat (limited to 'examples/opengl/hellogl2')
-rw-r--r--examples/opengl/hellogl2/glwidget.cpp283
-rw-r--r--examples/opengl/hellogl2/glwidget.h103
-rw-r--r--examples/opengl/hellogl2/hellogl2.pro15
-rw-r--r--examples/opengl/hellogl2/logo.cpp141
-rw-r--r--examples/opengl/hellogl2/logo.h65
-rw-r--r--examples/opengl/hellogl2/main.cpp71
-rw-r--r--examples/opengl/hellogl2/mainwindow.cpp66
-rw-r--r--examples/opengl/hellogl2/mainwindow.h57
-rw-r--r--examples/opengl/hellogl2/window.cpp133
-rw-r--r--examples/opengl/hellogl2/window.h78
10 files changed, 1012 insertions, 0 deletions
diff --git a/examples/opengl/hellogl2/glwidget.cpp b/examples/opengl/hellogl2/glwidget.cpp
new file mode 100644
index 0000000000..c8db3047a1
--- /dev/null
+++ b/examples/opengl/hellogl2/glwidget.cpp
@@ -0,0 +1,283 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "glwidget.h"
+#include <QMouseEvent>
+#include <QOpenGLShaderProgram>
+#include <QCoreApplication>
+#include <math.h>
+
+GLWidget::GLWidget(QWidget *parent)
+ : QOpenGLWidget(parent),
+ m_xRot(0),
+ m_yRot(0),
+ m_zRot(0),
+ m_program(0)
+{
+ m_core = QCoreApplication::arguments().contains(QStringLiteral("--coreprofile"));
+}
+
+GLWidget::~GLWidget()
+{
+ cleanup();
+}
+
+QSize GLWidget::minimumSizeHint() const
+{
+ return QSize(50, 50);
+}
+
+QSize GLWidget::sizeHint() const
+{
+ return QSize(400, 400);
+}
+
+static void qNormalizeAngle(int &angle)
+{
+ while (angle < 0)
+ angle += 360 * 16;
+ while (angle > 360 * 16)
+ angle -= 360 * 16;
+}
+
+void GLWidget::setXRotation(int angle)
+{
+ qNormalizeAngle(angle);
+ if (angle != m_xRot) {
+ m_xRot = angle;
+ emit xRotationChanged(angle);
+ update();
+ }
+}
+
+void GLWidget::setYRotation(int angle)
+{
+ qNormalizeAngle(angle);
+ if (angle != m_yRot) {
+ m_yRot = angle;
+ emit yRotationChanged(angle);
+ update();
+ }
+}
+
+void GLWidget::setZRotation(int angle)
+{
+ qNormalizeAngle(angle);
+ if (angle != m_zRot) {
+ m_zRot = angle;
+ emit zRotationChanged(angle);
+ update();
+ }
+}
+
+void GLWidget::cleanup()
+{
+ makeCurrent();
+ m_logoVbo.destroy();
+ delete m_program;
+ m_program = 0;
+ doneCurrent();
+}
+
+static const char *vertexShaderSourceCore =
+ "#version 150\n"
+ "in vec4 vertex;\n"
+ "in vec3 normal;\n"
+ "out vec3 vert;\n"
+ "out vec3 vertNormal;\n"
+ "uniform mat4 projMatrix;\n"
+ "uniform mat4 mvMatrix;\n"
+ "uniform mat3 normalMatrix;\n"
+ "void main() {\n"
+ " vert = vertex.xyz;\n"
+ " vertNormal = normalMatrix * normal;\n"
+ " gl_Position = projMatrix * mvMatrix * vertex;\n"
+ "}\n";
+
+static const char *fragmentShaderSourceCore =
+ "#version 150\n"
+ "in highp vec3 vert;\n"
+ "in highp vec3 vertNormal;\n"
+ "out highp vec4 fragColor;\n"
+ "uniform highp vec3 lightPos;\n"
+ "void main() {\n"
+ " highp vec3 L = normalize(lightPos - vert);\n"
+ " highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n"
+ " highp vec3 color = vec3(0.39, 1.0, 0.0);\n"
+ " highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n"
+ " fragColor = vec4(col, 1.0);\n"
+ "}\n";
+
+static const char *vertexShaderSource =
+ "attribute vec4 vertex;\n"
+ "attribute vec3 normal;\n"
+ "varying vec3 vert;\n"
+ "varying vec3 vertNormal;\n"
+ "uniform mat4 projMatrix;\n"
+ "uniform mat4 mvMatrix;\n"
+ "uniform mat3 normalMatrix;\n"
+ "void main() {\n"
+ " vert = vertex.xyz;\n"
+ " vertNormal = normalMatrix * normal;\n"
+ " gl_Position = projMatrix * mvMatrix * vertex;\n"
+ "}\n";
+
+static const char *fragmentShaderSource =
+ "varying highp vec3 vert;\n"
+ "varying highp vec3 vertNormal;\n"
+ "uniform highp vec3 lightPos;\n"
+ "void main() {\n"
+ " highp vec3 L = normalize(lightPos - vert);\n"
+ " highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n"
+ " highp vec3 color = vec3(0.39, 1.0, 0.0);\n"
+ " highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n"
+ " gl_FragColor = vec4(col, 1.0);\n"
+ "}\n";
+
+void GLWidget::initializeGL()
+{
+ // In this example the widget's corresponding top-level window can change
+ // several times during the widget's lifetime. Whenever this happens, the
+ // QOpenGLWidget's associated context is destroyed and a new one is created.
+ // Therefore we have to be prepared to clean up the resources on the
+ // aboutToBeDestroyed() signal, instead of the destructor. The emission of
+ // the signal will be followed by an invocation of initializeGL() where we
+ // can recreate all resources.
+ connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &GLWidget::cleanup);
+
+ initializeOpenGLFunctions();
+
+ m_program = new QOpenGLShaderProgram;
+ m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_core ? vertexShaderSourceCore : vertexShaderSource);
+ m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_core ? fragmentShaderSourceCore : fragmentShaderSource);
+ m_program->bindAttributeLocation("vertex", 0);
+ m_program->bindAttributeLocation("normal", 1);
+ m_program->link();
+
+ m_program->bind();
+ m_projMatrixLoc = m_program->uniformLocation("projMatrix");
+ m_mvMatrixLoc = m_program->uniformLocation("mvMatrix");
+ m_normalMatrixLoc = m_program->uniformLocation("normalMatrix");
+ m_lightPosLoc = m_program->uniformLocation("lightPos");
+
+ // Create a vertex array object. In OpenGL ES 2.0 and OpenGL 2.x
+ // implementations this is optional and support may not be present
+ // at all. Nonetheless the below code works in all cases and makes
+ // sure there is a VAO when one is needed.
+ m_vao.create();
+ QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
+
+ // Setup our vertex buffer object.
+ m_logoVbo.create();
+ m_logoVbo.bind();
+ m_logoVbo.allocate(m_logo.constData(), m_logo.count() * sizeof(GLfloat));
+
+ // Store the vertex attribute bindings for the program.
+ setupVertexAttribs();
+
+ // Our camera never changes in this example.
+ m_camera.setToIdentity();
+ m_camera.translate(0, 0, -1);
+
+ // Light position is fixed.
+ m_program->setUniformValue(m_lightPosLoc, QVector3D(0, 0, 70));
+
+ m_program->release();
+}
+
+void GLWidget::setupVertexAttribs()
+{
+ m_logoVbo.bind();
+ 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)));
+ m_logoVbo.release();
+}
+
+void GLWidget::paintGL()
+{
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_CULL_FACE);
+
+ m_world.setToIdentity();
+ m_world.rotate(180.0f - (m_xRot / 16.0f), 1, 0, 0);
+ m_world.rotate(m_yRot / 16.0f, 0, 1, 0);
+ m_world.rotate(m_zRot / 16.0f, 0, 0, 1);
+
+ QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
+ m_program->bind();
+ m_program->setUniformValue(m_projMatrixLoc, m_proj);
+ m_program->setUniformValue(m_mvMatrixLoc, m_camera * m_world);
+ QMatrix3x3 normalMatrix = m_world.normalMatrix();
+ m_program->setUniformValue(m_normalMatrixLoc, normalMatrix);
+
+ glDrawArrays(GL_TRIANGLES, 0, m_logo.vertexCount());
+
+ m_program->release();
+}
+
+void GLWidget::resizeGL(int w, int h)
+{
+ m_proj.setToIdentity();
+ m_proj.perspective(45.0f, GLfloat(w) / h, 0.01f, 100.0f);
+}
+
+void GLWidget::mousePressEvent(QMouseEvent *event)
+{
+ m_lastPos = event->pos();
+}
+
+void GLWidget::mouseMoveEvent(QMouseEvent *event)
+{
+ int dx = event->x() - m_lastPos.x();
+ int dy = event->y() - m_lastPos.y();
+
+ if (event->buttons() & Qt::LeftButton) {
+ setXRotation(m_xRot + 8 * dy);
+ setYRotation(m_yRot + 8 * dx);
+ } else if (event->buttons() & Qt::RightButton) {
+ setXRotation(m_xRot + 8 * dy);
+ setZRotation(m_zRot + 8 * dx);
+ }
+ m_lastPos = event->pos();
+}
diff --git a/examples/opengl/hellogl2/glwidget.h b/examples/opengl/hellogl2/glwidget.h
new file mode 100644
index 0000000000..fcc6225999
--- /dev/null
+++ b/examples/opengl/hellogl2/glwidget.h
@@ -0,0 +1,103 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef GLWIDGET_H
+#define GLWIDGET_H
+
+#include <QOpenGLWidget>
+#include <QOpenGLFunctions>
+#include <QOpenGLVertexArrayObject>
+#include <QOpenGLBuffer>
+#include <QMatrix4x4>
+#include "logo.h"
+
+QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
+
+class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
+{
+ Q_OBJECT
+
+public:
+ GLWidget(QWidget *parent = 0);
+ ~GLWidget();
+
+ QSize minimumSizeHint() const Q_DECL_OVERRIDE;
+ QSize sizeHint() const Q_DECL_OVERRIDE;
+
+public slots:
+ void setXRotation(int angle);
+ void setYRotation(int angle);
+ void setZRotation(int angle);
+ void cleanup();
+
+signals:
+ void xRotationChanged(int angle);
+ void yRotationChanged(int angle);
+ void zRotationChanged(int angle);
+
+protected:
+ void initializeGL() Q_DECL_OVERRIDE;
+ void paintGL() Q_DECL_OVERRIDE;
+ void resizeGL(int width, int height) Q_DECL_OVERRIDE;
+ void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
+ void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
+
+private:
+ void setupVertexAttribs();
+
+ bool m_core;
+ int m_xRot;
+ int m_yRot;
+ int m_zRot;
+ 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;
+ QMatrix4x4 m_proj;
+ QMatrix4x4 m_camera;
+ QMatrix4x4 m_world;
+};
+
+#endif
diff --git a/examples/opengl/hellogl2/hellogl2.pro b/examples/opengl/hellogl2/hellogl2.pro
new file mode 100644
index 0000000000..3a52e04d74
--- /dev/null
+++ b/examples/opengl/hellogl2/hellogl2.pro
@@ -0,0 +1,15 @@
+HEADERS = glwidget.h \
+ window.h \
+ mainwindow.h \
+ logo.h
+SOURCES = glwidget.cpp \
+ main.cpp \
+ window.cpp \
+ mainwindow.cpp \
+ logo.cpp
+
+QT += widgets
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellogl2
+INSTALLS += target
diff --git a/examples/opengl/hellogl2/logo.cpp b/examples/opengl/hellogl2/logo.cpp
new file mode 100644
index 0000000000..1ba47ddfb5
--- /dev/null
+++ b/examples/opengl/hellogl2/logo.cpp
@@ -0,0 +1,141 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "logo.h"
+#include <qmath.h>
+
+Logo::Logo()
+ : m_count(0)
+{
+ m_data.resize(2500 * 6);
+
+ const GLfloat x1 = +0.06f;
+ const GLfloat y1 = -0.14f;
+ const GLfloat x2 = +0.14f;
+ const GLfloat y2 = -0.06f;
+ const GLfloat x3 = +0.08f;
+ const GLfloat y3 = +0.00f;
+ const GLfloat x4 = +0.30f;
+ const GLfloat y4 = +0.22f;
+
+ quad(x1, y1, x2, y2, y2, x2, y1, x1);
+ quad(x3, y3, x4, y4, y4, x4, y3, x3);
+
+ extrude(x1, y1, x2, y2);
+ extrude(x2, y2, y2, x2);
+ extrude(y2, x2, y1, x1);
+ extrude(y1, x1, x1, y1);
+ extrude(x3, y3, x4, y4);
+ extrude(x4, y4, y4, x4);
+ extrude(y4, x4, y3, x3);
+
+ const int NumSectors = 100;
+
+ for (int i = 0; i < NumSectors; ++i) {
+ GLfloat angle = (i * 2 * M_PI) / NumSectors;
+ GLfloat angleSin = qSin(angle);
+ GLfloat angleCos = qCos(angle);
+ const GLfloat x5 = 0.30f * angleSin;
+ const GLfloat y5 = 0.30f * angleCos;
+ const GLfloat x6 = 0.20f * angleSin;
+ const GLfloat y6 = 0.20f * angleCos;
+
+ angle = ((i + 1) * 2 * M_PI) / NumSectors;
+ angleSin = qSin(angle);
+ angleCos = qCos(angle);
+ const GLfloat x7 = 0.20f * angleSin;
+ const GLfloat y7 = 0.20f * angleCos;
+ const GLfloat x8 = 0.30f * angleSin;
+ const GLfloat y8 = 0.30f * angleCos;
+
+ quad(x5, y5, x6, y6, x7, y7, x8, y8);
+
+ extrude(x6, y6, x7, y7);
+ extrude(x8, y8, x5, y5);
+ }
+}
+
+void Logo::add(const QVector3D &v, const QVector3D &n)
+{
+ GLfloat *p = m_data.data() + m_count;
+ *p++ = v.x();
+ *p++ = v.y();
+ *p++ = v.z();
+ *p++ = n.x();
+ *p++ = n.y();
+ *p++ = n.z();
+ m_count += 6;
+}
+
+void Logo::quad(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4)
+{
+ QVector3D n = QVector3D::normal(QVector3D(x4 - x1, y4 - y1, 0.0f), QVector3D(x2 - x1, y2 - y1, 0.0f));
+
+ add(QVector3D(x1, y1, -0.05f), n);
+ add(QVector3D(x4, y4, -0.05f), n);
+ add(QVector3D(x2, y2, -0.05f), n);
+
+ add(QVector3D(x3, y3, -0.05f), n);
+ add(QVector3D(x2, y2, -0.05f), n);
+ add(QVector3D(x4, y4, -0.05f), n);
+
+ n = QVector3D::normal(QVector3D(x1 - x4, y1 - y4, 0.0f), QVector3D(x2 - x4, y2 - y4, 0.0f));
+
+ add(QVector3D(x4, y4, 0.05f), n);
+ add(QVector3D(x1, y1, 0.05f), n);
+ add(QVector3D(x2, y2, 0.05f), n);
+
+ add(QVector3D(x2, y2, 0.05f), n);
+ add(QVector3D(x3, y3, 0.05f), n);
+ add(QVector3D(x4, y4, 0.05f), n);
+}
+
+void Logo::extrude(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
+{
+ QVector3D n = QVector3D::normal(QVector3D(0.0f, 0.0f, -0.1f), QVector3D(x2 - x1, y2 - y1, 0.0f));
+
+ add(QVector3D(x1, y1, +0.05f), n);
+ add(QVector3D(x1, y1, -0.05f), n);
+ add(QVector3D(x2, y2, +0.05f), n);
+
+ add(QVector3D(x2, y2, -0.05f), n);
+ add(QVector3D(x2, y2, +0.05f), n);
+ add(QVector3D(x1, y1, -0.05f), n);
+}
diff --git a/examples/opengl/hellogl2/logo.h b/examples/opengl/hellogl2/logo.h
new file mode 100644
index 0000000000..29bb7fa241
--- /dev/null
+++ b/examples/opengl/hellogl2/logo.h
@@ -0,0 +1,65 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef LOGO_H
+#define LOGO_H
+
+#include <qopengl.h>
+#include <QVector>
+#include <QVector3D>
+
+class Logo
+{
+public:
+ Logo();
+ const GLfloat *constData() const { return m_data.constData(); }
+ int count() const { return m_count; }
+ int vertexCount() const { return m_count / 6; }
+
+private:
+ void quad(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4);
+ void extrude(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2);
+ void add(const QVector3D &v, const QVector3D &n);
+
+ QVector<GLfloat> m_data;
+ int m_count;
+};
+
+#endif // LOGO_H
diff --git a/examples/opengl/hellogl2/main.cpp b/examples/opengl/hellogl2/main.cpp
new file mode 100644
index 0000000000..2d439878e4
--- /dev/null
+++ b/examples/opengl/hellogl2/main.cpp
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QApplication>
+#include <QDesktopWidget>
+#include <QSurfaceFormat>
+
+#include "mainwindow.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ QSurfaceFormat fmt;
+ fmt.setDepthBufferSize(24);
+ if (QCoreApplication::arguments().contains(QStringLiteral("--multisample")))
+ fmt.setSamples(4);
+ if (QCoreApplication::arguments().contains(QStringLiteral("--coreprofile"))) {
+ fmt.setVersion(3, 2);
+ fmt.setProfile(QSurfaceFormat::CoreProfile);
+ }
+ QSurfaceFormat::setDefaultFormat(fmt);
+
+ MainWindow mainWindow;
+ mainWindow.resize(mainWindow.sizeHint());
+ int desktopArea = QApplication::desktop()->width() *
+ QApplication::desktop()->height();
+ int widgetArea = mainWindow.width() * mainWindow.height();
+ if (((float)widgetArea / (float)desktopArea) < 0.75f)
+ mainWindow.show();
+ else
+ mainWindow.showMaximized();
+ return app.exec();
+}
diff --git a/examples/opengl/hellogl2/mainwindow.cpp b/examples/opengl/hellogl2/mainwindow.cpp
new file mode 100644
index 0000000000..ee85cda35c
--- /dev/null
+++ b/examples/opengl/hellogl2/mainwindow.cpp
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "mainwindow.h"
+#include "window.h"
+#include <QMenuBar>
+#include <QMenu>
+#include <QMessageBox>
+
+MainWindow::MainWindow()
+{
+ QMenuBar *menuBar = new QMenuBar;
+ QMenu *menuWindow = menuBar->addMenu(tr("&Window"));
+ QAction *addNew = new QAction(menuWindow);
+ addNew->setText(tr("Add new"));
+ menuWindow->addAction(addNew);
+ connect(addNew, SIGNAL(triggered()), this, SLOT(onAddNew()));
+ setMenuBar(menuBar);
+
+ onAddNew();
+}
+
+void MainWindow::onAddNew()
+{
+ if (!centralWidget())
+ setCentralWidget(new Window(this));
+ else
+ QMessageBox::information(0, tr("Cannot add new window"), tr("Already occupied. Undock first."));
+}
diff --git a/examples/opengl/hellogl2/mainwindow.h b/examples/opengl/hellogl2/mainwindow.h
new file mode 100644
index 0000000000..53d4555ce8
--- /dev/null
+++ b/examples/opengl/hellogl2/mainwindow.h
@@ -0,0 +1,57 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ MainWindow();
+
+private slots:
+ void onAddNew();
+};
+
+#endif
diff --git a/examples/opengl/hellogl2/window.cpp b/examples/opengl/hellogl2/window.cpp
new file mode 100644
index 0000000000..0b20870487
--- /dev/null
+++ b/examples/opengl/hellogl2/window.cpp
@@ -0,0 +1,133 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "glwidget.h"
+#include "window.h"
+#include "mainwindow.h"
+#include <QSlider>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QKeyEvent>
+#include <QPushButton>
+#include <QDesktopWidget>
+#include <QApplication>
+#include <QMessageBox>
+
+Window::Window(MainWindow *mw)
+ : mainWindow(mw)
+{
+ glWidget = new GLWidget;
+
+ xSlider = createSlider();
+ ySlider = createSlider();
+ zSlider = createSlider();
+
+ connect(xSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setXRotation(int)));
+ connect(glWidget, SIGNAL(xRotationChanged(int)), xSlider, SLOT(setValue(int)));
+ connect(ySlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setYRotation(int)));
+ connect(glWidget, SIGNAL(yRotationChanged(int)), ySlider, SLOT(setValue(int)));
+ connect(zSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setZRotation(int)));
+ connect(glWidget, SIGNAL(zRotationChanged(int)), zSlider, SLOT(setValue(int)));
+
+ QVBoxLayout *mainLayout = new QVBoxLayout;
+ QHBoxLayout *container = new QHBoxLayout;
+ container->addWidget(glWidget);
+ container->addWidget(xSlider);
+ container->addWidget(ySlider);
+ container->addWidget(zSlider);
+
+ QWidget *w = new QWidget;
+ w->setLayout(container);
+ mainLayout->addWidget(w);
+ dockBtn = new QPushButton(tr("Undock"), this);
+ connect(dockBtn, SIGNAL(clicked()), this, SLOT(dockUndock()));
+ mainLayout->addWidget(dockBtn);
+
+ setLayout(mainLayout);
+
+ xSlider->setValue(15 * 16);
+ ySlider->setValue(345 * 16);
+ zSlider->setValue(0 * 16);
+
+ setWindowTitle(tr("Hello GL"));
+}
+
+QSlider *Window::createSlider()
+{
+ QSlider *slider = new QSlider(Qt::Vertical);
+ slider->setRange(0, 360 * 16);
+ slider->setSingleStep(16);
+ slider->setPageStep(15 * 16);
+ slider->setTickInterval(15 * 16);
+ slider->setTickPosition(QSlider::TicksRight);
+ return slider;
+}
+
+void Window::keyPressEvent(QKeyEvent *e)
+{
+ if (e->key() == Qt::Key_Escape)
+ close();
+ else
+ QWidget::keyPressEvent(e);
+}
+
+void Window::dockUndock()
+{
+ if (parent()) {
+ setParent(0);
+ setAttribute(Qt::WA_DeleteOnClose);
+ move(QApplication::desktop()->width() / 2 - width() / 2,
+ QApplication::desktop()->height() / 2 - height() / 2);
+ dockBtn->setText(tr("Dock"));
+ show();
+ } else {
+ if (!mainWindow->centralWidget()) {
+ if (mainWindow->isVisible()) {
+ setAttribute(Qt::WA_DeleteOnClose, false);
+ dockBtn->setText(tr("Undock"));
+ mainWindow->setCentralWidget(this);
+ } else {
+ QMessageBox::information(0, tr("Cannot dock"), tr("Main window already closed"));
+ }
+ } else {
+ QMessageBox::information(0, tr("Cannot dock"), tr("Main window already occupied"));
+ }
+ }
+}
diff --git a/examples/opengl/hellogl2/window.h b/examples/opengl/hellogl2/window.h
new file mode 100644
index 0000000000..a850bd6a5a
--- /dev/null
+++ b/examples/opengl/hellogl2/window.h
@@ -0,0 +1,78 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef WINDOW_H
+#define WINDOW_H
+
+#include <QWidget>
+
+QT_BEGIN_NAMESPACE
+class QSlider;
+class QPushButton;
+QT_END_NAMESPACE
+
+class GLWidget;
+class MainWindow;
+
+class Window : public QWidget
+{
+ Q_OBJECT
+
+public:
+ Window(MainWindow *mw);
+
+protected:
+ void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
+
+private slots:
+ void dockUndock();
+
+private:
+ QSlider *createSlider();
+
+ GLWidget *glWidget;
+ QSlider *xSlider;
+ QSlider *ySlider;
+ QSlider *zSlider;
+ QPushButton *dockBtn;
+ MainWindow *mainWindow;
+};
+
+#endif