Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QOpenGLShaderProgram example not working in QGraphicsItem
Qt 6.11 is out! See what's new in the release blog

QOpenGLShaderProgram example not working in QGraphicsItem

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bigred9678
    wrote on last edited by
    #1

    I am trying to use QOpenGLShaderProgram in a QGraphicsItem. I have a custom QGraphicsView where I have changed the viewport to a QGLWidget. My shader is getting compiled, linked and bound and is even drawing a triangle on the screen. But the color being passed to the fragment shader is being ignored. The triangle is always white. The shader example works if I use it in a QGLWidget, but not if it is part of a QGraphicsItem in a QGraphicsView. I am using Qt 5.2.

    Am I missing something in OpenGL? Or is this a bug?

    Here is some code: (though it is mostly copied from the example in the docs)

    @
    void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    if (mShaderProgram.bind())
    {
    static GLfloat const triangleVertices[] = {
    10.0f, 110.0f, 0.0f,
    110.0f, 110.0f, 0.0f,
    60.0f, 10.0f, 0.0f
    };

        QColor color(0, 255, 0, 255);
    
        QMatrix4x4 pmvMatrix;
    
        painter->beginNativePainting();
    
            pmvMatrix.ortho(mBoundingRect);
    
            mShaderProgram.enableAttributeArray("vertex");
            mShaderProgram.setAttributeArray("vertex", triangleVertices, 3);
            mShaderProgram.setUniformValue("matrix", pmvMatrix);
            mShaderProgram.setUniformValue("color", color);
    
            glDrawArrays(GL_TRIANGLES, 0, 3);
    
            mShaderProgram.disableAttributeArray("vertex");
    
        painter->beginNativePainting();
    
        mShaderProgram.release();
    }
    

    }
    @

    @
    void MyGraphicsItem::InitializeShaders()
    {
    if (!mShaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex,
    "attribute highp vec4 vertex;\n"
    "uniform highp mat4 matrix;\n"
    "void main(void)\n"
    "{\n"
    " gl_Position = matrix * vertex;\n"
    "}"))
    {
    qDebug() << "could not add vertex shader";
    }

    if (!mShaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment,
        "uniform mediump vec4 color;\n"
        "void main(void)\n"
        "{\n"
        "   gl_FragColor = color;\n"
        "}"))
    {
        qDebug() << "could not add fragment shader";
    }
    
    if (!mShaderProgram.link())
    {
        qDebug() << "could not link shader program";
    }
    if (!mShaderProgram.bind())
    {
        qDebug() << "could not bind shader program";
    }
    

    }
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved