[Interest] QPainter draws text quads counterclockwise and other primitives clockwise

John Cox amdreallyfast at hotmail.com
Fri Oct 7 05:15:14 CEST 2016


Using Qt 5.6.0 on Windows 10.

QPainter draws the quads for text bitmaps counterclockwise, while primitives like ellipses and the quads for QPixmap draw clockwise.  Result found on QOpenGLWidget.

Is this intentional?

Ex: I want to draw a 3D scene and then try to draw a framerate counter on top of it with QPainter's text functionality.

Justification:

*        According to glCullFace, OpenGL face culling defaults to culling the back face, so front faces are shown but back faces are not.

*        According to glFrontFace, OpenGL defines a "front face" as counterclockwise polygons.

*        Therefore, if face culling is enabled in an OpenGL-running widget, it will default to culling clockwise (back face) polygons and anything that uses them.

Demonstration code:
See the example "2D Painting Example" in Qt Creator (I'm running Qt 5.6.0). This example uses a QPainter object to draw text and rotating ellipses on a QOpenGLWidget.

1.      Add override of initializeGL() to GLwidget.

2.  In source file, add function:
#include <qopenglfunctions_2_1.h>

void GLWidget::initializeGL()

{

    QOpenGLFunctions_2_1 *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_2_1>();

    f->glEnable(GL_CULL_FACE);

    //f->glCullFace(GL_FRONT);      // uncomment to cull the text and show the rotating elipses

}

3.      Run the program.  The text "Qt" draws but the ellipses don't.  Uncomment glCullFace(GL_FRONT) to reverse the effect and show the ellipses but not the text.

4.  Now try drawing a pixmap after culling is turned on.  The following won't draw if culling is turned on and it is culling back faces (clockwise polygons).
#include <qpixmap.h>

void GLWidget::paintGL()

{

    QPixmap p(myImageFilePath);

    QPainter painter(this);

    painter.drawPixmap(0, 0, p.scaled(this->size());

}

5.  To draw a QPixmap when back faces are being culled, you have to do this:
void GLWidget::paintGL()

{

    QPixmap p(myImageFilePath);

    QOpenGLFunctions_2_1 *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_2_1>();

    GLBoolean faceCullingIsOn;



    // save current culling state

    f->glGetBooleanv(GL_CULL_FACE, &faceCullingIsOn);

    f->glDisable(GL_CULL_FACE);

    QPainter painter(this);

    painter.drawPixmap(0, 0, p.scaled(this->size());

    if (faceCullingIsOn == GL_TRUE)

    {

        // turn it back on

        f->glEnable(GL_CULL_FACE);

    }

}

Bug or intentional?  This isn't a show-stopper for me but it did cause some grief.




-        John Cox

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20161007/b9d60759/attachment.html>


More information about the Interest mailing list