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 <QtOpenGL/qgl.h>
51 QT_MODULE(Declarative)
53 class Q_DECLARATIVE_EXPORT QSGGeometry
66 const Attribute *attributes;
71 void set(float nx, float ny) {
75 struct TexturedPoint2D {
78 void set(float nx, float ny, float ntx, float nty) {
79 x = nx; y = ny; tx = ntx; ty = nty;
82 struct ColoredPoint2D {
84 unsigned char r, g, b, a;
85 void set(float nx, float ny, uchar nr, uchar ng, uchar nb, uchar na) {
87 r = nr; g = ng, b = nb; a = na;
91 static const AttributeSet &defaultAttributes_Point2D();
92 static const AttributeSet &defaultAttributes_TexturedPoint2D();
93 static const AttributeSet &defaultAttributes_ColoredPoint2D();
95 QSGGeometry(const QSGGeometry::AttributeSet &attribs,
98 int indexType = GL_UNSIGNED_SHORT);
99 virtual ~QSGGeometry();
101 void setDrawingMode(GLenum mode);
102 inline GLenum drawingMode() const { return m_drawing_mode; }
104 void allocate(int vertexCount, int indexCount = 0);
106 int vertexCount() const { return m_vertex_count; }
108 void *vertexData() { return m_data; }
109 inline Point2D *vertexDataAsPoint2D();
110 inline TexturedPoint2D *vertexDataAsTexturedPoint2D();
111 inline ColoredPoint2D *vertexDataAsColoredPoint2D();
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;
118 inline int indexType() const { return m_index_type; }
120 int indexCount() const { return m_index_count; }
123 inline uint *indexDataAsUInt();
124 inline quint16 *indexDataAsUShort();
126 const void *indexData() const;
127 inline const uint *indexDataAsUInt() const;
128 inline const quint16 *indexDataAsUShort() const;
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; }
134 static void updateRectGeometry(QSGGeometry *g, const QRectF &rect);
135 static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect);
142 const AttributeSet &m_attributes;
144 int m_index_data_offset;
146 void *m_reserved_pointer;
148 uint m_owns_data : 1;
149 uint m_reserved_bits : 31;
151 float m_prealloc[16];
154 inline uint *QSGGeometry::indexDataAsUInt()
156 Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
157 return (uint *) indexData();
160 inline quint16 *QSGGeometry::indexDataAsUShort()
162 Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
163 return (quint16 *) indexData();
166 inline const uint *QSGGeometry::indexDataAsUInt() const
168 Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
169 return (uint *) indexData();
172 inline const quint16 *QSGGeometry::indexDataAsUShort() const
174 Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
175 return (quint16 *) indexData();
178 inline QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()
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;
188 inline QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()
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;
201 inline QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()
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;
214 inline const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const
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;
224 inline const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const
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;
237 inline const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const
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;
254 #endif // QSGGEOMETRY_H