Is it possible to get the 3D coordinates of the point the mouse is pointing to.

This example might help.

Thanks,

Rowan

Is it possible to get the 3D coordinates of the point the mouse is pointing to.

This example might help.

Thanks,

Rowan

Yes. You have to do the following. Make sure that the depth test is enabled ofcourse.

- 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;
```

- Get the window z value from the depth buffer using,

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

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

Works a treat, thanks