Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL get 3D coordinates of nearest world 3D point to the current mouse Location

in an OpenGL context, I have seen it is possible to convert mouse coordinates to 3D world coordinates (e.g. MFC with Opengl get 3d coordinate from 2d coordinate of mouse). However, this does not work when I have simply a set of GLPoints and lots of empty space: when I'm hovering the mouse over empty space, the 3D coordinates have no meaning.

How can I get the coordinates of the nearest 3D point to my mouse position?

like image 758
manatttta Avatar asked Apr 28 '15 07:04

manatttta


1 Answers

Recognize that "unprojecting" 2D mouse coordinates into a 3D world requires additional information. A 2D position on the screen corresponds to an infinite number of points along a line, from the near plane to the far plane in 3D. What this means is that to get a 3D point, you need to provide a depth value as well.

The gluUnProject function is convenient way of doing this and provides a parameter for this winZ. 0.0 would find the 3D point at the near plane and 1.0 would find it at the far plane.

gluUnProject(winX, winY, winZ, model, proj, view, objX, objY, objZ);

Note: If gluUnProject is not available you can figure out the same thing pretty easily with matrices. source here

When the mouse is used to interact with 3D objects this depth value is typically found by sampling from the depth buffer, or intersecting a ray with scene geometry, or a primitive (such as a sphere or box). If the objective is to interact with points, you could use a sphere around each point. If you just want a 3D point "somewhere out there" you could decide on a depth value (maybe 0.0 on the near plane or 0.5 - halfway between the near and far plane).

like image 55
Justin Meiners Avatar answered Nov 11 '22 20:11

Justin Meiners