<p>Looks like I misunderstood how my windowing system was handling rendering.  It had created an offscreen buffer that was being used to update the window.  When I use <code>glBlitFramebuffer</code> with the buffer id <code>QOpenGLWdget</code> gives me, my image appears.</p>

CC BY-SA 4.0
2021-01-24T21:05:56.710Z
2021-01-24T21:05:56.710Z
kitfox
<p>I'm just starting to work with framebuffer objects and am having trouble trying to figure out why I can't blit my framebuffer to my viewport.  I know my framebuffer has data drawn to it since I can read its pixel using glReadPixels and save it as an image.  However, when I try to draw it to my back buffer so that I can see it in the window, it draws nothing at all - all I see is the color I cleared the viewport to.</p>
<p>What am I doing wrong?</p>
<pre><code>
void DocumentPanel::paintGL()
{
    QOpenGLFunctions_3_3_Core *f = QOpenGLContext::currentContext()-&gt;versionFunctions&lt;QOpenGLFunctions_3_3_Core&gt;();

    //Setup framebuffer
    setupFramebuffer();

    //Render to framebuffer
    f-&gt;glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
    f-&gt;glClearColor(0, 0, 1, 0);

    f-&gt;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    drawShapes();


    //--------------------------------------------

    f-&gt;glBindFramebuffer(GL_FRAMEBUFFER, 0);

    if (!_oneShot)
    {
        dumpFramebuffer();
        _oneShot = true;
    }

    f-&gt;glClearColor(0, 0.5, .5, 0);
    f-&gt;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    f-&gt;glDisable(GL_DEPTH_TEST);
    f-&gt;glDisable(GL_STENCIL_TEST);

    int w = width();
    int h = height();
    f-&gt;glBindFramebuffer(GL_READ_FRAMEBUFFER, _frameBufferID);
    f-&gt;glReadBuffer(GL_COLOR_ATTACHMENT0);
    f-&gt;glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    f-&gt;glDrawBuffer(GL_BACK);
    f-&gt;glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
    checkForErrors();
}


void DocumentPanel::setupFramebuffer()
{
    QOpenGLFunctions_3_3_Core *f = QOpenGLContext::currentContext()-&gt;versionFunctions&lt;QOpenGLFunctions_3_3_Core&gt;();

    int w = width();
    int h = height();
    int samples = 4;
    bool multisample = false;

    if (_frameBufferID == 0)
    {
        f-&gt;glGenFramebuffers(1, &amp;_frameBufferID);
        f-&gt;glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
    }

    if (_renderBufferColorID == 0)
    {
        f-&gt;glGenRenderbuffers(1, &amp;_renderBufferColorID);
        f-&gt;glBindRenderbuffer(GL_RENDERBUFFER, _renderBufferColorID);

        if (!multisample)
            f-&gt;glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, w, h);
        else
            f-&gt;glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_RGBA, w, h);
        f-&gt;glBindRenderbuffer(GL_RENDERBUFFER, 0);

        f-&gt;glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderBufferColorID);
    }

    if (_renderBufferDepthStencilID == 0)
    {
        f-&gt;glGenRenderbuffers(1, &amp;_renderBufferDepthStencilID);
        f-&gt;glBindRenderbuffer(GL_RENDERBUFFER, _renderBufferDepthStencilID);
        if (!multisample)
            f-&gt;glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, w, h);
        else
            f-&gt;glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_DEPTH24_STENCIL8, w, h);
        f-&gt;glBindRenderbuffer(GL_RENDERBUFFER, 0);

        f-&gt;glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBufferDepthStencilID);
    }

    GLenum status = f-&gt;glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE)
    {
        //error
        QString err;

        switch (status)
        {
        case GL_FRAMEBUFFER_UNDEFINED:
            err = &quot;GL_FRAMEBUFFER_UNDEFINED&quot;;
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
            err = &quot;GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT&quot;;
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT :
            err = &quot;GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT &quot;;
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER :
            err = &quot;GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER &quot;;
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER :
            err = &quot;GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER &quot;;
        break;
        case GL_FRAMEBUFFER_UNSUPPORTED :
            err = &quot;GL_FRAMEBUFFER_UNSUPPORTED &quot;;
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE :
            err = &quot;GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE &quot;;
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS  :
            err = &quot;GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS  &quot;;
        break;
        }

        f-&gt;glBindFramebuffer(GL_FRAMEBUFFER, 0);

        qDebug() &lt;&lt; &quot;Error building frambuffer: &quot; &lt;&lt; err;
        return;
    }

    //Rendering to window again
    f-&gt;glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2021-01-24T14:17:42.607Z
