Debian Codegraph @

tests/manual/qopenglwidget/openglwidget/openglwidget.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #define GL_GLEXT_PROTOTYPES
30
31 #include "openglwidget.h"
32 #include <QtWidgets/private/qwidget_p.h>
33 #include <QOpenGLFramebufferObject>
34 #include <QWindow>
35 #include <qpa/qplatformwindow.h>
36 #include <QDebug>
37 #include <QTimer>
38
39 #include <QtGui/QOpenGLFunctions>
40 #include <QtGui/QGuiApplication>
41 #include <QtGui/QMatrix4x4>
42 #include <QtGui/QOpenGLShaderProgram>
43 #include <QtGui/QScreen>
44
45 #include <QtCore/qmath.h>
46 #include <qopengl.h>
47
48 class OpenGLWidgetPrivate : protected QOpenGLFunctions
49 {
50 public:
51 OpenGLWidgetPrivate(QWidget *q)
52 : m_program(0), m_frame(0), q(q)
53 {
54
55 }
56
57 void initialize();
58 void render();
59
60
61 int width() {return w;}
62 int height() {return h;}
63
64 GLuint m_posAttr;
65 GLuint m_colAttr;
66 GLuint m_matrixUniform;
67
68 QOpenGLShaderProgram *m_program;
69 int m_frame;
70
71 int w,h;
72 QWidget *q;
73
74 int m_interval;
75 QVector3D m_rotAxis;
76
77 float clearColor[3];
78 };
79
80
81 OpenGLWidget::OpenGLWidget(int interval, const QVector3D &rotAxis, QWidget *parent)
82 : QOpenGLWidget(parent)
83 {
84 d.reset(new OpenGLWidgetPrivate(this));
85 d->clearColor[0] = d->clearColor[1] = d->clearColor[2] = 0.0f;
86 d->m_interval = interval;
87 d->m_rotAxis = rotAxis;
88 if (interval > 0) {
89 QTimer *timer = new QTimer(this);
90 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
91 timer->start(interval);
92 }
93 }
94
95 OpenGLWidget::~OpenGLWidget()
96 {
97
98 }
99
100 void OpenGLWidget::initializeGL()
101 {
102 // qDebug("*initializeGL*");
103 d->initialize();
104 }
105
106 void OpenGLWidget::resizeGL(int w, int h)
107 {
108 // qDebug("*resizeGL*");
109 d->w = w;
110 d->h = h;
111 }
112 void OpenGLWidget::paintGL()
113 {
114 // qDebug("*paintGL* %d", d->m_frame);
115 d->render();
116 }
117
118
119 static const char *vertexShaderSource =
120 "attribute highp vec4 posAttr;\n"
121 "attribute lowp vec4 colAttr;\n"
122 "varying lowp vec4 col;\n"
123 "uniform highp mat4 matrix;\n"
124 "void main() {\n"
125 " col = colAttr;\n"
126 " gl_Position = matrix * posAttr;\n"
127 "}\n";
128
129 static const char *fragmentShaderSource =
130 "varying lowp vec4 col;\n"
131 "void main() {\n"
132 " gl_FragColor = col;\n"
133 "}\n";
134
135 void OpenGLWidgetPrivate::initialize()
136 {
137 initializeOpenGLFunctions();
138 m_program = new QOpenGLShaderProgram;
139 m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
140 m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
141 m_program->link();
142 m_posAttr = m_program->attributeLocation("posAttr");
143 m_colAttr = m_program->attributeLocation("colAttr");
144 m_matrixUniform = m_program->uniformLocation("matrix");
145 }
146
147 void OpenGLWidgetPrivate::render()
148 {
149 const qreal retinaScale = q->devicePixelRatio();
150 glViewport(0, 0, width() * retinaScale, height() * retinaScale);
151
152 glClearColor(clearColor[0], clearColor[1], clearColor[2], 1.0f);
153 glClear(GL_COLOR_BUFFER_BIT);
154
155 m_program->bind();
156
157 QMatrix4x4 matrix;
158 matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
159 matrix.translate(0, 0, -2);
160 const qreal angle = 100.0f * m_frame / 30;
161 matrix.rotate(angle, m_rotAxis);
162
163 m_program->setUniformValue(m_matrixUniform, matrix);
164
165 GLfloat vertices[] = {
166 0.0f, 0.707f,
167 -0.5f, -0.5f,
168 0.5f, -0.5f
169 };
170
171 GLfloat colors[] = {
172 1.0f, 0.0f, 0.0f,
173 0.0f, 1.0f, 0.0f,
174 0.0f, 0.0f, 1.0f
175 };
176
177 glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
178 glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
179
180 glEnableVertexAttribArray(0);
181 glEnableVertexAttribArray(1);
182
183 glDrawArrays(GL_TRIANGLES, 0, 3);
184
185 glDisableVertexAttribArray(1);
186 glDisableVertexAttribArray(0);
187
188 m_program->release();
189
190 ++m_frame;
191
192 if (m_interval <= 0)
193 q->update();
194 }
195
196 void OpenGLWidget::setClearColor(const float *c)
197 {
198 d->clearColor[0] = c[0];
199 d->clearColor[1] = c[1];
200 d->clearColor[2] = c[2];
201 }