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 | #include "noisynode.h" |
52 | |
53 | #include <QtCore/QRandomGenerator> |
54 | #include <QtQuick/QSGSimpleMaterialShader> |
55 | #include <QtQuick/QSGTexture> |
56 | #include <QtQuick/QQuickWindow> |
57 | |
58 | #define NOISE_SIZE 64 |
59 | |
60 | struct NoisyMaterial |
61 | { |
62 | ~NoisyMaterial() { |
63 | delete texture; |
64 | } |
65 | |
66 | QColor color; |
67 | QSGTexture *texture; |
68 | }; |
69 | |
70 | class NoisyShader : public QSGSimpleMaterialShader<NoisyMaterial> |
71 | { |
72 | QSG_DECLARE_SIMPLE_SHADER(NoisyShader, NoisyMaterial) |
73 | |
74 | public: |
75 | NoisyShader() { |
76 | setShaderSourceFile(type: QOpenGLShader::Vertex, sourceFile: ":/scenegraph/graph/shaders/noisy.vsh" ); |
77 | setShaderSourceFile(type: QOpenGLShader::Fragment, sourceFile: ":/scenegraph/graph/shaders/noisy.fsh" ); |
78 | } |
79 | |
80 | QList<QByteArray> attributes() const override { return QList<QByteArray>() << "aVertex" << "aTexCoord" ; } |
81 | |
82 | void updateState(const NoisyMaterial *m, const NoisyMaterial *) override { |
83 | |
84 | // Set the color |
85 | program()->setUniformValue(location: id_color, color: m->color); |
86 | |
87 | // Bind the texture and set program to use texture unit 0 (the default) |
88 | m->texture->bind(); |
89 | |
90 | // Then set the texture size so we can adjust the texture coordinates accordingly in the |
91 | // vertex shader.. |
92 | QSize s = m->texture->textureSize(); |
93 | program()->setUniformValue(location: id_textureSize, size: QSizeF(1.0 / s.width(), 1.0 / s.height())); |
94 | } |
95 | |
96 | void resolveUniforms() override { |
97 | id_texture = program()->uniformLocation(name: "texture" ); |
98 | id_textureSize = program()->uniformLocation(name: "textureSize" ); |
99 | id_color = program()->uniformLocation(name: "color" ); |
100 | |
101 | // We will only use texture unit 0, so set it only once. |
102 | program()->setUniformValue(location: id_texture, value: 0); |
103 | } |
104 | |
105 | private: |
106 | int id_color = -1; |
107 | int id_texture = -1; |
108 | int id_textureSize = -1; |
109 | }; |
110 | |
111 | NoisyNode::NoisyNode(QQuickWindow *window) |
112 | { |
113 | // Make some noise... |
114 | QImage image(NOISE_SIZE, NOISE_SIZE, QImage::Format_RGB32); |
115 | uint *data = (uint *) image.bits(); |
116 | for (int i=0; i<NOISE_SIZE * NOISE_SIZE; ++i) { |
117 | uint g = QRandomGenerator::global()->bounded(highest: 0xff); |
118 | data[i] = 0xff000000 | (g << 16) | (g << 8) | g; |
119 | } |
120 | |
121 | QSGTexture *t = window->createTextureFromImage(image); |
122 | t->setFiltering(QSGTexture::Nearest); |
123 | t->setHorizontalWrapMode(QSGTexture::Repeat); |
124 | t->setVerticalWrapMode(QSGTexture::Repeat); |
125 | |
126 | QSGSimpleMaterial<NoisyMaterial> *m = NoisyShader::createMaterial(); |
127 | m->state()->texture = t; |
128 | m->state()->color = QColor::fromRgbF(r: 0.95, g: 0.95, b: 0.97); |
129 | m->setFlag(flags: QSGMaterial::Blending); |
130 | |
131 | setMaterial(m); |
132 | setFlag(OwnsMaterial, true); |
133 | |
134 | QSGGeometry *g = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); |
135 | QSGGeometry::updateTexturedRectGeometry(g, rect: QRect(), sourceRect: QRect()); |
136 | setGeometry(g); |
137 | setFlag(OwnsGeometry, true); |
138 | } |
139 | |
140 | void NoisyNode::setRect(const QRectF &bounds) |
141 | { |
142 | QSGGeometry::updateTexturedRectGeometry(g: geometry(), rect: bounds, sourceRect: QRectF(0, 0, 1, 1)); |
143 | markDirty(bits: QSGNode::DirtyGeometry); |
144 | } |
145 | |