Esempio n. 1
0
 public Plane(Vector3 normal, double distance) {
   this.normal = normal;
   this.distance = distance;
   this.planeOrigin = normal.clone().multiply(distance);
   this.planeZRotation = normal.clone().getRotationTo(new Vector3(0, 0, 1));
   this.inversePlaneZRotation = planeZRotation.clone().inverse();
 }
 /**
  * Sets the scale of this {@link ATransformable3D} object. If this is part of a scene graph, the
  * graph will be notified of the change.
  *
  * @param scale double The scaling factor on axes.
  * @return A reference to this {@link ATransformable3D} to facilitate chaining.
  */
 public ATransformable3D setScale(double scale) {
   mScale.x = scale;
   mScale.y = scale;
   mScale.z = scale;
   markModelMatrixDirty();
   return this;
 }
 /**
  * Resets the up axis for this {@link ATransformable3D} object to the +Y axis. If this is part of
  * a scene graph, the graph will be notified of the change.
  *
  * @return A reference to this {@link ATransformable3D} to facilitate chaining.
  */
 public ATransformable3D resetUpAxis() {
   mUpAxis.setAll(Vector3.getAxisVector(Vector3.Axis.Y));
   if (mLookAtEnabled && mLookAtValid) {
     mOrientation.lookAt(mLookAt, mUpAxis);
     markModelMatrixDirty();
   }
   return this;
 }
 /**
  * Orients this {@link ATransformable3D} object to 'look at' the specified point. If this is part
  * of a scene graph, the graph will be notified of the change.
  *
  * @param x {@code double} The look at target x coordinate.
  * @param y {@code double} The look at target y coordinate.
  * @param z {@code double} The look at target z coordinate.
  * @return A reference to this {@link ATransformable3D} to facilitate chaining.
  */
 public ATransformable3D setLookAt(double x, double y, double z) {
   mLookAt.x = x;
   mLookAt.y = y;
   mLookAt.z = z;
   resetToLookAt();
   markModelMatrixDirty();
   return this;
 }
