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?