Selection/Picking demo

Post: #1
Hey all. I've been looking over the OGL Programming Guide section on picking. I think I understand it, but I'd prefer to see a working implementation to get more of a feel for it before I try to implement it in StormCloud. It's next on the agenda, but I've been procrastinating--I have to get back into it.

I'd also like to see source code that runs on a Mac, although the NeHe sample will probably be sufficient (although he's mixed it up with a bunch of other stuff).

One place the OGL PG falls short is on strategies. They kind of run through it very quickly. I'll probably have to write my own demo outside of StormCloud anyway, but input is always welcome. It seems as though the OpenGL mechanism is sufficient (and probably necessary), but the fact that selection/picking is a user interface process makes me wish there was a more Mac-savvy way to do it.

If I'm forced to run through a drawing cycle, isn't it possible for me to test intersection myself? I guess that would mean a significantly more complex bit of test code mingled with the drawing code, compared to a simple call of glPushName() or glLoadName(). And whereas I would have to intersect a volume with a line, OpenGL only has to test for whether a fragment dirties a rectangle of pixels. I think.

Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
If you're only interested in which object was hit, rather than sub-object position, OpenGL's selection buffer mechanism is probably the nicest way to find what was clicked.

You probably want to check a rectangle a few pixels around the cursor, to allow for some slop in selection. Note that that means you might click multiple objects, so you'd have to deal with that somehow.

It really is as easy as the Red Book makes out Smile
Quote this message in a reply
Post: #3
Well, I believe you. But I'm still glad to suddenly have some code to study -- Bryan Blackburn just posted his Cocoa port of NeHe lesson 32 yesterday (see Poels's thread about tutorials). So I'm looking that over.

But I'm sure I will still need to do some thinking about how to use picking effectively when there are lots of objects on the screen (you know, like hundreds or thousands). I can't imagine having a picking name for every one of them. I should expect to take advantage of the scene graph hierarchy in the way hinted at by the Red Book, which is to push a series of numbers corresponding to the position of the object in the hierarchy. But its not immediately obvious to me how to handle the specifics.

Also, what if I did want to select a single polygon, edge, or vertex for selection and individual editing? For example, if I wanted to select a face to change the texture?

Quote this message in a reply
Post: #4

You can download from index.html space goon with source code, it is a game I made that lets you bash spaceships into each other. It uses OpenGL and Cocoa frameworks but it is not Object Oriented because I coded it really badly yet it is still useful and easy to read.

The Cocoa-OpenGL.sit thing should go in developer/ProjectBuilderExtras/ProjectBuilderTemplates/Applications in with Cocoa-Application Cocoa-DocumentBased-Application and all that. Then In project builder you should be able to go new Project and make an Cocoa-OpenGL application really quickly.

Source code for space goon and a good ProjectBuilder template should help... right.
Quote this message in a reply
Posts: 184
Joined: 2004.07
Post: #5
I don't see why it's a problem that you're 'forced into a drawing cycle' to do picking- after all, a drawing cycle should only take 1/30 to 1/60 of a second anyway, shouldn't it?

Instead of your own geometry-based collision detection, you could something similar to what OpenGL does, and watch the pixel under the cursor as you draw, and if the z-buffer value changes after drawing an object, change the selection to that object- this way you don't have to fool around with the gl*name() stuff if you don't want to.

If you need to check for polygons instead of objects, you can first check for objects, then with the selected object, check for polygons. If you also want to have vertex selection you could draw a billboard with a circle texture at each vertex and use the same strategy.

Quote this message in a reply
Post: #6
I'd really recommend you implementing a quadtree and using a line from the near clipping plane to the far clipping plane to test for intersections. It is not terribly hard and you'll gain _a lot_ of speed over the OGL picking method. Use gluUnProject to tranlate mouse coordinates into world coordinates.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How do I do Color Picking? dockode 2 7,658 Sep 24, 2011 08:02 PM
Last Post: dockode
  Issues with picking FFaqui 0 3,226 Nov 21, 2009 04:37 PM
Last Post: FFaqui
  Opengl picking problem (zip file) papillon68 1 6,423 Mar 1, 2009 08:49 PM
Last Post: chronus
  Picking and selection... chalamov33 2 5,258 Mar 31, 2008 02:08 PM
Last Post: wyrmmage
  glut picking clashes with drawing wyrmmage 2 5,099 Jan 24, 2008 06:39 PM
Last Post: wyrmmage