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

Last change on this file since 104 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtOpenGL module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qgl2pexvertexarray_p.h"
43
44#include <private/qbezier_p.h>
45
46void QGL2PEXVertexArray::clear()
47{
48 vertexArray.reset();
49 vertexArrayStops.clear();
50 boundingRectDirty = true;
51}
52
53
54QGLRect QGL2PEXVertexArray::boundingRect() const
55{
56 if (boundingRectDirty)
57 return QGLRect(0.0, 0.0, 0.0, 0.0);
58 else
59 return QGLRect(minX, minY, maxX, maxY);
60}
61
62void QGL2PEXVertexArray::addPath(const QVectorPath &path, GLfloat curveInverseScale)
63{
64 const QPointF* const points = reinterpret_cast<const QPointF*>(path.points());
65 const QPainterPath::ElementType* const elements = path.elements();
66
67 if (boundingRectDirty) {
68 minX = maxX = points[0].x();
69 minY = maxY = points[0].y();
70 boundingRectDirty = false;
71 }
72
73 vertexArray.add(points[0]); // The first element is always a moveTo
74
75 do {
76 if (!elements) {
77// qDebug("QVectorPath has no elements");
78 // If the path has a null elements pointer, the elements implicitly
79 // start with a moveTo (already added) and continue with lineTos:
80 for (int i=1; i<path.elementCount(); ++i)
81 lineToArray(points[i].x(), points[i].y());
82
83 break;
84 }
85// qDebug("QVectorPath has element types");
86
87 for (int i=1; i<path.elementCount(); ++i) {
88 const QPainterPath::ElementType elementType = elements[i];
89 switch (elementType) {
90 case QPainterPath::MoveToElement:
91// qDebug("element[%d] is a MoveToElement", i);
92 vertexArrayStops.append(vertexArray.size());
93 vertexArray.add(points[i]); // Add the moveTo as a new vertex
94 break;
95 case QPainterPath::LineToElement:
96// qDebug("element[%d] is a LineToElement", i);
97 lineToArray(points[i].x(), points[i].y());
98 break;
99 case QPainterPath::CurveToElement:
100// qDebug("element[%d] is a CurveToElement", i);
101 curveToArray(points[i], points[i+1], points[i+2], curveInverseScale);
102 i+=2;
103 break;
104 default:
105 break;
106 }
107 }
108 } while (0);
109
110 vertexArrayStops.append(vertexArray.size());
111}
112
113void QGL2PEXVertexArray::lineToArray(const GLfloat x, const GLfloat y)
114{
115 vertexArray.add(QGLPoint(x, y));
116
117 if (x > maxX)
118 maxX = x;
119 else if (x < minX)
120 minX = x;
121 if (y > maxY)
122 maxY = y;
123 else if (y < minY)
124 minY = y;
125}
126
127void QGL2PEXVertexArray::curveToArray(const QGLPoint &cp1, const QGLPoint &cp2, const QGLPoint &ep, GLfloat inverseScale)
128{
129 qreal inverseScaleHalf = inverseScale / 2;
130
131 QBezier beziers[32];
132 beziers[0] = QBezier::fromPoints(vertexArray.last(), cp1, cp2, ep);
133 QBezier *b = beziers;
134 while (b >= beziers) {
135 // check if we can pop the top bezier curve from the stack
136 qreal l = qAbs(b->x4 - b->x1) + qAbs(b->y4 - b->y1);
137 qreal d;
138 if (l > inverseScale) {
139 d = qAbs( (b->x4 - b->x1)*(b->y1 - b->y2) - (b->y4 - b->y1)*(b->x1 - b->x2) )
140 + qAbs( (b->x4 - b->x1)*(b->y1 - b->y3) - (b->y4 - b->y1)*(b->x1 - b->x3) );
141 d /= l;
142 } else {
143 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
144 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
145 }
146 if (d < inverseScaleHalf || b == beziers + 31) {
147 // good enough, we pop it off and add the endpoint
148 lineToArray(b->x4, b->y4);
149 --b;
150 } else {
151 // split, second half of the polygon goes lower into the stack
152 b->split(b+1, b);
153 ++b;
154 }
155 }
156}
Note: See TracBrowser for help on using the repository browser.