- (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);
}
所以如果你想取消投影到世界空间,你只使用你的lookAt矩阵。如果您想取消投影到特定对象的模型空间中,那么您可以使用model * view
矩阵。