7

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
manatttta
  • 3,054
  • 4
  • 34
  • 72
  • 3
    What point would that be? If you point your finger towards the empty sky, is the "nearest" point at your fingertip or at the end of the universe or somewhere inbetween? – molbdnilo Apr 28 '15 at 07:33
  • @molbdnilo I was thinking this test could be implemented in the xy plane only. If I cast a ray starting from the xy mouse position and return all points inside a cilinder of n pixels from z=[0.0, 1.0] with some fixed radius (in xy) – manatttta Apr 28 '15 at 07:38
  • Well, OpenGL is not a scene graph. It draws pixels on the screen and that's it. So whatever you do, OpenGL is just a tool to help you get there. Next non void point closest to a click into a window? That's a image space search problem. In theory you'd spiral outwards (in image space) from the clicked point looking for a non void pixel. – datenwolf Apr 28 '15 at 08:24
  • if you want to use this on OpenGL side instead of CPU then you can take the depth buffer as texture and blur it (ignoring clear depth values) so the depth coordinates will bleed into all empty areas of the screen. Then simply use that instead of the rendered image for mouse coordinate ... – Spektre Sep 10 '15 at 07:38

1 Answers1

4

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).

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92