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. [Solved] Over painting OpenGL
Forum Updated to NodeBB v4.3 + New Features

[Solved] Over painting OpenGL

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.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.
  • E Offline
    E Offline
    epalmero
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What are you doing in paintGL ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kenchan
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KiNgFrUiT
          wrote on last edited by
          #4

          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);
          }@

          1 Reply Last reply
          0
          • E Offline
            E Offline
            epalmero
            wrote on last edited by
            #5

            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

            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