【问题标题】:Android OpenGLES 2.0 ButtonsAndroid OpenGLES 2.0 按钮
【发布时间】:2013-02-11 15:22:58
【问题描述】:

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

【问题讨论】:

    标签: android button opengl-es-2.0


    【解决方案1】:

    如果有人需要,我找到了发布此问题的解决方案。

       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 类中,以便它给出准确的视图的高度和宽度。如果您的视图占据整个屏幕,您还可以使用显示指标来获取完整的屏幕宽度和高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多