Android OpenGL ES 2.0投影触摸位置

Android OpenGL ES 2.0投影触摸位置,android,opengl-es,opengl-es-2.0,touch-event,Android,Opengl Es,Opengl Es 2.0,Touch Event,我是Android版OpenGL ES 2.0的新手,我想制作一个简单的应用程序来训练我的技能。我对投射位置有问题。我在我的应用程序中画了一些方块,我想检测我在触摸屏幕时碰到的方块。如果我不使用模型视图投影矩阵,一切都很简单,但当我使用MVP矩阵时,我就是不明白它是如何工作的。下面是一些代码: 在Crender类别中: @Override public void onDrawFrame(GL10 unused) { GLES20.glCle

我是Android版OpenGL ES 2.0的新手,我想制作一个简单的应用程序来训练我的技能。我对投射位置有问题。我在我的应用程序中画了一些方块,我想检测我在触摸屏幕时碰到的方块。如果我不使用模型视图投影矩阵,一切都很简单,但当我使用MVP矩阵时,我就是不明白它是如何工作的。下面是一些代码: 在Crender类别中:

@Override public void onDrawFrame(GL10 unused) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // Set the camera position (View matrix) Matrix.setLookAtM(mViewMatrix, 0, 0f, 0f, 3f, 0f, 0f, 0f, 0f, 1f, 0f); // Calculate the projection and view transformation Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); lock.lock(); try { for(Entry> squares : getObjects()) { for(AngelicSquare square : squares.getValue()) { square.draw(mMVPMatrix); } } } finally { lock.unlock(); } } @Override public void onSurfaceChanged(GL10 unused, int width, int height) { GLES20.glViewport(0, 0, width, height); float ratio = (float) width / height; screenWidth = width; screenHeight = height; // this projection matrix is applied to object coordinates // in the onDrawFrame() method Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1f, 1f, 1f, 10f); } 在AngelicView类中,onTouchEvent:

for(Entry> squares : devilishRenderer.getObjects()) { for(AngelicSquare square : squares.getValue()) { float[] coords = square.getPosition(); float[] size = square.getSize(); float[] touchPoint = { e.getX(), e.getY(), 0.0f, 0.0f }; float[] touchPointNew = ViewCoordsToGLCoords(touchPoint); float[] topLeft = { coords[0], coords[1], 0.0f, 0.0f }; Matrix.multiplyMV(topLeft, 0, devilishRenderer.getMVPMatrix(), 0, topLeft, 0); float[] bottomRight = { coords[0] + size[0], coords[1] - size[1], 0.0f, 0.0f }; Matrix.multiplyMV(bottomRight, 0, devilishRenderer.getMVPMatrix(), 0, bottomRight, 0); Matrix.multiplyMV(touchPoint, 0, devilishRenderer.getMVPMatrix(), 0, touchPointNew, 0); if(touchPoint[0] > topLeft[0] && touchPoint[1] bottomRight[1]) { hookedSquare = square; } } } 如何使用模型视图投影矩阵将屏幕坐标投影到OpenGL坐标系统