Thursday, 23 May 2013

OpenGL ES 1.1: How to convert world coordinates to UIView coordinates

    Assuming world coordinates is in worldp of type vec4, UIView window size is in bounds.size, the following code converts world coordinates to UIView coordinates:

    mat4 modelview;
    mat4 projection;
    glGetFloatv(GL_MODELVIEW_MATRIX, (float*)modelview);
    glGetFloatv(GL_PROJECTION_MATRIX, (float*)projection);
    vec4 eyep = worldp * modelview * projection;
    eyep = eyep/eyep.w;
    screenp.x = (1+eyep.x)/2*bounds.size.width;
    screenp.y = (1-eyep.y)/2*bounds.size.height;

No comments:

Post a Comment