File indexing completed on 2024-05-12 03:45:46
0001 /* 0002 SPDX-FileCopyrightText: 2015 The Qt Company Ltd. <https://www.qt.io/licensing/> 0003 0004 This file is part of the demonstration applications of the Qt Toolkit. 0005 0006 $QT_BEGIN_LICENSE:LGPL21$ 0007 Commercial License Usage 0008 Licensees holding valid commercial Qt licenses may use this file in 0009 accordance with the commercial license agreement provided with the 0010 Software or, alternatively, in accordance with the terms contained in 0011 a written agreement between you and The Qt Company. For licensing terms 0012 and conditions see https://www.qt.io/terms-conditions. For further 0013 information use the contact form at https://www.qt.io/contact-us. 0014 0015 GNU Lesser General Public License Usage 0016 Alternatively, this file may be used under the terms of the GNU Lesser 0017 General Public License version 2.1 or version 3 as published by the Free 0018 Software Foundation and appearing in the file LICENSE.LGPLv21 and 0019 LICENSE.LGPLv3 included in the packaging of this file. Please review the 0020 following information to ensure the GNU Lesser General Public License 0021 requirements will be met: https://www.gnu.org/licenses/lgpl.html and 0022 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 0023 0024 As a special exception, The Qt Company gives you certain additional 0025 rights. These rights are described in The Qt Company LGPL Exception 0026 version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 0027 0028 $QT_END_LICENSE$ 0029 0030 */ 0031 0032 #pragma once 0033 0034 #include <qguiapplication.h> 0035 0036 #include <qsgmaterial.h> 0037 #include <qsgnode.h> 0038 0039 #include <qquickitem.h> 0040 #include <qquickview.h> 0041 0042 #include <qsgsimplerectnode.h> 0043 0044 #include <qsgsimplematerial.h> 0045 0046 //! [1] 0047 struct State 0048 { 0049 QColor color; 0050 0051 int compare(const State *other) const 0052 { 0053 uint rgb = color.rgba(); 0054 uint otherRgb = other->color.rgba(); 0055 0056 if (rgb == otherRgb) 0057 { 0058 return 0; 0059 } 0060 else if (rgb < otherRgb) 0061 { 0062 return -1; 0063 } 0064 else 0065 { 0066 return 1; 0067 } 0068 } 0069 }; 0070 //! [1] 0071 0072 //! [2] 0073 class Shader : public QSGSimpleMaterialShader<State> 0074 { 0075 QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State) 0076 //! [2] //! [3] 0077 public: 0078 const char *vertexShader() const 0079 { 0080 return "attribute highp vec4 aVertex; \n" 0081 "attribute highp vec2 aTexCoord; \n" 0082 "uniform highp mat4 qt_Matrix; \n" 0083 "uniform highp mat4 mvp_matrix; \n" 0084 "varying highp vec2 texCoord; \n" 0085 "void main() { \n" 0086 " gl_Position = qt_Matrix * aVertex; \n" 0087 " texCoord = aTexCoord; \n" 0088 "}"; 0089 } 0090 0091 const char *fragmentShader() const 0092 { 0093 return "uniform lowp float qt_Opacity; \n" 0094 "uniform lowp vec4 color; \n" 0095 "varying highp vec2 texCoord; \n" 0096 "void main () \n" 0097 "{ \n" 0098 //"if (cos(0.1*abs(distance(vec2(100.0,100.0).xy, texCoord.xy))) + 0.5 > 0.0) { \n" 0099 "if(texCoord.x < 0.1 || texCoord.x > 0.2 && texCoord.x < 0.3) { \n" 0100 " gl_FragColor = vec4(0.0,0.0,0.0,0.0) * qt_Opacity; \n" 0101 " } else {\n" 0102 " gl_FragColor = texCoord.y * texCoord.x * color * qt_Opacity; \n" 0103 " }\n" 0104 "}"; 0105 } 0106 //! [3] //! [4] 0107 QList<QByteArray> attributes() const 0108 { 0109 return QList<QByteArray>() << "aVertex" 0110 << "aTexCoord"; 0111 } 0112 //! [4] //! [5] 0113 void updateState(const State *state, const State *) { program()->setUniformValue(id_color, state->color); } 0114 //! [5] //! [6] 0115 void resolveUniforms() { id_color = program()->uniformLocation("color"); } 0116 0117 private: 0118 int id_color; 0119 //! [6] 0120 }; 0121 0122 //! [7] 0123 class ColorNode : public QSGGeometryNode 0124 { 0125 public: 0126 ColorNode() : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4) 0127 { 0128 setGeometry(&m_geometry); 0129 0130 QSGSimpleMaterial<State> *material = Shader::createMaterial(); 0131 material->setFlag(QSGMaterial::Blending); 0132 setMaterial(material); 0133 setFlag(OwnsMaterial); 0134 } 0135 0136 QSGGeometry m_geometry; 0137 }; 0138 //! [7] 0139 0140 //! [8] 0141 class Item : public QQuickItem 0142 { 0143 Q_OBJECT 0144 0145 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) 0146 0147 public: 0148 Item() { setFlag(ItemHasContents, true); } 0149 0150 void setColor(const QColor &color) 0151 { 0152 if (m_color != color) 0153 { 0154 m_color = color; 0155 emit colorChanged(); 0156 update(); 0157 } 0158 } 0159 QColor color() const { return m_color; } 0160 0161 signals: 0162 void colorChanged(); 0163 0164 private: 0165 QColor m_color; 0166 0167 //! [8] //! [9] 0168 public: 0169 QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) 0170 { 0171 ColorNode *n = static_cast<ColorNode *>(node); 0172 if (!node) 0173 n = new ColorNode(); 0174 0175 QSGGeometry::updateTexturedRectGeometry(n->geometry(), boundingRect(), QRectF(0, 0, 1, 1)); 0176 static_cast<QSGSimpleMaterial<State> *>(n->material())->state()->color = m_color; 0177 0178 n->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial); 0179 0180 return n; 0181 } 0182 }; 0183 0184 //#include "simplematerial.moc" 0185 //! [11]