source: trunk/src/opengl/gl2paintengineex/qgl2pexvertexarray.cpp@ 595

Last change on this file since 595 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 6.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtOpenGL module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qgl2pexvertexarray_p.h"
43
44#include <private/qbezier_p.h>
45
46QT_BEGIN_NAMESPACE
47
48void QGL2PEXVertexArray::clear()
49{
50 vertexArray.reset();
51 vertexArrayStops.reset();
52 boundingRectDirty = true;
53}
54
55
56QGLRect QGL2PEXVertexArray::boundingRect() const
57{
58 if (boundingRectDirty)
59 return QGLRect(0.0, 0.0, 0.0, 0.0);
60 else
61 return QGLRect(minX, minY, maxX, maxY);
62}
63
64void QGL2PEXVertexArray::addRect(const QRectF &rect)
65{
66 vertexArray << rect.topLeft() << rect.topRight() << rect.bottomRight()
67 << rect.bottomRight() << rect.bottomLeft() << rect.topLeft();
68}
69
70void QGL2PEXVertexArray::addClosingLine(int index)
71{
72 if (QPointF(vertexArray.at(index)) != QPointF(vertexArray.last()))
73 vertexArray.add(vertexArray.at(index));
74}
75
76void QGL2PEXVertexArray::addCentroid(const QVectorPath &path, int subPathIndex)
77{
78 const QPointF *const points = reinterpret_cast<const QPointF *>(path.points());
79 const QPainterPath::ElementType *const elements = path.elements();
80
81 QPointF sum = points[subPathIndex];
82 int count = 1;
83
84 for (int i = subPathIndex + 1; i < path.elementCount() && (!elements || elements[i] != QPainterPath::MoveToElement); ++i) {
85 sum += points[i];
86 ++count;
87 }
88
89 const QPointF centroid = sum / qreal(count);
90 vertexArray.add(centroid);
91}
92
93void QGL2PEXVertexArray::addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline)
94{
95 const QPointF* const points = reinterpret_cast<const QPointF*>(path.points());
96 const QPainterPath::ElementType* const elements = path.elements();
97
98 if (boundingRectDirty) {
99 minX = maxX = points[0].x();
100 minY = maxY = points[0].y();
101 boundingRectDirty = false;
102 }
103
104 if (!outline && !path.isConvex())
105 addCentroid(path, 0);
106
107 int lastMoveTo = vertexArray.size();
108 vertexArray.add(points[0]); // The first element is always a moveTo
109
110 do {
111 if (!elements) {
112// qDebug("QVectorPath has no elements");
113 // If the path has a null elements pointer, the elements implicitly
114 // start with a moveTo (already added) and continue with lineTos:
115 for (int i=1; i<path.elementCount(); ++i)
116 lineToArray(points[i].x(), points[i].y());
117
118 break;
119 }
120// qDebug("QVectorPath has element types");
121
122 for (int i=1; i<path.elementCount(); ++i) {
123 switch (elements[i]) {
124 case QPainterPath::MoveToElement:
125 if (!outline)
126 addClosingLine(lastMoveTo);
127// qDebug("element[%d] is a MoveToElement", i);
128 vertexArrayStops.add(vertexArray.size());
129 if (!outline) {
130 if (!path.isConvex()) addCentroid(path, i);
131 lastMoveTo = vertexArray.size();
132 }
133 lineToArray(points[i].x(), points[i].y()); // Add the moveTo as a new vertex
134 break;
135 case QPainterPath::LineToElement:
136// qDebug("element[%d] is a LineToElement", i);
137 lineToArray(points[i].x(), points[i].y());
138 break;
139 case QPainterPath::CurveToElement: {
140 QBezier b = QBezier::fromPoints(*(((const QPointF *) points) + i - 1),
141 points[i],
142 points[i+1],
143 points[i+2]);
144 QRectF bounds = b.bounds();
145 // threshold based on same algorithm as in qtriangulatingstroker.cpp
146 int threshold = qMin<float>(64, qMax(bounds.width(), bounds.height()) * 3.14f / (curveInverseScale * 6));
147 if (threshold < 3) threshold = 3;
148 qreal one_over_threshold_minus_1 = 1.f / (threshold - 1);
149 for (int t=0; t<threshold; ++t) {
150 QPointF pt = b.pointAt(t * one_over_threshold_minus_1);
151 lineToArray(pt.x(), pt.y());
152 }
153 i += 2;
154 break; }
155 default:
156 break;
157 }
158 }
159 } while (0);
160
161 if (!outline)
162 addClosingLine(lastMoveTo);
163 vertexArrayStops.add(vertexArray.size());
164}
165
166void QGL2PEXVertexArray::lineToArray(const GLfloat x, const GLfloat y)
167{
168 vertexArray.add(QGLPoint(x, y));
169
170 if (x > maxX)
171 maxX = x;
172 else if (x < minX)
173 minX = x;
174 if (y > maxY)
175 maxY = y;
176 else if (y < minY)
177 minY = y;
178}
179
180QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.