src/opengl/gl2paintengineex/qgl2pexvertexarray.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qgl2pexvertexarray_p.h"
       
    43 
       
    44 #include <private/qbezier_p.h>
       
    45 
       
    46 QT_BEGIN_NAMESPACE
       
    47 
       
    48 void QGL2PEXVertexArray::clear()
       
    49 {
       
    50     vertexArray.reset();
       
    51     vertexArrayStops.clear();
       
    52     boundingRectDirty = true;
       
    53 }
       
    54 
       
    55 
       
    56 QGLRect 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 
       
    64 void QGL2PEXVertexArray::addRect(const QRectF &rect)
       
    65 {
       
    66     vertexArray << rect.topLeft() << rect.topRight() << rect.bottomRight()
       
    67                 << rect.bottomRight() << rect.bottomLeft() << rect.topLeft();
       
    68 }
       
    69 
       
    70 void QGL2PEXVertexArray::addClosingLine(int index)
       
    71 {
       
    72     if (QPointF(vertexArray.at(index)) != QPointF(vertexArray.last()))
       
    73         vertexArray.add(vertexArray.at(index));
       
    74 }
       
    75 
       
    76 void 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 
       
    93 void 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)
       
   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             const QPainterPath::ElementType elementType = elements[i];
       
   124             switch (elementType) {
       
   125             case QPainterPath::MoveToElement:
       
   126                 if (!outline)
       
   127                     addClosingLine(lastMoveTo);
       
   128 //                qDebug("element[%d] is a MoveToElement", i);
       
   129                 vertexArrayStops.append(vertexArray.size());
       
   130                 if (!outline) {
       
   131                     addCentroid(path, i);
       
   132                     lastMoveTo = vertexArray.size();
       
   133                 }
       
   134                 lineToArray(points[i].x(), points[i].y()); // Add the moveTo as a new vertex
       
   135                 break;
       
   136             case QPainterPath::LineToElement:
       
   137 //                qDebug("element[%d] is a LineToElement", i);
       
   138                 lineToArray(points[i].x(), points[i].y());
       
   139                 break;
       
   140             case QPainterPath::CurveToElement:
       
   141 //                qDebug("element[%d] is a CurveToElement", i);
       
   142                 curveToArray(points[i], points[i+1], points[i+2], curveInverseScale);
       
   143                 i+=2;
       
   144                 break;
       
   145             default:
       
   146                 break;
       
   147             }
       
   148         }
       
   149     } while (0);
       
   150 
       
   151     if (!outline)
       
   152         addClosingLine(lastMoveTo);
       
   153     vertexArrayStops.append(vertexArray.size());
       
   154 }
       
   155 
       
   156 void QGL2PEXVertexArray::lineToArray(const GLfloat x, const GLfloat y)
       
   157 {
       
   158     vertexArray.add(QGLPoint(x, y));
       
   159 
       
   160     if (x > maxX)
       
   161         maxX = x;
       
   162     else if (x < minX)
       
   163         minX = x;
       
   164     if (y > maxY)
       
   165         maxY = y;
       
   166     else if (y < minY)
       
   167         minY = y;
       
   168 }
       
   169 
       
   170 void QGL2PEXVertexArray::curveToArray(const QGLPoint &cp1, const QGLPoint &cp2, const QGLPoint &ep, GLfloat inverseScale)
       
   171 {
       
   172     qreal inverseScaleHalf = inverseScale / 2;
       
   173 
       
   174     QBezier beziers[32];
       
   175     beziers[0] = QBezier::fromPoints(vertexArray.last(), cp1, cp2, ep);
       
   176     QBezier *b = beziers;
       
   177     while (b >= beziers) {
       
   178         // check if we can pop the top bezier curve from the stack
       
   179         qreal l = qAbs(b->x4 - b->x1) + qAbs(b->y4 - b->y1);
       
   180         qreal d;
       
   181         if (l > inverseScale) {
       
   182             d = qAbs( (b->x4 - b->x1)*(b->y1 - b->y2) - (b->y4 - b->y1)*(b->x1 - b->x2) )
       
   183                 + qAbs( (b->x4 - b->x1)*(b->y1 - b->y3) - (b->y4 - b->y1)*(b->x1 - b->x3) );
       
   184             d /= l;
       
   185         } else {
       
   186             d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
       
   187                 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
       
   188         }
       
   189         if (d < inverseScaleHalf || b == beziers + 31) {
       
   190             // good enough, we pop it off and add the endpoint
       
   191             lineToArray(b->x4, b->y4);
       
   192             --b;
       
   193         } else {
       
   194             // split, second half of the polygon goes lower into the stack
       
   195             b->split(b+1, b);
       
   196            ++b;
       
   197         }
       
   198     }
       
   199 }
       
   200 
       
   201 QT_END_NAMESPACE