0
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ 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? $\endgroup$
    – Dan Hulme
    Commented Feb 16, 2017 at 16:55
  • 2
    $\begingroup$ 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")? $\endgroup$
    – Dan Hulme
    Commented Feb 16, 2017 at 16:56
  • 1
    $\begingroup$ 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 $\endgroup$
    – Stackmm
    Commented Feb 16, 2017 at 21:09
  • 2
    $\begingroup$ 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. $\endgroup$
    – Dan Hulme
    Commented Feb 17, 2017 at 16:47

1 Answer 1

2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.