Color Picker with AntiAliasing in OpenGL?

I have a problem with color picking and anti-aliasing in OpenGL. When AA is activated, the results of glReadPixels are obviously erroneous at the boundaries of objects and intersections of objects. For instance:

I put box # 28 (RGBA: 28, 0, 0, 0) next to box number 32 (RGBA: 32, 0, 0, 0). Using AA, I can get the wrong ReadPixel value (e.g. 30), where the cube and triangle overlap or the value 14 on the edge of the boxes due to the AA algorithm.

I have ~ 4,000 thousand objects that I need to select (this is a puzzle game). It is imperative to be able to select objects by shape.

I tried to disable AA using glDisable (GL_MULTISAMPLE), but it does not work with certain AA modes (I read that it depends on the implementation of AA - SS, MS, CS ..)

So how do I select a base object?

  • How to temporarily disable AA?
  • Using a different buffer or even rendering context?
  • Any other suggestion?
+3
source share
2 answers

Why not use FBO as a selection buffer?

+7
source

I use this hack: we select not one pixel, but all 3x3 = 9 pixels around the selection point. If they are all the same, we are safe. Otherwise, it should be on the verge, and we can skip this.

int renderer::pick_(int x, int y)
{
    static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__,
            "only works on little-endian architecture");
    static_assert(sizeof(int) == 4,
            "only works on architecture that has int size of 4");

    // sort of edge detection. selection only happens at non-edge
    // since the edge may cause anti-aliasing glitch
    int ids[3*3];
    glReadPixels(x-1, y-1, 3, 3, GL_RGBA, GL_UNSIGNED_BYTE, ids);
    for (auto& id: ids) id &= 0x00FFFFFF;       // mask out alpha
    if (ids[0] == 0x00FFFFFF) return -1;        // pure white for background

    // prevent anti-aliasing glitch
    bool same = true;
    for (auto id: ids) same = (same && id == ids[0]);
    if (same) return ids[0];

    return -2;                                  // edge
}
+1
source

All Articles