gluUnproject on GL_Points or finding the depth coordinates from mouse coordinates

911 views Asked by At

I have a scene consisting of a lot of points which I drew using

glBegin(GL_POINTS);
glVertex3f(x[i],y[i],z[i]); // the points are displayed properly .. 
glEnd();

What I wish to do is to be able to click on one of the points on the scene using the mouse and get its 3-D coordinate.

I have seen other threads to use :

glReadPixels((GLdouble)mouse_x, 
    (GLdouble) (rect.Height()-mouse_y-1),1, 1,GL_DEPTH_COMPONENT, GL_FLOAT, &Z);

and use the value of z in

gluUnProject(mouse_x, mouse_y, 0, modelview, projection, viewport, out posX, out posY, out posZ);

but i always get z=0 as the output .Is this because these are points and not a polygon?Is there any way to get the coordinates of the z?

2

There are 2 answers

5
Jerry Coffin On BEST ANSWER

Unfortunately, it can't be done. Any point x,y point on the screen can refer to any point along a given ray in the scene.

Given that you're drawing points, you probably want to use select mode to select a specific point, and then determine the coordinates of that point.

0
Razzupaltuff On

I think you are calling glReadPixels the wrong way. x, y, width and height must be GLint, not double. This has nothing to do with the format of the result glReadPixels returns. So you should pass the window coordinates for the mouse position and window sizes to glReadPixels (e.g. glReadPixels (mouse_x, rect.Height() - mouse_y, rect.Width(), rect.Height(), GL_DEPTH_COMPONENT, GLfloat, &z);. If mouse_x and mouse_y value range is [0.0 .. 1.0], you need to properly scale them in your call to glReadPixels (rect.Width * mouse_x, rect.Height() * (1.0 - mouse_y) If you get this right, imo your code should work as expected.