1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickshadereffectmesh_p.h"
43 #include <QtQuick/qsggeometry.h>
44 #include "qquickshadereffect_p.h"
48 QQuickShaderEffectMesh::QQuickShaderEffectMesh(QObject *parent)
54 \qmlclass GridMesh QQuickGridMesh
55 \inqmlmodule QtQuick 2
56 \ingroup qml-utility-elements
57 \brief GridMesh defines a mesh with vertices arranged in a grid.
59 GridMesh defines a rectangular mesh consisting of vertices arranged in an
60 evenly spaced grid. It is used to generate \l{QSGGeometry}{geometry}.
61 The grid resolution is specified with the \l resolution property.
64 QQuickGridMesh::QQuickGridMesh(QObject *parent)
65 : QQuickShaderEffectMesh(parent)
68 connect(this, SIGNAL(resolutionChanged()), this, SIGNAL(geometryChanged()));
71 QSGGeometry *QQuickGridMesh::updateGeometry(QSGGeometry *geometry, const QVector<QByteArray> &attributes, const QRectF &dstRect)
73 int vmesh = m_resolution.height();
74 int hmesh = m_resolution.width();
75 int attrCount = attributes.count();
77 int positionIndex = attributes.indexOf(qtPositionAttributeName());
78 int texCoordIndex = attributes.indexOf(qtTexCoordAttributeName());
83 m_log = QLatin1String("Error: No attributes specified.");
86 if (positionIndex != 0) {
87 m_log = QLatin1String("Error: Missing \'");
88 m_log += QLatin1String(qtPositionAttributeName());
89 m_log += QLatin1String("\' attribute.\n");
94 if (positionIndex == -1 || texCoordIndex == -1) {
96 if (positionIndex == -1) {
97 m_log = QLatin1String("Error: Missing \'");
98 m_log += QLatin1String(qtPositionAttributeName());
99 m_log += QLatin1String("\' attribute.\n");
101 if (texCoordIndex == -1) {
102 m_log += QLatin1String("Error: Missing \'");
103 m_log += QLatin1String(qtTexCoordAttributeName());
104 m_log += QLatin1String("\' attribute.\n");
110 m_log = QLatin1String("Error: Too many attributes specified.");
114 geometry = new QSGGeometry(attrCount == 1
115 ? QSGGeometry::defaultAttributes_Point2D()
116 : QSGGeometry::defaultAttributes_TexturedPoint2D(),
117 (vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2),
121 geometry->allocate((vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2));
124 QSGGeometry::Point2D *vdata = static_cast<QSGGeometry::Point2D *>(geometry->vertexData());
126 QRectF srcRect(0, 0, 1, 1);
127 for (int iy = 0; iy <= vmesh; ++iy) {
128 float fy = iy / float(vmesh);
129 float y = float(dstRect.top()) + fy * float(dstRect.height());
130 float ty = float(srcRect.top()) + fy * float(srcRect.height());
131 for (int ix = 0; ix <= hmesh; ++ix) {
132 float fx = ix / float(hmesh);
133 for (int ia = 0; ia < attrCount; ++ia) {
134 if (ia == positionIndex) {
135 vdata->x = float(dstRect.left()) + fx * float(dstRect.width());
139 vdata->x = float(srcRect.left()) + fx * float(srcRect.width());
147 quint16 *indices = (quint16 *)geometry->indexDataAsUShort();
149 for (int iy = 0; iy < vmesh; ++iy) {
150 *(indices++) = i + hmesh + 1;
151 for (int ix = 0; ix <= hmesh; ++ix, ++i) {
152 *(indices++) = i + hmesh + 1;
155 *(indices++) = i - 1;
162 \qmlproperty size QtQuick2::GridMesh::resolution
164 This property holds the grid resolution. The resolution's width and height
165 specify the number of cells or spacings between vertices horizontally and
166 vertically respectively. The minimum and default is 1x1, which corresponds
167 to four vertices in total, one in each corner.
168 For non-linear vertex transformations, you probably want to set the
172 \o \image declarative-gridmesh.png
180 resolution: Qt.size(20, 20)
182 property variant source: Image {
183 source: "qt-logo.png"
184 sourceSize { width: 200; height: 200 }
188 uniform highp mat4 qt_Matrix;
189 attribute highp vec4 qt_Vertex;
190 attribute highp vec2 qt_MultiTexCoord0;
191 varying highp vec2 qt_TexCoord0;
192 uniform highp float width;
194 highp vec4 pos = qt_Vertex;
195 highp float d = .5 * smoothstep(0., 1., qt_MultiTexCoord0.y);
196 pos.x = width * mix(d, 1.0 - d, qt_MultiTexCoord0.x);
197 gl_Position = qt_Matrix * pos;
198 qt_TexCoord0 = qt_MultiTexCoord0;
205 void QQuickGridMesh::setResolution(const QSize &res)
207 if (res == m_resolution)
209 if (res.width() < 1 || res.height() < 1) {
213 emit resolutionChanged();
216 QSize QQuickGridMesh::resolution() const