Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgshadereffectmesh.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgshadereffectmesh_p.h"
43 #include "qsggeometry.h"
44 #include "qsgshadereffectitem_p.h"
45
46 QT_BEGIN_NAMESPACE
47
48 QSGShaderEffectMesh::QSGShaderEffectMesh(QObject *parent)
49     : QObject(parent)
50 {
51 }
52
53
54 QSGGridMesh::QSGGridMesh(QObject *parent)
55     : QSGShaderEffectMesh(parent)
56     , m_resolution(1, 1)
57 {
58     connect(this, SIGNAL(resolutionChanged()), this, SIGNAL(geometryChanged()));
59 }
60
61 QSGGeometry *QSGGridMesh::updateGeometry(QSGGeometry *geometry, const QVector<QByteArray> &attributes, const QRectF &dstRect) const
62 {
63     int vmesh = m_resolution.height();
64     int hmesh = m_resolution.width();
65     int attrCount = attributes.count();
66
67     if (!geometry) {
68         bool error = true;
69         switch (attrCount) {
70         case 0:
71             qWarning("QSGGridMesh:: No attributes specified.");
72             break;
73         case 1:
74             if (attributes.at(0) == qtPositionAttributeName()) {
75                 error = false;
76                 break;
77             }
78             qWarning("QSGGridMesh:: Missing \'%s\' attribute.",
79                      qtPositionAttributeName());
80             break;
81         case 2:
82             if (attributes.contains(qtPositionAttributeName())
83                 && attributes.contains(qtTexCoordAttributeName()))
84             {
85                 error = false;
86                 break;
87             }
88             qWarning("QSGGridMesh:: Missing \'%s\' or \'%s\' attribute.",
89                      qtPositionAttributeName(), qtTexCoordAttributeName());
90             break;
91         default:
92             qWarning("QSGGridMesh:: Too many attributes specified.");
93             break;;
94         }
95
96         if (error) {
97             delete geometry;
98             return 0;
99         }
100
101         geometry = new QSGGeometry(attrCount == 1
102                                    ? QSGGeometry::defaultAttributes_Point2D()
103                                    : QSGGeometry::defaultAttributes_TexturedPoint2D(),
104                                    (vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2),
105                                    GL_UNSIGNED_SHORT);
106
107     } else {
108         geometry->allocate((vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2));
109     }
110
111     QSGGeometry::Point2D *vdata = static_cast<QSGGeometry::Point2D *>(geometry->vertexData());
112
113     bool positionFirst = attributes.at(0) == qtPositionAttributeName();
114
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());
125                     vdata->y = y;
126                     ++vdata;
127                 } else {
128                     vdata->x = float(srcRect.left()) + fx * float(srcRect.width());
129                     vdata->y = ty;
130                     ++vdata;
131                 }
132             }
133         }
134     }
135
136     quint16 *indices = (quint16 *)geometry->indexDataAsUShort();
137     int i = 0;
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;
142             *(indices++) = i;
143         }
144         *(indices++) = i - 1;
145     }
146
147     return geometry;
148 }
149
150 void QSGGridMesh::setResolution(const QSize &res)
151 {
152     if (res == m_resolution)
153         return;
154     if (res.width() < 1 || res.height() < 1) {
155         qWarning("QSGGridMesh: Resolution must be at least 1x1");
156         return;
157     }
158     m_resolution = res;
159     emit resolutionChanged();
160 }
161
162 QSize QSGGridMesh::resolution() const
163 {
164     return m_resolution;
165 }
166
167 QT_END_NAMESPACE