PDA

View Full Version : Which is the best way of doing mouse picking?



doodlemunch
06-15-2005, 07:31 PM
I got some tutorials where they use GL_SELECT as render mode and do some pass to check which object id was clicked

But is there an easier and better way? because when I click at the surroundings of the cube it doesnt detect I clicked it I want more accurate method if possibel.

thansk

T101
06-15-2005, 10:13 PM
You could give each object a specific color, disable lighting, antialiasing, blending and texturing, render the whole thing to a texture or the backbuffer, and read it back.
Then just read the pixel at the mouse coordinates and that's it.

Alternatively you'd have to do it from the source - make your own raycast etc.

doodlemunch
06-16-2005, 02:58 AM
uh sounds like a lot of mess i just want to do simple picking not pixel-perfect but quite accurate yes!

T101
06-16-2005, 03:21 AM
You have to understand that OpenGL renders triangles (and quads, but those are often just split into triangles under the hood).

So OpenGL does not know about objects and object IDs.

Without using your own raycasting routines (or copying them from somewhere else of course), the easiest methods are the feedback mode (the GL_SELECT stuff), and reading back colors.

doodlemunch
06-16-2005, 05:23 AM
of course graphic cards can only take triangle data but that has nothing to do with my question i know opengl has nothing to do with picking as well and i know this is an opengl forum but as my question is related to opengl i did it here and as im a newbie i did it at the beginners section. i think theres a glu function for picking, am i wrong?

06-22-2005, 05:42 AM
The OpenGL tips claim that the fastest way to identify the object under the mouse pointer is to identify the color of the pixel (using glreadpixel) and then using this color value to identify the object(assuming that each object is given a unique ID based on its color).

The other more complex way is to use Selection buffer and gluPickMatrix. You can find a working example in OpenGL red book for the same.

Aeluned
06-22-2005, 06:47 AM
Originally posted by <Karthik>:
The OpenGL tips claim that the fastest way to identify the object under the mouse pointer is to identify the color of the pixel (using glreadpixel) and then using this color value to identify the object(assuming that each object is given a unique ID based on its color).

The other more complex way is to use Selection buffer and gluPickMatrix. You can find a working example in OpenGL red book for the same.It's too constraining to assume that each object will be distinguishable by it's color.

There is nothing wrong with using the selection buffer. whilst in GL_SELECT mode, nothing more occurs than generating transformed fragments (they are unlit, uncolored, etc...).

It's fast and straightforward to implement.

check out the NeHe tutorial: clicky (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32)

RigidBody
06-22-2005, 07:50 AM
It's too constraining to assume that each object will be distinguishable by it's color. if you do picking using the color buffer, you would give each object a unique color and draw the scene in the back buffer without swapping.

Aeluned
06-22-2005, 08:46 AM
fine, but why go through the trouble?

RigidBody
06-22-2005, 09:25 AM
because it it simple and reliable.
you draw the scene, read the pixel color and you
always get one element- not a list of elements.

Aeluned
06-22-2005, 11:34 AM
Originally posted by RigidBody:
because it it simple and reliable.
you draw the scene, read the pixel color and you
always get one element- not a list of elements.A list of elements isn't always a bad thing...you may not want to select the object closest to the viewer.

Either way, to each their own...