1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qquickshadereffectmesh_p.h"
#include <QtQuick/qsggeometry.h>
#include "qquickshadereffect_p.h"
QT_BEGIN_NAMESPACE
QQuickShaderEffectMesh::QQuickShaderEffectMesh(QObject *parent)
: QObject(parent)
{
}
/*!
\qmlclass GridMesh QQuickGridMesh
\inqmlmodule QtQuick 2
\ingroup qml-utility-elements
\brief GridMesh defines a mesh with vertices arranged in a grid.
GridMesh defines a rectangular mesh consisting of vertices arranged in an
evenly spaced grid. It is used to generate \l{QSGGeometry}{geometry}.
The grid resolution is specified with the \l resolution property.
*/
QQuickGridMesh::QQuickGridMesh(QObject *parent)
: QQuickShaderEffectMesh(parent)
, m_resolution(1, 1)
{
connect(this, SIGNAL(resolutionChanged()), this, SIGNAL(geometryChanged()));
}
QSGGeometry *QQuickGridMesh::updateGeometry(QSGGeometry *geometry, const QVector<QByteArray> &attributes, const QRectF &dstRect)
{
int vmesh = m_resolution.height();
int hmesh = m_resolution.width();
int attrCount = attributes.count();
int positionIndex = attributes.indexOf(qtPositionAttributeName());
int texCoordIndex = attributes.indexOf(qtTexCoordAttributeName());
if (!geometry) {
switch (attrCount) {
case 0:
m_log = QLatin1String("Error: No attributes specified.");
return 0;
case 1:
if (positionIndex != 0) {
m_log = QLatin1String("Error: Missing \'");
m_log += QLatin1String(qtPositionAttributeName());
m_log += QLatin1String("\' attribute.\n");
return 0;
}
break;
case 2:
if (positionIndex == -1 || texCoordIndex == -1) {
m_log.clear();
if (positionIndex == -1) {
m_log = QLatin1String("Error: Missing \'");
m_log += QLatin1String(qtPositionAttributeName());
m_log += QLatin1String("\' attribute.\n");
}
if (texCoordIndex == -1) {
m_log += QLatin1String("Error: Missing \'");
m_log += QLatin1String(qtTexCoordAttributeName());
m_log += QLatin1String("\' attribute.\n");
}
return 0;
}
break;
default:
m_log = QLatin1String("Error: Too many attributes specified.");
return 0;
}
geometry = new QSGGeometry(attrCount == 1
? QSGGeometry::defaultAttributes_Point2D()
: QSGGeometry::defaultAttributes_TexturedPoint2D(),
(vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2),
GL_UNSIGNED_SHORT);
} else {
geometry->allocate((vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2));
}
QSGGeometry::Point2D *vdata = static_cast<QSGGeometry::Point2D *>(geometry->vertexData());
QRectF srcRect(0, 0, 1, 1);
for (int iy = 0; iy <= vmesh; ++iy) {
float fy = iy / float(vmesh);
float y = float(dstRect.top()) + fy * float(dstRect.height());
float ty = float(srcRect.top()) + fy * float(srcRect.height());
for (int ix = 0; ix <= hmesh; ++ix) {
float fx = ix / float(hmesh);
for (int ia = 0; ia < attrCount; ++ia) {
if (ia == positionIndex) {
vdata->x = float(dstRect.left()) + fx * float(dstRect.width());
vdata->y = y;
++vdata;
} else {
vdata->x = float(srcRect.left()) + fx * float(srcRect.width());
vdata->y = ty;
++vdata;
}
}
}
}
quint16 *indices = (quint16 *)geometry->indexDataAsUShort();
int i = 0;
for (int iy = 0; iy < vmesh; ++iy) {
*(indices++) = i + hmesh + 1;
for (int ix = 0; ix <= hmesh; ++ix, ++i) {
*(indices++) = i + hmesh + 1;
*(indices++) = i;
}
*(indices++) = i - 1;
}
return geometry;
}
/*!
\qmlproperty size QtQuick2::GridMesh::resolution
This property holds the grid resolution. The resolution's width and height
specify the number of cells or spacings between vertices horizontally and
vertically respectively. The minimum and default is 1x1, which corresponds
to four vertices in total, one in each corner.
For non-linear vertex transformations, you probably want to set the
resolution higher.
\row
\o \image declarative-gridmesh.png
\o \qml
import QtQuick 2.0
ShaderEffect {
width: 200
height: 200
mesh: GridMesh {
resolution: Qt.size(20, 20)
}
property variant source: Image {
source: "qt-logo.png"
sourceSize { width: 200; height: 200 }
smooth: true
}
vertexShader: "
uniform highp mat4 qt_Matrix;
attribute highp vec4 qt_Vertex;
attribute highp vec2 qt_MultiTexCoord0;
varying highp vec2 qt_TexCoord0;
uniform highp float width;
void main() {
highp vec4 pos = qt_Vertex;
highp float d = .5 * smoothstep(0., 1., qt_MultiTexCoord0.y);
pos.x = width * mix(d, 1.0 - d, qt_MultiTexCoord0.x);
gl_Position = qt_Matrix * pos;
qt_TexCoord0 = qt_MultiTexCoord0;
}"
}
\endqml
\endrow
*/
void QQuickGridMesh::setResolution(const QSize &res)
{
if (res == m_resolution)
return;
if (res.width() < 1 || res.height() < 1) {
return;
}
m_resolution = res;
emit resolutionChanged();
}
QSize QQuickGridMesh::resolution() const
{
return m_resolution;
}
QT_END_NAMESPACE
|