Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflict between shaders and QPainter in paintGL() for rendering 2D text

I have the following class inheriting from QOpenGLWidget and QOpenGLFunctions:

class OpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    OpenGLWidget();
    virtual ~OpenGLWidget();

    void initializeGL();
    void paintGL()
    {
       QPainter painter(this);

       painter.beginNativePainting();
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       // Calls OpenGL draw functions with VBOs
       m_viewport.render(m_shader, m_entities);
       painter.endNativePainting();

       painter.drawText(0, 0, width(), height(), Qt::AlignCenter, "Hello World!");

    }
    void resizeGL(int width, int height);

    [...]
}

"Hello World" is drawn as intended, but the 3D scene is broken. I should have 3D axis in the center and in the top-right of the screen:

enter image description here

To me, it seems that the vertex and fragment shaders I'm using are the source of the problem. Otherwise, given the simplicity of the code and the examples I've found, it should work.

A good output would be:

enter image description here

with the "Hello World" at the center. This is what I get when I comment the QPainter calls.

like image 877
Grégoire Borel Avatar asked Sep 05 '25 12:09

Grégoire Borel


1 Answers

It seems that your shader program is released when you use QPainter. Bind the shader program before OpenGL calls and release it afterwards. It should fix it.

painter.beginNativePainting();
// Bind shader program
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Calls OpenGL draw functions with VBOs
m_viewport.render(m_shader, m_entities);
// Release shader program
painter.endNativePainting();
like image 105
M. Brigaud Avatar answered Sep 10 '25 14:09

M. Brigaud