public Vector3 calculatePos(int x, int y, Object3D object3D) { Matrix4 mViewMatrix = getCurrentCamera().getViewMatrix(); Matrix4 mProjectionMatrix = getCurrentCamera().getProjectionMatrix(); double[] mNearPos4 = new double[4]; double[] mFarPos4 = new double[4]; Vector3 mNearPos = new Vector3(); Vector3 mFarPos = new Vector3(); Vector3 mNewObjPos = new Vector3(); int[] mViewport = new int[] {0, 0, getViewportWidth(), getViewportHeight()}; GLU.gluUnProject( x, getViewportHeight() - y, 0, mViewMatrix.getDoubleValues(), 0, mProjectionMatrix.getDoubleValues(), 0, mViewport, 0, mNearPos4, 0); GLU.gluUnProject( x, getViewportHeight() - y, 1.f, mViewMatrix.getDoubleValues(), 0, mProjectionMatrix.getDoubleValues(), 0, mViewport, 0, mFarPos4, 0); mNearPos.setAll( mNearPos4[0] / mNearPos4[3], mNearPos4[1] / mNearPos4[3], mNearPos4[2] / mNearPos4[3]); mFarPos.setAll( mFarPos4[0] / mFarPos4[3], mFarPos4[1] / mFarPos4[3], mFarPos4[2] / mFarPos4[3]); double factor = (Math.abs(object3D.getZ()) + mNearPos.z) / (getCurrentCamera().getFarPlane() - getCurrentCamera().getNearPlane()); mNewObjPos.setAll(mFarPos); mNewObjPos.subtract(mNearPos); mNewObjPos.multiply(factor); mNewObjPos.add(mNearPos); return mNewObjPos; }
/** * Left multiplies this {@link Matrix4} with the given one, storing the result in this {@link * Matrix}. * * <pre> * A.leftMultiply(B) results in A = BA. * </pre> * * @param matrix {@link Matrix4} The LHS {@link Matrix4}. * @return A reference to this {@link Matrix4} to facilitate chaining. */ @NonNull public Matrix4 leftMultiply(@NonNull Matrix4 matrix) { System.arraycopy(m, 0, mTmp, 0, 16); Matrix.multiplyMM(m, 0, matrix.getDoubleValues(), 0, mTmp, 0); return this; }