| 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 examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 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 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "hellowindow.h" |
| 52 | |
| 53 | #include <QOpenGLContext> |
| 54 | #include <QOpenGLFunctions> |
| 55 | #include <QRandomGenerator> |
| 56 | #include <qmath.h> |
| 57 | |
| 58 | Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen) |
| 59 | : m_initialized(false) |
| 60 | , m_format(format) |
| 61 | , m_currentWindow(0) |
| 62 | { |
| 63 | m_context = new QOpenGLContext(this); |
| 64 | if (screen) |
| 65 | m_context->setScreen(screen); |
| 66 | m_context->setFormat(format); |
| 67 | if (share) |
| 68 | m_context->setShareContext(share->m_context); |
| 69 | m_context->create(); |
| 70 | |
| 71 | m_backgroundColor = QColor::fromRgbF(r: 0.1f, g: 0.1f, b: 0.2f, a: 1.0f); |
| 72 | m_backgroundColor.setRed(QRandomGenerator::global()->bounded(highest: 64)); |
| 73 | m_backgroundColor.setGreen(QRandomGenerator::global()->bounded(highest: 128)); |
| 74 | m_backgroundColor.setBlue(QRandomGenerator::global()->bounded(highest: 256)); |
| 75 | } |
| 76 | |
| 77 | HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer, QScreen *screen) |
| 78 | : m_colorIndex(0), m_renderer(renderer) |
| 79 | { |
| 80 | setSurfaceType(QWindow::OpenGLSurface); |
| 81 | setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint); |
| 82 | |
| 83 | setGeometry(QRect(10, 10, 640, 480)); |
| 84 | |
| 85 | setFormat(renderer->format()); |
| 86 | if (screen) |
| 87 | setScreen(screen); |
| 88 | |
| 89 | create(); |
| 90 | |
| 91 | updateColor(); |
| 92 | } |
| 93 | |
| 94 | void HelloWindow::exposeEvent(QExposeEvent *) |
| 95 | { |
| 96 | m_renderer->setAnimating(window: this, animating: isExposed()); |
| 97 | if (isExposed()) |
| 98 | m_renderer->render(); |
| 99 | } |
| 100 | |
| 101 | bool HelloWindow::event(QEvent *ev) |
| 102 | { |
| 103 | if (ev->type() == QEvent::UpdateRequest) { |
| 104 | m_renderer->render(); |
| 105 | requestUpdate(); |
| 106 | } |
| 107 | return QWindow::event(ev); |
| 108 | } |
| 109 | |
| 110 | void HelloWindow::mousePressEvent(QMouseEvent *) |
| 111 | { |
| 112 | updateColor(); |
| 113 | } |
| 114 | |
| 115 | QColor HelloWindow::color() const |
| 116 | { |
| 117 | QMutexLocker locker(&m_colorLock); |
| 118 | return m_color; |
| 119 | } |
| 120 | |
| 121 | void HelloWindow::updateColor() |
| 122 | { |
| 123 | QMutexLocker locker(&m_colorLock); |
| 124 | |
| 125 | QColor colors[] = |
| 126 | { |
| 127 | QColor(100, 255, 0), |
| 128 | QColor(0, 100, 255) |
| 129 | }; |
| 130 | |
| 131 | m_color = colors[m_colorIndex]; |
| 132 | m_colorIndex = 1 - m_colorIndex; |
| 133 | } |
| 134 | |
| 135 | void Renderer::setAnimating(HelloWindow *window, bool animating) |
| 136 | { |
| 137 | QMutexLocker locker(&m_windowLock); |
| 138 | if (m_windows.contains(t: window) == animating) |
| 139 | return; |
| 140 | |
| 141 | if (animating) { |
| 142 | m_windows << window; |
| 143 | if (m_windows.size() == 1) |
| 144 | window->requestUpdate(); |
| 145 | } else { |
| 146 | m_currentWindow = 0; |
| 147 | m_windows.removeOne(t: window); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void Renderer::render() |
| 152 | { |
| 153 | QMutexLocker locker(&m_windowLock); |
| 154 | |
| 155 | if (m_windows.isEmpty()) |
| 156 | return; |
| 157 | |
| 158 | HelloWindow *surface = m_windows.at(i: m_currentWindow); |
| 159 | QColor color = surface->color(); |
| 160 | |
| 161 | m_currentWindow = (m_currentWindow + 1) % m_windows.size(); |
| 162 | |
| 163 | if (!m_context->makeCurrent(surface)) |
| 164 | return; |
| 165 | |
| 166 | QSize viewSize = surface->size(); |
| 167 | |
| 168 | locker.unlock(); |
| 169 | |
| 170 | if (!m_initialized) { |
| 171 | initialize(); |
| 172 | m_initialized = true; |
| 173 | } |
| 174 | |
| 175 | QOpenGLFunctions *f = m_context->functions(); |
| 176 | f->glViewport(x: 0, y: 0, width: viewSize.width() * surface->devicePixelRatio(), height: viewSize.height() * surface->devicePixelRatio()); |
| 177 | f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 178 | |
| 179 | f->glClearColor(red: m_backgroundColor.redF(), green: m_backgroundColor.greenF(), blue: m_backgroundColor.blueF(), alpha: m_backgroundColor.alphaF()); |
| 180 | f->glFrontFace(GL_CW); |
| 181 | f->glCullFace(GL_FRONT); |
| 182 | f->glEnable(GL_CULL_FACE); |
| 183 | f->glEnable(GL_DEPTH_TEST); |
| 184 | |
| 185 | m_program->bind(); |
| 186 | m_vbo.bind(); |
| 187 | |
| 188 | m_program->enableAttributeArray(location: vertexAttr); |
| 189 | m_program->enableAttributeArray(location: normalAttr); |
| 190 | m_program->setAttributeBuffer(location: vertexAttr, GL_FLOAT, offset: 0, tupleSize: 3); |
| 191 | const int verticesSize = vertices.count() * 3 * sizeof(GLfloat); |
| 192 | m_program->setAttributeBuffer(location: normalAttr, GL_FLOAT, offset: verticesSize, tupleSize: 3); |
| 193 | |
| 194 | QMatrix4x4 modelview; |
| 195 | modelview.rotate(angle: m_fAngle, x: 0.0f, y: 1.0f, z: 0.0f); |
| 196 | modelview.rotate(angle: m_fAngle, x: 1.0f, y: 0.0f, z: 0.0f); |
| 197 | modelview.rotate(angle: m_fAngle, x: 0.0f, y: 0.0f, z: 1.0f); |
| 198 | modelview.translate(x: 0.0f, y: -0.2f, z: 0.0f); |
| 199 | |
| 200 | m_program->setUniformValue(location: matrixUniform, value: modelview); |
| 201 | m_program->setUniformValue(location: colorUniform, color); |
| 202 | |
| 203 | m_context->functions()->glDrawArrays(GL_TRIANGLES, first: 0, count: vertices.size()); |
| 204 | |
| 205 | m_context->swapBuffers(surface); |
| 206 | |
| 207 | m_fAngle += 1.0f; |
| 208 | } |
| 209 | |
| 210 | Q_GLOBAL_STATIC(QMutex, initMutex) |
| 211 | |
| 212 | void Renderer::initialize() |
| 213 | { |
| 214 | // Threaded shader compilation can confuse some drivers. Avoid it. |
| 215 | QMutexLocker lock(initMutex()); |
| 216 | |
| 217 | QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); |
| 218 | vshader->compileSourceCode( |
| 219 | source: "attribute highp vec4 vertex;" |
| 220 | "attribute mediump vec3 normal;" |
| 221 | "uniform mediump mat4 matrix;" |
| 222 | "uniform lowp vec4 sourceColor;" |
| 223 | "varying mediump vec4 color;" |
| 224 | "void main(void)" |
| 225 | "{" |
| 226 | " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));" |
| 227 | " float angle = max(dot(normal, toLight), 0.0);" |
| 228 | " vec3 col = sourceColor.rgb;" |
| 229 | " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);" |
| 230 | " color = clamp(color, 0.0, 1.0);" |
| 231 | " gl_Position = matrix * vertex;" |
| 232 | "}" ); |
| 233 | |
| 234 | QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); |
| 235 | fshader->compileSourceCode( |
| 236 | source: "varying mediump vec4 color;" |
| 237 | "void main(void)" |
| 238 | "{" |
| 239 | " gl_FragColor = color;" |
| 240 | "}" ); |
| 241 | |
| 242 | m_program = new QOpenGLShaderProgram(this); |
| 243 | m_program->addShader(shader: vshader); |
| 244 | m_program->addShader(shader: fshader); |
| 245 | m_program->link(); |
| 246 | m_program->bind(); |
| 247 | |
| 248 | vertexAttr = m_program->attributeLocation(name: "vertex" ); |
| 249 | normalAttr = m_program->attributeLocation(name: "normal" ); |
| 250 | matrixUniform = m_program->uniformLocation(name: "matrix" ); |
| 251 | colorUniform = m_program->uniformLocation(name: "sourceColor" ); |
| 252 | |
| 253 | m_fAngle = 0; |
| 254 | createGeometry(); |
| 255 | |
| 256 | m_vbo.create(); |
| 257 | m_vbo.bind(); |
| 258 | const int verticesSize = vertices.count() * 3 * sizeof(GLfloat); |
| 259 | m_vbo.allocate(count: verticesSize * 2); |
| 260 | m_vbo.write(offset: 0, data: vertices.constData(), count: verticesSize); |
| 261 | m_vbo.write(offset: verticesSize, data: normals.constData(), count: verticesSize); |
| 262 | } |
| 263 | |
| 264 | void Renderer::createGeometry() |
| 265 | { |
| 266 | vertices.clear(); |
| 267 | normals.clear(); |
| 268 | |
| 269 | qreal x1 = +0.06f; |
| 270 | qreal y1 = -0.14f; |
| 271 | qreal x2 = +0.14f; |
| 272 | qreal y2 = -0.06f; |
| 273 | qreal x3 = +0.08f; |
| 274 | qreal y3 = +0.00f; |
| 275 | qreal x4 = +0.30f; |
| 276 | qreal y4 = +0.22f; |
| 277 | |
| 278 | quad(x1, y1, x2, y2, x3: y2, y3: x2, x4: y1, y4: x1); |
| 279 | quad(x1: x3, y1: y3, x2: x4, y2: y4, x3: y4, y3: x4, x4: y3, y4: x3); |
| 280 | |
| 281 | extrude(x1, y1, x2, y2); |
| 282 | extrude(x1: x2, y1: y2, x2: y2, y2: x2); |
| 283 | extrude(x1: y2, y1: x2, x2: y1, y2: x1); |
| 284 | extrude(x1: y1, y1: x1, x2: x1, y2: y1); |
| 285 | extrude(x1: x3, y1: y3, x2: x4, y2: y4); |
| 286 | extrude(x1: x4, y1: y4, x2: y4, y2: x4); |
| 287 | extrude(x1: y4, y1: x4, x2: y3, y2: x3); |
| 288 | |
| 289 | const int NumSectors = 100; |
| 290 | const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors; |
| 291 | for (int i = 0; i < NumSectors; ++i) { |
| 292 | qreal angle = i * sectorAngle; |
| 293 | qreal x5 = 0.30 * qSin(v: angle); |
| 294 | qreal y5 = 0.30 * qCos(v: angle); |
| 295 | qreal x6 = 0.20 * qSin(v: angle); |
| 296 | qreal y6 = 0.20 * qCos(v: angle); |
| 297 | |
| 298 | angle += sectorAngle; |
| 299 | qreal x7 = 0.20 * qSin(v: angle); |
| 300 | qreal y7 = 0.20 * qCos(v: angle); |
| 301 | qreal x8 = 0.30 * qSin(v: angle); |
| 302 | qreal y8 = 0.30 * qCos(v: angle); |
| 303 | |
| 304 | quad(x1: x5, y1: y5, x2: x6, y2: y6, x3: x7, y3: y7, x4: x8, y4: y8); |
| 305 | |
| 306 | extrude(x1: x6, y1: y6, x2: x7, y2: y7); |
| 307 | extrude(x1: x8, y1: y8, x2: x5, y2: y5); |
| 308 | } |
| 309 | |
| 310 | for (int i = 0;i < vertices.size();i++) |
| 311 | vertices[i] *= 2.0f; |
| 312 | } |
| 313 | |
| 314 | void Renderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4) |
| 315 | { |
| 316 | vertices << QVector3D(x1, y1, -0.05f); |
| 317 | vertices << QVector3D(x2, y2, -0.05f); |
| 318 | vertices << QVector3D(x4, y4, -0.05f); |
| 319 | |
| 320 | vertices << QVector3D(x3, y3, -0.05f); |
| 321 | vertices << QVector3D(x4, y4, -0.05f); |
| 322 | vertices << QVector3D(x2, y2, -0.05f); |
| 323 | |
| 324 | QVector3D n = QVector3D::normal |
| 325 | (v1: QVector3D(x2 - x1, y2 - y1, 0.0f), v2: QVector3D(x4 - x1, y4 - y1, 0.0f)); |
| 326 | |
| 327 | normals << n; |
| 328 | normals << n; |
| 329 | normals << n; |
| 330 | |
| 331 | normals << n; |
| 332 | normals << n; |
| 333 | normals << n; |
| 334 | |
| 335 | vertices << QVector3D(x4, y4, 0.05f); |
| 336 | vertices << QVector3D(x2, y2, 0.05f); |
| 337 | vertices << QVector3D(x1, y1, 0.05f); |
| 338 | |
| 339 | vertices << QVector3D(x2, y2, 0.05f); |
| 340 | vertices << QVector3D(x4, y4, 0.05f); |
| 341 | vertices << QVector3D(x3, y3, 0.05f); |
| 342 | |
| 343 | n = QVector3D::normal |
| 344 | (v1: QVector3D(x2 - x4, y2 - y4, 0.0f), v2: QVector3D(x1 - x4, y1 - y4, 0.0f)); |
| 345 | |
| 346 | normals << n; |
| 347 | normals << n; |
| 348 | normals << n; |
| 349 | |
| 350 | normals << n; |
| 351 | normals << n; |
| 352 | normals << n; |
| 353 | } |
| 354 | |
| 355 | void Renderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2) |
| 356 | { |
| 357 | vertices << QVector3D(x1, y1, +0.05f); |
| 358 | vertices << QVector3D(x2, y2, +0.05f); |
| 359 | vertices << QVector3D(x1, y1, -0.05f); |
| 360 | |
| 361 | vertices << QVector3D(x2, y2, -0.05f); |
| 362 | vertices << QVector3D(x1, y1, -0.05f); |
| 363 | vertices << QVector3D(x2, y2, +0.05f); |
| 364 | |
| 365 | QVector3D n = QVector3D::normal |
| 366 | (v1: QVector3D(x2 - x1, y2 - y1, 0.0f), v2: QVector3D(0.0f, 0.0f, -0.1f)); |
| 367 | |
| 368 | normals << n; |
| 369 | normals << n; |
| 370 | normals << n; |
| 371 | |
| 372 | normals << n; |
| 373 | normals << n; |
| 374 | normals << n; |
| 375 | } |
| 376 | |