Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / scenegraph / coreapi / qsggeometry.h
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 Qt scene graph research project.
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 #ifndef QSGGEOMETRY_H
43 #define QSGGEOMETRY_H
44
45 #include <QtOpenGL/qgl.h>
46
47 QT_BEGIN_HEADER
48
49 QT_BEGIN_NAMESPACE
50
51 QT_MODULE(Declarative)
52
53 class Q_DECLARATIVE_EXPORT QSGGeometry
54 {
55 public:
56     struct Attribute
57     {
58         int position;
59         int tupleSize;
60         int type;
61     };
62
63     struct AttributeSet {
64         int count;
65         int stride;
66         const Attribute *attributes;
67     };
68
69     struct Point2D {
70         float x, y;
71         void set(float nx, float ny) {
72             x = nx; y = ny;
73         }
74     };
75     struct TexturedPoint2D {
76         float x, y;
77         float tx, ty;
78         void set(float nx, float ny, float ntx, float nty) {
79             x = nx; y = ny; tx = ntx; ty = nty;
80         }
81     };
82     struct ColoredPoint2D {
83         float x, y;
84         unsigned char r, g, b, a;
85         void set(float nx, float ny, uchar nr, uchar ng, uchar nb, uchar na) {
86             x = nx; y = ny;
87             r = nr; g = ng, b = nb; a = na;
88         }
89     };
90
91     static const AttributeSet &defaultAttributes_Point2D();
92     static const AttributeSet &defaultAttributes_TexturedPoint2D();
93     static const AttributeSet &defaultAttributes_ColoredPoint2D();
94
95     QSGGeometry(const QSGGeometry::AttributeSet &attribs,
96                 int vertexCount,
97                 int indexCount = 0,
98                 int indexType = GL_UNSIGNED_SHORT);
99     virtual ~QSGGeometry();
100
101     void setDrawingMode(GLenum mode);
102     inline GLenum drawingMode() const { return m_drawing_mode; }
103
104     void allocate(int vertexCount, int indexCount = 0);
105
106     int vertexCount() const { return m_vertex_count; }
107
108     void *vertexData() { return m_data; }
109     inline Point2D *vertexDataAsPoint2D();
110     inline TexturedPoint2D *vertexDataAsTexturedPoint2D();
111     inline ColoredPoint2D *vertexDataAsColoredPoint2D();
112
113     inline const void *vertexData() const { return m_data; }
114     inline const Point2D *vertexDataAsPoint2D() const;
115     inline const TexturedPoint2D *vertexDataAsTexturedPoint2D() const;
116     inline const ColoredPoint2D *vertexDataAsColoredPoint2D() const;
117
118     inline int indexType() const { return m_index_type; }
119
120     int indexCount() const { return m_index_count; }
121
122     void *indexData();
123     inline uint *indexDataAsUInt();
124     inline quint16 *indexDataAsUShort();
125
126     const void *indexData() const;
127     inline const uint *indexDataAsUInt() const;
128     inline const quint16 *indexDataAsUShort() const;
129
130     inline int attributeCount() const { return m_attributes.count; }
131     inline const Attribute *attributes() const { return m_attributes.attributes; }
132     inline int stride() const { return m_attributes.stride; }
133
134     static void updateRectGeometry(QSGGeometry *g, const QRectF &rect);
135     static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect);
136
137 private:
138     int m_drawing_mode;
139     int m_vertex_count;
140     int m_index_count;
141     int m_index_type;
142     const AttributeSet &m_attributes;
143     void *m_data;
144     int m_index_data_offset;
145
146     void *m_reserved_pointer;
147
148     uint m_owns_data : 1;
149     uint m_reserved_bits : 31;
150
151     float m_prealloc[16];
152 };
153
154 inline uint *QSGGeometry::indexDataAsUInt()
155 {
156     Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
157     return (uint *) indexData();
158 }
159
160 inline quint16 *QSGGeometry::indexDataAsUShort()
161 {
162     Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
163     return (quint16 *) indexData();
164 }
165
166 inline const uint *QSGGeometry::indexDataAsUInt() const
167 {
168     Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
169     return (uint *) indexData();
170 }
171
172 inline const quint16 *QSGGeometry::indexDataAsUShort() const
173 {
174     Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
175     return (quint16 *) indexData();
176 }
177
178 inline QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()
179 {
180     Q_ASSERT(m_attributes.count == 1);
181     Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
182     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
183     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
184     Q_ASSERT(m_attributes.attributes[0].position == 0);
185     return (Point2D *) m_data;
186 }
187
188 inline QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()
189 {
190     Q_ASSERT(m_attributes.count == 2);
191     Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
192     Q_ASSERT(m_attributes.attributes[0].position == 0);
193     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
194     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
195     Q_ASSERT(m_attributes.attributes[1].position == 1);
196     Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
197     Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
198     return (TexturedPoint2D *) m_data;
199 }
200
201 inline QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()
202 {
203     Q_ASSERT(m_attributes.count == 2);
204     Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
205     Q_ASSERT(m_attributes.attributes[0].position == 0);
206     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
207     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
208     Q_ASSERT(m_attributes.attributes[1].position == 1);
209     Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
210     Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
211     return (ColoredPoint2D *) m_data;
212 }
213
214 inline const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const
215 {
216     Q_ASSERT(m_attributes.count == 1);
217     Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
218     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
219     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
220     Q_ASSERT(m_attributes.attributes[0].position == 0);
221     return (const Point2D *) m_data;
222 }
223
224 inline const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const
225 {
226     Q_ASSERT(m_attributes.count == 2);
227     Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
228     Q_ASSERT(m_attributes.attributes[0].position == 0);
229     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
230     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
231     Q_ASSERT(m_attributes.attributes[1].position == 1);
232     Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
233     Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
234     return (const TexturedPoint2D *) m_data;
235 }
236
237 inline const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const
238 {
239     Q_ASSERT(m_attributes.count == 2);
240     Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
241     Q_ASSERT(m_attributes.attributes[0].position == 0);
242     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
243     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
244     Q_ASSERT(m_attributes.attributes[1].position == 1);
245     Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
246     Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
247     return (const ColoredPoint2D *) m_data;
248 }
249
250 QT_END_NAMESPACE
251
252 QT_END_HEADER
253
254 #endif // QSGGEOMETRY_H