如何使用GLKMathUnproject来确定世界空间中的位置?用户需要在世界空间中选择几个对象中的一个。
在doc里面写着
GLKVector3 GLKMathUnproject (
GLKVector3 window,
GLKMatrix4 model,
GLKMatrix4 projection,
int *viewport,
bool *success
);但是哪个modelView矩阵呢?我在世界上有一堆模特儿。我应该使用哪个模型矩阵。
我的世界是二维的,在x,y平面上。Z用于摄像机移动。
如果我理解正确的话,GLKMathUnproject是用来在模型空间中寻找一个点的。我怎么知道是哪种型号?我需要首先确定手指下面的模型还是什么?
发布于 2013-05-24 15:12:39
- (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矩阵。
发布于 2013-05-21 17:55:20
创建一个简单的工程,你可以从Xcode的模板中创建,叫做"OpenGL游戏“。
型号- modelViewMatrix或self.effect.transform.modelviewMatrix
投影- projectionMatrix或self.effect.transform.projectionMatrix
视口:
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);您的代码将类似于:
GLKVector point = ...;
bool boolValue;
GLKMathUnproject(point,
modelViewMatrix,
projectionMatrix, viewport, &boolValue);https://stackoverflow.com/questions/16665002
复制相似问题