PDA

View Full Version : OpenGL rubber band selection issue



sevenfold
05-20-2016, 08:13 AM
I'm using this typical code for rubber band selection:



glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_XOR);

// draw rect

glDisable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_COPY);


And it works ok for everything except a middle gray background (128,128,128), as the inverse of gray is also gray,
so the selection lines turn invisible.

I was just wondering if there's some kind of hack to fix this case, aside from not using a gray background.

sevenfold
05-20-2016, 04:49 PM
If I had access to the pixels, then a hack might be possible.

Is there an example of how to do rubber band selection using shaders?



GL_XOR s ^ d
s represents the incoming color and
d represents the color in the frame buffer.

GClements
05-20-2016, 09:18 PM
I was just wondering if there's some kind of hack to fix this case, aside from not using a gray background.
The easiest solution is to use #808080 as the band colour rather than #fffffff. That way, the difference between the modified value and the original will always be +/- 128 rather than varying between +/-1 and +/-255.

A shader can't read from the framebuffer which is being modified, so to use a shader you'd need to either render into a texture (glFramebufferTexture() etc) or copy the framebuffer contents to a texture (glCopyTex[Sub]Image2D()).

sevenfold
05-21-2016, 10:45 AM
The easiest solution is to use #808080 as the band colour rather than #fffffff. That way, the difference between the modified value and the original will always be +/- 128 rather than varying between +/-1 and +/-255.

Thanks for the idea!

I can take your idea further, and dynamically change the band color depending on the background color set by the user.

So, if the background color is gray, I can set the band color to (128,128,128), otherwise I can leave the band color to all white (255,255,255).

However, even though a gray band color works well on a gray background, it doesn't seem to work well when there are other colors there too, like red. The selection line disappears right through a red box. Oh well.

GClements
05-22-2016, 12:50 AM
I can take your idea further, and dynamically change the band color depending on the background color set by the user.

So, if the background color is gray, I can set the band color to (128,128,128), otherwise I can leave the band color to all white (255,255,255).

That's fine provided that you only need the band to be visible against the background. If it needs to be visible against any colour, then (128,128,128) is the best you can get in terms of the difference vector.



However, even though a gray band color works well on a gray background, it doesn't seem to work well when there are other colors there too, like red. The selection line disappears right through a red box. Oh well.

Check that the values are reproduced exactly; (127,127,127) is no better than white. OpenGL mostly tries to treat colour components as real numbers in the range 0..1, but for logic ops the bit pattern matters. If you're using a 5:5:5 or 5:6:5 framebuffer, it's possible that the values might not get converted correctly.