-1

我是 android OpenGL 的新手,我正在尝试使用 OpenGL 绘制按钮我已经为 GLSurface 视图添加了一个手势监听器,现在当用户触摸时我有运动事件。我的问题是如何将motionevent.getx 和motionevent.gety(在像素范围内)转换为视图的窗口或对象坐标?

4

1 回答 1

0

如果有人需要,我找到了这个问题的解决方案。

   public float[] convertToObjectCoordinates(MotionEvent event) {
        float[] worldPos = new float[2];
        float[] invertedMatrix, transformMatrix,
                normalizedInPoint, outPoint, mProjMatrix, mVMatrix;
        invertedMatrix = new float[16];
        transformMatrix = new float[16];
        mProjMatrix = new float[16];
        mProjMatrix = mRenderer.getmProjMatrix();
        mVMatrix = new float[16];
        //Change the Proj and ModelView matrix according to your model and view matrix or you can use your mvpMatrix directly instead of transform Matrix                                     
        Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0, 0, 0f, 0.0f, 1.0f, 0.0f);
        normalizedInPoint = new float[4];
        outPoint = new float[4];
        float y = screenHeight - event.getY();
        setHeightAndWidth();
        normalizedInPoint[0] = (float) ((event.getX()) * 2.0f / screenWidth - 1.0);
        normalizedInPoint[1] = (float) ((y) * 2.0f / screenHeight - 1.0);
        normalizedInPoint[2] = - 1.0f;
        normalizedInPoint[3] = 1.0f;
        Matrix.multiplyMM( transformMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
        Matrix.invertM(invertedMatrix, 0, transformMatrix, 0);
        Matrix.multiplyMV(outPoint, 0, invertedMatrix, 0, normalizedInPoint, 0);
        if (outPoint[3] !=  0.0)
        {
            worldPos[0] = outPoint[0] / outPoint[3];
            worldPos[1] = outPoint[1] / outPoint[3];
        } else {
            Log.e("Error", "Normalised Zero Error");
        }
        return worldPos;
    }

这是对 Android OpenGL2.0 的以下帖子的重制: Android OpenGL ES 2.0 屏幕坐标到世界坐标 EROl 的回答感谢 EROl 的回复。

      public void setHeightAndWidth() {
         screenHeight = this.getHeight();
         screenWidth = this.getWidth();
      }

上面的方法应该写在 GLSurfaceView 类中,以便它给出确切的视图的高度和宽度。如果您的视图占据整个屏幕,您还可以使用显示指标来获取完整的屏幕宽度和高度。

于 2013-02-11T20:48:48.903 回答