PDA

View Full Version : Get 3D Mouse Corordinates.



Fezziwig
03-25-2012, 01:52 PM
Is it possible to get the 3D coordinates of the point the mouse is pointing to.

This example might help.
http://i.imgur.com/e9v5v.jpg

Thanks,
Rowan

mobeen
03-25-2012, 09:48 PM
Yes. You have to do the following. Make sure that the depth test is enabled ofcourse.
1) Get the window x and y values where the mouse is clicked. Dont forget to flip the y value (height-mouseY).


float winX = mouseX;
float winY = height - mouseY;

2) Get the window z value from the depth buffer using,


float winZ=0;
glReadPixels( winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

3) Once you have the window x,y,z position, unproject this value using the current modelview, projection and viewport values, this will give you the world coordinate position of the point.


GLdouble MV[16];
GLdouble P[16];
GLint viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, MV);
glGetDoublev(GL_PROJECTION_MATRIX, P);
glGetIntegerv(GL_VIEWPORT, viewport);

double objX=0, objY=0, objZ=0;
gluUnProject(winX, winY, winZ, MV, P, viewport, &objX, &objY, &objZ);


See if this helps.

Fezziwig
03-26-2012, 01:10 PM
Works a treat, thanks