OpenGL back and front buffer - counting and adjusting the number of red pixels before displaying to screen 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)&gt;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 &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();


Any ideas?
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 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).