| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2017 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 | #ifndef FBITEM_H | 
| 52 | #define FBITEM_H | 
| 53 |  | 
| 54 | #include <QQuickFramebufferObject> | 
| 55 | #include <QOpenGLVertexArrayObject> | 
| 56 | #include <QOpenGLBuffer> | 
| 57 | #include <QOpenGLShaderProgram> | 
| 58 | #include <QVector3D> | 
| 59 | #include "logo.h" | 
| 60 |  | 
| 61 | struct StateBinder; | 
| 62 |  | 
| 63 | class FbItemRenderer : public QQuickFramebufferObject::Renderer | 
| 64 | { | 
| 65 | public: | 
| 66 |     FbItemRenderer(bool multisample); | 
| 67 |     void synchronize(QQuickFramebufferObject *item) override; | 
| 68 |     void render() override; | 
| 69 |     QOpenGLFramebufferObject *createFramebufferObject(const QSize &size) override; | 
| 70 |  | 
| 71 | private: | 
| 72 |     void ensureInit(); | 
| 73 |     void initBuf(); | 
| 74 |     void setupVertexAttribs(); | 
| 75 |     void initProgram(); | 
| 76 |     void updateDirtyUniforms(); | 
| 77 |  | 
| 78 |     bool m_inited; | 
| 79 |     bool m_multisample; | 
| 80 |     QMatrix4x4 m_proj; | 
| 81 |     QMatrix4x4 m_camera; | 
| 82 |     QMatrix4x4 m_baseWorld; | 
| 83 |     QMatrix4x4 m_world; | 
| 84 |     QOpenGLVertexArrayObject m_vao; | 
| 85 |     QOpenGLBuffer m_logoVbo; | 
| 86 |     Logo m_logo; | 
| 87 |     QScopedPointer<QOpenGLShaderProgram> m_program; | 
| 88 |     int m_projMatrixLoc; | 
| 89 |     int m_camMatrixLoc; | 
| 90 |     int m_worldMatrixLoc; | 
| 91 |     int m_normalMatrixLoc; | 
| 92 |     int m_lightPosLoc; | 
| 93 |     QVector3D m_rotation; | 
| 94 |  | 
| 95 |     enum Dirty { | 
| 96 |         DirtyProjection = 0x01, | 
| 97 |         DirtyCamera = 0x02, | 
| 98 |         DirtyWorld = 0x04, | 
| 99 |         DirtyLight = 0x08, | 
| 100 |         DirtyAll = 0xFF | 
| 101 |     }; | 
| 102 |     int m_dirty; | 
| 103 |  | 
| 104 |     friend struct StateBinder; | 
| 105 | }; | 
| 106 |  | 
| 107 | class FbItem : public QQuickFramebufferObject | 
| 108 | { | 
| 109 |     Q_OBJECT | 
| 110 |     Q_PROPERTY(QVector3D eye READ eye WRITE setEye) | 
| 111 |     Q_PROPERTY(QVector3D target READ target WRITE setTarget) | 
| 112 |     Q_PROPERTY(QVector3D rotation READ rotation WRITE setRotation) | 
| 113 |     Q_PROPERTY(bool multisample READ multisample WRITE setMultisample) | 
| 114 |     QML_ELEMENT | 
| 115 |  | 
| 116 | public: | 
| 117 |     explicit FbItem(QQuickItem *parent = 0); | 
| 118 |  | 
| 119 |     QQuickFramebufferObject::Renderer *createRenderer() const override; | 
| 120 |  | 
| 121 |     QVector3D eye() const { return m_eye; } | 
| 122 |     void setEye(const QVector3D &v); | 
| 123 |     QVector3D target() const { return m_target; } | 
| 124 |     void setTarget(const QVector3D &v); | 
| 125 |  | 
| 126 |     QVector3D rotation() const { return m_rotation; } | 
| 127 |     void setRotation(const QVector3D &v); | 
| 128 |  | 
| 129 |     enum SyncState { | 
| 130 |         CameraNeedsSync = 0x01, | 
| 131 |         RotationNeedsSync = 0x02, | 
| 132 |         AllNeedsSync = 0xFF | 
| 133 |     }; | 
| 134 |     int swapSyncState(); | 
| 135 |  | 
| 136 |     bool multisample() const { return m_multisample; } | 
| 137 |     void setMultisample(bool m) { m_multisample = m; } | 
| 138 |  | 
| 139 | private: | 
| 140 |     QVector3D m_eye; | 
| 141 |     QVector3D m_target; | 
| 142 |     QVector3D m_rotation; | 
| 143 |     int m_syncState; | 
| 144 |     bool m_multisample; | 
| 145 | }; | 
| 146 |  | 
| 147 | #endif // FBITEM_H | 
| 148 |  |