How can I get Z for screenToWorld()?

Hello forum,

I want to use screenToWorld() to interact with 3D objects. So how can I get the z coordinate of the argument ofVec3f ScreenXYZ?

I expected this function to calculate a 3D coordinate, given a 2D coordinate of screen such as mouseX and mouseY. Of course it’s impossible to specify one 3D coordinate from a 2D coordinate. So I guessed there were “magical” calculations behind, but there is a simple transformation with a matrix.

I checked out the advanced3dExample, but I didn’t get it.

So, anyway, how can I get the Z?


it is some thing like this
not sure if this code will work for you, but it is originally from grabCam, there is a function called findCursor()
ofVec3f mouse;
mouse.x = ofGetMouseX();
mouse.y = ofGetMouseY();
glReadPixels(mouse.x, ofGetHeight()-1-mouse.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mouse.z);