简体   繁体   中英

gluUnproject on GL_Points or finding the depth coordinates from mouse coordinates

I have searched a lot for this qn and have got some info. but I hope that u cud point me wat I am doing wrong.

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 ?

Thank u very much and i am sorry if this is a real noob qn ...

Cheers!

Rahul

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.

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM