0

如何使用 GLKMathUnproject 确定世界空间中的位置?用户需要在世界空间中选择几个对象之一。

在文档中它说

GLKVector3 GLKMathUnproject (
   GLKVector3 window,
   GLKMatrix4 model,
   GLKMatrix4 projection,
   int *viewport,
   bool *success
);

但是哪个模型视图矩阵?我在世界上有很多模型。我应该使用哪个模型矩阵。

我的世界是二维的,在 x,y 平面上。z 用于相机移动。

如果我理解正确,GLKMathUnproject用于在模型空间中找到一个点。我怎么知道哪个型号?我需要先确定手指下方的模型还是什么?

4

2 回答 2

1
- (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-24T07:12:39.070 回答
0

创建一个简单的项目,您可以从 Xcode 的模板中创建它,称为“OpenGL 游戏”。

模型 - modelViewMatrixself.effect.transform.modelviewMatrix

投影 - projectionMatrixself.effect.transform.projectionMatrix

视口:

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

您的代码将类似于:

GLKVector point = ...;
bool boolValue;    
GLKMathUnproject(point,
                         modelViewMatrix,
                         projectionMatrix, viewport, &boolValue);
于 2013-05-21T09:55:20.083 回答