From b786b486b6c05e7bcdef53f6086daae179ffd6e3 Mon Sep 17 00:00:00 2001 From: Ben Doherty Date: Wed, 22 Jul 2020 15:12:03 -0700 Subject: [PATCH] Add getCullingProjectionMatrix JNI method (#2847) --- .../filament-android/src/main/cpp/Camera.cpp | 10 ++++++ .../com/google/android/filament/Camera.java | 31 +++++++++++++++++-- filament/include/filament/Camera.h | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/android/filament-android/src/main/cpp/Camera.cpp b/android/filament-android/src/main/cpp/Camera.cpp index 2e54e0b11d..347eb00e71 100644 --- a/android/filament-android/src/main/cpp/Camera.cpp +++ b/android/filament-android/src/main/cpp/Camera.cpp @@ -103,6 +103,16 @@ Java_com_google_android_filament_Camera_nGetProjectionMatrix(JNIEnv *env, jclass env->ReleaseDoubleArrayElements(out_, out, 0); } +extern "C" JNIEXPORT void JNICALL +Java_com_google_android_filament_Camera_nGetCullingProjectionMatrix(JNIEnv *env, jclass, + jlong nativeCamera, jdoubleArray out_) { + Camera *camera = (Camera *) nativeCamera; + jdouble *out = env->GetDoubleArrayElements(out_, NULL); + const filament::math::mat4& m = camera->getCullingProjectionMatrix(); + std::copy_n(&m[0][0], 16, out); + env->ReleaseDoubleArrayElements(out_, out, 0); +} + extern "C" JNIEXPORT void JNICALL Java_com_google_android_filament_Camera_nGetScaling(JNIEnv *env, jclass, jlong nativeCamera, jdoubleArray out_) { diff --git a/android/filament-android/src/main/java/com/google/android/filament/Camera.java b/android/filament-android/src/main/java/com/google/android/filament/Camera.java index b1394e9a6d..d7c1723d6d 100644 --- a/android/filament-android/src/main/java/com/google/android/filament/Camera.java +++ b/android/filament-android/src/main/java/com/google/android/filament/Camera.java @@ -284,7 +284,15 @@ public class Camera { * ratio:
* * - * camera->setScaling(double4 {1.0, width / height, 1.0, 1.0}); + * double aspect = width / height; + * + * // with Fov.HORIZONTAL passed to setProjection: + * double[] s = {1.0, aspect, 1.0, 1.0}; + * camera.setScaling(s); + * + * // with Fov.VERTICAL passed to setProjection: + * double[] s = {1.0 / aspect, 1.0, 1.0, 1.0}; + * camera.setScaling(s); * * * By default, this is an identity matrix. @@ -354,7 +362,9 @@ public class Camera { } /** - * Retrieves the camera's projection matrix. + * Retrieves the camera's projection matrix. The projection matrix used for rendering always has + * its far plane set to infinity. This is why it may differ from the matrix set through + * setProjection() or setLensProjection(). * * @param out A 16-float array where the projection matrix will be stored, or null in which * case a new array is allocated. @@ -368,6 +378,22 @@ public class Camera { return out; } + /** + * Retrieves the camera's culling matrix. The culling matrix is the same as the projection + * matrix, except the far plane is finite. + * + * @param out A 16-float array where the projection matrix will be stored, or null in which + * case a new array is allocated. + * + * @return A 16-float array containing the camera's projection as a column-major matrix. + */ + @NonNull @Size(min = 16) + public double[] getCullingProjectionMatrix(@Nullable @Size(min = 16) double[] out) { + out = Asserts.assertMat4d(out); + nGetCullingProjectionMatrix(getNativeObject(), out); + return out; + } + /** * Returns the scaling amount used to scale the projection matrix. * @@ -566,6 +592,7 @@ public class Camera { private static native float nGetNear(long nativeCamera); private static native float nGetCullingFar(long nativeCamera); private static native void nGetProjectionMatrix(long nativeCamera, double[] out); + private static native void nGetCullingProjectionMatrix(long nativeCamera, double[] out); private static native void nGetScaling(long nativeCamera, double[] out); private static native void nGetModelMatrix(long nativeCamera, float[] out); private static native void nGetViewMatrix(long nativeCamera, float[] out); diff --git a/filament/include/filament/Camera.h b/filament/include/filament/Camera.h index d7bdeec616..60ecd042dc 100644 --- a/filament/include/filament/Camera.h +++ b/filament/include/filament/Camera.h @@ -232,7 +232,7 @@ public: /** Returns the projection matrix used for rendering. * * The projection matrix used for rendering always has its far plane set to infinity. This - * it why it may differ from the matrix set through setProjection() or setLensProjection(). + * is why it may differ from the matrix set through setProjection() or setLensProjection(). * * @return The projection matrix used for rendering *