From ad95b64b882395a653321b21718af08ea155e8a7 Mon Sep 17 00:00:00 2001 From: unknown <> Date: Sun, 23 Jul 2023 12:20:14 -0400 Subject: [PATCH] Multiarrays --- src/main/c/chunkmask.c | 102 ++ src/main/c/fluidsim.c | 100 +- src/main/c/includes/chunkmask.h | 48 + src/main/c/includes/electrosphere_FluidSim.h | 20 +- src/main/c/includes/utilities.h | 15 + src/main/c/linearsolver.c | 59 + src/main/java/electrosphere/FluidSim.java | 246 +++- src/main/java/electrosphere/Main.java | 99 +- .../electrosphere/render/GLFWContext.java | 10 +- src/main/java/electrosphere/render/Mesh.java | 1275 ++--------------- 10 files changed, 716 insertions(+), 1258 deletions(-) create mode 100644 src/main/c/chunkmask.c create mode 100644 src/main/c/includes/chunkmask.h create mode 100644 src/main/c/includes/utilities.h create mode 100644 src/main/c/linearsolver.c diff --git a/src/main/c/chunkmask.c b/src/main/c/chunkmask.c new file mode 100644 index 0000000..a83e330 --- /dev/null +++ b/src/main/c/chunkmask.c @@ -0,0 +1,102 @@ +#include +#include +#include "includes/utilities.h" +#include "includes/chunkmask.h" + +uint32_t matrix_transform(JNIEnv * env, jobjectArray jrx); + +/** + * Calculates the bitmask for available chunks for the provided chunk's neighbor array +*/ +JNIEXPORT jint JNICALL Java_electrosphere_FluidSim_calculateChunkMask(JNIEnv * env, jobject this, jobjectArray jrx){ + return matrix_transform(env,jrx); +} + +/** + * Calculates a mask that represents all nearby chunks that are actually accessible and exist +*/ +uint32_t matrix_transform(JNIEnv * env, jobjectArray jrx){ + + //The returned value, an availability mask that contains the availability of each neighbor chunk + uint32_t rVal = 0; + + //Add to maks for initial chunks + for(int i = 0; i < CENTER_LOC; i++){ + if((*env)->GetObjectArrayElement(env,jrx,i)!=NULL){ + rVal = rVal + 1; + } + rVal = rVal << 1; + } + //add 1 for center chunk because we already have that + rVal = rVal + 1; + rVal = rVal << 1; + //continue on for remaining chunks + for(int i = CENTER_LOC+1; i < 27; i++){ + if((*env)->GetObjectArrayElement(env,jrx,i)!=NULL){ + rVal = rVal + 1; + } + if(i < 26){ + rVal = rVal << 1; + } + } + + return rVal; +} + +const uint32_t CHUNK_INDEX_ARR[] = { + CHUNK_000, CHUNK_100, CHUNK_200, + CHUNK_010, CHUNK_110, CHUNK_210, + CHUNK_020, CHUNK_120, CHUNK_220, + + CHUNK_001, CHUNK_101, CHUNK_201, + CHUNK_011, CHUNK_111, CHUNK_211, + CHUNK_021, CHUNK_121, CHUNK_221, + + CHUNK_002, CHUNK_102, CHUNK_202, + CHUNK_012, CHUNK_112, CHUNK_212, + CHUNK_022, CHUNK_122, CHUNK_222, +}; + + +//control offsetting the advect sampler location if a valid neighbor chunk is hit +const char CHUNK_NORMALIZE_U[] = { + -1, 0, 1, + -1, 0, 1, + -1, 0, 1, + + -1, 0, 1, + -1, 0, 1, + -1, 0, 1, + + -1, 0, 1, + -1, 0, 1, + -1, 0, 1, +}; + +const char CHUNK_NORMALIZE_V[] = { + -1, -1, -1, + 0, 0, 0, + 1, 1, 1, + + -1, -1, -1, + 0, 0, 0, + 1, 1, 1, + + -1, -1, -1, + 0, 0, 0, + 1, 1, 1, +}; + +const char CHUNK_NORMALIZE_W[] = { + -1, -1, -1, + -1, -1, -1, + -1, -1, -1, + + 0, 0, 0, + 0, 0, 0, + 0, 0, 0, + + 1, 1, 1, + 1, 1, 1, + 1, 1, 1, +}; \ No newline at end of file diff --git a/src/main/c/fluidsim.c b/src/main/c/fluidsim.c index 44eacf7..bda4fac 100644 --- a/src/main/c/fluidsim.c +++ b/src/main/c/fluidsim.c @@ -1,22 +1,24 @@ #include #include #include +#include + +#include "includes/utilities.h" +#include "includes/chunkmask.h" -#define SWAP(x0,x) {float *tmp=x0;x0=x;x=tmp;} -#define IX(i,j,k) ((i)+(N)*(j)+(N*N)*(k)) #define LINEARSOLVERTIMES 10 -void diffuse(int N, int b, float * x, float * x0, float diff, float dt); -void advect(int N, int b, float * d, float * d0, float * u, float * v, float * w, float dt); -void project(int N, float * u, float * v, float * w, float * p, float * div); -void set_bnd(int N, int b, float * x); -void dens_step(int N, float * x, float * x0, float * u, float * v, float * w, float diff, float dt); -void vel_step(int N, float * u, float * v, float * w, float * u0, float * v0, float * w0, float visc, float dt); -void lin_solve(int N, int b, float* x, float* x0, float a, float c); +void diffuse(JNIEnv * env, uint32_t chunk_mask, int N, int b, float * x, float * x0, float diff, float dt); +void advect(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, float * d0, float * u, float * v, float * w, float dt); +void project(JNIEnv * env, uint32_t chunk_mask, int N, jobjectArray jru, jobjectArray jrv, jobjectArray jrw, float * p, float * div); +void set_bnd(JNIEnv * env, uint32_t chunk_mask, int N, int b, float * x); +void dens_step(JNIEnv * env, uint32_t chunk_mask, int N, jobjectArray jrx, float * x0, jobjectArray jru, jobjectArray jrv, jobjectArray jrw, float diff, float dt); +void vel_step(JNIEnv * env, uint32_t chunk_mask, int N, jobjectArray jru, jobjectArray jrv, jobjectArray jrw, float * u0, float * v0, float * w0, float visc, float dt); +void lin_solve(JNIEnv * env, uint32_t chunk_mask, int N, int b, float* x, float* x0, float a, float c); /** * The core simulation function @@ -25,8 +27,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate( JNIEnv * env, jobject this, jint DIM_X, - jint DIM_Y, - jint DIM_Z, + jint chunk_mask, jobject jx, jobject jx0, jobject ju, @@ -39,18 +40,18 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate( jfloat VISCOSITY_RATE, jfloat timestep){ jboolean isCopy; - float * x = (*env)->GetDirectBufferAddress(env,jx); + // float * x = (*env)->GetDirectBufferAddress(env,jx); float * x0 = (*env)->GetDirectBufferAddress(env,jx0); - float * u = (*env)->GetDirectBufferAddress(env,ju); - float * v = (*env)->GetDirectBufferAddress(env,jv); - float * w = (*env)->GetDirectBufferAddress(env,jw); + // float * u = (*env)->GetDirectBufferAddress(env,ju); + // float * v = (*env)->GetDirectBufferAddress(env,jv); + // float * w = (*env)->GetDirectBufferAddress(env,jw); float * u0 = (*env)->GetDirectBufferAddress(env,ju0); float * v0 = (*env)->GetDirectBufferAddress(env,jv0); float * w0 = (*env)->GetDirectBufferAddress(env,jw0); int N = DIM_X; int i,j,k; - vel_step(DIM_X, u, v, w, u0, v0, w0, VISCOSITY_RATE, timestep); - dens_step(DIM_X, x, x0, u, v, w, DIFFUSION_RATE, timestep); + vel_step(env, chunk_mask, DIM_X, ju, jv, jw, u0, v0, w0, VISCOSITY_RATE, timestep); + dens_step(env, chunk_mask, DIM_X, jx, x0, ju, jv, jw, DIFFUSION_RATE, timestep); } /** @@ -67,20 +68,22 @@ void add_source(int N, float * x, float * s, float dt){ /** * Diffuses a given array by a diffusion constant */ -void diffuse(int N, int b, float * x, float * x0, float diff, float dt){ +void diffuse(JNIEnv * env, uint32_t chunk_mask, int N, int b, float * x, float * x0, float diff, float dt){ float a=dt*diff*N*N*N; - lin_solve(N, b, x, x0, a, 1+6*a); + lin_solve(env, chunk_mask, N, b, x, x0, a, 1+6*a); } /** * Advects a given array based on the force vectors in the simulation */ -void advect(int N, int b, float * d, float * d0, float * u, float * v, float * w, float dt){ +void advect(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, float * d0, float * u, float * v, float * w, float dt){ int i, j, k, i0, j0, k0, i1, j1, k1; float x, y, z, s0, t0, s1, t1, u1, u0, dtx,dty,dtz; dtx=dty=dtz=dt*N; + float * d = GET_ARR(env,jrd,CENTER_LOC); + for(k=1; k + +#ifndef CHUNKMASK_H +#define CHUNKMASK_H + +#define CENTER_LOC 13 + +#define CHUNK_222 1 +#define CHUNK_122 2 +#define CHUNK_022 4 +#define CHUNK_212 8 +#define CHUNK_112 16 +#define CHUNK_012 32 +#define CHUNK_202 64 +#define CHUNK_102 128 +#define CHUNK_002 256 + +#define CHUNK_221 512 +#define CHUNK_121 1024 +#define CHUNK_021 2048 +#define CHUNK_211 4096 +#define CHUNK_111 8192 +#define CHUNK_011 16384 +#define CHUNK_201 32768 +#define CHUNK_101 65536 +#define CHUNK_001 131072 + +#define CHUNK_220 262144 +#define CHUNK_120 524288 +#define CHUNK_020 1048576 +#define CHUNK_210 2097152 +#define CHUNK_110 4194304 +#define CHUNK_010 8388608 +#define CHUNK_200 16777216 +#define CHUNK_100 33554432 +#define CHUNK_000 67108864 + +extern const uint32_t CHUNK_INDEX_ARR[]; + + +//control offsetting the advect sampler location if a valid neighbor chunk is hit +extern const char CHUNK_NORMALIZE_U[]; + +extern const char CHUNK_NORMALIZE_V[]; + +extern const char CHUNK_NORMALIZE_W[]; + +#endif \ No newline at end of file diff --git a/src/main/c/includes/electrosphere_FluidSim.h b/src/main/c/includes/electrosphere_FluidSim.h index dc017ba..c56a218 100644 --- a/src/main/c/includes/electrosphere_FluidSim.h +++ b/src/main/c/includes/electrosphere_FluidSim.h @@ -20,10 +20,26 @@ extern "C" { /* * Class: electrosphere_FluidSim * Method: simulate - * Signature: (IIILjava/nio/ByteBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;FFF)V + * Signature: (II[Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;FFF)V */ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate - (JNIEnv *, jobject, jint, jint, jint, jobject, jobject, jobject, jobject, jobject, jobject, jobject, jobject, jfloat, jfloat, jfloat); + (JNIEnv *, jobject, jint, jint, jobjectArray, jobject, jobjectArray, jobjectArray, jobjectArray, jobject, jobject, jobject, jfloat, jfloat, jfloat); + +/* + * Class: electrosphere_FluidSim + * Method: linSolve + * Signature: (IIILjava/nio/ByteBuffer;Ljava/nio/ByteBuffer;FF)V + */ +JNIEXPORT void JNICALL Java_electrosphere_FluidSim_linSolve + (JNIEnv *, jobject, jint, jint, jint, jobject, jobject, jfloat, jfloat); + +/* + * Class: electrosphere_FluidSim + * Method: calculateChunkMask + * Signature: ([Ljava/nio/ByteBuffer;)I + */ +JNIEXPORT jint JNICALL Java_electrosphere_FluidSim_calculateChunkMask + (JNIEnv *, jobject, jobjectArray); #ifdef __cplusplus } diff --git a/src/main/c/includes/utilities.h b/src/main/c/includes/utilities.h new file mode 100644 index 0000000..d79bf75 --- /dev/null +++ b/src/main/c/includes/utilities.h @@ -0,0 +1,15 @@ +#include +#include + +#ifndef UTILITIES_H +#define UTILITIES_H + +#define SWAP(x0,x) {float *tmp=x0;x0=x;x=tmp;} +#define IX(i,j,k) ((i)+(N)*(j)+(N*N)*(k)) +#define CK(m,n,o) ((m)+(n)*(3)+(o)*(3)*(3)) +#define GET_ARR(env,src,i) (*env)->GetDirectBufferAddress(env,(*env)->GetObjectArrayElement(env,src,i)) +#define ARR_EXISTS(chunk_mask,m,n,o) (chunk_mask & CHUNK_INDEX_ARR[CK(m,n,o)]) > 0 + +void lin_solve(JNIEnv * env, uint32_t chunk_mask, int N, int b, float* x, float* x0, float a, float c); + +#endif \ No newline at end of file diff --git a/src/main/c/linearsolver.c b/src/main/c/linearsolver.c new file mode 100644 index 0000000..769bb27 --- /dev/null +++ b/src/main/c/linearsolver.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include "includes/utilities.h" + + + +JNIEXPORT void JNICALL Java_electrosphere_FluidSim_linSolve( + JNIEnv * env, + jobject this, + jint chunk_mask_raw, + jint DIM_X, + jint br, + jobject jrx, + jobject jrx0, + jfloat ar, + jfloat cr + ){ + //adapt object types + float * x = (*env)->GetDirectBufferAddress(env,jrx); + float * x0 = (*env)->GetDirectBufferAddress(env,jrx0); + + //solve + lin_solve(env,chunk_mask_raw,DIM_X,br,x,x0,ar,cr); +} + +/** + * Solves a linear system of equations in a vectorized manner +*/ +void lin_solve(JNIEnv * env, uint32_t chunk_mask, int N, int b, float* x, float* x0, float a, float c){ + int i, j, k, l, m; + __m256 aScalar = _mm256_set1_ps(a); + __m256 cScalar = _mm256_set1_ps(c); + // update for each cell + for(k=1; kN-1){ + for(i=i-8; i < N-1; i++){ + x[IX(i,j,k)] = (x0[IX(i,j,k)] + a*(x[IX(i-1,j,k)]+x[IX(i+1,j,k)]+x[IX(i,j-1,k)]+x[IX(i,j+1,k)]+x[IX(i,j,k-1)]+x[IX(i,j,k+1)]))/c; + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/electrosphere/FluidSim.java b/src/main/java/electrosphere/FluidSim.java index c5a1b1c..0059e62 100644 --- a/src/main/java/electrosphere/FluidSim.java +++ b/src/main/java/electrosphere/FluidSim.java @@ -12,6 +12,7 @@ import java.util.Random; import java.util.Set; import org.joml.Vector2i; +import org.joml.Vector3i; import org.lwjgl.BufferUtils; import org.lwjgl.PointerBuffer; import org.lwjgl.system.MemoryUtil; @@ -28,14 +29,41 @@ public class FluidSim { public static final int DIM = 18; - ByteBuffer x = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); - ByteBuffer x0 = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); - ByteBuffer u = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); - ByteBuffer v = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); - ByteBuffer w = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); - ByteBuffer u0 = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); - ByteBuffer v0 = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); - ByteBuffer w0 = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + // + // +-------------+ ^ Y + // / | / | | _ + // / | / | | /\ Z + //(0,2,0) +-----+-------+ | | / + // | +-------+-----+ | / + // | / | / | / + // | / | / |/ + // +-------------+ (2,0,0) +---------------> X + + //Buffers that contain density for current frame + ByteBuffer[] density = new ByteBuffer[27]; + //Buffers that contain new density to add to the simulation + ByteBuffer densityAddition; + //Buffers that contain u vector directions + ByteBuffer[] uVector = new ByteBuffer[27]; + //Buffers that contain v vector directions + ByteBuffer[] vVector = new ByteBuffer[27]; + //Buffers that contain w vector directions + ByteBuffer[] wVector = new ByteBuffer[27]; + //Buffers that contain u vector directions to add to the simulation + ByteBuffer uAdditionVector; + //Buffers that contain v vector directions to add to the simulation + ByteBuffer vAdditionVector; + //Buffers that contain w vector directions to add to the simulation + ByteBuffer wAdditionVector; + + // ByteBuffer x = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + // ByteBuffer x0 = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + // ByteBuffer u = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + // ByteBuffer v = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + // ByteBuffer w = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + // ByteBuffer u0 = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + // ByteBuffer v0 = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + // ByteBuffer w0 = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); //The densities for every voxel for the current frame float[] densityArrayView = new float[DIM * DIM * DIM]; @@ -58,19 +86,31 @@ public class FluidSim { static final float GRAVITY = -1000f; - public void setup(){ - x.order(ByteOrder.LITTLE_ENDIAN); - x0.order(ByteOrder.LITTLE_ENDIAN); - u.order(ByteOrder.LITTLE_ENDIAN); - v.order(ByteOrder.LITTLE_ENDIAN); - w.order(ByteOrder.LITTLE_ENDIAN); - u0.order(ByteOrder.LITTLE_ENDIAN); - v0.order(ByteOrder.LITTLE_ENDIAN); - w0.order(ByteOrder.LITTLE_ENDIAN); - FloatBuffer xf = x.asFloatBuffer(); - FloatBuffer uf = u.asFloatBuffer(); - FloatBuffer vf = v.asFloatBuffer(); - FloatBuffer wf = w.asFloatBuffer(); + public void setup(Vector3i offset){ + //allocate buffers for this chunk + density[13] = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + densityAddition = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + uVector[13] = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + vVector[13] = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + wVector[13] = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + uAdditionVector = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + vAdditionVector = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + wAdditionVector = ByteBuffer.allocateDirect(DIM * DIM * DIM * 4); + //order endian-ness + density[13].order(ByteOrder.LITTLE_ENDIAN); + densityAddition.order(ByteOrder.LITTLE_ENDIAN); + uVector[13].order(ByteOrder.LITTLE_ENDIAN); + vVector[13].order(ByteOrder.LITTLE_ENDIAN); + wVector[13].order(ByteOrder.LITTLE_ENDIAN); + uAdditionVector.order(ByteOrder.LITTLE_ENDIAN); + vAdditionVector.order(ByteOrder.LITTLE_ENDIAN); + wAdditionVector.order(ByteOrder.LITTLE_ENDIAN); + + //set initial values for chunk + FloatBuffer xf = density[13].asFloatBuffer(); + FloatBuffer uf = uVector[13].asFloatBuffer(); + FloatBuffer vf = vVector[13].asFloatBuffer(); + FloatBuffer wf = wVector[13].asFloatBuffer(); Random rand = new Random(1); //make a cube of water in the center @@ -78,12 +118,12 @@ public class FluidSim { for(int j = 0; j < DIM; j++){ for(int k = 0; k < DIM; k++){ if( - Math.abs(16 - i) < 4 && + Math.abs(8 - i) < 4 && Math.abs(j) < 4 && - Math.abs(10 - k) < 4 + Math.abs(16 - k) < 4 ){ xf.put(1); - uf.put(rand.nextFloat() * 0.1f); + uf.put(18); vf.put(-1f); wf.put(rand.nextFloat() * 0.1f); } else { @@ -112,31 +152,45 @@ public class FluidSim { // writeNewStateIntoBuffers(); - if(x.position() > 0){ - x.position(0); - } - if(x0.position() > 0){ - x0.position(0); - } - if(u.position() > 0){ - u.position(0); - } - if(v.position() > 0){ - v.position(0); - } - if(w.position() > 0){ - w.position(0); - } - if(u0.position() > 0){ - u0.position(0); - } - if(v0.position() > 0){ - v0.position(0); - } - if(w0.position() > 0){ - w0.position(0); - } - simulate(DIM, DIM, DIM, x, x0, u, v, w, u0, v0, w0, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep); + // if(x.position() > 0){ + // x.position(0); + // } + // if(x0.position() > 0){ + // x0.position(0); + // } + // if(u.position() > 0){ + // u.position(0); + // } + // if(v.position() > 0){ + // v.position(0); + // } + // if(w.position() > 0){ + // w.position(0); + // } + // if(u0.position() > 0){ + // u0.position(0); + // } + // if(v0.position() > 0){ + // v0.position(0); + // } + // if(w0.position() > 0){ + // w0.position(0); + // } + simulate( + DIM, + 0, + density, + densityAddition, + uVector, + vVector, + wVector, + uAdditionVector, + vAdditionVector, + wAdditionVector, + DIFFUSION_CONSTANT, + VISCOSITY_CONSTANT, + timestep + ); // //Reads out the results of the fluid sim @@ -153,25 +207,29 @@ public class FluidSim { // //Read data back into arrays // - if(x.position() > 0){ - x.position(0); + if(density[13].position() > 0){ + density[13].position(0); } - if(u.position() > 0){ - u.position(0); + if(uVector[13].position() > 0){ + uVector[13].position(0); } - if(v.position() > 0){ - v.position(0); + if(vVector[13].position() > 0){ + vVector[13].position(0); } - if(w.position() > 0){ - w.position(0); + if(wVector[13].position() > 0){ + wVector[13].position(0); } + FloatBuffer xFloatView = density[13].asFloatBuffer(); + FloatBuffer uFloatView = uVector[13].asFloatBuffer(); + FloatBuffer vFloatView = vVector[13].asFloatBuffer(); + FloatBuffer wFloatView = wVector[13].asFloatBuffer(); for(int i = 0; i < DIM; i++){ for(int j = 0; j < DIM; j++){ for(int k = 0; k < DIM; k++){ - densityArrayView[IX(i,j,k)] = x.getFloat(); - uArrayView[IX(i,j,k)] = u.getFloat(); - vArrayView[IX(i,j,k)] = v.getFloat(); - wArrayView[IX(i,j,k)] = w.getFloat(); + densityArrayView[IX(i,j,k)] = xFloatView.get(); + uArrayView[IX(i,j,k)] = uFloatView.get(); + vArrayView[IX(i,j,k)] = vFloatView.get(); + wArrayView[IX(i,j,k)] = wFloatView.get(); } } } @@ -181,22 +239,22 @@ public class FluidSim { * Writes data from the java-side arrays into buffers that get passed into c-side */ private void writeNewStateIntoBuffers(){ - if(x0.position() > 0){ - x0.position(0); + if(densityAddition.position() > 0){ + densityAddition.position(0); } - if(u0.position() > 0){ - u0.position(0); + if(uAdditionVector.position() > 0){ + uAdditionVector.position(0); } - if(v0.position() > 0){ - v0.position(0); + if(vAdditionVector.position() > 0){ + vAdditionVector.position(0); } - if(w0.position() > 0){ - w0.position(0); + if(wAdditionVector.position() > 0){ + wAdditionVector.position(0); } - FloatBuffer x0FloatView = x0.asFloatBuffer(); - FloatBuffer u0FloatView = u0.asFloatBuffer(); - FloatBuffer v0FloatView = v0.asFloatBuffer(); - FloatBuffer w0FloatView = w0.asFloatBuffer(); + FloatBuffer x0FloatView = densityAddition.asFloatBuffer(); + FloatBuffer u0FloatView = uAdditionVector.asFloatBuffer(); + FloatBuffer v0FloatView = vAdditionVector.asFloatBuffer(); + FloatBuffer w0FloatView = wAdditionVector.asFloatBuffer(); for(int i = 0; i < DIM; i++){ for(int j = 0; j < DIM; j++){ for(int k = 0; k < DIM; k++){ @@ -241,13 +299,12 @@ public class FluidSim { */ private native void simulate( int DIM_X, - int DIM_Y, - int DIM_Z, - ByteBuffer x, + int chunkMask, + ByteBuffer[] x, ByteBuffer x0, - ByteBuffer u, - ByteBuffer v, - ByteBuffer w, + ByteBuffer[] u, + ByteBuffer[] v, + ByteBuffer[] w, ByteBuffer u0, ByteBuffer v0, ByteBuffer w0, @@ -256,6 +313,10 @@ public class FluidSim { float timestep ); + private native void linSolve(int chunk_mask, int N, int b, ByteBuffer x, ByteBuffer x0, float a, float c); + + private native int calculateChunkMask(ByteBuffer[] densityBuffers); + public float[] getData(){ return densityArrayView; } @@ -264,4 +325,31 @@ public class FluidSim { return ((x)+(DIM)*(y) + (DIM)*(DIM)*(z)); } + public static final int getNeighborIndex(int x, int y, int z){ + return x + y * 3 + z * 3 * 3; + } + + public ByteBuffer getDensityBuffer(){ + return density[getNeighborIndex(1,1,1)]; + } + + public ByteBuffer getUBuffer(){ + return uVector[getNeighborIndex(1,1,1)]; + } + + public ByteBuffer getVBuffer(){ + return vVector[getNeighborIndex(1,1,1)]; + } + + public ByteBuffer getWBuffer(){ + return wVector[getNeighborIndex(1,1,1)]; + } + + public void setNeighbor(int x, int y, int z, FluidSim neighbor){ + density[getNeighborIndex(x,y,z)] = neighbor.getDensityBuffer(); + uVector[getNeighborIndex(x,y,z)] = neighbor.getUBuffer(); + vVector[getNeighborIndex(x,y,z)] = neighbor.getVBuffer(); + wVector[getNeighborIndex(x,y,z)] = neighbor.getWBuffer(); + } + } diff --git a/src/main/java/electrosphere/Main.java b/src/main/java/electrosphere/Main.java index 150ab15..9095f35 100644 --- a/src/main/java/electrosphere/Main.java +++ b/src/main/java/electrosphere/Main.java @@ -4,8 +4,11 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.Scanner; import java.util.concurrent.TimeUnit; +import org.joml.Vector3i; + import electrosphere.render.GLFWContext; import electrosphere.render.Mesh; @@ -14,30 +17,114 @@ import electrosphere.render.Mesh; */ public class Main { + + + public static void main(String args[]){ try { GLFWContext.init(); - FluidSim sim = new FluidSim(); - sim.setup(); - Mesh.meshInitially(sim); + //init shader program + Mesh.initShaderProgram(); + int dim = 1; + FluidSim[][][] simArray = new FluidSim[dim][1][1]; + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + simArray[x][y][z] = new FluidSim(); + simArray[x][y][z].setup(new Vector3i(x,y,z)); + } + } + } + //set sim adjacencies + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + FluidSim current = simArray[x][y][z]; + + for(int i = -1; i < 2; i++){ + for(int j = -1; j < 2; j++){ + for(int k = -1; k < 2; k++){ + if(i == j && j == k && k == 0){ + continue; + } + if( + 0 <= x + i && x + i < simArray.length && + 0 <= y + j && y + j < simArray[0].length && + 0 <= z + k && z + k < simArray[0][0].length + ){ + current.setNeighbor(i+1,j+1,k+1,simArray[x+i][y+j][z+k]); + } + } + } + } + + } + } + } + // FluidSim sim = new FluidSim(); + // sim.setup(); + Mesh[][][] meshArray = new Mesh[dim][1][1]; + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + meshArray[x][y][z] = new Mesh(simArray[x][y][z], new Vector3i(z * 16, y * 16, x * 16)); + meshArray[x][y][z].meshInitially(); + } + } + } + // Mesh mesh = new Mesh(sim); + // mesh.meshInitially(); int i = 0; long time = 0; long lastTime = 0; + Scanner scan = new Scanner(System.in); while(true){ try { TimeUnit.MILLISECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } + // + //Transfer data + // lastTime = System.currentTimeMillis(); - sim.simulate(i,0.001f); + // for(int x = 0; x < simArray.length; x++){ + // for(int y = 0; y < simArray[0].length; y++){ + // for(int z = 0; z < simArray[0][0].length; z++){ + // simArray[x][y][z].transfer(); + // } + // } + // } + // + //Simulate + // + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + simArray[x][y][z].simulate(i, 0.001f); + } + } + } time = time + (System.currentTimeMillis() - lastTime); - Mesh.remesh(sim,i); - GLFWContext.redraw(); + // + //Remesh + // + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + meshArray[x][y][z].remesh(); + } + } + } + //redraw + GLFWContext.redraw(meshArray); i++; if(i == 1000){ System.out.println(time / 1000.0); } + if(i > 3){ + // scan.next(); + } } } catch(Throwable ex){ ex.printStackTrace(System.err); diff --git a/src/main/java/electrosphere/render/GLFWContext.java b/src/main/java/electrosphere/render/GLFWContext.java index b2c7f77..e28db56 100644 --- a/src/main/java/electrosphere/render/GLFWContext.java +++ b/src/main/java/electrosphere/render/GLFWContext.java @@ -82,13 +82,19 @@ public class GLFWContext { GLFW.glfwSwapInterval(0); } - public static void redraw(){ + public static void redraw(Mesh[][][] meshes){ //clear screen GL44.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); GL44.glClear(GL44.GL_COLOR_BUFFER_BIT | GL44. GL_DEPTH_BUFFER_BIT); //draw here - Mesh.draw(); + for(int x = 0; x < meshes.length; x++){ + for(int y = 0; y < meshes[0].length; y++){ + for(int z = 0; z < meshes[0][0].length; z++){ + meshes[x][y][z].draw(); + } + } + } //ending stuff GLFW.glfwSwapBuffers(window); GLFW.glfwPollEvents(); diff --git a/src/main/java/electrosphere/render/Mesh.java b/src/main/java/electrosphere/render/Mesh.java index fe0ec27..b7a4a8c 100644 --- a/src/main/java/electrosphere/render/Mesh.java +++ b/src/main/java/electrosphere/render/Mesh.java @@ -14,6 +14,7 @@ import java.util.Map; import org.joml.Matrix4f; import org.joml.Vector3f; +import org.joml.Vector3i; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL33; import org.lwjgl.opengl.GL44; @@ -25,22 +26,28 @@ import electrosphere.FluidSim; public class Mesh { - public static int vaoPtr; - static int vertBufferPtr; - static int normBufferPtr; - static int faceBufferPtr; + public int vaoPtr; + int vertBufferPtr; + int normBufferPtr; + int faceBufferPtr; static int shaderProgram; - public static int elementCount = 0; + public int elementCount = 0; - static Matrix4f viewMatrix = new Matrix4f(); - static Matrix4f projectionMatrix = new Matrix4f(); - static Matrix4f model = new Matrix4f().identity(); + Matrix4f viewMatrix = new Matrix4f(); + Matrix4f projectionMatrix = new Matrix4f(); + Matrix4f model = new Matrix4f().identity(); - static MemoryStack stack = MemoryStack.create(32 * 1000 * 1000); + FluidSim sim; + Vector3i offset; - public static void meshInitially(FluidSim sim){ + public Mesh(FluidSim sim, Vector3i offset){ + this.sim = sim; + this.offset = offset; + } + + public void meshInitially(){ //create and bind vao vaoPtr = GL44.glGenVertexArrays(); GL44.glBindVertexArray(vaoPtr); @@ -115,1106 +122,26 @@ public class Mesh { ex.printStackTrace(); } - String vsSrc = ""; - ClassLoader classloader = Thread.currentThread().getContextClassLoader(); - try (BufferedReader is = new BufferedReader(new InputStreamReader(classloader.getResourceAsStream("shader.vs")))){ - String temp; - while((temp = is.readLine())!=null){ - vsSrc = vsSrc + temp + "\n"; - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - String fsSrc = ""; - try (BufferedReader is = new BufferedReader(new InputStreamReader(classloader.getResourceAsStream("shader.fs")))){ - String temp; - while((temp = is.readLine())!=null){ - fsSrc = fsSrc + temp + "\n"; - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - // - //Create shader - // - int vertexShader = GL45.glCreateShader(GL45.GL_VERTEX_SHADER); - GL45.glShaderSource(vertexShader, vsSrc); - //Compiles the source for the vertex shader object - GL45.glCompileShader(vertexShader); - //The following tests if the vertex shader compiles successfully - int success; - success = GL45.glGetShaderi(vertexShader, GL45.GL_COMPILE_STATUS); - if (success != GL45.GL_TRUE) { - System.err.println("Vertex Shader failed to compile!"); - System.err.println("Source is: "); - System.err.println(GL45.glGetShaderSource(vertexShader)); - System.err.println(GL45.glGetShaderInfoLog(vertexShader)); - } - //Creates and opengl object for a fragment shader and assigns its 'pointer' to the integer fragmentShader - int fragmentShader = GL45.glCreateShader(GL45.GL_FRAGMENT_SHADER); - //This points the opengl shadder object to its proper source - GL45.glShaderSource(fragmentShader, fsSrc); - //This compiles the shader object - GL45.glCompileShader(fragmentShader); - //This tests for the success of the compile attempt - success = GL45.glGetShaderi(fragmentShader, GL45.GL_COMPILE_STATUS); - if (success != GL45.GL_TRUE) { - System.err.println("Fragment Shader failed to compile!"); - System.err.println("Source is: "); - System.err.println(GL45.glGetShaderSource(fragmentShader)); - System.err.println(GL45.glGetShaderInfoLog(fragmentShader)); - } - //This creates a shader program opengl object and assigns its 'pointer' to the integer shaderProgram - shaderProgram = GL45.glCreateProgram(); - //This attaches the vertex and fragment shaders to the program - GL45.glAttachShader(shaderProgram, vertexShader); - GL45.glAttachShader(shaderProgram, fragmentShader); - //This links the program to the GPU (I think its to the GPU anyway) - GL45.glLinkProgram(shaderProgram); - //Tests for the success of the shader program creation - success = GL45.glGetProgrami(shaderProgram, GL45.GL_LINK_STATUS); - if (success != GL45.GL_TRUE) { - throw new RuntimeException(GL45.glGetProgramInfoLog(shaderProgram)); - } - //Deletes the individual shader objects to free up memory - GL45.glDeleteShader(vertexShader); - GL45.glDeleteShader(fragmentShader); - - //bind shader - GL45.glUseProgram(shaderProgram); viewMatrix = new Matrix4f(); - viewMatrix.setLookAt(new Vector3f(-8,20,0), new Vector3f(8,8,8), new Vector3f(0,1,0)); + viewMatrix.setLookAt(new Vector3f(0,20,16), new Vector3f(8,8,16), new Vector3f(0,1,0)); projectionMatrix = new Matrix4f(); projectionMatrix.setPerspective(90,1920.0f/1080.0f,0.0001f,1000f); - model = new Matrix4f().identity(); + model = new Matrix4f().identity().translate(offset.x, offset.y, offset.z); GL45.glUniformMatrix4fv(GL45.glGetUniformLocation(shaderProgram, "view"), false, viewMatrix.get(new float[16])); GL45.glUniformMatrix4fv(GL45.glGetUniformLocation(shaderProgram, "projection"), false, projectionMatrix.get(new float[16])); GL45.glUniformMatrix4fv(GL45.glGetUniformLocation(shaderProgram, "model"), false, model.get(new float[16])); } - static final double[] badFloats = new double[]{ - -0.018000, --0.167732, --0.034520, --0.268217, --0.404129, --0.329857, --0.175737, --0.145785, --0.149072, --0.038037, -0.000000, -0.000000, -0.038791, -0.005947, -0.054136, -0.131334, -0.041070, -0.219556, -0.013012, --0.000474, --0.003495, --0.007041, --0.011935, --0.019575, --0.196675, --0.034183, --0.275531, -0.073701, -0.000000, -0.131132, -0.173321, -0.366712, --0.443129, --0.297482, --0.133390, --0.133561, --0.133382, --0.092817, -0.000000, -0.076602, -0.332903, -0.344484, -0.024823, -0.010065, -0.001889, --0.001913, --0.006263, --0.009909, --0.014340, --0.022077, --0.219929, --0.037611, --0.328364, -0.000000, -0.155105, -0.239719, -0.312616, -0.364682, --0.407077, --0.225081, --0.181973, -0.189571, -0.000000, --0.098907, -0.000000, -0.072802, -0.326937, -0.024930, -0.010063, -0.002380, --0.003024, --0.006717, --0.008438, --0.010542, --0.015266, --0.024102, --0.212639, --0.035003, --0.277600, -0.368061, -0.432556, -0.436311, --0.380157, --0.119563, --0.111123, -0.000000, -0.000000, -0.213821, -0.000000, -0.413332, -0.053989, -0.324272, -0.021295, -0.008637, -0.000263, --0.004780, --0.006487, --0.007215, --0.007749, --0.009837, --0.016093, --0.026589, --0.253034, -0.512641, -0.488769, --0.480608, --0.198494, -0.000000, -0.225807, -0.000000, -0.436726, -0.043122, -0.271077, -0.017983, -0.005864, --0.002037, --0.005304, --0.005656, --0.006245, --0.006233, --0.006318, --0.010326, --0.019034, --0.231762, --0.033760, --0.344924, -0.571839, -0.470798, --0.300783, -0.000000, -0.191491, -0.000000, -0.459070, -0.068636, -0.408031, -0.368998, -0.030833, -0.015313, -0.003982, --0.002728, --0.004762, --0.005201, --0.005699, --0.005469, --0.004502, --0.007140, --0.014826, --0.031157, --0.301593, -0.533975, -0.533483, -0.469790, --0.290873, -0.000000, -0.283629, -0.000000, -0.076956, -0.357002, -0.029194, -0.013023, -0.004027, --0.002150, --0.004331, --0.004951, --0.005318, --0.005488, --0.003910, --0.005899, --0.013239, --0.029597, --0.309455, -0.579415, -0.536260, --0.291285, -0.000000, -0.251015, -0.000000, -0.072413, -0.357138, -0.025313, -0.011642, -0.004667, --0.001054, --0.003693, --0.004300, --0.004821, --0.005949, --0.004510, --0.006221, --0.014187, --0.029782, --0.305493, -0.581444, -0.536346, --0.282511, -0.000000, -0.252069, -0.000000, -0.068788, -0.355483, -0.022685, -0.010894, -0.005060, -0.000040, --0.002243, --0.003605, --0.004750, --0.006146, --0.005269, --0.007333, --0.015962, --0.030888, --0.296169, -0.582632, -0.536954, --0.274567, -0.000000, -0.252103, -0.000000, -0.066128, -0.352637, -0.021578, -0.010707, -0.005483, -0.000911, --0.001477, --0.003854, --0.004975, --0.006008, --0.005478, --0.007961, --0.017182, --0.032008, --0.290503, -0.583358, -0.537797, --0.271735, -0.000000, -0.251796, -0.000000, -0.064248, -0.350713, -0.021487, -0.011018, -0.005796, -0.001492, --0.001446, --0.004609, --0.005432, --0.005788, --0.005414, --0.008082, --0.017619, --0.032598, --0.289137, -0.583433, -0.538447, --0.271427, -0.000000, -0.251606, -0.000000, -0.075279, -0.351757, -0.023928, -0.012115, -0.006575, -0.002317, --0.001037, --0.004058, --0.006023, --0.005324, --0.005906, --0.006663, --0.015873, --0.029480, --0.288718, -0.583416, -0.538438, --0.270202, -0.000000, -0.251179, -0.000000, --0.707107, --0.609691, --0.534279, --0.312016, --0.314372, --0.202355, --0.168835, --0.148586, --0.035095, --0.060746, --0.018168, --0.003235, --0.006578, --0.015891, --0.175989, --0.058824, --0.243817, --0.314923, --0.373025, --0.234019, --0.341931, --0.278721, --0.108869, --0.169309, -0.000000, -0.000000, --0.464408, --0.417410, --0.382316, --0.402162, --0.301695, --0.316724, --0.169985, --0.202919, --0.119212, --0.138991, --0.395962, --0.316815, --0.280960, --0.296943, --0.204948, --0.125533, --0.069137, --0.022660, --0.033534, --0.020315, --0.087644, --0.258042, --0.231446, --0.121655, --0.082574, --0.055176, --0.037919, --0.104150, --0.080524, --0.129350, --0.168287, --0.157452, --0.052999, -0.000000, --0.126392, --0.139212, --0.175566, --0.148839, --0.084813, -0.000000, --0.406294, --0.338302, --0.423234, --0.444802, --0.331531, --0.293501, --0.363346, --0.283406, --0.272504, --0.240522, --0.107328, --0.114170, --0.064533, --0.053806, --0.123257, --0.113460, --0.109624, --0.214561, --0.199608, --0.172652, --0.134810, --0.142858, --0.094800, --0.114297, --0.042854, -0.000000, --0.086405, -0.000000, --0.142424, --0.098170, -0.000000, -0.000000, --0.337920, --0.303188, --0.031983, -0.000000, --0.137537, --0.249734, --0.314415, --0.159726, --0.223788, --0.099384, --0.100844, --0.038531, --0.017544, --0.014326, --0.019552, --0.118278, --0.032677, --0.184731, --0.340128, --0.304856, --0.198917, --0.117018, --0.113405, --0.134894, --0.135819, --0.026373, -0.000000, -0.000000, --0.067051, --0.014953, -0.000000, --0.005772, --0.081030, --0.003931, -0.010439, -0.009098, --0.000979, --0.005016, --0.009917, --0.018029, --0.167774, --0.034455, --0.268567, --0.403480, --0.329246, --0.175508, --0.144101, --0.146419, --0.035732, -0.000000, -0.000000, -0.039687, -0.006877, -0.050828, -0.126469, -0.042130, -0.220615, -0.013450, --0.000186, --0.003363, --0.007046, --0.011998, --0.019600, --0.196217, --0.034000, --0.274693, -0.074873, -0.000000, -0.134010, -0.178279, -0.366220, --0.442187, --0.297784, --0.132200, --0.129306, --0.131791, --0.065941, -0.000000, -0.077162, -0.333899, -0.345473, -0.025300, -0.010401, -0.002052, --0.001914, --0.006312, --0.009951, --0.014327, --0.021974, --0.219037, --0.037170, --0.327182, -0.000000, -0.158362, -0.243386, -0.318651, -0.367928, --0.406799, --0.234304, --0.070347, --0.064678, -0.000000, -0.000000, -0.190637, -0.000000, -0.073120, -0.327314, -0.025301, -0.010217, -0.002395, --0.003101, --0.006745, --0.008437, --0.010496, --0.015130, --0.023873, --0.211788, --0.034571, --0.276605, -0.372852, -0.435053, -0.438724, --0.380379, --0.130109, -0.000000, -0.212506, -0.000000, -0.414904, -0.054096, -0.324428, -0.021488, -0.008659, -0.000204, --0.004802, --0.006475, --0.007195, --0.007710, --0.009715, --0.015899, --0.026305, --0.252293, -0.514157, -0.488563, --0.480304, --0.199191, -0.000000, -0.225044, -0.000000, -0.435370, -0.043128, -0.271094, -0.018051, -0.005853, --0.002042, --0.005285, --0.005645, --0.006221, --0.006185, --0.006225, --0.010188, --0.018857, --0.231048, --0.033315, --0.343979, -0.571701, -0.470073, --0.300500, -0.000000, -0.191295, -0.000000, -0.458156, -0.068252, -0.407288, -0.368600, -0.030782, -0.015324, -0.004006, --0.002667, --0.004743, --0.005224, --0.005676, --0.005404, --0.004409, --0.007053, --0.014733, --0.030762, --0.300654, -0.533487, -0.532563, -0.469294, --0.290191, -0.000000, -0.283314, -0.000000, -0.076314, -0.356186, -0.029119, -0.013034, -0.004092, --0.002090, --0.004340, --0.004990, --0.005304, --0.005416, --0.003794, --0.005818, --0.013189, --0.029233, --0.308485, -0.578557, -0.535070, --0.290566, -0.000000, -0.250595, -0.000000, -0.071724, -0.356216, -0.025229, -0.011682, -0.004711, --0.001034, --0.003721, --0.004322, --0.004814, --0.005894, --0.004365, --0.006112, --0.014126, --0.029403, --0.304434, -0.580369, -0.535021, --0.281689, -0.000000, -0.251640, -0.000000, -0.068082, -0.354468, -0.022613, -0.010923, -0.005074, -0.000030, --0.002260, --0.003605, --0.004739, --0.006114, --0.005116, --0.007192, --0.015868, --0.030476, --0.294994, -0.581370, -0.535521, --0.273698, -0.000000, -0.251675, -0.000000, -0.065422, -0.351571, -0.021509, -0.010705, -0.005483, -0.000905, --0.001480, --0.003842, --0.004963, --0.005983, --0.005327, --0.007796, --0.017061, --0.031572, --0.289325, -0.581952, -0.536315, --0.270927, -0.000000, -0.251368, -0.000000, -0.063545, -0.349628, -0.021416, -0.011001, -0.005782, -0.001492, --0.001442, --0.004588, --0.005417, --0.005767, --0.005262, --0.007906, --0.017484, --0.032150, --0.288028, -0.581958, -0.536956, --0.270671, -0.000000, -0.251179, -0.000000, -0.074384, -0.350675, -0.023842, -0.012093, -0.006561, -0.002312, --0.001032, --0.004042, --0.005998, --0.005313, --0.005789, --0.006460, --0.015749, --0.029120, --0.287637, -0.581927, -0.536948, --0.269457, -0.000000, -0.250755, -0.000000, --0.707107, --0.609337, --0.534763, --0.312394, --0.310026, --0.202502, --0.164767, --0.144594, --0.036986, --0.060648, --0.022643, --0.006009, --0.016546, --0.020663, --0.189685, --0.067713, --0.253065, --0.325253, --0.365646, --0.237307, --0.320501, --0.262711, --0.101391, --0.158928, -0.000000, -0.000000, --0.453941, --0.405876, --0.381332, --0.401496, --0.289610, --0.307137, --0.165874, --0.197375, --0.119796, --0.139635, --0.387245, --0.306199, --0.281258, --0.289695, --0.197717, --0.124005, --0.067851, --0.022977, --0.035196, --0.022723, --0.096255, --0.255117, --0.221126, --0.116896, --0.080584, --0.055462, --0.038972, --0.108726, --0.084312, --0.127447, --0.167202, --0.150169, --0.051255, -0.000000, --0.132815, --0.138552, --0.167928, --0.140004, --0.079981, -0.000000, --0.447442, --0.405145, --0.279765, --0.308282, --0.376103, --0.361096, --0.354694, --0.320337, --0.292019, --0.353734, --0.282068, --0.266847, --0.233328, --0.102188, --0.109615, --0.062030, --0.052969, --0.124319, --0.113367, --0.114000, --0.214698, --0.201925, --0.173516, --0.135534, --0.139440, --0.090348, --0.108313, --0.040526, -0.000000, --0.081942, -0.000000, --0.135002, --0.075693, -0.000000, -0.000000, --0.029055, -0.000000, --0.132175, --0.271183, --0.278238, --0.161898, --0.174536, --0.091563, --0.094251, --0.034464, --0.015814, --0.013873, --0.019339, --0.117407, --0.032349, --0.183659, --0.340039, --0.304628, --0.198892, --0.115657, --0.111445, --0.130273, --0.131076, --0.023190, -0.000000, -0.000000, --0.068200, --0.015742, -0.000000, --0.051728, --0.080379, --0.027413, -0.003106, -0.030335, -0.013485, -0.010201, --0.000268, --0.004873, --0.009897, --0.018058, --0.167773, --0.034387, --0.268859, -0.035122, --0.402769, --0.328637, --0.175268, --0.142379, --0.143912, --0.033652, -0.000000, -0.000000, -0.040424, --0.030352, -0.131556, -0.043146, -0.221579, -0.013864, -0.000099, --0.003237, --0.007053, --0.012060, --0.019620, --0.195744, --0.033813, --0.273835, -0.075811, -0.000000, -0.106391, -0.247250, -0.326729, --0.441240, --0.298083, --0.131033, --0.129604, --0.130252, --0.064848, -0.000000, -0.077673, -0.334831, -0.346395, -0.025768, -0.010729, -0.002209, --0.001918, --0.006362, --0.009990, --0.014309, --0.021865, --0.218143, --0.036733, --0.326005, -0.000000, -0.161321, -0.246712, -0.324331, -0.370975, --0.406531, --0.235363, --0.069792, --0.064842, -0.000000, -0.000000, -0.191568, -0.000000, -0.073410, -0.327666, -0.025660, -0.010364, -0.002406, --0.003175, --0.006769, --0.008434, --0.010449, --0.014992, --0.023643, --0.210956, --0.034148, --0.275636, -0.377321, -0.437392, -0.440900, --0.380594, --0.130717, -0.000000, -0.211201, -0.000000, -0.416237, -0.054188, -0.324561, -0.021670, -0.008676, -0.000147, --0.004820, --0.006461, --0.007175, --0.007668, --0.009593, --0.015704, --0.026026, --0.251562, -0.515534, -0.488259, --0.479980, --0.199870, -0.000000, -0.224329, -0.000000, -0.434004, -0.043120, -0.271094, -0.018113, -0.005844, --0.002043, --0.005263, --0.005634, --0.006195, --0.006134, --0.006132, --0.010051, --0.018686, --0.230334, --0.032878, --0.343021, -0.571528, -0.469342, --0.300201, -0.000000, -0.191107, -0.000000, -0.457260, -0.067854, -0.406533, -0.368184, -0.030725, -0.015331, -0.004031, --0.002604, --0.004723, --0.005248, --0.005650, --0.005336, --0.004315, --0.006969, --0.014643, --0.030376, --0.299708, -0.532971, -0.531598, -0.468777, --0.289499, -0.000000, -0.282992, -0.000000, -0.075661, -0.355358, -0.029038, -0.013043, -0.004154, --0.002031, --0.004346, --0.005028, --0.005287, --0.005341, --0.003678, --0.005737, --0.013142, --0.028877, --0.307512 - }; - public static void remesh(FluidSim sim, long frame){ + public void remesh(){ //generate verts TerrainChunkData data = generateTerrainChunkData(sim.getData()); + GL44.glBindVertexArray(vaoPtr); + // for(int i = 0; i < data.normals.size(); i+=3){ // // if(i > 100 && data.normals.get(i+1) < 0.7){ // data.normals.set(i+1, 1f); @@ -1288,9 +215,9 @@ public class Mesh { static float angle = 0; - public static void draw(){ + public void draw(){ GL45.glUseProgram(shaderProgram); - GL45.glBindFramebuffer(GL45.GL_FRAMEBUFFER, 0); + // GL45.glBindFramebuffer(GL45.GL_FRAMEBUFFER, 0); GL44.glBindVertexArray(vaoPtr); // angle = angle + 0.01f; // viewMatrix.identity().setLookAt( @@ -1303,7 +230,86 @@ public class Mesh { GL45.glUniformMatrix4fv(GL45.glGetUniformLocation(shaderProgram, "view"), false, viewMatrix.get(new float[16])); GL45.glUniformMatrix4fv(GL45.glGetUniformLocation(shaderProgram, "projection"), false, projectionMatrix.get(new float[16])); GL45.glUniformMatrix4fv(GL45.glGetUniformLocation(shaderProgram, "model"), false, model.get(new float[16])); - GL44.glDrawElements(GL44.GL_TRIANGLES, Mesh.elementCount, GL44.GL_UNSIGNED_INT, 0); + GL44.glDrawElements(GL44.GL_TRIANGLES, elementCount, GL44.GL_UNSIGNED_INT, 0); + } + + + + + + + public static void initShaderProgram(){ + String vsSrc = ""; + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); + try (BufferedReader is = new BufferedReader(new InputStreamReader(classloader.getResourceAsStream("shader.vs")))){ + String temp; + while((temp = is.readLine())!=null){ + vsSrc = vsSrc + temp + "\n"; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + String fsSrc = ""; + try (BufferedReader is = new BufferedReader(new InputStreamReader(classloader.getResourceAsStream("shader.fs")))){ + String temp; + while((temp = is.readLine())!=null){ + fsSrc = fsSrc + temp + "\n"; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + // + //Create shader + // + int vertexShader = GL45.glCreateShader(GL45.GL_VERTEX_SHADER); + GL45.glShaderSource(vertexShader, vsSrc); + //Compiles the source for the vertex shader object + GL45.glCompileShader(vertexShader); + //The following tests if the vertex shader compiles successfully + int success; + success = GL45.glGetShaderi(vertexShader, GL45.GL_COMPILE_STATUS); + if (success != GL45.GL_TRUE) { + System.err.println("Vertex Shader failed to compile!"); + System.err.println("Source is: "); + System.err.println(GL45.glGetShaderSource(vertexShader)); + System.err.println(GL45.glGetShaderInfoLog(vertexShader)); + } + //Creates and opengl object for a fragment shader and assigns its 'pointer' to the integer fragmentShader + int fragmentShader = GL45.glCreateShader(GL45.GL_FRAGMENT_SHADER); + //This points the opengl shadder object to its proper source + GL45.glShaderSource(fragmentShader, fsSrc); + //This compiles the shader object + GL45.glCompileShader(fragmentShader); + //This tests for the success of the compile attempt + success = GL45.glGetShaderi(fragmentShader, GL45.GL_COMPILE_STATUS); + if (success != GL45.GL_TRUE) { + System.err.println("Fragment Shader failed to compile!"); + System.err.println("Source is: "); + System.err.println(GL45.glGetShaderSource(fragmentShader)); + System.err.println(GL45.glGetShaderInfoLog(fragmentShader)); + } + //This creates a shader program opengl object and assigns its 'pointer' to the integer shaderProgram + shaderProgram = GL45.glCreateProgram(); + //This attaches the vertex and fragment shaders to the program + GL45.glAttachShader(shaderProgram, vertexShader); + GL45.glAttachShader(shaderProgram, fragmentShader); + //This links the program to the GPU (I think its to the GPU anyway) + GL45.glLinkProgram(shaderProgram); + //Tests for the success of the shader program creation + success = GL45.glGetProgrami(shaderProgram, GL45.GL_LINK_STATUS); + if (success != GL45.GL_TRUE) { + throw new RuntimeException(GL45.glGetProgramInfoLog(shaderProgram)); + } + + //Deletes the individual shader objects to free up memory + GL45.glDeleteShader(vertexShader); + GL45.glDeleteShader(fragmentShader); + + //bind shader + GL45.glUseProgram(shaderProgram); } @@ -1803,34 +809,46 @@ public class Mesh { // // } - + float oldProportion; + float newProportion; + Vector3f currentNormal; //for each vertex, average the new normal with the normals that are already there int trianglesSharingIndex0 = trianglesSharingVert.get(index0); - //calculate proportion of each normal - float oldProportion = trianglesSharingIndex0 / (float)(trianglesSharingIndex0 + 1); - float newProportion = 1.0f / (float)(trianglesSharingIndex0 + 1); - //increment number of triangles sharing vert - trianglesSharingVert.set(index0, trianglesSharingIndex0 + 1); + if(trianglesSharingIndex0 > 0){ + //calculate proportion of each normal + oldProportion = trianglesSharingIndex0 / (float)(trianglesSharingIndex0 + 1); + newProportion = 1.0f / (float)(trianglesSharingIndex0 + 1); + //increment number of triangles sharing vert + trianglesSharingVert.set(index0, trianglesSharingIndex0 + 1); - Vector3f currentNormal = normals.get(index0); - currentNormal = averageNormals(currentNormal,oldProportion,n,newProportion); - normals.get(index0).set(currentNormal); + currentNormal = normals.get(index0); + currentNormal = averageNormals(currentNormal,oldProportion,n,newProportion); + normals.get(index0).set(currentNormal); + } else { + trianglesSharingVert.set(index0, trianglesSharingIndex0 + 1); + normals.get(index0).set(n); + } int trianglesSharingIndex1 = trianglesSharingVert.get(index1); - //calculate proportion of each normal - oldProportion = trianglesSharingIndex1 / (float)(trianglesSharingIndex1 + 1); - newProportion = 1.0f / (float)(trianglesSharingIndex1 + 1); - //increment number of triangles sharing vert - trianglesSharingVert.set(index1, trianglesSharingIndex1 + 1); + if(trianglesSharingIndex1 > 0){ + //calculate proportion of each normal + oldProportion = trianglesSharingIndex1 / (float)(trianglesSharingIndex1 + 1); + newProportion = 1.0f / (float)(trianglesSharingIndex1 + 1); + //increment number of triangles sharing vert + trianglesSharingVert.set(index1, trianglesSharingIndex1 + 1); - currentNormal = normals.get(index1); - currentNormal = averageNormals(currentNormal,oldProportion,n,newProportion); - normals.get(index1).set(currentNormal); + currentNormal = normals.get(index1); + currentNormal = averageNormals(currentNormal,oldProportion,n,newProportion); + normals.get(index1).set(currentNormal); + } else { + trianglesSharingVert.set(index1, trianglesSharingIndex1 + 1); + normals.get(index1).set(n); + } @@ -1840,15 +858,20 @@ public class Mesh { int trianglesSharingIndex2 = trianglesSharingVert.get(index2); - //calculate proportion of each normal - oldProportion = trianglesSharingIndex2 / (float)(trianglesSharingIndex2 + 1); - newProportion = 1.0f / (float)(trianglesSharingIndex2 + 1); - //increment number of triangles sharing vert - trianglesSharingVert.set(index2, trianglesSharingIndex2 + 1); + if(trianglesSharingIndex2 > 0){ + //calculate proportion of each normal + oldProportion = trianglesSharingIndex2 / (float)(trianglesSharingIndex2 + 1); + newProportion = 1.0f / (float)(trianglesSharingIndex2 + 1); + //increment number of triangles sharing vert + trianglesSharingVert.set(index2, trianglesSharingIndex2 + 1); - currentNormal = normals.get(index2); - currentNormal = averageNormals(currentNormal,oldProportion,n,newProportion); - normals.get(index2).set(currentNormal); + currentNormal = normals.get(index2); + currentNormal = averageNormals(currentNormal,oldProportion,n,newProportion); + normals.get(index2).set(currentNormal); + } else { + trianglesSharingVert.set(index2, trianglesSharingIndex2 + 1); + normals.get(index2).set(n); + } } @@ -1903,9 +926,9 @@ public class Mesh { - for(int x = 0; x < FluidSim.DIM - 1; x++){ - for(int y = 0; y < FluidSim.DIM - 1; y++){ - for(int z = 0; z < FluidSim.DIM - 1; z++){ + for(int x = 1; x < FluidSim.DIM - 1; x++){ + for(int y = 1; y < FluidSim.DIM - 1; y++){ + for(int z = 1; z < FluidSim.DIM - 1; z++){ //push the current cell's values into the gridcell currentCell.setValues( new Vector3f(x+0,y+0,z+0), new Vector3f(x+0,y+0,z+1), new Vector3f(x+1,y+0,z+1), new Vector3f(x+1,y+0,z+0),