예제 #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);
 }
    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;
    }
예제 #3
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);
 }
예제 #4
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);
 }
 /**
  * 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;
 }
 /**
  * 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;
 }
 /**
  * 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();
 }
 /**
  * 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;
 }
예제 #9
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;
    }
예제 #10
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);
 }
예제 #11
0
 @NonNull
 public Vector3 getTranslation(Vector3 vec) {
   return vec.setAll(m[M03], m[M13], m[M23]);
 }
예제 #12
0
 public void setCameraOffset(Vector3 offset) {
   mCameraOffset.setAll(offset);
 }
 /**
  * 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;
 }
 /**
  * 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();
 }
 /**
  * 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();
 }