Reputation: 1708
How to use GLKMathUnproject to determine location in world space? The user needs to select one of several objects in world space.
In doc it says
GLKVector3 GLKMathUnproject (
GLKVector3 window,
GLKMatrix4 model,
GLKMatrix4 projection,
int *viewport,
bool *success
);
but which modelView matrix? I have bunch of models in world. Which model matrix should i use.
My world is 2d, on x,y plane. z is used for camera movement.
If i understood correctly, GLKMathUnproject
is used to find a point in model space.
How do i know which model? Do i need to determine first the model below the fingers or what?
Upvotes: 0
Views: 1256
Reputation: 1708
- (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 you want to unproject to world space, you use just your lookAt matrix.
And if you want to unproject into model space of a specific object, then you use model * view
matrix.
Upvotes: 1
Reputation: 3441
Create simple project, which you be able to create from templates of Xcode, called "OpenGL Game".
model - modelViewMatrix or self.effect.transform.modelviewMatrix
projection - projectionMatrix or self.effect.transform.projectionMatrix
Viewport:
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
Your code will be similar to:
GLKVector point = ...;
bool boolValue;
GLKMathUnproject(point,
modelViewMatrix,
projectionMatrix, viewport, &boolValue);
Upvotes: 0