以下screenToWorld()方法返回Ray原点(或)对象的交点。如果它是对象的交叉点,如何识别特定对象上的点击事件?
public PointF hitTest(MotionEvent e) {
return screenToWorld(modelViewMatrix, modelViewProjectionMatrix, e.getX(), e.getY());
}
public static PointF screenToWorld(float[] viewMatrix,
float[] projMatrix, float screenX, float screenY) {
float[] nearPos = unProject(viewMatrix, projMatrix, screenX, screenY, 0);
float[] farPos = unProject(viewMatrix, projMatrix, screenX, screenY, 1);
Log.d(LOGTAG,"nearPos ->"+nearPos.length+" "+nearPos);
Log.d(LOGTAG,"farPos ->"+farPos.length+" "+farPos);
// The click occurred in somewhere on the line between the two points
// nearPos and farPos. We want to find
// where that line intersects the plane at z=0
float distance = nearPos[2] / (nearPos[2] - farPos[2]); // Distance between nearPos and z=0
float x = nearPos[0] + (farPos[0] - nearPos[0]) * distance;
float y = nearPos[1] + (farPos[1] - nearPos[0]) * distance;
return new PointF(x, y);
}
private static float[] unProject(float[] viewMatrix,
float[] projMatrix, float screenX, float screenY, float depth) {
float[] position = {0, 0, 0, 0};
int[] viewPort = {0, 0, 1, 1};
GLU.gluUnProject(screenX, screenY, depth, viewMatrix, 0, projMatrix, 0,
viewPort, 0, position, 0);
position[0] /= position[3];
position[1] /= position[3];
position[2] /= position[3];
position[3] = 1;
return position;
}