Which is the best way of doing mouse picking?

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

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.

uh sounds like a lot of mess i just want to do simple picking not pixel-perfect but quite accurate yes!

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.

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?

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.

Originally posted by <Karthik>:
[b]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.[/b]
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

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.

fine, but why go through the trouble?

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.

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…