PDA

View Full Version : Selection and pick object



dandosan
06-17-2013, 05:48 AM
Hi everyone
I would like to get information about a clicked object.
Actually i would like to implement a sort of osnap function like in autocad (when the cursor
get near a line and i click, i can get the information about that line).

I think to do it with the glRenderMode(gl_select) but i don't know how to start...
Someone could help me?
Is this method right?
or maybe it's better doing that with the gluunproject funtcion?

I read tons of documentation but nothing for vb.net

Thanks

RigidBody
06-17-2013, 06:07 AM
basically there are 3 ways to implement picking (at least 3 i know of)

1. using the select buffer (your 1st idea)

2. map your object to screen coordinates using gluProject and calculate the distance to the mouse pointer (your 2nd idea).
this approach is very easy to implement. you have to keep in mind that for your OS which sends you a mouse event
y=0 at the top of the screen, while for OpenGL y=0 at the bottom of the screen (or vice versa, i never remember that correctly).

3. if your models consists of quads/trias and you want only to pick the quad/tria that is currently in front, you can draw the scene
into the back buffer, using a different color for each quad/tria; then use glReadPixels() to read the color at the mouse position and
calculate the quad/tria id from the color value. this is a slightly more complicated way.

dandosan
06-17-2013, 09:19 AM
Ok perfect.
I read about the three ways you described but I'm quite sure that the 3rd is not good for me.
I have to handle just lines for my project.
The second one could be good. I already wrote a function that get the world coordinates
form the screen coordinates. So I can do it just adding some geometric funciotn that calculates
the distance form the cursor to the many lines rendered.
You suggest to go on with the second way or with the select buffer?

And about the select buffer, what I have to do to get informations form the clicked object.
I can't understand what's the way...

Thanks a lot!

Aleksandar
06-17-2013, 10:20 AM
And about the select buffer, what I have to do to get informations form the clicked object.
I can't understand what's the way...
Instead of paraphrasing, I would rather advise you to read the following (http://fly.cc.fer.hr/~unreal/theredbook/chapter12.html). That is the 12th chapter of the old "Red book".

dandosan
06-19-2013, 06:45 AM
I yet read it many times but I can't understand how to start!

Anyway I'm trying with the other way of glunproject and hope I can do what I need

Aleksandar
06-19-2013, 10:54 AM
I yet read it many times but I can't understand how to start!
OpenGL Programming Guide is a well written book. I cannot realize what could be a problem.
If you have a particular question, just ask. Otherwise, you should find answers by yourself.
gluUnProject() only maps window coordinates to object coordinates, but does not answer on the question what object is clicked on. You should use it together with "pure color" rendering and glReadPixels(), or something like that.