From 69a4f4d657427fef61551775852b1bed9da883eb Mon Sep 17 00:00:00 2001 From: stephengold Date: Sun, 8 Sep 2024 17:36:54 -0700 Subject: [PATCH] add the RayCastSettings class --- .../stephengold/joltjni/RayCastSettings.java | 141 ++++++++++++++++++ src/main/native/glue/r/RayCastSettings.cpp | 133 +++++++++++++++++ 2 files changed, 274 insertions(+) create mode 100644 src/main/java/com/github/stephengold/joltjni/RayCastSettings.java create mode 100644 src/main/native/glue/r/RayCastSettings.cpp diff --git a/src/main/java/com/github/stephengold/joltjni/RayCastSettings.java b/src/main/java/com/github/stephengold/joltjni/RayCastSettings.java new file mode 100644 index 00000000..fe21d893 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/RayCastSettings.java @@ -0,0 +1,141 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +import com.github.stephengold.joltjni.enumerate.EBackFaceMode; + +/** + * Raycast configuration options. + * + * @author Stephen Gold sgold@sonic.net + */ +public class RayCastSettings extends JoltPhysicsObject { + // ************************************************************************* + // constructors + + /** + * Instantiate default settings. + */ + public RayCastSettings() { + long settingsVa = createDefaultSettings(); + setVirtualAddress(settingsVa, () -> free(settingsVa)); + } + // ************************************************************************* + // new methods exposed + + /** + * Return the policy for back-facing triangles in convex shapes. The + * settings are unaffected. (native field: mBackFaceModeConvex) + * + * @return the enum value (not null) + */ + public EBackFaceMode getBackFaceModeConvex() { + long settingsVa = va(); + int ordinal = getBackFaceModeConvex(settingsVa); + EBackFaceMode result = EBackFaceMode.values()[ordinal]; + + return result; + } + + /** + * Return the policy for back-facing triangles in non-convex shapes. The + * settings are unaffected. (native field: mBackFaceModeTriangles) + * + * @return the enum value (not null) + */ + public EBackFaceMode getBackFaceModeTriangles() { + long settingsVa = va(); + int ordinal = getBackFaceModeTriangles(settingsVa); + EBackFaceMode result = EBackFaceMode.values()[ordinal]; + + return result; + } + + /** + * Test whether convex shapes should be treated as solid. The settings are + * unaffected. (native field: mTreatConvexAsSolid) + * + * @return true if treated as solid, otherwise false + */ + public boolean getTreatConvexAsSolid() { + long settingsVa = va(); + boolean result = getTreatConvexAsSolid(settingsVa); + + return result; + } + + /** + * Alter the policy for back-facing triangles in convex shapes. (native + * field: mBackFaceModeConvex) + * + * @param setting the enum value (not null, default=IgnoreBackFaces) + */ + public void setBackFaceModeConvex(EBackFaceMode setting) { + long settingsVa = va(); + int ordinal = setting.ordinal(); + setBackFaceModeConvex(settingsVa, ordinal); + } + + /** + * Alter the policy for back-facing triangles in non-convex shapes. (native + * field: mBackFaceModeTriangles) + * + * @param setting the enum value (not null, default=IgnoreBackFaces) + */ + public void setBackFaceModeTriangles(EBackFaceMode setting) { + long settingsVa = va(); + int ordinal = setting.ordinal(); + setBackFaceModeTriangles(settingsVa, ordinal); + } + + /** + * Alter whether convex shapes should be treated as solid. (native field: + * mTreatConvexAsSolid) + * + * @param setting true→solid, false→hollow (default=true) + */ + public void setTreatConvexAsSolid(boolean setting) { + long settingsVa = va(); + setTreatConvexAsSolid(settingsVa, setting); + } + // ************************************************************************* + // native private methods + + native private static long createDefaultSettings(); + + native private static void free(long settingsVa); + + native private static int getBackFaceModeConvex(long settingsVa); + + native private static int getBackFaceModeTriangles(long settingsVa); + + native private static boolean getTreatConvexAsSolid(long settingsVa); + + native private static void setBackFaceModeConvex( + long settingsVa, int ordinal); + + native private static void setBackFaceModeTriangles( + long settingsVa, int ordinal); + + native private static void setTreatConvexAsSolid( + long settingsVa, boolean setting); +} diff --git a/src/main/native/glue/r/RayCastSettings.cpp b/src/main/native/glue/r/RayCastSettings.cpp new file mode 100644 index 00000000..ee709ee9 --- /dev/null +++ b/src/main/native/glue/r/RayCastSettings.cpp @@ -0,0 +1,133 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ + +/* + * Author: Stephen Gold + */ +#include +#include +#include "auto/com_github_stephengold_joltjni_RayCastSettings.h" +#include "glue/glue.h" + +using namespace JPH; + +/* + * Class: com_github_stephengold_joltjni_RayCastSettings + * Method: createDefaultSettings + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RayCastSettings_createDefaultSettings + (JNIEnv *, jclass) { + RayCastSettings * const pResult = new RayCastSettings(); + TRACE_NEW("RayCastSettings", pResult) + return reinterpret_cast (pResult); +} + +/* + * Class: com_github_stephengold_joltjni_RayCastSettings + * Method: free + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RayCastSettings_free + (JNIEnv *, jclass, jlong settingsVa) { + RayCastSettings * const pSettings + = reinterpret_cast (settingsVa); + TRACE_DELETE("RayCastSettings", pSettings); + delete pSettings; +} + +/* + * Class: com_github_stephengold_joltjni_RayCastSettings + * Method: getBackFaceModeConvex + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_RayCastSettings_getBackFaceModeConvex + (JNIEnv *, jclass, jlong settingsVa) { + const RayCastSettings * const pSettings + = reinterpret_cast (settingsVa); + EBackFaceMode result = pSettings->mBackFaceModeConvex; + return (jint) result; +} + +/* + * Class: com_github_stephengold_joltjni_RayCastSettings + * Method: getBackFaceModeTriangles + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_RayCastSettings_getBackFaceModeTriangles + (JNIEnv *, jclass, jlong settingsVa) { + const RayCastSettings * const pSettings + = reinterpret_cast (settingsVa); + EBackFaceMode result = pSettings->mBackFaceModeTriangles; + return (jint) result; +} + +/* + * Class: com_github_stephengold_joltjni_RayCastSettings + * Method: getTreatConvexAsSolid + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_RayCastSettings_getTreatConvexAsSolid + (JNIEnv *, jclass, jlong settingsVa) { + const RayCastSettings * const pSettings + = reinterpret_cast (settingsVa); + bool result = pSettings->mTreatConvexAsSolid; + return result; +} + +/* + * Class: com_github_stephengold_joltjni_RayCastSettings + * Method: setBackFaceModeConvex + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RayCastSettings_setBackFaceModeConvex + (JNIEnv *, jclass, jlong settingsVa, jint ordinal) { + RayCastSettings * const pSettings + = reinterpret_cast (settingsVa); + EBackFaceMode mode = (EBackFaceMode) ordinal; + pSettings->mBackFaceModeConvex = mode; +} + +/* + * Class: com_github_stephengold_joltjni_RayCastSettings + * Method: setBackFaceModeTriangles + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RayCastSettings_setBackFaceModeTriangles + (JNIEnv *, jclass, jlong settingsVa, jint ordinal) { + RayCastSettings * const pSettings + = reinterpret_cast (settingsVa); + EBackFaceMode mode = (EBackFaceMode) ordinal; + pSettings->mBackFaceModeTriangles = mode; +} + +/* + * Class: com_github_stephengold_joltjni_RayCastSettings + * Method: setTreatConvexAsSolid + * Signature: (JZ)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RayCastSettings_setTreatConvexAsSolid + (JNIEnv *, jclass, jlong settingsVa, jboolean setting) { + RayCastSettings * const pSettings + = reinterpret_cast (settingsVa); + pSettings->mTreatConvexAsSolid = setting; +} \ No newline at end of file