objective c - iOS Using GLKMathUnproject to find screen point in world space -
how use glkmathunproject determine location in world space? user needs select 1 of several objects in world space.
in doc says
glkvector3 glkmathunproject ( glkvector3 window, glkmatrix4 model, glkmatrix4 projection, int *viewport, bool *success );
but modelview matrix? have bunch of models in world. model matrix should use.
my world 2d, on x,y plane. z used camera movement.
if understood correctly, glkmathunproject
used find point in model space. how know model? need determine first model below fingers or what?
- (ibaction)handletapunproject:(id)recognizer { bool success = no; glfloat realy; glint viewport[4] = {}; glgetintegerv(gl_viewport, viewport); nslog(@"%d, %d, %d, %d", viewport[0], viewport[1], viewport[2], viewport[3]); cgpoint touchorigin = [recognizer locationinview:self.view]; nslog(@"tap coordinates: %8.2f, %8.2f", touchorigin.x, touchorigin.y); realy = viewport[3] - touchorigin.y; glkmatrix4 modelview = lookat; // near glkvector3 origininwindownear = glkvector3make(touchorigin.x, realy, 0.0f); glkvector3 result1 = glkmathunproject(origininwindownear, modelview, projectionmatrix, viewport, &success); nsassert(success == yes, @"unproject failure"); glkmatrix4 matrix4_1 = glkmatrix4translate(glkmatrix4identity, result1.x, result1.y, 0.0f); _squareunprojectnear.modelmatrixusage = glkmatrix4multiply(matrix4_1, _squareunprojectnear.modelmatrixbase); glkvector3 rayorigin = glkvector3make(result1.x, result1.y, result1.z); // far glkvector3 origininwindowfar = glkvector3make(touchorigin.x, realy, 1.0f); glkvector3 result2 = glkmathunproject(origininwindowfar, modelview, projectionmatrix, viewport, &success); nsassert(success == yes, @"unproject failure"); glkmatrix4 matrix4_2 = glkmatrix4translate(glkmatrix4identity, result2.x, result2.y, 0.0f); glkvector3 raydirection = glkvector3make(result2.x - rayorigin.x, result2.y - rayorigin.y, result2.z - rayorigin.z); }
so if want unproject world space, use lookat matrix. , if want unproject model space of specific object, use model * view
matrix.
Comments
Post a Comment