<blockquote>
  <p>I have tried doing glCear(GL_BACK_LEFT) between each iteration but it is not clearing, I'm wondering if I'm missing a line of code before the glClear, or if I'm calling it right. I think the problem is that the back buffer isn't clearing</p>
</blockquote>

<p>This comment of yours sheds some light on the matter. This function call doesn't do what you think it does and is actually wrong usage. The <a href="https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glClear.xhtml" rel="nofollow noreferrer"><code>glClear</code></a> function doesn't actually take the buffer identificator (that you give to <code>glDraw/ReadBuffer</code>) to clear rather than a mask of the general parts of the framebuffer to clear.</p>

<p>The framebuffer usually consists of one or more color buffers, a possible depth buffer and a possible stencil buffer. Therefore you give <code>glClear</code> an ORed combination of the respective flags <code>GL_COLOR_BUFFER_BIT</code>, <code>GL_DEPTH_BUFFER_BIT</code> and <code>GL_STENCIL_BUFFER_BIT</code> and those buffers (of the currently specified draw buffer for the color buffer) are then cleared.</p>

<p>So <em>after</em> setting the draw buffer with <code>glDrawBuffer</code> you call <code>glClear</code> to clear that. As said, the flags depend on what you want cleared (usually at least <code>GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT</code>, special algorithms notwithstanding).</p>

CC BY-SA 3.0
2017-09-30T16:09:36.020Z
2017-09-30T16:09:36.020Z
Christian Rau
<p>I'm writing a function that reads the number of red pixels on the left and right halves of the screen, it makes adjustments while the difference between the number of red pixels falls outside of a threshold. I'm having trouble with the buffers (back and front) because when I finally swap to the front buffer, it dynamically draws all changes I made in the loop meant for the back buffer but I only want the final one drawn to screen. In pseudocode, </p>

<pre><code>do {
    draw scene to back buffer
    read number of left, right red pixels
    if (abs(left-right)&gt;threshold) {
        //some function to jitter objects in the scene
    }
    else done=true,
}while(!done);

//draw to front buffer
</code></pre>

<p>Here is some C++/OpenGL code:</p>

<pre><code>do {
    glDrawBuffer(GL_BACK_LEFT);
    display(0);

    glDrawBuffer(GL_BACK_RIGHT);
    display(1);

    redsleft = 0;
    redsright = 0;

    glReadBuffer(GL_BACK_LEFT);
    glReadPixels(0, 0, resolution.first, resolution.second, GL_RGBA, GL_UNSIGNED_BYTE, frameBuffer);
    for (int j = 0; j &lt; resolution.first*resolution.second * 4; j += 4)
    {
        if (frameBuffer[j]&gt;0 &amp;&amp; frameBuffer[j + 1] == 0 &amp;&amp; frameBuffer[j + 2] == 0 &amp;&amp; frameBuffer[j + 3] &gt; 0)
        {
            if (j % 4 == 0 &amp;&amp; i == 0)
            {
                int redX = (j / 4) % resolution.first;
                int redY = j / 4 / resolution.first;

                if (redX &lt; resolution.first / 2)
                    redsleft++;
                else
                    redsright++;
            }
        }
    }
    //if above threshold, call function to adjust 
    //else done
} while (!done);

glDrawBuffer(GL_BACK_LEFT);
display(0);

glDrawBuffer(GL_BACK_RIGHT);
display(1);

glutSwapBuffers();
glutPostRedisplay();
</code></pre>

<p>Any ideas?</p>

