Picking in 2D

I have rendered some objects in 2D, using calls to glVertex2*(), so there is no concept of depth applicable. In some instances, I draw an object over another, thus creating an overlap. When I click the mouse button, over the objects while in selection mode, my returned name is only the object behind. I can’t use depth values the determine which is in the foreground and which in the background, because everything has a zValue of 0 essentially. What am I doing wrong? How can I ensure the object drawn on top of the other one is returned?

Do a search in this branch of the forum and you will find several discussions about picking.

One popular technique is to draw the world to a second, invisible rendering target. The colors used are encoded values that uniquely identify the objects. When the user clicks on an object, you sample the color from the rendering texture at that point. Then decode the color into an indicator of the object in question.

I would add that the problem you’re getting is probably related to the fact that feedback mode tends to be done in software rather than hardware, so there may well be differences in whether a pixel is determined to come from one polygon or the other, since the z-values are all the same.

Using an invisible buffer with the ID stored as color has the advantage that it’s both done in hardware, so there should be no discrepancies.

By the way, you should be able to do this by using the regular backbuffer:

  1. Clear the backbuffer and render the scene to the backbuffer
  2. swap buffers
  3. Clear the backbuffer and render the scene with the polygon (or object) ID stored in the color. No texturing, no lighting.
  4. do a glReadPixels from the backbuffer and work with that to allow selection

I have looked into selection mode rendering on the back buffer with a unique color value per object. The problem is that I don’t want to have to assign a unique color value for an object. Currently, I take the selected ID and use that as a key to a hash table to obtain a reference to my object that was selected. Doing so gives me a pointer to my object and I can poll information from it. It works fine when nothing overlaps though. Can you think of any other way to do this? If so, do you know a good way to ensure a unique color value per object that I register a new selectionID with?

When picking on overlapped objects, you should get a list of IDs with depth values.

Read this tutorial, around section “Searching closest object picked” :
http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial27.php

Thank you, but this is being rendered in 2D so there are no distinct depth values.

Then do as proposed above, do a first (hidden) render with color as ids, then glReadPixels where you click, then clear and draw your real scene before swap.

ZBuffer, the point of me writing this post is to see if I can come up with something other than using color as a unique identifier. Please read the previous posts and gather all the information. I am trying to use this community to figure out new, untried techniques that might be more suitable to my project.

Well I think you were given pretty good advices already.

The problem is that I don’t want to have to assign a unique color value for an object. Currently, I take the selected ID and use that as a key to a hash table to obtain a reference to my object that was selected. Doing so gives me a pointer to my object and I can poll information from it.

You can do exactly the same thing if you encode your ID (24 bits) as a color.
ID = r * 2^16 + g * 2^8 + b
And use bitmasks to reverse.

What is still holding you from using colors ? As you have no depth information, it is the only choice.
OpenGL selection buffer is not accelerated anyway :slight_smile:

Or just use depth… If your objects overlap, it is wrong to say that you have no depth information. Besides, you should be using the selection mode in first place, it is – as it seems now – deprecated.

so I should have depth information even though I’m using 2D rendering, an never giving a z coordinate for my vertices? Does this mean the overlapping object will be returned as closer?

No, you should have depth information because your application know the order of rendering. So you can emulate depth by offsetting the overlay objects on the z-axis

Are you saying that I should render to the back buffer with a Z value when in selection mode and use the depth information obtained during that?

You could do it if you insist on using the selection mode. I still suggest that you do the selection yourself (in you app) using the knowledge about objects you (obviously) posess. Or just do what other suggested and use unique colors: this would be the simplest solution.

What is the reason you are hesitant to use the unique colors method?