iPhone OpenGL ES - How to Pick

后端 未结 3 804
说谎
说谎 2021-01-05 12:36

I\'m working on an OpenGL ES1 app which displays a 2D grid and allows user to navigate and scale/rotate it. I need to know the exact translation of View Touch coordinates in

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 13:17

    Picking mode is not available in openGL ES. But it's easy to calculate the screen coordinate of any 3d point using the projection matrix retrieved from current openGL state. Here is how I do it (IMPoint2D and IMPoint3D are basic (x,y) and (x,y,z) structures)

    + (IMPoint2D) getScreenCoorOfPoint:(IMPoint3D)_point3D
    {
        GLfloat     p[16];                                              // Where The 16 Doubles Of The Projection Matrix Are To Be Stored
        glGetFloatv(GL_PROJECTION_MATRIX, p);                           // Retrieve The Projection Matrix
    /*
    Multiply M * point
    */
        GLfloat _p[] = {p[0]*_point3D.x +p[4]*_point3D.y +p[8]*_point3D.z + p[12],
                        p[1]*_point3D.x +p[5]*_point3D.y +p[9]*_point3D.z + p[13],
                        p[2]*_point3D.x +p[6]*_point3D.y +p[10]*_point3D.z+ p[14],
                        p[3]*_point3D.x +p[7]*_point3D.y +p[11]*_point3D.z+ p[15]};
    /*
    divide by scale factor
    */
        IMPoint2D _p2D = {_p[0]/_p[3], _p[1]/_p[3]};
    /*
    get result in screen coordinates. In this case I'm in landscape mode
    */
        return (IMPoint2D) {_p2D.x*240.0f + 240.0f, (1.0f - _p2D.y) *160.0f};
    }
    

提交回复
热议问题