ganeswidgets/src/hgvgquadrenderer.cpp
changeset 1 e48454f237ca
parent 0 89c329efa980
child 2 49c70dcc3f17
equal deleted inserted replaced
0:89c329efa980 1:e48454f237ca
    36     ~HgVgQuad();
    36     ~HgVgQuad();
    37     
    37     
    38     int index() const;
    38     int index() const;
    39     bool isPointInside(const QPointF& point) const;
    39     bool isPointInside(const QPointF& point) const;
    40     void transformQuad(int index, const QMatrix4x4& matrix, HgQuad* quad, 
    40     void transformQuad(int index, const QMatrix4x4& matrix, HgQuad* quad, 
    41         const QRectF& rect, qreal mirroringPlaneY);
    41         const QRectF& rect, qreal mirroringPlaneY, const QVector2D& translate);
    42     void draw();
    42     void draw();
    43 
    43 
    44     void getTransformedPoints(QPolygonF& polygon) const;
    44     void getTransformedPoints(QPolygonF& polygon) const;
    45         
    45         
    46     void computeMirrorMatrix(const QMatrix4x4& tm, const QMatrix4x4& projView, 
    46     void computeMirrorMatrix(const QMatrix4x4& tm, const QMatrix4x4& projView, 
    47         const QRectF& rect, qreal mirroringPlaneY);
    47         const QRectF& rect, qreal mirroringPlaneY, 
       
    48         const QVector2D& translate);
    48     
    49     
    49     bool perspectiveTransformPoints(QVector2D* points, const QMatrix4x4& matrix, 
    50     bool perspectiveTransformPoints(QVector2D* points, const QMatrix4x4& matrix, 
    50         const QRectF& rect);
    51         const QRectF& rect);
    51     
    52     
    52     void computeWarpMatrix(VGfloat* matrix, int pxWidth, int pxHeight, const QVector2D* points);
    53     void computeWarpMatrix(VGfloat* matrix, int pxWidth, int pxHeight, const QVector2D* points, 
       
    54         const QVector2D& translate);
    53     
    55     
    54     void drawImage(HgVgImage* image, qreal alpha);
    56     void drawImage(HgVgImage* image, qreal alpha);
    55     
    57     
    56     int mIndex;
    58     int mIndex;
    57     HgQuad* mQuad;
    59     HgQuad* mQuad;
    97     return false;
    99     return false;
    98 }
   100 }
    99 
   101 
   100 
   102 
   101 void HgVgQuad::computeMirrorMatrix(const QMatrix4x4& trans, const QMatrix4x4& projView, 
   103 void HgVgQuad::computeMirrorMatrix(const QMatrix4x4& trans, const QMatrix4x4& projView, 
   102     const QRectF& rect, qreal mirroringPlaneY)
   104     const QRectF& rect, qreal mirroringPlaneY, const QVector2D& translate)
   103 {
   105 {
   104     HgQuad* quad = mQuad;
   106     HgQuad* quad = mQuad;
   105 
   107 
   106     QMatrix4x4 mirror = trans;
   108     QMatrix4x4 mirror = trans;
   107 
   109 
   120     HgVgImage* image = (HgVgImage*)mQuad->image();
   122     HgVgImage* image = (HgVgImage*)mQuad->image();
   121     
   123     
   122     if (image == NULL)
   124     if (image == NULL)
   123     {
   125     {
   124         image = mRenderer->defaultImage();
   126         image = mRenderer->defaultImage();
       
   127         if (!image) {
       
   128             return;
       
   129         }
   125     }
   130     }
   126     
   131     
   127     int pxWidth = image->mirrorImageWidth();
   132     int pxWidth = image->mirrorImageWidth();
   128     int pxHeight = image->mirrorImageHeight();
   133     int pxHeight = image->mirrorImageHeight();
   129 
   134 
   130     computeWarpMatrix(mMirrorMatrix, pxWidth, pxHeight, temp);
   135     computeWarpMatrix(mMirrorMatrix, pxWidth, pxHeight, temp, translate);
   131 }
   136 }
   132 
   137 
   133 void HgVgQuad::transformQuad(int index, const QMatrix4x4& projView, HgQuad* quad, 
   138 void HgVgQuad::transformQuad(int index, const QMatrix4x4& projView, HgQuad* quad, 
   134     const QRectF& rect, qreal mirroringPlaneY)
   139     const QRectF& rect, qreal mirroringPlaneY, const QVector2D& translate)
   135 {
   140 {
   136     mIndex = index;
   141     mIndex = index;
   137     mQuad = quad;
   142     mQuad = quad;
   138         
   143         
   139     QMatrix4x4 tm;
   144     QMatrix4x4 tm;
   140     tm.setToIdentity();
   145     tm.setToIdentity();
   141     tm.rotate(quad->outerRotation());
   146     tm.rotate(quad->outerRotation());
   142 
   147 
   143     if (mQuad->mirrorImageEnabled())
   148     if (mQuad->mirrorImageEnabled())
   144     {
   149     {
   145         computeMirrorMatrix(tm, projView, rect, mirroringPlaneY);
   150         computeMirrorMatrix(tm, projView, rect, mirroringPlaneY, translate);
   146     }
   151     }
   147     
   152     
   148     tm.translate(quad->position());    
   153     tm.translate(quad->position());    
   149     tm.rotate(quad->rotation());    
   154     tm.rotate(quad->rotation());    
   150     tm.scale(quad->scale().x(), quad->scale().y());
   155     tm.scale(quad->scale().x(), quad->scale().y());
   161     HgVgImage* image = (HgVgImage*)mQuad->image();
   166     HgVgImage* image = (HgVgImage*)mQuad->image();
   162     
   167     
   163     if (image == NULL)
   168     if (image == NULL)
   164     {
   169     {
   165         image = mRenderer->defaultImage();
   170         image = mRenderer->defaultImage();
       
   171         if (!image)
       
   172             return;
   166     }
   173     }
   167     
   174     
   168     int pxWidth = image->width();
   175     int pxWidth = image->width();
   169     int pxHeight = image->height();
   176     int pxHeight = image->height();
   170     
   177     
   171     computeWarpMatrix(mMatrix, pxWidth, pxHeight, mTransformedPoints);
   178     
       
   179     computeWarpMatrix(mMatrix, pxWidth, pxHeight, mTransformedPoints, translate);
       
   180     
       
   181     for (int i = 0; i < 4; i++)
       
   182         mTransformedPoints[i] += translate;
   172     
   183     
   173 }
   184 }
   174 
   185 
   175 bool HgVgQuad::perspectiveTransformPoints(QVector2D* outPoints, const QMatrix4x4& matrix, 
   186 bool HgVgQuad::perspectiveTransformPoints(QVector2D* outPoints, const QMatrix4x4& matrix, 
   176     const QRectF& rect)
   187     const QRectF& rect)
   197     }
   208     }
   198     
   209     
   199     return true;
   210     return true;
   200 }
   211 }
   201 
   212 
   202 void HgVgQuad::computeWarpMatrix(VGfloat* matrix, int pxWidth, int pxHeight, const QVector2D* points)
   213 void HgVgQuad::computeWarpMatrix(VGfloat* matrix, int pxWidth, int pxHeight, const QVector2D* points, 
       
   214     const QVector2D& translate)
   203 {        
   215 {        
       
   216 
   204     vguComputeWarpQuadToQuad(
   217     vguComputeWarpQuadToQuad(
   205         points[0].x(), points[0].y(), 
   218         points[0].x() + translate.x(), points[0].y() + translate.y(), 
   206         points[1].x(), points[1].y(),
   219         points[1].x() + translate.x(), points[1].y() + translate.y(),
   207         points[2].x(), points[2].y(),
   220         points[2].x() + translate.x(), points[2].y() + translate.y(),
   208         points[3].x(), points[3].y(),
   221         points[3].x() + translate.x(), points[3].y() + translate.y(),
   209         0, pxHeight,
   222         0, pxHeight,
   210         pxWidth, pxHeight,
   223         pxWidth, pxHeight,
   211         pxWidth, 0,
   224         pxWidth, 0,
   212         0, 0,
   225         0, 0,
   213         matrix);
   226         matrix);
   230     HgVgImage* image = (HgVgImage*)mQuad->image();
   243     HgVgImage* image = (HgVgImage*)mQuad->image();
   231     
   244     
   232     
   245     
   233     if (image == NULL  || image->alpha() == 0)
   246     if (image == NULL  || image->alpha() == 0)
   234     {
   247     {
   235         return;
   248         if (mRenderer->defaultImage()) {
   236         //drawImage(mRenderer->defaultImage(), 1.0f);
   249             drawImage(mRenderer->defaultImage(), 1.0f);
       
   250         }
   237     }
   251     }
   238     else
   252     else
   239     {
   253     {
   240         image->upload(mQuad->mirrorImageEnabled());
   254         image->upload(mQuad->mirrorImageEnabled());
   241         
   255         
   242         if (image->image() == VG_INVALID_HANDLE)
   256         if (image->image() == VG_INVALID_HANDLE)
   243         {
   257         {
   244             drawImage(mRenderer->defaultImage(), 1.0f);
   258             if (mRenderer->defaultImage()) {
       
   259                 drawImage(mRenderer->defaultImage(), 1.0f);
       
   260             }
   245         }
   261         }
   246         else
   262         else
   247         {
   263         {
   248 
   264 
   249             if ( mQuad->alpha() < 1.0f )
   265             if ( mQuad->alpha() < 1.0f )
   250             {
   266             {
   251                 drawImage(mRenderer->defaultImage(), 1.0f - mQuad->alpha());            
   267                 if (mRenderer->defaultImage()) {
       
   268                     drawImage(mRenderer->defaultImage(), 1.0f - mQuad->alpha());            
       
   269                 }
   252             }
   270             }
   253             
   271             
   254             drawImage(image, mQuad->alpha());
   272             drawImage(image, mQuad->alpha());
   255         }
   273         }
   256     }
   274     }
   352             
   370             
   353         HgVgQuad* tq = mTransformedQuads[i];
   371         HgVgQuad* tq = mTransformedQuads[i];
   354         
   372         
   355         if (q->visible())
   373         if (q->visible())
   356         {
   374         {
   357             tq->transformQuad(i, pv, q, rect, mMirroringPlaneY);   
   375             tq->transformQuad(i, pv, q, rect, mMirroringPlaneY, mTranslation);   
   358             mSortedQuads.append(tq);
   376             mSortedQuads.append(tq);
   359         }
   377         }
   360     }
   378     }
   361         
   379         
   362     qSort(mSortedQuads.begin(), mSortedQuads.end(), quadSorter);
   380     qSort(mSortedQuads.begin(), mSortedQuads.end(), quadSorter);
   432     return new HgVgImage(this);   
   450     return new HgVgImage(this);   
   433 }
   451 }
   434 
   452 
   435 HgVgImage* HgVgQuadRenderer::defaultImage()
   453 HgVgImage* HgVgQuadRenderer::defaultImage()
   436 {
   454 {
   437     if (mDefaultVgImage == NULL)
   455     if (mDefaultVgImage && mDefaultVgImage->image() == VG_INVALID_HANDLE) {
       
   456         mDefaultVgImage->upload(true);
       
   457     }
       
   458 /*    if (mDefaultVgImage == NULL)
   438     {
   459     {
   439         QImage defaultImage(64,64,QImage::Format_RGB16);
   460         QImage defaultImage(64,64,QImage::Format_RGB16);
   440         defaultImage.fill(qRgb(255,0,0));
   461         defaultImage.fill(qRgb(255,0,0));
   441         mDefaultVgImage = static_cast<HgVgImage*>(createNativeImage());
   462         mDefaultVgImage = static_cast<HgVgImage*>(createNativeImage());
   442         mDefaultVgImage->setImage(defaultImage);
   463         mDefaultVgImage->setImage(defaultImage);
   443         mDefaultVgImage->upload(true);        
   464         mDefaultVgImage->upload(true);        
   444     }
   465     }
       
   466     */
   445     return mDefaultVgImage;
   467     return mDefaultVgImage;
       
   468 }
       
   469 
       
   470 void HgVgQuadRenderer::setDefaultImage(QImage defaultImage)
       
   471 {
       
   472     HgQuadRenderer::setDefaultImage(defaultImage);
       
   473     delete mDefaultVgImage;
       
   474     mDefaultVgImage = 0;
       
   475     mDefaultVgImage = static_cast<HgVgImage*>(createNativeImage());
       
   476     mDefaultVgImage->setImage(mDefaultImage);
       
   477     mDefaultVgImage->upload(true);    
   446 }
   478 }
   447 
   479 
   448 HgImageFader* HgVgQuadRenderer::imageFader()
   480 HgImageFader* HgVgQuadRenderer::imageFader()
   449 {
   481 {
   450     return mImageFader;
   482     return mImageFader;