OpenGL gets the 3D coordinates of the closest 3D world point to the current mouse. Location

in the context of OpenGL, I saw that it is possible to convert the coordinates of the mouse to the 3D coordinates of the world (for example, MFC with Opengl get the 3d coordinate from the 2nd coordinate of the mouse ), However, this does not work when I just have a GLPoint set and a lot of free spaces: when I hover over an empty spot, the 3D coordinates have no meaning.

How can I get the coordinates of the closest 3D point in my mouse position?

+6
source share
1 answer

Recognize that additional information is required to β€œnot design” two-dimensional mouse coordinates in the 3D world. The two-dimensional position on the screen corresponds to an infinite number of points along the line, from the near plane to the far plane in 3D. This means that to get a 3D point, you also need to specify a depth value.

The gluUnProject function is a convenient way to do this and provides a parameter for this winZ . 0.0 will find a 3D point in the near plane, and 1.0 will find it in the background.

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

Note: If gluUnProject not available, you can easily find the same with matrices only)

When a mouse is used to interact with 3D objects, this depth value is usually determined by fetching the depth from the buffer or intersecting the beam with the geometry of the scene or primitive (for example, a sphere or rectangle). If the goal is to interact with points, you can use a sphere around each point. If you just want the 3D point to be somewhere out there, you could determine the depth value (maybe 0.0 on the near plane, or 0.5 halfway between the near and far plane).

+2
source

Source: https://habr.com/ru/post/986108/


All Articles