1

我正在寻找一个描述来触摸我的 OpenGL 对象或在我触摸它时获取一个事件。GLKit 或 OpenGL ES 有一些功能可以使用吗?或者我必须计算我的对象的位置,并且必须将它与我的 Touch 的坐标进行比较?

4

1 回答 1

3

没有内置的,但这里是 gluProject 函数的移植版本,它会将对象中的一个点放在屏幕坐标中,因此您可以查看您的触摸是否在该点附近:

GLKVector3 gluProject(GLKVector3 position, 
                  GLKMatrix4 projMatrix,
                  GLKMatrix4 modelMatrix,
                  CGRect viewport
                  )
{
GLKVector4 in;
GLKVector4 out;

in = GLKVector4Make(position.x, position.y, position.z, 1.0);

out = GLKMatrix4MultiplyVector4(modelMatrix, in);
in = GLKMatrix4MultiplyVector4(projMatrix, out);

if (in.w == 0.0) NSLog(@"W = 0 in project function\n");
in.x /= in.w;
in.y /= in.w;
in.z /= in.w;
/* Map x, y and z to range 0-1 */
in.x = in.x * 0.5 + 0.5;
in.y = in.y * 0.5 + 0.5;
in.z = in.z * 0.5 + 0.5;

/* Map x,y to viewport */
in.x = in.x * (viewport.size.width) + viewport.origin.x;
in.y = in.y * (viewport.size.height) + viewport.origin.y;

return GLKVector3Make(in.x, in.y, in.z);

}

- (GLKVector2) getScreenCoordOfPoint {

GLKVector3 out = gluProject(self.point, modelMatrix, projMatrix, view.frame);

GLKVector2 point = GLKVector2Make(out.x, view.frame.size.height - out.y);

return point;
}
于 2012-12-14T14:55:07.940 回答