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; }
static { // Setup bounding box. boundingBox = new BoundingBox(new Cube(1000, false).getGeometry()); boundingBox.setMin(new Vector3(-200, 0, -200)); boundingBox.setMax(new Vector3(200, 300, 200)); }