0
votes

How can I efficiently perform selection in OpenGL?

I have read this question, and still some things remain uncertain to me.

In my application, I would like to support two forms of selection. First clicking with a mouse and second selecting using a pickbox. My application has also inherited a bunch of bad programming practices from the 90's and early 2000's specifically related to OpenGL, e.g., it uses GL_SELECT.

After all my reading, I understand this can be very inefficient. I have learned of two ways to address this:

  1. Color picking: assign a unique color to every object in the scene. Render it on a 1x1px imaginary window and read the color.
  2. Ray casting using collision detection

Questions:

  1. How do I perform color picking for a box? Do I simply render the scene on a window the size of my box and read the matrix of pixels collecting the unique colors to select appropriately?
  2. Is ray casting the preferred method? If so, how can this be extended for pick boxes?
  3. Is there another method other than these which is commonly used?
1
Unless you've profiled to verify that this is causing a problem (which I'll bet you haven't and it isn't) leave it alone. Finding the object(s) a microsecond faster after you've just waited hundreds of milliseconds for mouse input makes no sense at all. - Jerry Coffin
Do you want to select all objects within your selection rectangle or just the ones which are not completely occluded? Because if you want to select all, that would rule out color picking. - Kristian Duske
@JerryCoffin I have scenes with quite a few objects in them, in the hundred thousands. Depending on the complexity of the objects and the number of objects it can take a second or more to select objects with the current setup. I don't think this is the entire bottleneck in the process, but I would like to know for future reference what is the ideal method of picking. - pippin1289
@KristianDuske I think just the directly visible would be fine. I don't think selecting the occluded items is necessary. Good information though, thank you. - pippin1289

1 Answers

2
votes

1. How do I perform color picking for a box? Do I simply render the scene on a window the size of my box and read the matrix of pixels collecting the unique colors to select appropriately?

This is what I would do, but this will only select such objects which are at least partially visible. Objects that are fully occluded will not be selected with color picking.

2. Is ray casting the preferred method? If so, how can this be extended for pick boxes?

I would say that ray casting is the preferred method, yes. AFAIK, it cannot be easily extended for pick boxes (which really are pick frustums, though). I would use a spacial data structure such as an octree to quickly select candidate objects for the frustum, then I would test the frustum against the bounding box of each candidate. For the remaining candidates, I would check whether any of its vertices lies within the frustum.

The octree can be reused to speed up ray picking (for click selection) as well.

Depending on the number of objects and vertices, this might not be fast enough, though. I'm sure there are ways to speed it up though.

3. Is there another method other than these which is commonly used?

Not that I know of.