I have a shape rendered on the screen and I would like to find the x, y and z coordinates of the surface that is directly under the mouse coordinate. When i do this currently I get the x and y coordinates of the viewport and not the surface.
Does anyone have any advice on how to do this?
Any comments will be much appreciated.
Check out gluUnProject(…).
Try to use the picking… searching about picking in the forum. this technique retry the X,Y,Z coords.
The technique is called raytracing/casting. Search for threads/docs. Or ofcourse you could gluProject your shape and check as well…
it can convert your screen cood. to model cood.
If you hadn’t guessed it already there are a number of ways to do what you want. glUnproject() is probably the easiest (I’ve never used it), picking is (AFAIK) wrong (Picking is for checking what object your mouse is over) and raycasting is the most powerful.
I would recommend raycasting as it’s theory and practise can be used in a number of different situations (eg. collision detection).
Basically you just have to imagine a ray originating where you camera is positioned, passing through the near plane (at x,y - scaled to suit your near plane) and out into space (say until it hits the far plane). You then test that plane against the polys in your [transformed] object and find the nearest hit to the camera.
You can optimize this by doing bounds checking as a first sweep, and through using face culling (compare the normal to the direction of the ray).
All that said and done if you don’t think you’ll need raycasting, use glUnproject().