1 /****************************************************************************
3 ** Copyright (C) 2010 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qsgshadereffectmesh_p.h"
43 #include "qsggeometry.h"
44 #include "qsgshadereffectitem_p.h"
48 QSGShaderEffectMesh::QSGShaderEffectMesh(QObject *parent)
54 QSGGridMesh::QSGGridMesh(QObject *parent)
55 : QSGShaderEffectMesh(parent)
58 connect(this, SIGNAL(resolutionChanged()), this, SIGNAL(geometryChanged()));
61 QSGGeometry *QSGGridMesh::updateGeometry(QSGGeometry *geometry, const QVector<QByteArray> &attributes, const QRectF &dstRect) const
63 int vmesh = m_resolution.height();
64 int hmesh = m_resolution.width();
65 int attrCount = attributes.count();
71 qWarning("QSGGridMesh:: No attributes specified.");
74 if (attributes.at(0) == qtPositionAttributeName()) {
78 qWarning("QSGGridMesh:: Missing \'%s\' attribute.",
79 qtPositionAttributeName());
82 if (attributes.contains(qtPositionAttributeName())
83 && attributes.contains(qtTexCoordAttributeName()))
88 qWarning("QSGGridMesh:: Missing \'%s\' or \'%s\' attribute.",
89 qtPositionAttributeName(), qtTexCoordAttributeName());
92 qWarning("QSGGridMesh:: Too many attributes specified.");
101 geometry = new QSGGeometry(attrCount == 1
102 ? QSGGeometry::defaultAttributes_Point2D()
103 : QSGGeometry::defaultAttributes_TexturedPoint2D(),
104 (vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2),
108 geometry->allocate((vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2));
111 QSGGeometry::Point2D *vdata = static_cast<QSGGeometry::Point2D *>(geometry->vertexData());
113 bool positionFirst = attributes.at(0) == qtPositionAttributeName();
115 QRectF srcRect(0, 1, 1, -1);
116 for (int iy = 0; iy <= vmesh; ++iy) {
117 float fy = iy / float(vmesh);
118 float y = float(dstRect.top()) + fy * float(dstRect.height());
119 float ty = float(srcRect.top()) + fy * float(srcRect.height());
120 for (int ix = 0; ix <= hmesh; ++ix) {
121 float fx = ix / float(hmesh);
122 for (int ia = 0; ia < attrCount; ++ia) {
123 if (positionFirst == (ia == 0)) {
124 vdata->x = float(dstRect.left()) + fx * float(dstRect.width());
128 vdata->x = float(srcRect.left()) + fx * float(srcRect.width());
136 quint16 *indices = (quint16 *)geometry->indexDataAsUShort();
138 for (int iy = 0; iy < vmesh; ++iy) {
139 *(indices++) = i + hmesh + 1;
140 for (int ix = 0; ix <= hmesh; ++ix, ++i) {
141 *(indices++) = i + hmesh + 1;
144 *(indices++) = i - 1;
150 void QSGGridMesh::setResolution(const QSize &res)
152 if (res == m_resolution)
154 if (res.width() < 1 || res.height() < 1) {
155 qWarning("QSGGridMesh: Resolution must be at least 1x1");
159 emit resolutionChanged();
162 QSize QSGGridMesh::resolution() const