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. QPainter on a QGLWidget using VBOs
Forum Updated to NodeBB v4.3 + New Features

QPainter on a QGLWidget using VBOs

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 953 Views 2 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.
  • J Offline
    J Offline
    jrevans
    wrote on last edited by
    #1

    I have a QGLWidget that I wish to overpaint onto with a QPainter. I have followed the various examples out there to do this, but I still cannot seem to get it to work properly when I am using VBOs. I have the source code available "here":https://dl.dropboxusercontent.com/u/36712950/QtGlText.tgz

    The most relevant sections of code are as follows:

    @
    void
    GlWidget::
    paintEvent( QPaintEvent* event )
    {
    if ( !updatesEnabled() )
    {
    // do not update this widget
    return;
    }

    if ( !isValid() )
    {
    // no valid render context
    return;
    }

    // make sure this render context is the active one
    makeCurrent();

    QPainter painter;
    painter.begin( this );
    painter.setRenderHint( QPainter::Antialiasing );

    #ifndef NO_2D_3D_MIX
    // begin OpenGL calls
    painter.beginNativePainting();
    #endif

    // Save the current OpenGL state (extra paranoid)
    glPushAttrib( GL_ALL_ATTRIB_BITS );
    glMatrixMode( GL_PROJECTION );
    glPushMatrix();
    glMatrixMode( GL_MODELVIEW );
    glPushMatrix();

    // setup the viewport
    resizeGL( width(), height() );

    // pre-render callback
    preRender();

    // begin rendering
    emit rendering();

    // 3D render
    paintGL();

    // Restore OpenGL state
    glMatrixMode( GL_MODELVIEW );
    glPopMatrix();
    glMatrixMode( GL_PROJECTION );
    glPopMatrix();
    glPopAttrib();

    // make sure there are no loaded shader programs
    glUseProgram( 0 );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );

    #ifndef NO_2D_3D_MIX
    // end OpenGL calls
    painter.endNativePainting();

    // 2D render
    render2d( &painter );
    #endif

    // post-render callback
    postRender();

    // end painting and swap the buffers
    painter.end();
    }
    @

    and

    @
    void
    TestWidget::
    render3d()
    {
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    // This works
    glBegin( GL_TRIANGLES );
    glColor3ub( 255, 15, 125 );
    glVertex3f( -0.85, -0.90, 0.8 ); // triangle #1
    glVertex3f( 0.90, -0.90, 0.8 );
    glVertex3f( 0.90, 0.85, 0.8 );

    glVertex3f( -0.90, -0.85, 0.8 ); // triangle #2
    glVertex3f( 0.85, 0.90, 0.8 );
    glVertex3f( -0.90, 0.90, 0.8 );
    glEnd();

    // Bind the shader program
    m_shader.bind();

    // Bind the vertex buffers
    m_vertexBuffer.bind();

    // draw them
    glDrawArrays( GL_TRIANGLES, 0, NUM_VERTS );
    }
    @

    A couple of things to note are that the fixed-function OpenGL calls (glBegin, glVertex, glEnd) work properly and draw. The vertex buffer items only draw when I define 'NO_2D_3D_MIX'. I feel like I must be missing something simple here, but I cannot figure out what.

    Due to other constraints I am using Qt v4.8.5, Mesa-9.2.1 GL headers with the nVidia 304.43 run-time drivers on a 64-bit RedHat Linux machine.

    Thanks in advance for any possible help.

    1 Reply Last reply
    0
    • 8Observer88 Offline
      8Observer88 Offline
      8Observer8
      wrote on last edited by 8Observer8
      #2

      This is a very old question but I think it will not be bad if I answer here.

      • You should draw with QPainter in paintGL()
      • Place all GL stuff between painter.beginNativePainting(); and painter.endNativePainting();:
      • Unbind VBO's: m_vertPosBufferForLines.release();
      • Draw using QPainter after painter.endNativePainting();
      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