Esempio n. 5
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);
 }
 /**
  * Resets the orientation of this {@link ATransformable3D} object to look at its look at target
  * and use the specified {@link Vector3} as up. If this is part of a scene graph, the graph will
  * be notified of the change.
  *
  * @param upAxis {@link Vector3} The direction to use as the up axis.
  * @return A reference to this {@link ATransformable3D} to facilitate chaining.
  */
 public ATransformable3D resetToLookAt(Vector3 upAxis) {
   mTempVec.subtractAndSet(mLookAt, mPosition);
   // In OpenGL, Cameras are defined such that their forward axis is -Z, not +Z like we have
   // defined objects.
   if (mIsCamera) mTempVec.inverse();
   mOrientation.lookAt(mTempVec, upAxis);
   mLookAtValid = true;
   markModelMatrixDirty();
   return this;
 }
 /**
  * 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();
 }
  private void showHudPositionHelper(Quaternion currentOrientation) {
    final Vector3 movement = WorldParameters.FORWARD_AXIS.clone();
    Quaternion relative = currentOrientation.clone();
    movement.rotateBy(relative).multiply(3);
    movement.inverse();
    centralPane.getPosition().add(movement);

    // TODO move off to the side.
    leftTopPlane.getPosition().add(movement);
    //        leftBottomPlane.getPosition().add(movement);
  }
    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;
    }
Esempio n. 10
0
 private Vector3 findPointNear(Vector3 searchPosition) {
   if (depth == 0) {
     return point;
   } else if (searchPosition.x < position.x
       || searchPosition.x > position.x + range
       || searchPosition.y < position.y
       || searchPosition.y > position.y + range
       || searchPosition.z < position.z
       || searchPosition.z > position.z + range) {
     throw new IllegalArgumentException(
         "Outside of range: "
             + searchPosition.toString()
             + " does not belong to "
             + position.toString()
             + " with range "
             + range);
   } else {
     if (searchPosition.x < position.x + halfRange) {
       if (searchPosition.y < position.y + halfRange) {
         if (searchPosition.z < position.z + halfRange) {
           return findPointNear(searchPosition, 0);
         } else {
           return findPointNear(searchPosition, 1);
         }
       } else {
         if (searchPosition.z < position.z + halfRange) {
           return findPointNear(searchPosition, 2);
         } else {
           return findPointNear(searchPosition, 3);
         }
       }
     } else {
       if (searchPosition.y < position.y + halfRange) {
         if (searchPosition.z < position.z + halfRange) {
           return findPointNear(searchPosition, 4);
         } else {
           return findPointNear(searchPosition, 5);
         }
       } else {
         if (searchPosition.z < position.z + halfRange) {
           return findPointNear(searchPosition, 6);
         } else {
           return findPointNear(searchPosition, 7);
         }
       }
     }
   }
 }
Esempio n. 11
0
 public void put(Vector3 point) {
   if (depth == 0) {
     this.point = point;
   } else if (point.x < position.x
       || point.x > position.x + range
       || point.y < position.y
       || point.y > position.y + range
       || point.z < position.z
       || point.z > position.z + range) {
     throw new IllegalArgumentException(
         "Outside of range: "
             + point.toString()
             + " does not belong to "
             + position.toString()
             + " with range "
             + range);
   } else {
     if (point.x < position.x + halfRange) {
       if (point.y < position.y + halfRange) {
         if (point.z < position.z + halfRange) {
           put(point, 0, position.x, position.y, position.z);
         } else {
           put(point, 1, position.x, position.y, position.z + halfRange);
         }
       } else {
         if (point.z < position.z + halfRange) {
           put(point, 2, position.x, position.y + halfRange, position.z);
         } else {
           put(point, 3, position.x, position.y + halfRange, position.z + halfRange);
         }
       }
     } else {
       if (point.y < position.y + halfRange) {
         if (point.z < position.z + halfRange) {
           put(point, 4, position.x + halfRange, position.y, position.z);
         } else {
           put(point, 5, position.x + halfRange, position.y, position.z + halfRange);
         }
       } else {
         if (point.z < position.z + halfRange) {
           put(point, 6, position.x + halfRange, position.y + halfRange, position.z);
         } else {
           put(point, 7, position.x + halfRange, position.y + halfRange, position.z + halfRange);
         }
       }
     }
   }
 }
Esempio n. 12
0
 /**
  * Sets the components of the provided {@link Vector3} representing the scaling component of this
  * {@link Matrix4}.
  *
  * @param vec {@link Vector3} to store the result in.
  * @return {@link Vector3} representing the scaling.
  */
 @NonNull
 public Vector3 getScaling(@NonNull Vector3 vec) {
   final double x = Math.sqrt(m[M00] * m[M00] + m[M01] * m[M01] + m[M02] * m[M02]);
   final double y = Math.sqrt(m[M10] * m[M10] + m[M11] * m[M11] + m[M12] * m[M12]);
   final double z = Math.sqrt(m[M20] * m[M20] + m[M21] * m[M21] + m[M22] * m[M22]);
   return vec.setAll(x, y, z);
 }
