# 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)>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;

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?

• 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 Feb 16 '17 at 16:55
• 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 Feb 16 '17 at 16:56
• 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 Feb 16 '17 at 21:09
• 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 Feb 17 '17 at 16:47

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).