Friday 12 April 2013

iOS: OpenGL window coordinates calculated from location in UIView is wrong

Usually a point's OpenGL window coordinates can be calculated from its location in UIView by:

    CGPoint pt = [gestureRecognizer locationInView:self];
    pt.y = self.bounds.size.height - pt.y;

However this calculation could be wrong if OpenGL viewport size does not match UIView size. 

I tried to resize OpenGL viewport at various point to match UIView size but did not succeed. Finally I got a workaround which allows me to get the correct OpenGL window coordinates:

    float viewport[4];
    glGetFloatv(GL_VIEWPORT, viewport);
    pt.y = (1.0f - pt.y/self.bounds.size.height) * viewport[3];

No comments:

Post a Comment