Skip to content

Commit

Permalink
implement Perlin noise
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Nov 5, 2024
1 parent e919520 commit 274e195
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/downloads/
/src/main/native/auto/
/src/main/native/Jolt/
/src/main/native/TestFramework/

# Ignore Gradle's project-specific cache directory:
/.gradle/
Expand Down
16 changes: 15 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ext {

// Regenerate all JNI header files and unpack Jolt before compiling any C++ code.
tasks.withType(CppCompile) {
dependsOn('classes', 'compileTestJava', 'unpackJoltSource')
dependsOn('classes', 'compileTestJava', 'unpackJoltSource', 'unpackTestFramework')
}

String javaHome = org.gradle.internal.jvm.Jvm.current().javaHome.absolutePath
Expand Down Expand Up @@ -97,6 +97,7 @@ model {

sources.cpp.source {
srcDir 'src/main/native/Jolt'
srcDir 'src/main/native/TestFramework/Math'
srcDir 'src/main/native/glue'
include '**/*.cpp'
}
Expand Down Expand Up @@ -150,6 +151,7 @@ model {
cppCompiler.args "/I$javaHome/include"
cppCompiler.args "/I$javaHome/include/win32"
cppCompiler.args "/I$projectDir/src/main/native"
cppCompiler.args "/I$projectDir/src/main/native/TestFramework" // for Math/Perlin.h
cppCompiler.args '/std:c++17'

if (isDebug) {
Expand All @@ -167,6 +169,7 @@ model {

cppCompiler.args '-I', "$javaHome/include"
cppCompiler.args '-I', "$projectDir/src/main/native"
cppCompiler.args '-I', "$projectDir/src/main/native/TestFramework" // for Math/Perlin.h
cppCompiler.args '-std=c++17'
cppCompiler.args '-Werror=return-type'

Expand Down Expand Up @@ -448,6 +451,17 @@ tasks.register('unpackJoltSource', Copy) {
}
into layout.projectDirectory.dir('src/main/native/Jolt')
}
tasks.register('unpackTestFramework', Copy) {
dependsOn 'downloadJoltSource'
from (zipTree(downloadZip)) {
include 'JoltPhysics-5.2.0/TestFramework/**'
eachFile { fcd ->
fcd.relativePath = new RelativePath(true, fcd.relativePath.segments.drop(2))
}
includeEmptyDirs = false
}
into layout.projectDirectory.dir('src/main/native/TestFramework')
}

// Register cleanup tasks:

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/Jolt.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ public static IntBuffer newDirectIntBuffer(int numInts) {
return result;
}

/**
* Generate 3-D Perlin noise.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @param xWrap the wraparound interval for the X coordinate (power of 2) or
* 0 for don't care
* @param yWrap the wraparound interval for the Y coordinate (power of 2) or
* 0 for don't care
* @param zWrap the wraparound interval for the Z coordinate (power of 2) or
* 0 for don't care
* @return a sample value
*/
native public static float perlinNoise3(
float x, float y, float z, int xWrap, int yWrap, int zWrap);

/**
* Dump profiler data.
*
Expand Down
1 change: 1 addition & 0 deletions src/main/native/TestFramework.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file was added to mask the Jolt Physics TestFramework.h, which is Windows-specific.
12 changes: 12 additions & 0 deletions src/main/native/glue/j/Jolt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SOFTWARE.
#include "Jolt/Geometry/RayAABox.h"
#include "Jolt/Physics/DeterminismLog.h"
#include "Jolt/RegisterTypes.h"
#include "TestFramework/Math/Perlin.h"

#include "auto/com_github_stephengold_joltjni_Jolt.h"
#include "glue/glue.h"
Expand Down Expand Up @@ -207,6 +208,17 @@ JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Jolt_isDoublePrec
#endif
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: perlinNoise3
* Signature: (FFFIII)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Jolt_perlinNoise3
(JNIEnv *, jclass, jfloat x, jfloat y, jfloat z, jint xWrap, jint yWrap, jint zWrap) {
const float result = PerlinNoise3(x, y, z, xWrap, yWrap, zWrap);
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: profileDump
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/testjoltjni/app/samples/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ of this software and associated documentation files (the "Software"), to deal
import com.github.stephengold.joltjni.enumerate.*;
import com.github.stephengold.joltjni.operator.Op;
import java.util.*;
import testjoltjni.app.testframework.*;

/**
* A line-for-line Java translation of the Jolt Physics abstract test class.
Expand Down Expand Up @@ -145,7 +144,7 @@ public Body CreateMeshTerrain()
float[][] heights = new float[n + 1][n + 1];
for (int x = 0; x <= n; ++x)
for (int z = 0; z <= n; ++z)
heights[x][z] = max_height * Perlin.Noise3((float)(x) * 8.0f / n, 0, (float)(z) * 8.0f / n, 256, 256, 256);
heights[x][z] = max_height * Jolt.perlinNoise3((float)(x) * 8.0f / n, 0, (float)(z) * 8.0f / n, 256, 256, 256);

// Create regular grid of triangles
List<Triangle> triangles = new ArrayList<>(2 * n * n);
Expand Down Expand Up @@ -186,7 +185,7 @@ public Body CreateHeightFieldTerrain()
float[] heights = new float[n * n];
for (int y = 0; y < n; ++y)
for (int x = 0; x < n; ++x)
heights[y * n + x] = max_height * Perlin.Noise3((float)(x) * 8.0f / n, 0, (float)(y) * 8.0f / n, 256, 256, 256);
heights[y * n + x] = max_height * Jolt.perlinNoise3((float)(x) * 8.0f / n, 0, (float)(y) * 8.0f / n, 256, 256, 256);

// Create height field
ShapeSettings height_field = new HeightFieldShapeSettings(heights, new Vec3(-0.5f * cell_size * n, 0.0f, -0.5f * cell_size * n), new Vec3(cell_size, 1.0f, cell_size), n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ of this software and associated documentation files (the "Software"), to deal
import java.nio.FloatBuffer;
import java.util.function.BiFunction;
import testjoltjni.app.samples.*;
import testjoltjni.app.testframework.Perlin;
/**
* A line-for-line Java translation of the Jolt Physics deformed heightfield-shape test.
* <p>
Expand All @@ -51,7 +50,7 @@ public void Initialize()
mHeightSamples=new float[cSampleCount * cSampleCount];
for (int y = 0; y < cSampleCount; ++y)
for (int x = 0; x < cSampleCount; ++x)
mHeightSamples[y * cSampleCount + x] = cMaxHeight * Perlin.Noise3((float)(x) * 8.0f / cSampleCount, 0, (float)(y) * 8.0f / cSampleCount, 256, 256, 256);
mHeightSamples[y * cSampleCount + x] = cMaxHeight * Jolt.perlinNoise3((float)(x) * 8.0f / cSampleCount, 0, (float)(y) * 8.0f / cSampleCount, 256, 256, 256);

// Determine scale and offset of the terrain
Vec3 offset=new Vec3(-0.5f * cCellSize * cSampleCount, 0, -0.5f * cCellSize * cSampleCount);
Expand Down
28 changes: 0 additions & 28 deletions src/test/java/testjoltjni/app/testframework/Perlin.java

This file was deleted.

0 comments on commit 274e195

Please sign in to comment.