From 6e5b05d0b582ede2503b290732cd90f4c4ed2fcd Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Mon, 17 May 2021 14:28:01 +0200 Subject: [PATCH] KTXLoader.getSphericalHarmonics Co-authored-by: Fabian Rueckert --- .../filament-utils-android/src/main/cpp/Utils.cpp | 14 ++++++++++++++ .../com/google/android/filament/utils/KTXLoader.kt | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/android/filament-utils-android/src/main/cpp/Utils.cpp b/android/filament-utils-android/src/main/cpp/Utils.cpp index 77ed1b6520..8b443c9b8a 100644 --- a/android/filament-utils-android/src/main/cpp/Utils.cpp +++ b/android/filament-utils-android/src/main/cpp/Utils.cpp @@ -76,6 +76,19 @@ static jlong nCreateSkybox(JNIEnv* env, jclass, return (jlong) Skybox::Builder().environment(cubemap).showSun(true).build(*engine); } +static jboolean nGetSphericalHarmonics(JNIEnv* env, jclass, jobject javaBuffer, jint remaining, + jfloatArray outSphericalHarmonics_) { + AutoBuffer buffer(env, javaBuffer, remaining); + KtxBundle bundle((const uint8_t*) buffer.getData(), buffer.getSize()); + + jfloat* outSphericalHarmonics = env->GetFloatArrayElements(outSphericalHarmonics_, nullptr); + const auto success = bundle.getSphericalHarmonics( + reinterpret_cast(outSphericalHarmonics) + ); + env->ReleaseFloatArrayElements(outSphericalHarmonics_, outSphericalHarmonics, JNI_ABORT); + + return success ? JNI_TRUE : JNI_FALSE; +} JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) { JNIEnv* env; @@ -92,6 +105,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) { {"nCreateKTXTexture", "(JLjava/nio/Buffer;IZ)J", reinterpret_cast(nCreateKTXTexture)}, {"nCreateIndirectLight", "(JLjava/nio/Buffer;IZ)J", reinterpret_cast(nCreateIndirectLight)}, {"nCreateSkybox", "(JLjava/nio/Buffer;IZ)J", reinterpret_cast(nCreateSkybox)}, + {"nGetSphericalHarmonics", "(Ljava/nio/Buffer;I[F)Z", reinterpret_cast(nGetSphericalHarmonics)}, }; rc = env->RegisterNatives(ktxloaderClass, ktxMethods, sizeof(ktxMethods) / sizeof(JNINativeMethod)); if (rc != JNI_OK) return rc; diff --git a/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KTXLoader.kt b/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KTXLoader.kt index e71e7b17d3..ebb8e7a749 100644 --- a/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KTXLoader.kt +++ b/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KTXLoader.kt @@ -76,7 +76,20 @@ object KTXLoader { return Skybox(nativeSkybox) } + /** + * Retrieves spherical harmonics from the content of a KTX file. + * + * @param buffer The content of the KTX File. + * @return The resulting array of 9 * 3 floats, or null on failure. + */ + fun getSphericalHarmonics(buffer: Buffer): FloatArray? { + val sphericalHarmonics = FloatArray(9 * 3) + val success = nGetSphericalHarmonics(buffer, buffer.remaining(), sphericalHarmonics) + return if (success) sphericalHarmonics else null + } + private external fun nCreateKTXTexture(nativeEngine: Long, buffer: Buffer, remaining: Int, srgb: Boolean): Long private external fun nCreateIndirectLight(nativeEngine: Long, buffer: Buffer, remaining: Int, srgb: Boolean): Long + private external fun nGetSphericalHarmonics(buffer: Buffer, remaining: Int, outSphericalHarmonics: FloatArray): Boolean private external fun nCreateSkybox(nativeEngine: Long, buffer: Buffer, remaining: Int, srgb: Boolean): Long } \ No newline at end of file