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 demonstration applications 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 "simplematerialitem.h"
52
53#include <QtQuick/qsgsimplematerial.h>
54#include <QtQuick/qsggeometry.h>
55#include <QtQuick/qsgnode.h>
56
57//! [1]
58struct State
59{
60 QColor color;
61
62 int compare(const State *other) const {
63 uint rgb = color.rgba();
64 uint otherRgb = other->color.rgba();
65
66 if (rgb == otherRgb) {
67 return 0;
68 } else if (rgb < otherRgb) {
69 return -1;
70 } else {
71 return 1;
72 }
73 }
74};
75//! [1]
76
77//! [2]
78class Shader : public QSGSimpleMaterialShader<State>
79{
80 QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State);
81//! [2] //! [3]
82public:
83
84 const char *vertexShader() const override {
85 return
86 "attribute highp vec4 aVertex; \n"
87 "attribute highp vec2 aTexCoord; \n"
88 "uniform highp mat4 qt_Matrix; \n"
89 "varying highp vec2 texCoord; \n"
90 "void main() { \n"
91 " gl_Position = qt_Matrix * aVertex; \n"
92 " texCoord = aTexCoord; \n"
93 "}";
94 }
95
96 const char *fragmentShader() const override {
97 return
98 "uniform lowp float qt_Opacity; \n"
99 "uniform lowp vec4 color; \n"
100 "varying highp vec2 texCoord; \n"
101 "void main () \n"
102 "{ \n"
103 " gl_FragColor = texCoord.y * texCoord.x * color * qt_Opacity; \n"
104 "}";
105 }
106//! [3] //! [4]
107 QList<QByteArray> attributes() const override
108 {
109 return QList<QByteArray>() << "aVertex" << "aTexCoord";
110 }
111//! [4] //! [5]
112 void updateState(const State *state, const State *) override
113 {
114 program()->setUniformValue(location: id_color, color: state->color);
115 }
116//! [5] //! [6]
117 void resolveUniforms() override
118 {
119 id_color = program()->uniformLocation(name: "color");
120 }
121
122private:
123 int id_color;
124//! [6]
125};
126
127
128//! [7]
129class ColorNode : public QSGGeometryNode
130{
131public:
132 ColorNode()
133 : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
134 {
135 setGeometry(&m_geometry);
136
137 QSGSimpleMaterial<State> *material = Shader::createMaterial();
138 material->setFlag(flags: QSGMaterial::Blending);
139 setMaterial(material);
140 setFlag(OwnsMaterial);
141 }
142
143 QSGGeometry m_geometry;
144};
145//! [7]
146
147void SimpleMaterialItem::setColor(const QColor &color) {
148 if (m_color != color) {
149 m_color = color;
150 emit colorChanged();
151 update();
152 }
153}
154
155//! [9]
156QSGNode *SimpleMaterialItem::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *)
157{
158 ColorNode *n = static_cast<ColorNode *>(node);
159 if (!node)
160 n = new ColorNode();
161
162 QSGGeometry::updateTexturedRectGeometry(g: n->geometry(), rect: boundingRect(), sourceRect: QRectF(0, 0, 1, 1));
163 static_cast<QSGSimpleMaterial<State>*>(n->material())->state()->color = m_color;
164
165 n->markDirty(bits: QSGNode::DirtyGeometry | QSGNode::DirtyMaterial);
166
167 return n;
168}
169//! [9]
170

source code of qtdeclarative/examples/quick/scenegraph/simplematerial/simplematerialitem.cpp