I'm trying to create an app for Android using OpenGL ES, but I'm having trouble handling touch input.
I've created a class CubeGLRenderer which spawns a Cube. CubeGLRenderer is in charge of the projection and view matrix, and Cube is in charge of its model matrix. The Cube is moving along the positive X axis, with no movement in Y nor Z.
CubeGLRenderer updates the view matrix each frame in order to move along with the cube, making the cube look stationary on screen:
Matrix.setLookAtM(mViewMatrix, 0, 0.0f, cubePos.y, -10.0f, 0.0f, cubePos.y, 0.0f, 0.0f, 1.0f, 0.0f);
The projection matrix is calculated whenever the screen dimension changes (i.e. when the orientation of the device changes). The two matrices are then muliplied and passed to Cube.draw() where it applies its model matrix and renders itself to screen.
So far, so good. Let's move on to the problem.
I want to touch the screen and calculate an angle from the center of the cube's screen coordinates to the point of the screen that I touched.
I thought I'd just accomplish this using GLU.gluProject(), but I'm either not using it correctly or simply haven't understood it at all.
Here's the code I use to calculate the screen coordinates from the cube's world coordinates:
public boolean onTouchEvent(MotionEvent e) {
Vec3 cubePos = cube.getPos();
float[] modelMatrix = cube.getModelMatrix();
float[] modelViewMatrix = new float[16];
Matrix.multiplyMM(modelViewMatrix, 0, mViewMatrix, 0, modelMatrix, 0);
int[] view = {0, 0, width, height};
float[] screenCoordinates = new float[3];
GLU.gluProject(cubePos.x, cubePos.y, cubePos.z, modelViewMatrix, 0, mProjectionMatrix, 0, view, 0, screenCoordinates, 0);
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("CUBEAPP", "screenX: " + String.valueOf(screenCoordinates[0]));
break;
}
return true;
}
What am I doing wrong?