diff --git a/VBO/.classpath b/VBO/.classpath index 27b5ef8..0d779fa 100644 --- a/VBO/.classpath +++ b/VBO/.classpath @@ -4,5 +4,8 @@ + + + diff --git a/VBO/lib/affinity-1.6-sources.jar b/VBO/lib/affinity-1.6-sources.jar new file mode 100644 index 0000000..0720131 Binary files /dev/null and b/VBO/lib/affinity-1.6-sources.jar differ diff --git a/VBO/lib/affinity-1.6.jar b/VBO/lib/affinity-1.6.jar new file mode 100644 index 0000000..e276362 Binary files /dev/null and b/VBO/lib/affinity-1.6.jar differ diff --git a/VBO/lib/jna-4.0.0.jar b/VBO/lib/jna-4.0.0.jar new file mode 100644 index 0000000..9038048 Binary files /dev/null and b/VBO/lib/jna-4.0.0.jar differ diff --git a/VBO/lib/jna-platform-3.4.0.jar b/VBO/lib/jna-platform-3.4.0.jar new file mode 100644 index 0000000..2d9a0f2 Binary files /dev/null and b/VBO/lib/jna-platform-3.4.0.jar differ diff --git a/VBO/src/main/Game.java b/VBO/src/main/Game.java index a03868e..e670fe7 100644 --- a/VBO/src/main/Game.java +++ b/VBO/src/main/Game.java @@ -5,13 +5,18 @@ import world.*; public class Game { private World world; + private int update = 0; public Game(){ world= new World(0); } public void update(){ - world.update(); + if(update >= 2){ + world.update(); + update = 0; + } + update++; } public void render(){ diff --git a/VBO/src/main/Main.java b/VBO/src/main/Main.java index d2fa85f..7a13dbd 100644 --- a/VBO/src/main/Main.java +++ b/VBO/src/main/Main.java @@ -1,9 +1,13 @@ package main; +import java.util.concurrent.*; + import org.lwjgl.input.*; import org.lwjgl.opengl.*; import rendering.*; +import vanilla.java.affinity.*; +import vanilla.java.affinity.impl.*; public class Main { @@ -16,8 +20,9 @@ public class Main { private static final String TITLE = "Test VBO"; private static final int width = 1280, height = 720; - private static Thread[] threadArray; + private static AffinityLock al; + private static Game game; /** @@ -25,13 +30,16 @@ public class Main { * @Info Fonction principal */ public static void main(String[] args) { +// mainPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors()); +// AffinityLock.cpuLayout(new NoCpuLayout(Runtime.getRuntime().availableProcessors() + 8)); + al = AffinityLock.acquireLock(); + System.out.println(AffinityLock.cpuLayout().coresPerSocket()); try { Display.setTitle(TITLE); Display.setDisplayMode(new DisplayMode(width, height)); Display.setResizable(true); Mouse.setGrabbed(true); Display.create(); - threadArray = new Thread[Runtime.getRuntime().availableProcessors()]; game = new Game(); loop(); } catch (Exception e) { @@ -39,16 +47,10 @@ public class Main { } } - public static Thread addThread(Thread t){ - for(Thread c : threadArray){ - c = t; - try { - c.join(); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + public static Runnable addThread(Runnable t,String name){ +// mainPool.execute(t); + new Thread(t, name).start(); +// System.out.println("Details" + AffinityLock.dumpLocks()); return t; } diff --git a/VBO/src/world/Chunk.java b/VBO/src/world/Chunk.java index 289e549..bb53029 100644 --- a/VBO/src/world/Chunk.java +++ b/VBO/src/world/Chunk.java @@ -1,5 +1,6 @@ package world; +import vanilla.java.affinity.*; import blocks.*; import main.*; import math.*; @@ -14,12 +15,15 @@ public class Chunk { private boolean IsLoad = false; private boolean IsGenerated = false; private boolean IsCurrentGenerate = false; + private boolean IsDestroy = false; + private WorldNoise worldNoise; - public Chunk(int x, int y, int z, World world) { + public Chunk(int x, int y, int z, World world,WorldNoise worldNoise) { this.x = x; this.y = y; this.z = z; this.world = world; + this.worldNoise = worldNoise; this.blocks = new Block[SIZE][SIZE][SIZE]; vbo = new VBO(); } @@ -35,7 +39,7 @@ public class Chunk { public void createChunk(World world) { this.world = world; - Main.addThread((new Thread(new Generate(this, world)))).start(); + Main.addThread(new Generate(this, world),"Create Chunk"); IsCurrentGenerate = true; } @@ -84,6 +88,7 @@ public class Chunk { public void destroyChunk() { vbo.destroyVBO(); + IsDestroy = false; } public void loopChunk(int x, int y, int z) { @@ -107,7 +112,13 @@ public class Chunk { // up + 1 = down - 1 = y // left - 1 = right + 1 = x // front - 1 = back + 1 = z - +// up = true; +// down = true; +// left = true; +// right = true; +// back = true; +// front = true; + if (up) { // aa ab bb ba float[] a = new float[] { 1, 1, 1, 1 }; @@ -260,6 +271,20 @@ public class Chunk { public boolean isCurrentGenerate(){ return IsCurrentGenerate; } + + public boolean isDestroy() { + return IsDestroy; + } + + public WorldNoise getWorldNoise() { + return worldNoise; + } + + public void setWorldNoise(WorldNoise worldNoise) { + this.worldNoise = worldNoise; + } + + } class Generate implements Runnable { @@ -271,27 +296,63 @@ class Generate implements Runnable { this.chunk = chunk; this.world = world; } + + /* + * boolean grounded = noise.getNoise(xx, zz) > yy - 1 && noise.getNoise(xx, zz) < yy; + + float percentOfSpawnTree = 0.005f; + if(random.nextFloat() < percentOfSpawnTree && grounded){ + if(random.nextInt(2) == 0) + Tree.addOak(world, xx, yy, zz); + else + Tree.addFir(world, xx, yy, zz); + }(non-Javadoc) + * @see java.lang.Runnable#run() + */ public void run() { - for (int i = 0; i < chunk.SIZE; i++) { - for (int j = 0; j < 2; j++) { - for (int k = 0; k < chunk.SIZE; k++) { - chunk.blocks[i][j][k] = Block.GRASS; - } - } - } - synchronized (world.chunks) { - for (int i = 0; i < chunk.SIZE; i++) { - for (int j = 0; j < 2; j++) { - for (int k = 0; k < chunk.SIZE; k++) { - chunk.loopChunk(i, j, k); + AffinityLock al = null; + int cpuId = 0; + try{ + al = AffinityLock.acquireCore(); + }catch(Exception e){} + long current = System.currentTimeMillis(); + boolean IsError = true; + Noise noise = new Noise(world.seed, 20, 5); + for (int x = 0; x < chunk.SIZE; x++) { + for (int z = 0; z < chunk.SIZE; z++) { + for (int y = 0; y < chunk.SIZE; y++) { + int xx = chunk.getX() * chunk.SIZE + x; + int yy = chunk.getY() * chunk.SIZE + y; + int zz = chunk.getZ() * chunk.SIZE + z; + if(noise.getNoise(xx, zz) > yy){ + chunk.blocks[x][y][z] = Block.GRASS; + }else{ + continue; } } } } - chunk.setGenerated(true); - System.out.println(Thread.currentThread().getName()); + while(IsError){ + IsError = false; + try{ + synchronized (world.chunks) { + for (int i = 0; i < chunk.SIZE; i++) { + for (int j = 0; j < chunk.SIZE; j++) { + for (int k = 0; k < chunk.SIZE; k++) { + chunk.loopChunk(i, j, k); + } + } + } + } + chunk.setGenerated(true); + }catch (Exception e){ + IsError = true; + } + } + System.out.println(Thread.currentThread().getName() + " | " + (System.currentTimeMillis()-current) + " | " + cpuId); Thread.currentThread().stop(); + al.release(); } } diff --git a/VBO/src/world/Noise.java b/VBO/src/world/Noise.java new file mode 100644 index 0000000..a4a69fb --- /dev/null +++ b/VBO/src/world/Noise.java @@ -0,0 +1,62 @@ +package world; + +import java.util.Random; + +import math.Vector2f; + +public class Noise { + + private long seed; + private Random rand; + private int octave; + private float amplitude; + + public Noise(long seed,int octave, float amplitude){ + this.seed = seed; + this.octave = octave; + this.amplitude = amplitude; + + rand = new Random(); + } + + public float getNoise(float x,float z){ + int xmin = (int)(double)x/octave; + int xmax = (int) xmin + 1; + int zmin = (int)(double)z/octave; + int zmax = (int) zmin + 1; + + Vector2f a = new Vector2f(xmin,zmin); + Vector2f b = new Vector2f(xmax,zmin); + Vector2f c = new Vector2f(xmax,zmax); + Vector2f d = new Vector2f(xmin,zmax); + + float ra = (float)noise(a); + float rb = (float)noise(b); + float rc = (float)noise(c); + float rd = (float)noise(d); + + float t = (x - xmin * octave) / octave; + + float i1 = interpolate(ra,rb,t); + float i2 = interpolate(rd,rc,t); + + float h = interpolate(i1,i2,(z - zmin * octave) / octave); + return h * amplitude; + } + + private float interpolate(float a, float b, float t){ + float ft = (float) (t * Math.PI); + float f = (float )((1f - Math.cos(ft)) * 0.5f); + float ret = a * ( 1f - f) + b * f; + + return ret; + } + + private double noise(Vector2f coord){ + double var = 10000 * (Math.sin(coord.getX() + Math.cos(coord.getY())) + Math.tan(seed)); + rand.setSeed((long)var); + double ret = rand.nextDouble(); + + return ret; + } +} diff --git a/VBO/src/world/World.java b/VBO/src/world/World.java index 0fd9751..628d7e4 100644 --- a/VBO/src/world/World.java +++ b/VBO/src/world/World.java @@ -11,16 +11,18 @@ public class World { public long seed; public final int SIZE = 1,HEIGHT = 1; public static final float GRAVITY = 1; - public static final int VIEW_CHUNK = 4; + public static final int VIEW_CHUNK = 2; + public static WorldNoise worldNoise; public ArrayList chunks = new ArrayList(); public World(long seed){ this.seed= seed; + this.worldNoise = new WorldNoise(seed); for(int x = 0;x < SIZE;x++){ for(int y = 0;y < HEIGHT;y++){ for(int z = 0;z < SIZE;z++){ - Chunk ch = new Chunk(x,y,z,this); + Chunk ch = new Chunk(x,y,z,this,worldNoise); chunks.add(ch); } } @@ -41,24 +43,28 @@ public class World { for(int k = 0;k <= delta_z;k++){ for(int j = 0; j < HEIGHT; j++){ if(getChunk((xa + i), 0, (za + k)) == null){ - Chunk ch = new Chunk((xa + i),j,(za + k),this); + Chunk ch = new Chunk((xa + i),j,(za + k),this,worldNoise); chunks.add(ch); } } } } + ArrayList removeList = new ArrayList(); for(int i = 0; i < chunks.size();i++){ Chunk c = chunks.get(i); if(c.getPosition().getX() < xa || c.getPosition().getX() > xb || c.getPosition().getZ() < za || c.getPosition().getZ() > zb){ c.destroyChunk(); - chunks.remove(i); - break; + removeList.add(c); } } + for(Chunk c: removeList){ + removeByChunk(c); + } + removeList.clear(); for(Chunk c : chunks){ - if(!c.isLoaded() && !c.isGenerated() && !c.isCurrentGenerate())c.createChunk(this); - if(!c.isLoaded() && c.isGenerated())c.loadBufferData(); - c.update(); + if(!c.isLoaded() && !c.isGenerated() && !c.isCurrentGenerate() && !c.isDestroy())c.createChunk(this); + if(!c.isLoaded() && c.isGenerated() && !c.isDestroy())c.loadBufferData(); + if(!c.isDestroy())c.update(); } System.gc(); } @@ -70,6 +76,14 @@ public class World { } } + public void removeByChunk(Chunk ch){ + for(int i = 0;i < chunks.size();i++){ + if(chunks.get(i).equals(ch)){ + chunks.remove(i); + } + } + } + public Chunk getChunk(int xc, int yc, int zc) { Chunk c = null; Object[] chunk = chunks.toArray(); @@ -94,6 +108,16 @@ public class World { int xb = x % Chunk.SIZE; int yb = y % Chunk.SIZE; int zb = z % Chunk.SIZE; + +// if(xb < 0){ +// xb = -xb; +// } +// if(yb < 0){ +// yb = -yb; +// } +// if(zb < 0){ +// zb = -zb; +// } return chunk.getBlock(xb, yb, zb); } diff --git a/VBO/src/world/WorldNoise.java b/VBO/src/world/WorldNoise.java new file mode 100644 index 0000000..7e5aa2d --- /dev/null +++ b/VBO/src/world/WorldNoise.java @@ -0,0 +1,17 @@ +package world; + +import java.util.*; + +public class WorldNoise { + + public Noise noise; + public Noise colorVariationNoise; + public Random random; + + public WorldNoise(long seed){ + noise = new Noise(seed, 20, 5); + colorVariationNoise = new Noise(seed, 40, 2); + random = new Random(seed); + } + +}