views:

48

answers:

2

Hi, everyone, i have some questions about how to pick up mouse in 3d world.

Now i have several points in 3d world,by using these points i can draw curve

i want to choose a point on this curve using mouse, the viewport of the 3d world can be changed, so i want to know at any viewport when i chick mouse button near the curve, which point is the nearest to the mouse ?or which point is chose by me?

i do have no idea about how to implement it,help me please if you know.

Thanks. Good luck.

+1  A: 

OpenGL has a selection mode, where you can take the position of a mouse click, and create a matrix that restricts drawing to some arbitrary (small) area around the mouse (e.g., a 7x7 pixel block). You can then draw your "stuff" (curve, points, whatever) and it'll create a record of all the objects that fell inside of the block you defined. If more than one item fell inside that block, you get records of whatever did, one record per item, sorted by z-order from front to back.

When you're done, you get a record of the points that were close to the mouse click. Most of the time, you'll just use the closest to the front, but once in a while you'll want to do a bit more work to figure out which of them (if there was more than one, obviously) was really closest to the original mouse click point. gluProject and (possibly) gluUnProject can be handy if you decide to do that.

Jerry Coffin
+2  A: 

If you are trying to click discrete points (perhaps nodal points on a curve) another approach is to project them back into screen coords and check for a close match to the mouse location - since this is done in integer coordinates and need only be done roughly accurate (assuming your points are more than a few screen pixels apart.

Another trick used to be to redraw the scene in an off-screen buffer with each point (or position on the curve) drawn in a different RGB color - then use the getPixel function to return the color under the mouse point and look up the point id. This doesn't work in complex scenes with fog.

Martin Beckett
The "other trick" will work, but definitely go for solution #1. Take your coordinates, multiply them by the modelview matrix, then by the projection matrix, multiply by your viewport size, get the nearest match.
Calvin1602
The shading trick is very useful if you need to interesect with surfaces - finding a point inside a triangle (even in 2d) is expensive.
Martin Beckett