CC BY-SA 3.0
2017-02-16T16:55:58.680Z
I'm not sure I've understood what's going wrong, but couldn't you just repeatedly render to a texture and then just draw a single textured quad when your texture is how you like it?
Dan Hulme
CC BY-SA 3.0
2017-02-16T16:56:59.633Z
Have you remembered to glClear() between each iteration of the loop (or ensure that you're drawing over every pixel in your "function to adjust")?
Dan Hulme
CC BY-SA 3.0
2017-02-16T21:09:07.387Z
I have tried doing glCear(GL_BACK_LEFT) between each iteration but it is not clearing, I'm wondering if I'm missing a line of code before the glClear, or if I'm calling it right. I think the problem is that the back buffer isn't clearing
Stackmm
CC BY-SA 3.0
2017-02-17T16:47:30.573Z
There's a clear mask that affects which buffers are cleared. But I'm just guessing at things here, since you haven't posted a minimal, verifiable example.
Dan Hulme
<opengl><buffers><frame-buffer>
OpenGL back and front buffer - counting and adjusting the number of red pixels before displaying to screen
Stackmm
# OpenGL back and front buffer - counting and adjusting the number of red pixels before displaying to screen

(Asked by: username_0 on 2017-02-16)

I'm writing a function that reads the number of red pixels on the left and right halves of the screen, it makes adjustments while the difference between the number of red pixels falls outside of a threshold. I'm having trouble with the buffers (back and front) because when I finally swap to the front buffer, it dynamically draws all changes I made in the loop meant for the back buffer but I only want the final one drawn to screen. In pseudocode,

```
do {
    draw scene to back buffer
    read number of left, right red pixels
    if (abs(left-right)>threshold) {
        //some function to jitter objects in the scene
    }
    else done=true,
}while(!done);

//draw to front buffer

```

Here is some C++/OpenGL code:

```
do {
    glDrawBuffer(GL_BACK_LEFT);
    display(0);

    glDrawBuffer(GL_BACK_RIGHT);
    display(1);

    redsleft = 0;
    redsright = 0;

    glReadBuffer(GL_BACK_LEFT);
    glReadPixels(0, 0, resolution.first, resolution.second, GL_RGBA, GL_UNSIGNED_BYTE, frameBuffer);
    for (int j = 0; j < resolution.first*resolution.second * 4; j += 4)
    {
        if (frameBuffer[j]>0 && frameBuffer[j + 1] == 0 && frameBuffer[j + 2] == 0 && frameBuffer[j + 3] > 0)
        {
            if (j % 4 == 0 && i == 0)
            {
                int redX = (j / 4) % resolution.first;
                int redY = j / 4 / resolution.first;

                if (redX < resolution.first / 2)
                    redsleft++;
                else
                    redsright++;
            }
        }
    }
    //if above threshold, call function to adjust 
    //else done
} while (!done);

glDrawBuffer(GL_BACK_LEFT);
display(0);

glDrawBuffer(GL_BACK_RIGHT);
display(1);

glutSwapBuffers();
glutPostRedisplay();

```

Any ideas?

**Tags:** opengl, buffers, frame-buffer

> **Comments:**
> **username_1**: I'm not sure I've understood what's going wrong, but couldn't you just repeatedly render to a texture and then just draw a single textured quad when your texture is how you like it?  
> **username_1**: Have you remembered to glClear() between each iteration of the loop (or ensure that you're drawing over every pixel in your "function to adjust")?  
> **username_0**: I have tried doing glCear(GL_BACK_LEFT) between each iteration but it is not clearing, I'm wondering if I'm missing a line of code before the glClear, or if I'm calling it right. I think the problem is that the back buffer isn't clearing  
> **username_1**: There's a clear mask that affects which buffers are cleared. But I'm just guessing at things here, since you haven't posted a minimal, verifiable example.  

---

## 1 answers

### Answer by username_2 on 2017-09-30. Score: 2

> I have tried doing glCear(GL\_BACK\_LEFT) between each iteration but it is not clearing, I'm wondering if I'm missing a line of code before the glClear, or if I'm calling it right. I think the problem is that the back buffer isn't clearing

This comment of yours sheds some light on the matter. This function call doesn't do what you think it does and is actually wrong usage. The [`glClear`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glClear.xhtml) function doesn't actually take the buffer identificator (that you give to `glDraw/ReadBuffer`) to clear rather than a mask of the general parts of the framebuffer to clear.

The framebuffer usually consists of one or more color buffers, a possible depth buffer and a possible stencil buffer. Therefore you give `glClear` an ORed combination of the respective flags `GL_COLOR_BUFFER_BIT`, `GL_DEPTH_BUFFER_BIT` and `GL_STENCIL_BUFFER_BIT` and those buffers (of the currently specified draw buffer for the color buffer) are then cleared.

So *after* setting the draw buffer with `glDrawBuffer` you call `glClear` to clear that. As said, the flags depend on what you want cleared (usually at least `GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT`, special algorithms notwithstanding).


---

