Android OpenGLES 2.0按钮

时间:2013-02-11 15:22:58

标签: android button opengl-es-2.0

我是Android OpenGL的新手我正在尝试使用OpenGL绘制按钮我已经为GLSurface View添加了一个Gesture Listener,现在我有动作事件,当用户接触时。我的问题是我如何转换motionevent.getx和motionevent.gety(它们在像素范围内 )到窗口或视图的对象坐标?

1 个答案:

答案 0 :(得分: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 screen coordinates to world coordinates EROl的回答 感谢EROl的回复。

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

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

相关问题