Get 3d-coordinates of cursor


I’d like to get the coordinates of cursor (x,y,z).
I use glReadPixels() with DEPTH_COMPONENT and gluUnproject() functions. The method returns correct result when cursor is over a 3d-vertex (or over some other object). But when it isn’t over any object I cannot use this method because glReadPixels() always assigns Z to 1 (z=1 as result). Are there any ways to get 3d - coordinates of cursor if it isn’t over any object ( it’s pointing to nothing, plane)?

I need this to move 3d-vertex by mouse (with changing it’s World x,y and z coordinates). I’ll be very happy if anybody help me with this problem.

(sorry for my English)

What would you expect to get back when you’re over an area for which you have not drawn any 3D object?

You’re getting back the far clip value (i.e. 1, assuming you leave glDepthRange to the default 0…1 range). Which is exactly what you likely “drew” in there when you cleared the depth buffer. If you want a 3D coordinate for all pixels, ensure that you draw something (e.g. a 3D backdrop) over your entire window.

Thank you, but which 3d shape should I use as a background to provide getting correct value of Z?
For example we have a 3d point. When we move it right we expect it’s X coordinate to increase (from x1 to x1+m), and Z, Y must not change. But if we use an empty hemisphere as a background and move point right, Z coordinate changes it’s value.

Correct is defined by you. So whatever shape you want that you judge to provide “correct” Z values for your use case.