Qt 4.8
qsggeometry.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QML Shaders plugin of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsggeometry.h"
43 
45 
46 
48 {
49  static Attribute data[] = {
50  { 0, 2, GL_FLOAT }
51  };
52  static AttributeSet attrs = { 1, sizeof(float) * 2, data };
53  return attrs;
54 }
55 
56 
58 {
59  static Attribute data[] = {
60  { 0, 2, GL_FLOAT },
61  { 1, 2, GL_FLOAT }
62  };
63  static AttributeSet attrs = { 2, sizeof(float) * 4, data };
64  return attrs;
65 }
66 
67 
69 {
70  static Attribute data[] = {
71  { 0, 2, GL_FLOAT },
72  { 1, 4, GL_UNSIGNED_BYTE }
73  };
74  static AttributeSet attrs = { 2, 2 * sizeof(float) + 4 * sizeof(char), data };
75  return attrs;
76 }
77 
78 
79 
81  int vertexCount,
82  int indexCount,
83  int indexType)
85  , m_vertex_count(0)
86  , m_index_count(0)
87  , m_index_type(indexType)
88  , m_attributes(attributes)
89  , m_data(0)
91  , m_owns_data(false)
92 {
95 
96  // Because allocate reads m_vertex_count, m_index_count and m_owns_data, these
97  // need to be set before calling allocate...
98  allocate(vertexCount, indexCount);
99 }
100 
102 {
103  if (m_owns_data)
104  qFree(m_data);
105 }
106 
108 {
109  return m_index_data_offset < 0
110  ? 0
111  : ((char *) m_data + m_index_data_offset);
112 }
113 
114 const void *QSGGeometry::indexData() const
115 {
116  return m_index_data_offset < 0
117  ? 0
118  : ((char *) m_data + m_index_data_offset);
119 }
120 
122 {
123  m_drawing_mode = mode;
124 }
125 
127 {
128  if (vertexCount == m_vertex_count && indexCount == m_index_count)
129  return;
130 
133 
134  bool canUsePrealloc = m_index_count <= 0;
135  int vertexByteSize = m_attributes.stride * m_vertex_count;
136 
137  if (m_owns_data)
138  qFree(m_data);
139 
140  if (canUsePrealloc && vertexByteSize <= (int) sizeof(m_prealloc)) {
141  m_data = (void *) &m_prealloc[0];
142  m_index_data_offset = -1;
143  m_owns_data = false;
144  } else {
146  int indexByteSize = indexCount * (m_index_type == GL_UNSIGNED_SHORT ? sizeof(quint16) : sizeof(quint32));
147  m_data = (void *) qMalloc(vertexByteSize + indexByteSize);
148  m_index_data_offset = vertexByteSize;
149  m_owns_data = true;
150  }
151 
152 }
153 
155 {
156  Point2D *v = g->vertexDataAsPoint2D();
157  v[0].x = rect.left();
158  v[0].y = rect.top();
159 
160  v[1].x = rect.right();
161  v[1].y = rect.top();
162 
163  v[2].x = rect.left();
164  v[2].y = rect.bottom();
165 
166  v[3].x = rect.right();
167  v[3].y = rect.bottom();
168 }
169 
170 void QSGGeometry::updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &textureRect)
171 {
173  v[0].x = rect.left();
174  v[0].y = rect.top();
175  v[0].tx = textureRect.left();
176  v[0].ty = textureRect.top();
177 
178  v[1].x = rect.right();
179  v[1].y = rect.top();
180  v[1].tx = textureRect.right();
181  v[1].ty = textureRect.top();
182 
183  v[2].x = rect.left();
184  v[2].y = rect.bottom();
185  v[2].tx = textureRect.left();
186  v[2].ty = textureRect.bottom();
187 
188  v[3].x = rect.right();
189  v[3].y = rect.bottom();
190  v[3].tx = textureRect.right();
191  v[3].ty = textureRect.bottom();
192 }
193 
qreal right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:527
TexturedPoint2D * vertexDataAsTexturedPoint2D()
Definition: qsggeometry.h:168
#define GL_UNSIGNED_INT
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static const AttributeSet & defaultAttributes_TexturedPoint2D()
Definition: qsggeometry.cpp:57
int indexType() const
Definition: qsggeometry.h:98
int m_drawing_mode
Definition: qsggeometry.h:118
static const AttributeSet & defaultAttributes_Point2D()
Definition: qsggeometry.cpp:47
Q_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58
void * indexData()
static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect)
qreal left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:525
Q_CORE_EXPORT void * qMalloc(size_t size)
Definition: qmalloc.cpp:53
QSGGeometry(const QSGGeometry::AttributeSet &attribs, int vertexCount, int indexCount=0, int indexType=GL_UNSIGNED_SHORT)
Definition: qsggeometry.cpp:80
const AttributeSet & m_attributes
Definition: qsggeometry.h:122
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int m_vertex_count
Definition: qsggeometry.h:119
uint m_owns_data
Definition: qsggeometry.h:128
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
#define GL_TRIANGLE_STRIP
static const AttributeSet & defaultAttributes_ColoredPoint2D()
Definition: qsggeometry.cpp:68
float m_prealloc[16]
Definition: qsggeometry.h:131
void * m_data
Definition: qsggeometry.h:123
#define GL_UNSIGNED_SHORT
unsigned short quint16
Definition: qglobal.h:936
static const char * data(const QByteArray &arr)
#define GL_FLOAT
static void updateRectGeometry(QSGGeometry *g, const QRectF &rect)
void setDrawingMode(GLenum mode)
unsigned int GLenum
Definition: main.cpp:50
Point2D * vertexDataAsPoint2D()
Definition: qsggeometry.h:158
#define GL_UNSIGNED_BYTE
int m_index_count
Definition: qsggeometry.h:120
unsigned int quint32
Definition: qglobal.h:938
int m_index_data_offset
Definition: qsggeometry.h:124
qreal top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:526
const Attribute * attributes() const
Definition: qsggeometry.h:111
qreal bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:528
int indexCount() const
Definition: qsggeometry.h:100
void allocate(int vertexCount, int indexCount=0)
int vertexCount() const
Definition: qsggeometry.h:86
int m_index_type
Definition: qsggeometry.h:121