Android的OpenGL ES 2.0的屏幕坐标为世界坐标(Android OpenGL ES

2019-06-25 09:55发布

我建立一个使用OpenGL ES 2.0的Android应用程序,我已经碰壁。 我想屏幕坐标(其中用户触摸)为世界坐标转换。 我试着阅读和与GLU.gluUnProject玩弄但我要么做错了,或只是不明白。

这是我尝试....

public void getWorldFromScreen(float x, float y) {
    int viewport[] = { 0, 0, width , height};

    float startY = ((float) (height) - y);
    float[] near = { 0.0f, 0.0f, 0.0f, 0.0f };
    float[] far = { 0.0f, 0.0f, 0.0f, 0.0f };

    float[] mv = new float[16];
    Matrix.multiplyMM(mv, 0, mViewMatrix, 0, mModelMatrix, 0);

    GLU.gluUnProject(x, startY, 0, mv, 0, mProjectionMatrix, 0, viewport, 0, near, 0);
    GLU.gluUnProject(x, startY, 1, mv, 0, mProjectionMatrix, 0, viewport, 0, far, 0);

    float nearX = near[0] / near[3];
    float nearY = near[1] / near[3];
    float nearZ = near[2] / near[3];

    float farX = far[0] / far[3];
    float farY = far[1] / far[3];
    float farZ = far[2] / far[3];
}

我越来越似乎不正确的数字,这是利用这种方法,正确的方法是什么? 不知是否对OpenGL ES 2.0的工作? 我应该做模型矩阵这些计算(Matrix.setIdentityM(mModelMatix,0))之前的身份矩阵?

作为后续行动,如果这是正确的,我怎么挑输出端Z? 基本上,我总是知道在什么距离,我想世界坐标所在,但在GLU.gluUnProject在Z参数似乎是某种远近平面之间的插值。 难道仅仅是一个线性插值?

提前致谢

Answer 1:

/**
    * Calculates the transform from screen coordinate
    * system to world coordinate system coordinates
    * for a specific point, given a camera position.
    *
    * @param touch Vec2 point of screen touch, the
      actual position on physical screen (ej: 160, 240)
    * @param cam camera object with x,y,z of the
      camera and screenWidth and screenHeight of
      the device.
    * @return position in WCS.
    */
   public Vec2 GetWorldCoords( Vec2 touch, Camera cam)
   {  
       // Initialize auxiliary variables.
       Vec2 worldPos = new Vec2();

       // SCREEN height & width (ej: 320 x 480)
       float screenW = cam.GetScreenWidth();
       float screenH = cam.GetScreenHeight();

       // Auxiliary matrix and vectors
       // to deal with ogl.
       float[] invertedMatrix, transformMatrix,
           normalizedInPoint, outPoint;
       invertedMatrix = new float[16];
       transformMatrix = new float[16];
       normalizedInPoint = new float[4];
       outPoint = new float[4];

       // Invert y coordinate, as android uses
       // top-left, and ogl bottom-left.
       int oglTouchY = (int) (screenH - touch.Y());

       /* Transform the screen point to clip
       space in ogl (-1,1) */       
       normalizedInPoint[0] =
        (float) ((touch.X()) * 2.0f / screenW - 1.0);
       normalizedInPoint[1] =
        (float) ((oglTouchY) * 2.0f / screenH - 1.0);
       normalizedInPoint[2] = - 1.0f;
       normalizedInPoint[3] = 1.0f;

       /* Obtain the transform matrix and
       then the inverse. */
       Print("Proj", getCurrentProjection(gl));
       Print("Model", getCurrentModelView(gl));
       Matrix.multiplyMM(
           transformMatrix, 0,
           getCurrentProjection(gl), 0,
           getCurrentModelView(gl), 0);
       Matrix.invertM(invertedMatrix, 0,
           transformMatrix, 0);       

       /* Apply the inverse to the point
       in clip space */
       Matrix.multiplyMV(
           outPoint, 0,
           invertedMatrix, 0,
           normalizedInPoint, 0);

       if (outPoint[3] == 0.0)
       {
           // Avoid /0 error.
           Log.e("World coords", "ERROR!");
           return worldPos;
       }

       // Divide by the 3rd component to find
       // out the real position.
       worldPos.Set(
           outPoint[0] / outPoint[3],
           outPoint[1] / outPoint[3]);

       return worldPos;       
   }

进一步解释算法在这里。



Answer 2:

希望我的问题(和答案)应该帮助你:

如何找到点击的绝对位置,而在放大

它不仅具有代码,但也图表和图形和图表解释吧:)我花了年龄搞清楚为好。



Answer 3:

恕我直言,一个不需要重新实现此功能。我尝试用埃罗尔的解决方案,它的工作,所以非常感谢它埃罗尔。 此外,我打了

        Matrix.orthoM(mtrxProjection, 0, left, right, bottom, top, near, far);

并能正常工作,以及在我的小小白例如2D OpenGL ES 2.0的项目:

  • 公共无效onSurfaceChanged(GL10未使用的,INT宽度,高度INT){...


文章来源: Android OpenGL ES 2.0 screen coordinates to world coordinates