1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the Qt scene graph research project.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtQuick/qtquickglobal.h>
46 #include <QtGui/qopengl.h>
53 class QSGGeometryData;
55 class Q_QUICK_EXPORT QSGGeometry
65 uint isVertexCoordinate : 1;
66 uint migrateYourCodeToUseTheCreateFunction: 31; // ### Remove before release
68 static Attribute create(int pos, int tupleSize, int primitiveType, bool isPosition = false);
74 const Attribute *attributes;
79 void set(float nx, float ny) {
83 struct TexturedPoint2D {
86 void set(float nx, float ny, float ntx, float nty) {
87 x = nx; y = ny; tx = ntx; ty = nty;
90 struct ColoredPoint2D {
92 unsigned char r, g, b, a;
93 void set(float nx, float ny, uchar nr, uchar ng, uchar nb, uchar na) {
95 r = nr; g = ng, b = nb; a = na;
99 static const AttributeSet &defaultAttributes_Point2D();
100 static const AttributeSet &defaultAttributes_TexturedPoint2D();
101 static const AttributeSet &defaultAttributes_ColoredPoint2D();
104 AlwaysUploadPattern = 0,
110 QSGGeometry(const QSGGeometry::AttributeSet &attribs,
113 int indexType = GL_UNSIGNED_SHORT);
114 virtual ~QSGGeometry();
116 void setDrawingMode(GLenum mode);
117 inline GLenum drawingMode() const { return m_drawing_mode; }
119 void allocate(int vertexCount, int indexCount = 0);
121 int vertexCount() const { return m_vertex_count; }
123 void *vertexData() { return m_data; }
124 inline Point2D *vertexDataAsPoint2D();
125 inline TexturedPoint2D *vertexDataAsTexturedPoint2D();
126 inline ColoredPoint2D *vertexDataAsColoredPoint2D();
128 inline const void *vertexData() const { return m_data; }
129 inline const Point2D *vertexDataAsPoint2D() const;
130 inline const TexturedPoint2D *vertexDataAsTexturedPoint2D() const;
131 inline const ColoredPoint2D *vertexDataAsColoredPoint2D() const;
133 inline int indexType() const { return m_index_type; }
135 int indexCount() const { return m_index_count; }
138 inline uint *indexDataAsUInt();
139 inline quint16 *indexDataAsUShort();
141 inline int sizeOfIndex() const;
143 const void *indexData() const;
144 inline const uint *indexDataAsUInt() const;
145 inline const quint16 *indexDataAsUShort() const;
147 inline int attributeCount() const { return m_attributes.count; }
148 inline const Attribute *attributes() const { return m_attributes.attributes; }
149 inline int sizeOfVertex() const { return m_attributes.stride; }
151 static void updateRectGeometry(QSGGeometry *g, const QRectF &rect);
152 static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect);
154 void setIndexDataPattern(DataPattern p);
155 DataPattern indexDataPattern() const { return (DataPattern) m_index_usage_pattern; }
157 void setVertexDataPattern(DataPattern p);
158 DataPattern vertexDataPattern() const { return (DataPattern) m_vertex_usage_pattern; }
160 void markIndexDataDirty();
161 void markVertexDataDirty();
163 float lineWidth() const;
164 void setLineWidth(float w);
167 friend class QSGGeometryData;
173 const AttributeSet &m_attributes;
175 int m_index_data_offset;
177 QSGGeometryData *m_server_data;
179 uint m_owns_data : 1;
180 uint m_index_usage_pattern : 2;
181 uint m_vertex_usage_pattern : 2;
182 uint m_dirty_index_data : 1;
183 uint m_dirty_vertex_data : 1;
184 uint m_reserved_bits : 27;
186 float m_prealloc[16];
191 inline uint *QSGGeometry::indexDataAsUInt()
193 Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
194 return (uint *) indexData();
197 inline quint16 *QSGGeometry::indexDataAsUShort()
199 Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
200 return (quint16 *) indexData();
203 inline const uint *QSGGeometry::indexDataAsUInt() const
205 Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
206 return (uint *) indexData();
209 inline const quint16 *QSGGeometry::indexDataAsUShort() const
211 Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
212 return (quint16 *) indexData();
215 inline QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()
217 Q_ASSERT(m_attributes.count == 1);
218 Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
219 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
220 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
221 Q_ASSERT(m_attributes.attributes[0].position == 0);
222 return (Point2D *) m_data;
225 inline QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()
227 Q_ASSERT(m_attributes.count == 2);
228 Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
229 Q_ASSERT(m_attributes.attributes[0].position == 0);
230 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
231 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
232 Q_ASSERT(m_attributes.attributes[1].position == 1);
233 Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
234 Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
235 return (TexturedPoint2D *) m_data;
238 inline QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()
240 Q_ASSERT(m_attributes.count == 2);
241 Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
242 Q_ASSERT(m_attributes.attributes[0].position == 0);
243 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
244 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
245 Q_ASSERT(m_attributes.attributes[1].position == 1);
246 Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
247 Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
248 return (ColoredPoint2D *) m_data;
251 inline const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const
253 Q_ASSERT(m_attributes.count == 1);
254 Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
255 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
256 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
257 Q_ASSERT(m_attributes.attributes[0].position == 0);
258 return (const Point2D *) m_data;
261 inline const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const
263 Q_ASSERT(m_attributes.count == 2);
264 Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
265 Q_ASSERT(m_attributes.attributes[0].position == 0);
266 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
267 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
268 Q_ASSERT(m_attributes.attributes[1].position == 1);
269 Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
270 Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
271 return (const TexturedPoint2D *) m_data;
274 inline const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const
276 Q_ASSERT(m_attributes.count == 2);
277 Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
278 Q_ASSERT(m_attributes.attributes[0].position == 0);
279 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
280 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
281 Q_ASSERT(m_attributes.attributes[1].position == 1);
282 Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
283 Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
284 return (const ColoredPoint2D *) m_data;
287 int QSGGeometry::sizeOfIndex() const
289 if (m_index_type == GL_UNSIGNED_SHORT) return 2;
290 else if (m_index_type == GL_UNSIGNED_BYTE) return 1;
291 else if (m_index_type == GL_UNSIGNED_INT) return 4;
299 #endif // QSGGEOMETRY_H