问题
I've created a simple 3D world using GLKit. I'm trying to find a specific point in my 3D world based on a screen tap. It seems that I would want to use GLKMathUnproject. The signature of the method is:
GLKVector3 GLKMathUnproject (
GLKVector3 window,
GLKMatrix4 model,
GLKMatrix4 projection,
int *viewport,
bool *success
);
A few things confuse me about the params it requires. First shouldn't the window coordinates be GLKVector2? And most importantly wouldn't projecting a point on the screen into 3D space create a ray instead of a single point. When you project points in 3D space onto a 2D plain (aka the screen) you loose one dimension of information about that point. So how would it be possible to transform a 2D screen point into a 3D world point without specifying at what depth you'd like to 'unproject' your point to? There is absolutely no comment about this method anywhere on the internet (besides apple docs).
回答1:
The first parameter is GLKVector3 because the z component stores the desired z-depth.
So window x,y are the screen coords and window z is the desired depth.
eg with z=0 GLKMathUnproject will give the coordinates of the object at the near plane with z=1, GLKMathUnproject will give you the coordinates of the object at the far plane.
This is necessary because GLKMathUnproject maps from a plane to a line in the 3d space formed by the projection frustum. The z - bounds of the frustum are constrained by the near and far planes.
So say you wanted to map the unprojected mouse point at the z=0 (object space) plane in the frustum.
Then unproject 2 points with window z=0 and window z =1 1. Use these points to form a line in object space. Interesect this line with the plane z=0 to determine the final target point.
Hope this helps someone -
You can read more here CasualDistraction Blog
回答2:
You have an 'eye direction' but you don't know the depth. Then you calculate how much you must scale this vector to reach the same depth as each object you want to test. Once you have done this, check that the extended vector actually ends up inside a reasonable proximity of your target object. It's called 'ray casting'.
来源:https://stackoverflow.com/questions/11056391/how-to-use-glkmathunproject