Esempio n. 13
0
 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
   mCameraOffset.setAll(
       (mSeekBarX.getProgress() * 0.2f) - 10,
       (mSeekBarY.getProgress() * 0.2f) - 10,
       (mSeekBarZ.getProgress() * 0.2f));
   ((ChaseCameraRenderer) mRenderer).setCameraOffset(mCameraOffset);
 }
 /**
  * Sets the up axis for this {@link ATransformable3D} object. If this is part of a scene graph,
  * the graph will be notified of the change.
  *
  * @param x double The x component of the new up axis.
  * @param y double The y component of the new up axis.
  * @param z double The z component of the new up axis.
  * @return A reference to this {@link ATransformable3D} to facilitate chaining.
  */
 public ATransformable3D setUpAxis(double x, double y, double z) {
   mUpAxis.setAll(x, y, z);
   if (mLookAtEnabled && mLookAtValid) {
     mOrientation.lookAt(mLookAt, mUpAxis);
     markModelMatrixDirty();
   }
   return this;
 }
 /**
  * Orients this {@link ATransformable3D} object to 'look at' the specified point. If this is part
  * of a scene graph, the graph will be notified of the change.
  *
  * @param lookAt {@link Vector3} The look at target. Must not be null.
  * @return A reference to this {@link ATransformable3D} to facilitate chaining.
  */
 public ATransformable3D setLookAt(Vector3 lookAt) {
   if (lookAt == null) {
     throw new IllegalArgumentException(
         "As of Rajawali v0.10, you cannot set a "
             + "null look target. If you want to remove the look target, use "
             + "clearLookAt(boolean) instead.");
   }
   mLookAt.setAll(lookAt);
   resetToLookAt();
   markModelMatrixDirty();
   return this;
 }
Esempio n. 16
0
  /**
   * create Hesse Normal Plane from 3 points
   *
   * @param p0 point 1
   * @param p1 point 2
   * @param p2 point 3
   * @return returns plane in following form {nx,ny,nz,d}
   */
  protected static Plane createHessePlane(Vector3 p0, Vector3 p1, Vector3 p2) {
    // Vector3s
    Vector3 a = p1.clone().subtract(p0);
    Vector3 b = p2.clone().subtract(p0);

    // cross product -> normal Vector3
    Vector3 normal = a.cross(b);
    normal.normalize();

    // distance to origin
    Vector3 scale = p0.clone().multiply(normal);
    double distance = scale.x + scale.y + scale.z;

    return new Plane(normal, distance);
  }
Esempio n. 17
0
    private Matrix4 createLightViewProjectionMatrix(DirectionalLight light) {
      //
      // -- Get the frustum corners in world space
      //
      mCamera.getFrustumCorners(mFrustumCorners, true);
      //
      // -- Get the frustum centroid
      //
      mFrustumCentroid.setAll(0, 0, 0);
      for (int i = 0; i < 8; i++) mFrustumCentroid.add(mFrustumCorners[i]);
      mFrustumCentroid.divide(8.0);

      //
      // --
      //

      BoundingBox lightBox = new BoundingBox(mFrustumCorners);
      double distance = mFrustumCentroid.distanceTo(lightBox.getMin());
      Vector3 lightDirection = light.getDirectionVector().clone();
      lightDirection.normalize();
      Vector3 lightPosition =
          Vector3.subtractAndCreate(
              mFrustumCentroid, Vector3.multiplyAndCreate(lightDirection, distance));

      //
      // --
      //

      mLightViewMatrix.setToLookAt(lightPosition, mFrustumCentroid, Vector3.Y);

      for (int i = 0; i < 8; i++) mFrustumCorners[i].multiply(mLightViewMatrix);

      BoundingBox b = new BoundingBox(mFrustumCorners);
      mLightProjectionMatrix.setToOrthographic(
          b.getMin().x, b.getMax().x, b.getMin().y, b.getMax().y, -b.getMax().z, -b.getMin().z);

      mLightModelViewProjectionMatrix.setAll(mLightProjectionMatrix);
      mLightModelViewProjectionMatrix.multiply(mLightViewMatrix);
      return mLightModelViewProjectionMatrix;
    }
