Using QPainter over OpenGL in QGLWidget when using shaders

前端 未结 2 1771
难免孤独
难免孤独 2021-01-02 06:31

Many of you Qt (4.6 specifically) users will be familiar with the Overpainting example supplied in the OpenGL tutorials, I\'m trying to do something very similar but using s

相关标签:
2条回答
  • 2021-01-02 07:08

    Call beginNativePainting() before making OpenGL calls. A glPush/Pop of the OpenGL state may also be necessary. Try something like the following:

    QPainter p( this );
    p.beginNativePainting();
    
    // Maybe necessary
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    
    // Put OpenGL code here
    
    // Necessary if used glPush-es above
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glPopAttrib();
    
    p.endNativePainting();
    
    p.fillRect( 10, 10, 100, 100,
            QColor( 255, 0, 0 ) );
    
    0 讨论(0)
  • 2021-01-02 07:22

    I'm posting this answer to strengthen and clarify what I regard as the true solution, found by cmannet85 (comment to answer by baysmith above), which is to clean up OpenGL vertex buffer bindings before calling QPainter code.

    I had an almost identical problem, in that I needed to switch between using QPainter functions and OpenGL functions. As cmannet85 found out, one cause is that if OpenGL functions leave bound vertex buffers behind, they interfere with QPainter's use of OpenGL.

    I was able to fix the problem by adding this statement at the end of all the parts of my OpenGL ES 2.0 code which called glBindBuffer:

    glBindBuffer(GL_ARRAY_BUFFER,0);
    

    A second parameter of 0 unbinds the buffer.

    There was no need for any other changes such as adding calls to beginNativePainting ... endNativePainting, etc.

    0 讨论(0)
提交回复
热议问题