read the depth buffer

How can I read the z coordinate value from the z buffer, for a given x & y coordinates?

Requiered for a program where the user selects a 3D object (in perspective presentation) using the mouse’s cursor.
Thanks

In a Windows app, I do something like this -

float fWinZ;
int nViewport[4];
glGetIntegerv(GL_VIEWPORT, nViewport);

// reverse y coordinate for windows
int y = nViewport[3] - y - 1;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &fWinZ);

Any good?

[This message has been edited by Rog (edited 07-06-2003).]

Thanks, i will check this.

I have a new problem:
I have a program that receives from the user control points of two B-Spline curves (de Boor points) and draws the surface with these curves as edges, connecting in a straight line between each two control points in different curves (ruled surface).
My problem is the gluNurbsSurface method doesn’t work correctly when the control points array is dynamically allocated (two or three dimentions array), where it works correctly when it is statically allocated. i didn’t find how to operate it right.

I would be grateful for any help anyone can give me with this.
Thanks

Originally posted by Rog:
[b]In a Windows app, I do something like this -

[quote]

float fWinZ;
int nViewport[4];
glGetIntegerv(GL_VIEWPORT, nViewport);

// reverse y coordinate for windows
int y = nViewport[3] - y - 1;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &fWinZ);

Any good?

[This message has been edited by Rog (edited 07-06-2003).][/b][/QUOTE]