maybe 
void QOpenGLContext::swapBuffers(QSurface *surface)
Saleh
<frame-buffer>
Can’t blit framebuffer to backbuffer
kitfox
# Can’t blit framebuffer to backbuffer

(Asked by: username_0 on 2021-01-24)

I'm just starting to work with framebuffer objects and am having trouble trying to figure out why I can't blit my framebuffer to my viewport. I know my framebuffer has data drawn to it since I can read its pixel using glReadPixels and save it as an image. However, when I try to draw it to my back buffer so that I can see it in the window, it draws nothing at all - all I see is the color I cleared the viewport to.

What am I doing wrong?

```

void DocumentPanel::paintGL()
{
    QOpenGLFunctions_3_3_Core *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();

    //Setup framebuffer
    setupFramebuffer();

    //Render to framebuffer
    f->glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
    f->glClearColor(0, 0, 1, 0);

    f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    drawShapes();

    //--------------------------------------------

    f->glBindFramebuffer(GL_FRAMEBUFFER, 0);

    if (!_oneShot)
    {
        dumpFramebuffer();
        _oneShot = true;
    }

    f->glClearColor(0, 0.5, .5, 0);
    f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    f->glDisable(GL_DEPTH_TEST);
    f->glDisable(GL_STENCIL_TEST);

    int w = width();
    int h = height();
    f->glBindFramebuffer(GL_READ_FRAMEBUFFER, _frameBufferID);
    f->glReadBuffer(GL_COLOR_ATTACHMENT0);
    f->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    f->glDrawBuffer(GL_BACK);
    f->glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
    checkForErrors();
}

void DocumentPanel::setupFramebuffer()
{
    QOpenGLFunctions_3_3_Core *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();

    int w = width();
    int h = height();
    int samples = 4;
    bool multisample = false;

    if (_frameBufferID == 0)
    {
        f->glGenFramebuffers(1, &_frameBufferID);
        f->glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
    }

    if (_renderBufferColorID == 0)
    {
        f->glGenRenderbuffers(1, &_renderBufferColorID);
        f->glBindRenderbuffer(GL_RENDERBUFFER, _renderBufferColorID);

        if (!multisample)
            f->glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, w, h);
        else
            f->glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_RGBA, w, h);
        f->glBindRenderbuffer(GL_RENDERBUFFER, 0);

        f->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderBufferColorID);
    }

    if (_renderBufferDepthStencilID == 0)
    {
        f->glGenRenderbuffers(1, &_renderBufferDepthStencilID);
        f->glBindRenderbuffer(GL_RENDERBUFFER, _renderBufferDepthStencilID);
        if (!multisample)
            f->glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, w, h);
        else
            f->glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_DEPTH24_STENCIL8, w, h);
        f->glBindRenderbuffer(GL_RENDERBUFFER, 0);

        f->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBufferDepthStencilID);
    }

    GLenum status = f->glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE)
    {
        //error
        QString err;

        switch (status)
        {
        case GL_FRAMEBUFFER_UNDEFINED:
            err = "GL_FRAMEBUFFER_UNDEFINED";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
            err = "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT :
            err = "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT ";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER :
            err = "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER ";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER :
            err = "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER ";
        break;
        case GL_FRAMEBUFFER_UNSUPPORTED :
            err = "GL_FRAMEBUFFER_UNSUPPORTED ";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE :
            err = "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE ";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS  :
            err = "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS  ";
        break;
        }

        f->glBindFramebuffer(GL_FRAMEBUFFER, 0);

        qDebug() << "Error building frambuffer: " << err;
        return;
    }

    //Rendering to window again
    f->glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
```

```

**Tags:** frame-buffer

> **Comments:**
> **username_1**: maybe  void QOpenGLContext::swapBuffers(QSurface *surface)  

---

## 1 answers

### Answer by username_0 on 2021-01-24. Score: 0 | Accepted answer

Looks like I misunderstood how my windowing system was handling rendering. It had created an offscreen buffer that was being used to update the window. When I use `glBlitFramebuffer` with the buffer id `QOpenGLWdget` gives me, my image appears.


---