Esempio n. 18
0
 public void setCameraOffset(Vector3 offset) {
   mCameraOffset.setAll(offset);
 }
 /**
  * Sets the x component of the position for this {@link ATransformable3D}. If this is part of a
  * scene graph, the graph will be notified of the change.
  *
  * @param x double The new x component for the position.
  */
 public void setX(double x) {
   mPosition.x = x;
   if (mLookAtEnabled && mLookAtValid) resetToLookAt();
   markModelMatrixDirty();
 }
 /**
  * Sets the y component of the position for this {@link ATransformable3D}. If this is part of a
  * scene graph, the graph will be notified of the change.
  *
  * @param y double The new y component for the position.
  */
 public void setY(double y) {
   mPosition.y = y;
   if (mLookAtEnabled && mLookAtValid) resetToLookAt();
   markModelMatrixDirty();
 }
 /**
  * Sets the z component of the position for this {@link ATransformable3D}. If this is part of a
  * scene graph, the graph will be notified of the change.
  *
  * @param z double The new z component for the position.
  */
 public void setZ(double z) {
   mPosition.z = z;
   if (mLookAtEnabled && mLookAtValid) resetToLookAt();
   markModelMatrixDirty();
 }
 /**
  * Sets the scale of this {@link ATransformable3D} object. If this is part of a scene graph, the
  * graph will be notified of the change.
  *
  * @param scale {@link Vector3} Containing the scaling factor in each axis.
  * @return A reference to this {@link ATransformable3D} to facilitate chaining.
  */
 public ATransformable3D setScale(Vector3 scale) {
   mScale.setAll(scale);
   markModelMatrixDirty();
   return this;
 }
Esempio n. 23
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);
 }
Esempio n. 24
0
 /**
  * Rotates the given {@link Vector3} by the rotation specified by this {@link Matrix4}.
  *
  * @param vec {@link Vector3} The vector to rotate.
  */
 public void rotateVector(@NonNull Vector3 vec) {
   double x = vec.x * m[M00] + vec.y * m[M01] + vec.z * m[M02];
   double y = vec.x * m[M10] + vec.y * m[M11] + vec.z * m[M12];
   double z = vec.x * m[M20] + vec.y * m[M21] + vec.z * m[M22];
   vec.setAll(x, y, z);
 }
Esempio n. 25
0
 public Point2D transferTo2D(Vector3 point) {
   point = point.clone();
   point.subtract(planeOrigin);
   point.rotateBy(planeZRotation);
   return new Point2D(point.x, point.y);
 }
Esempio n. 26
0
 @NonNull
 public Vector3 getTranslation(Vector3 vec) {
   return vec.setAll(m[M03], m[M13], m[M23]);
 }
 /**
  * Sets the position of this {@link ATransformable3D}. If this is part of a scene graph, the graph
  * will be notified of the change.
  *
  * @param x double The x coordinate new position.
  * @param y double The y coordinate new position.
  * @param z double The z coordinate new position.
  */
 public void setPosition(double x, double y, double z) {
   mPosition.setAll(x, y, z);
   if (mLookAtEnabled && mLookAtValid) resetToLookAt();
   markModelMatrixDirty();
 }
Esempio n. 28
0
 /**
  * Sets this {@link Matrix4} to a look at matrix with the given position, target and up {@link
  * Vector3}s.
  *
  * @param position {@link Vector3} The eye position.
  * @param target {@link Vector3} The target position.
  * @param up {@link Vector3} The up axis.
  * @return A reference to this {@link Matrix4} to facilitate chaining.
  */
 @NonNull
 public Matrix4 setToLookAt(
     @NonNull Vector3 position, @NonNull Vector3 target, @NonNull Vector3 up) {
   mVec1.subtractAndSet(target, position);
   return setToLookAt(mVec1, up);
 }
Esempio n. 29
0
 public Vector3 transferTo3D(Point2D point) {
   Vector3 newPoint = new Vector3(point.x(), point.y(), 0);
   newPoint.rotateBy(inversePlaneZRotation);
   newPoint.add(planeOrigin);
   return newPoint;
 }
 /**
  * Sets the position of this {@link ATransformable3D}. If this is part of a scene graph, the graph
  * will be notified of the change.
  *
  * @param position {@link Vector3} The new position. This is copied into an internal store and can
  *     be used after this method returns.
  */
 public void setPosition(Vector3 position) {
   mPosition.setAll(position);
   if (mLookAtEnabled && mLookAtValid) resetToLookAt();
   markModelMatrixDirty();
 }