Beispiel #1
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);
  }
    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;
    }