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 Qt scene graph research project.
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 ****************************************************************************/
45 #include <QtGui/qopengl.h>
52 QT_MODULE(Declarative)
54 class QSGGeometryData;
56 class Q_DECLARATIVE_EXPORT QSGGeometry
66 uint isVertexCoordinate : 1;
67 uint migrateYourCodeToUseTheCreateFunction: 31; // ### Remove before release
69 static Attribute create(int pos, int tupleSize, int primitiveType, bool isPosition = false);
75 const Attribute *attributes;
80 void set(float nx, float ny) {
84 struct TexturedPoint2D {
87 void set(float nx, float ny, float ntx, float nty) {
88 x = nx; y = ny; tx = ntx; ty = nty;
91 struct ColoredPoint2D {
93 unsigned char r, g, b, a;
94 void set(float nx, float ny, uchar nr, uchar ng, uchar nb, uchar na) {
96 r = nr; g = ng, b = nb; a = na;
100 static const AttributeSet &defaultAttributes_Point2D();
101 static const AttributeSet &defaultAttributes_TexturedPoint2D();
102 static const AttributeSet &defaultAttributes_ColoredPoint2D();
105 AlwaysUploadPattern = 0,
111 QSGGeometry(const QSGGeometry::AttributeSet &attribs,
114 int indexType = GL_UNSIGNED_SHORT);
115 virtual ~QSGGeometry();
117 void setDrawingMode(GLenum mode);
118 inline GLenum drawingMode() const { return m_drawing_mode; }
120 void allocate(int vertexCount, int indexCount = 0);
122 int vertexCount() const { return m_vertex_count; }
124 void *vertexData() { return m_data; }
125 inline Point2D *vertexDataAsPoint2D();
126 inline TexturedPoint2D *vertexDataAsTexturedPoint2D();
127 inline ColoredPoint2D *vertexDataAsColoredPoint2D();
129 inline const void *vertexData() const { return m_data; }
130 inline const Point2D *vertexDataAsPoint2D() const;
131 inline const TexturedPoint2D *vertexDataAsTexturedPoint2D() const;
132 inline const ColoredPoint2D *vertexDataAsColoredPoint2D() const;
134 inline int indexType() const { return m_index_type; }
136 int indexCount() const { return m_index_count; }
139 inline uint *indexDataAsUInt();
140 inline quint16 *indexDataAsUShort();
142 inline int sizeOfIndex() const;
144 const void *indexData() const;
145 inline const uint *indexDataAsUInt() const;
146 inline const quint16 *indexDataAsUShort() const;
148 inline int attributeCount() const { return m_attributes.count; }
149 inline const Attribute *attributes() const { return m_attributes.attributes; }
150 inline int sizeOfVertex() const { return m_attributes.stride; }
152 static void updateRectGeometry(QSGGeometry *g, const QRectF &rect);
153 static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect);
155 void setIndexDataPattern(DataPattern p);
156 DataPattern indexDataPattern() const { return (DataPattern) m_index_usage_pattern; }
158 void setVertexDataPattern(DataPattern p);
159 DataPattern vertexDataPattern() const { return (DataPattern) m_vertex_usage_pattern; }
161 void markIndexDataDirty();
162 void markVertexDataDirty();
165 friend class QSGGeometryData;
171 const AttributeSet &m_attributes;
173 int m_index_data_offset;
175 QSGGeometryData *m_server_data;
177 uint m_owns_data : 1;
178 uint m_index_usage_pattern : 2;
179 uint m_vertex_usage_pattern : 2;
180 uint m_dirty_index_data : 1;
181 uint m_dirty_vertex_data : 1;
182 uint m_reserved_bits : 27;
184 float m_prealloc[16];
187 inline uint *QSGGeometry::indexDataAsUInt()
189 Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
190 return (uint *) indexData();
193 inline quint16 *QSGGeometry::indexDataAsUShort()
195 Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
196 return (quint16 *) indexData();
199 inline const uint *QSGGeometry::indexDataAsUInt() const
201 Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
202 return (uint *) indexData();
205 inline const quint16 *QSGGeometry::indexDataAsUShort() const
207 Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
208 return (quint16 *) indexData();
211 inline QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()
213 Q_ASSERT(m_attributes.count == 1);
214 Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
215 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
216 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
217 Q_ASSERT(m_attributes.attributes[0].position == 0);
218 return (Point2D *) m_data;
221 inline QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()
223 Q_ASSERT(m_attributes.count == 2);
224 Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
225 Q_ASSERT(m_attributes.attributes[0].position == 0);
226 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
227 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
228 Q_ASSERT(m_attributes.attributes[1].position == 1);
229 Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
230 Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
231 return (TexturedPoint2D *) m_data;
234 inline QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()
236 Q_ASSERT(m_attributes.count == 2);
237 Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
238 Q_ASSERT(m_attributes.attributes[0].position == 0);
239 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
240 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
241 Q_ASSERT(m_attributes.attributes[1].position == 1);
242 Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
243 Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
244 return (ColoredPoint2D *) m_data;
247 inline const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const
249 Q_ASSERT(m_attributes.count == 1);
250 Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
251 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
252 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
253 Q_ASSERT(m_attributes.attributes[0].position == 0);
254 return (const Point2D *) m_data;
257 inline const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const
259 Q_ASSERT(m_attributes.count == 2);
260 Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
261 Q_ASSERT(m_attributes.attributes[0].position == 0);
262 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
263 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
264 Q_ASSERT(m_attributes.attributes[1].position == 1);
265 Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
266 Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
267 return (const TexturedPoint2D *) m_data;
270 inline const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const
272 Q_ASSERT(m_attributes.count == 2);
273 Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
274 Q_ASSERT(m_attributes.attributes[0].position == 0);
275 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
276 Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
277 Q_ASSERT(m_attributes.attributes[1].position == 1);
278 Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
279 Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
280 return (const ColoredPoint2D *) m_data;
283 int QSGGeometry::sizeOfIndex() const
285 if (m_index_type == GL_UNSIGNED_SHORT) return 2;
286 else if (m_index_type == GL_UNSIGNED_BYTE) return 1;
287 else if (m_index_type == GL_UNSIGNED_INT) return 4;
295 #endif // QSGGEOMETRY_H