[Solved] Over painting OpenGL



  • Dear All:

    I am doing a scientific application and for that I need to draw in OpenGL and at the same time draw some Text with the standard QPainter. I have seem some examples like the one in this page "http://qt-project.org/doc/qt-4.8/opengl-overpainting.html" but I don't manage to get mine working.

    My paintEvent look like this:

    @
    makeCurrent();

    qglClearColor(getBackGroundColor());
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    paintGL();
    
    QPainter painter(this);
    painter.drawRect(1, 1, 100, 100);
    painter.end();
    

    @

    and in PaintGL I just call the OpenGl stuff. The problem is that I can't see the rectangle. If I remove the call to paintGL then I see it.

    Is there someone who has doing this before?

    Best and thanks
    Ernesto


  • Lifetime Qt Champion

    Hi,

    What are you doing in paintGL ?



  • Which OpenGL widget are you using?
    I think you need to call painter.beginNativePainting() before the open GL stuff
    and painter,endNativePainting() after it.

    That is what I do and it that works with a QGLWidget. I have no experience with the newer QOpenGLWidget however.



  • The code snippet you posted looks correct, without more detail it is hard to diagnose. I use the same technique with a QGLWidget based class.

    If your text is not changing often then you could render it into an OpenGL texture. You only need to upload the texture data again when your text changes. This is also useful for 2d HUDs etc.

    @void GLVideoWidget::setOverlay(QImage *overlay)
    {
    makeCurrent();

    if(!fboSetup_)
    {
    overlayTextureWidth_ = TM_GetNextPowerOf2(overlay->width());
    overlayTextureHeight_ = TM_GetNextPowerOf2(overlay->height());

    if (QGLFramebufferObject::hasOpenGLFramebufferBlit())
    {
    QGLFramebufferObjectFormat format;
    format.setSamples(4);
    format.setAttachment(QGLFramebufferObject::CombinedDepthStencil);

    renderFBO_ = new QGLFramebufferObject(overlayTextureWidth_, overlayTextureHeight_, format);
    textureFBO_ = new QGLFramebufferObject(overlayTextureWidth_, overlayTextureHeight_);
    }
    else
    {
    renderFBO_ = new QGLFramebufferObject(overlayTextureWidth_, overlayTextureHeight_);
    textureFBO_ = renderFBO_;
    }

    fboSetup_ = true;
    }

    // draw the image on the render fbo
    QPainter fboPainter(renderFBO_);
    fboPainter.setCompositionMode(QPainter::CompositionMode_Source);
    fboPainter.drawImage(0, 0, *overlay);
    fboPainter.end();

    // copy from the render fbo to the texture fbo
    if(renderFBO_ != textureFBO_)
    {
    QRect rect(0, 0, renderFBO_->width(), renderFBO_->height());
    QGLFramebufferObject::blitFramebuffer(textureFBO_, rect, renderFBO_, rect);
    }
    }@

    To draw the texture add the following to the end of your paintGL function

    @// draw the fbo overlay
    if(useOverlay_ && textureFBO_)
    {
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);

    TM_BindTexture(textureFBO_->texture());
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glColor3f(1.0, 1.0, 1.0);

    glBegin(GL_QUADS);
    glTexCoord2f (0, 1); glVertex2f (0.0, 0.0 ); // flip image upside down using the tex coords
    glTexCoord2f (1, 1); glVertex2f (textureFBO_->width(), 0.0 );
    glTexCoord2f (1, 0); glVertex2f (textureFBO_->width(), textureFBO_->height());
    glTexCoord2f (0, 0); glVertex2f (0.0, textureFBO_->height());
    glEnd();

    glDisable(GL_BLEND);
    }@



  • Dear all:

    thanks for your answer I have found the problem and it was a did not free the Shaders and the OpenGLBuffer.

    Thanks a lot


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.