Beispiel #1
0
 /**
  * Sets this {@link Matrix4} to a world matrix with the specified cardinal axis and the origin at
  * the provided position.
  *
  * @param position {@link Vector3} The position to use as the origin of the world coordinates.
  * @param forward {@link Vector3} The direction of the forward (z) vector.
  * @param up {@link Vector3} The direction of the up (y) vector.
  * @return A reference to this {@link Matrix4} to facilitate chaining.
  */
 @NonNull
 public Matrix4 setToWorld(
     @NonNull Vector3 position, @NonNull Vector3 forward, @NonNull Vector3 up) {
   mVec1.setAll(forward).normalize(); // Forward
   mVec2.setAll(mVec1).cross(up).normalize(); // Right
   mVec3.setAll(mVec2).cross(mVec1).normalize(); // Up
   return setAll(mVec2, mVec3, mVec1.multiply(-1d), position);
 }
 /**
  * Utility method to move the specified number of units along the current up axis. This will also
  * adjust the look at target (if a valid one is currently set).
  *
  * @param units {@code double} Number of units to move. If negative, movement will be in the
  *     "down" direction.
  */
 public void moveUp(double units) {
   mTempVec.setAll(WorldParameters.UP_AXIS);
   mTempVec.rotateBy(mOrientation).normalize();
   mTempVec.multiply(units);
   mPosition.add(mTempVec);
   if (mLookAtEnabled && mLookAtValid) {
     mLookAt.add(mTempVec);
     resetToLookAt();
   }
   markModelMatrixDirty();
 }
    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;
    }
Beispiel #4
0
 /**
  * Projects a given {@link Vector3} with this {@link Matrix4} storing the result in the given
  * {@link Vector3}.
  *
  * @param vec {@link Vector3} The vector to multiply by.
  * @return {@link Vector3} The resulting vector.
  */
 @NonNull
 public Vector3 projectVector(@NonNull Vector3 vec) {
   double inv = 1.0 / (m[M30] * vec.x + m[M31] * vec.y + m[M32] * vec.z + m[M33]);
   vec.multiply(m);
   return vec.multiply(inv);
 }