1
0
Fork 0

Upgrade chunk performance

This commit is contained in:
MrDev023 2015-07-18 01:06:06 +02:00
parent 64b1ab1c6d
commit 521e58acb9
5 changed files with 155 additions and 181 deletions

View file

@ -10,11 +10,12 @@ import org.lwjgl.opengl.*;
import rendering.*;
import vanilla.java.affinity.*;
import vanilla.java.affinity.impl.*;
import world.*;
public class Main {
private static boolean IsRunning = true;
private static long current = System.currentTimeMillis(), elapsedInfo = 0,
private static long current = System.currentTimeMillis(),current2, elapsedInfo = 0,
elapsed = 0, previous = 0;
private static long timeTicks = 0, timeFps = 0;
private static int FPS = 0, TICKS = 0, LAST_TICKS = 60, LAST_FPS = 60;
@ -72,6 +73,7 @@ public class Main {
/**
* @Info Boucle principal avec Timer
*/
public static long time = 0;
public static void loop() {
while (IsRunning) {
previous = current;
@ -84,13 +86,14 @@ public class Main {
DisplayManager.updateDisplay();
}
current2 = System.nanoTime();
if (elapsed >= 1000 / 60) {
Update.updateMouse();
Update.updateKeyboard();
Update.update();
TICKS++;
elapsed = 0;
timeTicks = System.currentTimeMillis() - current;
timeTicks = System.nanoTime() - current2;
} else {
DisplayManager.clearScreen();
DisplayManager.preRender3D();
@ -102,23 +105,25 @@ public class Main {
DisplayManager.preRender2D();
DisplayManager.render2D();
FPS++;
timeFps = System.currentTimeMillis() - current;
timeFps = System.nanoTime() - current2;
}
if (elapsedInfo >= 1000) {
LAST_FPS = FPS;
LAST_TICKS = TICKS;
Display.setTitle(TITLE + " | FPS:" + LAST_FPS + " TICKS:"
+ LAST_TICKS + " timeFps:" + timeFps + "ms timeTicks:"
+ timeTicks + "ms" + " | PX:"
Display.setTitle(TITLE + " | FPS:" + (int)(1000000000.0f/timeFps) + " TICKS:"
+ (int)(1000000000.0f/timeTicks) + " timeFps:" + timeFps + "ns timeTicks:"
+ timeTicks + "ns" + " | PX:"
+ Camera.getPosition().getX() + " PY:"
+ Camera.getPosition().getY() + " PZ:"
+ Camera.getPosition().getZ() + " | "
+ mainPool);
+ World.updateWorldTime + " " + DisplayManager.getDelta());
FPS = 0;
TICKS = 0;
elapsedInfo = 0;
}
Display.update();
}
}

View file

@ -105,7 +105,6 @@ public class Update {
*/
public static void update(){
Main.getGame().update();
Display.update();
}
}

View file

@ -1,10 +1,10 @@
package main;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import java.nio.*;
import java.util.*;
import org.lwjgl.*;
@ -14,6 +14,7 @@ public class VBO {
private int bufferSize = 0;
private int vboID = 0;
private FloatBuffer buffer;
private ArrayList<Float> floatlist = new ArrayList<Float>();
public VBO(){
this.vboID = createVBO();
@ -40,18 +41,8 @@ public class VBO {
public void addDataByFloatArray(float[] a){
if(a == null)return;
if(buffer != null){
int size = buffer.limit();
float[] previousBuffer = new float[size];
buffer.get(previousBuffer);
buffer.clear();
buffer = BufferUtils.createFloatBuffer(size + a.length);
buffer.put(previousBuffer).put(a);
buffer.flip();
}else{
buffer = BufferUtils.createFloatBuffer(a.length);
buffer.put(a);
buffer.flip();
for(float c :a){
floatlist.add(c);
}
}
@ -62,20 +53,11 @@ public class VBO {
*
*
*/
public void addDataByFloatList(float... a){
if(a == null)return;
if(buffer != null){
int size = buffer.limit();
float[] previousBuffer = new float[size];
buffer.get(previousBuffer);
buffer.clear();
buffer = BufferUtils.createFloatBuffer(size + a.length);
buffer.put(previousBuffer).put(a);
buffer.flip();
}else{
buffer = BufferUtils.createFloatBuffer(a.length);
buffer.put(a);
buffer.flip();
for(float c :a){
floatlist.add(c);
}
}
@ -85,6 +67,11 @@ public class VBO {
* @Info Stocke le FloatBuffer dans le GPU
*/
public void bufferData(){
buffer = BufferUtils.createFloatBuffer(floatlist.size());
for(Float f : floatlist){
buffer.put(f);
}
buffer.flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
// glBufferSubData(vboID, 0, buffer);

View file

@ -11,7 +11,7 @@ import math.*;
public class Chunk {
public final static int SIZE = 8;
public final static int SIZE = 16;
private int x, y, z;
private VBO vbo;
private World world;
@ -111,12 +111,12 @@ public class Chunk {
boolean right = true;
boolean back = true;
boolean front = true;
up = world.getBlock(xx, yy + 1, zz) == null;
down = world.getBlock(xx, yy - 1, zz) == null;
left = world.getBlock(xx - 1, yy, zz) == null;
right = world.getBlock(xx + 1, yy, zz) == null;
front = world.getBlock(xx, yy, zz - 1) == null;
back = world.getBlock(xx, yy, zz + 1) == null;
// up = world.getBlock(xx, yy + 1, zz) == null;
// down = world.getBlock(xx, yy - 1, zz) == null;
// left = world.getBlock(xx - 1, yy, zz) == null;
// right = world.getBlock(xx + 1, yy, zz) == null;
// front = world.getBlock(xx, yy, zz - 1) == null;
// back = world.getBlock(xx, yy, zz + 1) == null;
if (!up && !down && !left && !right && !front && !back)
return;
if (blocks[x][y][z] == null)
@ -133,22 +133,22 @@ public class Chunk {
// aa ab bb ba
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx - 1, yy + 1, zz) != null
|| world.getBlock(xx - 1, yy + 1, zz - 1) != null
|| world.getBlock(xx, yy + 1, zz - 1) != null)
a[0] = ao;
if (world.getBlock(xx + 1, yy + 1, zz) != null
|| world.getBlock(xx + 1, yy + 1, zz - 1) != null
|| world.getBlock(xx, yy + 1, zz - 1) != null)
a[1] = ao;
if (world.getBlock(xx + 1, yy + 1, zz) != null
|| world.getBlock(xx + 1, yy + 1, zz + 1) != null
|| world.getBlock(xx, yy + 1, zz + 1) != null)
a[2] = ao;
if (world.getBlock(xx - 1, yy + 1, zz) != null
|| world.getBlock(xx - 1, yy + 1, zz + 1) != null
|| world.getBlock(xx, yy + 1, zz + 1) != null)
a[3] = ao;
// if (world.getBlock(xx - 1, yy + 1, zz) != null
// || world.getBlock(xx - 1, yy + 1, zz - 1) != null
// || world.getBlock(xx, yy + 1, zz - 1) != null)
// a[0] = ao;
// if (world.getBlock(xx + 1, yy + 1, zz) != null
// || world.getBlock(xx + 1, yy + 1, zz - 1) != null
// || world.getBlock(xx, yy + 1, zz - 1) != null)
// a[1] = ao;
// if (world.getBlock(xx + 1, yy + 1, zz) != null
// || world.getBlock(xx + 1, yy + 1, zz + 1) != null
// || world.getBlock(xx, yy + 1, zz + 1) != null)
// a[2] = ao;
// if (world.getBlock(xx - 1, yy + 1, zz) != null
// || world.getBlock(xx - 1, yy + 1, zz + 1) != null
// || world.getBlock(xx, yy + 1, zz + 1) != null)
// a[3] = ao;
vbo.addDataByFloatArray(b.getDataUp(xx, yy, zz, a));
}
@ -156,22 +156,22 @@ public class Chunk {
float[] a = new float[] { 1, 1, 1, 1 };
// ambient occlusion
if (world.getBlock(xx - 1, yy - 1, zz) != null
|| world.getBlock(xx - 1, yy - 1, zz - 1) != null
|| world.getBlock(xx, yy - 1, zz - 1) != null)
a[1] = ao;
if (world.getBlock(xx + 1, yy - 1, zz) != null
|| world.getBlock(xx + 1, yy - 1, zz - 1) != null
|| world.getBlock(xx, yy - 1, zz - 1) != null)
a[0] = ao;
if (world.getBlock(xx + 1, yy - 1, zz) != null
|| world.getBlock(xx + 1, yy - 1, zz + 1) != null
|| world.getBlock(xx, yy - 1, zz + 1) != null)
a[3] = ao;
if (world.getBlock(xx - 1, yy - 1, zz) != null
|| world.getBlock(xx - 1, yy - 1, zz + 1) != null
|| world.getBlock(xx, yy - 1, zz + 1) != null)
a[2] = ao;
// if (world.getBlock(xx - 1, yy - 1, zz) != null
// || world.getBlock(xx - 1, yy - 1, zz - 1) != null
// || world.getBlock(xx, yy - 1, zz - 1) != null)
// a[1] = ao;
// if (world.getBlock(xx + 1, yy - 1, zz) != null
// || world.getBlock(xx + 1, yy - 1, zz - 1) != null
// || world.getBlock(xx, yy - 1, zz - 1) != null)
// a[0] = ao;
// if (world.getBlock(xx + 1, yy - 1, zz) != null
// || world.getBlock(xx + 1, yy - 1, zz + 1) != null
// || world.getBlock(xx, yy - 1, zz + 1) != null)
// a[3] = ao;
// if (world.getBlock(xx - 1, yy - 1, zz) != null
// || world.getBlock(xx - 1, yy - 1, zz + 1) != null
// || world.getBlock(xx, yy - 1, zz + 1) != null)
// a[2] = ao;
// affiche la face si il n'y a rien en dessous
vbo.addDataByFloatArray(b.getDataDown(xx, yy, zz, a));
@ -179,88 +179,88 @@ public class Chunk {
if (left) {
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx - 1, yy - 1, zz) != null
|| world.getBlock(xx - 1, yy - 1, zz - 1) != null
|| world.getBlock(xx - 1, yy, zz - 1) != null)
a[0] = ao;
if (world.getBlock(xx - 1, yy + 1, zz) != null
|| world.getBlock(xx - 1, yy + 1, zz - 1) != null
|| world.getBlock(xx - 1, yy, zz - 1) != null)
a[1] = ao;
if (world.getBlock(xx - 1, yy + 1, zz) != null
|| world.getBlock(xx - 1, yy + 1, zz + 1) != null
|| world.getBlock(xx - 1, yy, zz + 1) != null)
a[2] = ao;
if (world.getBlock(xx - 1, yy - 1, zz) != null
|| world.getBlock(xx - 1, yy - 1, zz + 1) != null
|| world.getBlock(xx - 1, yy, zz + 1) != null)
a[3] = ao;
// if (world.getBlock(xx - 1, yy - 1, zz) != null
// || world.getBlock(xx - 1, yy - 1, zz - 1) != null
// || world.getBlock(xx - 1, yy, zz - 1) != null)
// a[0] = ao;
// if (world.getBlock(xx - 1, yy + 1, zz) != null
// || world.getBlock(xx - 1, yy + 1, zz - 1) != null
// || world.getBlock(xx - 1, yy, zz - 1) != null)
// a[1] = ao;
// if (world.getBlock(xx - 1, yy + 1, zz) != null
// || world.getBlock(xx - 1, yy + 1, zz + 1) != null
// || world.getBlock(xx - 1, yy, zz + 1) != null)
// a[2] = ao;
// if (world.getBlock(xx - 1, yy - 1, zz) != null
// || world.getBlock(xx - 1, yy - 1, zz + 1) != null
// || world.getBlock(xx - 1, yy, zz + 1) != null)
// a[3] = ao;
vbo.addDataByFloatArray(b.getDataLeft(xx, yy, zz, a));
}
if (right) {
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx + 1, yy - 1, zz) != null
|| world.getBlock(xx + 1, yy - 1, zz - 1) != null
|| world.getBlock(xx + 1, yy, zz - 1) != null)
a[1] = ao;
if (world.getBlock(xx + 1, yy + 1, zz) != null
|| world.getBlock(xx + 1, yy + 1, zz - 1) != null
|| world.getBlock(xx + 1, yy, zz - 1) != null)
a[0] = ao;
if (world.getBlock(xx + 1, yy + 1, zz) != null
|| world.getBlock(xx + 1, yy + 1, zz + 1) != null
|| world.getBlock(xx + 1, yy, zz + 1) != null)
a[3] = ao;
if (world.getBlock(xx + 1, yy - 1, zz) != null
|| world.getBlock(xx + 1, yy - 1, zz + 1) != null
|| world.getBlock(xx + 1, yy, zz + 1) != null)
a[2] = ao;
// if (world.getBlock(xx + 1, yy - 1, zz) != null
// || world.getBlock(xx + 1, yy - 1, zz - 1) != null
// || world.getBlock(xx + 1, yy, zz - 1) != null)
// a[1] = ao;
// if (world.getBlock(xx + 1, yy + 1, zz) != null
// || world.getBlock(xx + 1, yy + 1, zz - 1) != null
// || world.getBlock(xx + 1, yy, zz - 1) != null)
// a[0] = ao;
// if (world.getBlock(xx + 1, yy + 1, zz) != null
// || world.getBlock(xx + 1, yy + 1, zz + 1) != null
// || world.getBlock(xx + 1, yy, zz + 1) != null)
// a[3] = ao;
// if (world.getBlock(xx + 1, yy - 1, zz) != null
// || world.getBlock(xx + 1, yy - 1, zz + 1) != null
// || world.getBlock(xx + 1, yy, zz + 1) != null)
// a[2] = ao;
vbo.addDataByFloatArray(b.getDataRight(xx, yy, zz, a));
}
if (front) {
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx, yy - 1, zz - 1) != null
|| world.getBlock(xx - 1, yy - 1, zz - 1) != null
|| world.getBlock(xx - 1, yy, zz - 1) != null)
a[0] = ao;
if (world.getBlock(xx, yy + 1, zz - 1) != null
|| world.getBlock(xx - 1, yy + 1, zz - 1) != null
|| world.getBlock(xx - 1, yy, zz - 1) != null)
a[3] = ao;
if (world.getBlock(xx, yy + 1, zz - 1) != null
|| world.getBlock(xx + 1, yy + 1, zz - 1) != null
|| world.getBlock(xx + 1, yy, zz - 1) != null)
a[2] = ao;
if (world.getBlock(xx, yy - 1, zz - 1) != null
|| world.getBlock(xx + 1, yy - 1, zz - 1) != null
|| world.getBlock(xx + 1, yy, zz - 1) != null)
a[1] = ao;
// if (world.getBlock(xx, yy - 1, zz - 1) != null
// || world.getBlock(xx - 1, yy - 1, zz - 1) != null
// || world.getBlock(xx - 1, yy, zz - 1) != null)
// a[0] = ao;
// if (world.getBlock(xx, yy + 1, zz - 1) != null
// || world.getBlock(xx - 1, yy + 1, zz - 1) != null
// || world.getBlock(xx - 1, yy, zz - 1) != null)
// a[3] = ao;
// if (world.getBlock(xx, yy + 1, zz - 1) != null
// || world.getBlock(xx + 1, yy + 1, zz - 1) != null
// || world.getBlock(xx + 1, yy, zz - 1) != null)
// a[2] = ao;
// if (world.getBlock(xx, yy - 1, zz - 1) != null
// || world.getBlock(xx + 1, yy - 1, zz - 1) != null
// || world.getBlock(xx + 1, yy, zz - 1) != null)
// a[1] = ao;
vbo.addDataByFloatArray(b.getDataFront(xx, yy, zz, a));
}
if (back) {
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx, yy - 1, zz + 1) != null
|| world.getBlock(xx - 1, yy - 1, zz + 1) != null
|| world.getBlock(xx - 1, yy, zz + 1) != null)
a[1] = ao;
if (world.getBlock(xx, yy + 1, zz + 1) != null
|| world.getBlock(xx - 1, yy + 1, zz + 1) != null
|| world.getBlock(xx - 1, yy, zz + 1) != null)
a[2] = ao;
if (world.getBlock(xx, yy + 1, zz + 1) != null
|| world.getBlock(xx + 1, yy + 1, zz + 1) != null
|| world.getBlock(xx + 1, yy, zz + 1) != null)
a[3] = ao;
if (world.getBlock(xx, yy - 1, zz + 1) != null
|| world.getBlock(xx + 1, yy - 1, zz + 1) != null
|| world.getBlock(xx + 1, yy, zz + 1) != null)
a[0] = ao;
// if (world.getBlock(xx, yy - 1, zz + 1) != null
// || world.getBlock(xx - 1, yy - 1, zz + 1) != null
// || world.getBlock(xx - 1, yy, zz + 1) != null)
// a[1] = ao;
// if (world.getBlock(xx, yy + 1, zz + 1) != null
// || world.getBlock(xx - 1, yy + 1, zz + 1) != null
// || world.getBlock(xx - 1, yy, zz + 1) != null)
// a[2] = ao;
// if (world.getBlock(xx, yy + 1, zz + 1) != null
// || world.getBlock(xx + 1, yy + 1, zz + 1) != null
// || world.getBlock(xx + 1, yy, zz + 1) != null)
// a[3] = ao;
// if (world.getBlock(xx, yy - 1, zz + 1) != null
// || world.getBlock(xx + 1, yy - 1, zz + 1) != null
// || world.getBlock(xx + 1, yy, zz + 1) != null)
// a[0] = ao;
vbo.addDataByFloatArray(b.getDataBack(xx, yy, zz, a));
}
@ -321,19 +321,13 @@ class Generate implements Runnable {
*/
public void run() {
// AffinityLock al = null;
// int cpuId = 0;
// try{
// al = AffinityLock.acquireLock();
// }catch(Exception e){}
long current = System.currentTimeMillis();
long elapsed1 = 0;
boolean IsError = true;
Noise noise = new Noise(world.seed, 30, 7);
Random random = new Random(world.seed);
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 xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
@ -344,32 +338,19 @@ class Generate implements Runnable {
return;
}
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.blocks[x][(int)noise.getNoise(xx, zz)][z] = Block.GRASS;
//
// float percentOfSpawnTree = 0.0005f;
// if(random.nextFloat() < percentOfSpawnTree){
// if(random.nextInt(2) == 0)
// Tree.addOak(world, xx, (int)noise.getNoise(xx, zz) - 1, zz);
// else
// Tree.addFir(world, xx, (int)noise.getNoise(xx, zz) - 1, zz);
// }
// }
}
}
elapsed1 = System.currentTimeMillis() - current;
while(IsError){
IsError = false;
try{
synchronized (world.chunks) {
// synchronized (world.chunks) {
for (int i = 0; i < chunk.SIZE; i++) {
for (int j = 0; j < chunk.SIZE; j++) {
// for (int j = 0; j < chunk.SIZE; j++) {
for (int k = 0; k < chunk.SIZE; k++) {
int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
@ -380,19 +361,21 @@ class Generate implements Runnable {
Thread.currentThread().stop();
return;
}
chunk.loopChunk(i, j, k);
int xx = chunk.getX() * chunk.SIZE + i;
int zz = chunk.getZ() * chunk.SIZE + k;
chunk.loopChunk(i, (int)noise.getNoise(xx, zz), k);
}
}
// }
}
}
// }
chunk.setGenerated(true);
}catch (Exception e){
e.printStackTrace();
IsError = true;
}
}
System.out.println(Thread.currentThread().getName() + " terminated | " + (System.currentTimeMillis()-current));
System.out.println(Thread.currentThread().getName() + " terminated | loop1:" + elapsed1 + "ms loop2:" + (System.currentTimeMillis()-current) + "ms");
Thread.currentThread().stop();
// al.release();
}
}

View file

@ -11,7 +11,7 @@ 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 = 8;
public static final int VIEW_CHUNK = 4;
public static WorldNoise worldNoise;
public ArrayList<Chunk> chunks = new ArrayList<Chunk>();
@ -30,7 +30,9 @@ public class World {
for(Chunk ch : chunks)ch.createChunk(this);
}
public static long updateWorldTime = 0;
public void update(){
long current = System.currentTimeMillis();
int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK;
int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + VIEW_CHUNK;
int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK;
@ -41,12 +43,11 @@ public class World {
int delta_z = zb - za;
for(int i = 0; i <= delta_x;i++){
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,worldNoise);
chunks.add(ch);
}
}
// for(int j = 0; j < HEIGHT; j++){
if(getChunk((xa + i), 0, (za + k)) != null)continue;
Chunk ch = new Chunk((xa + i),0,(za + k),this,worldNoise);
chunks.add(ch);
// }
}
}
ArrayList<Chunk> removeList = new ArrayList<Chunk>();
@ -55,18 +56,17 @@ public class World {
if(c.getPosition().getX() < xa || c.getPosition().getX() > xb || c.getPosition().getZ() < za || c.getPosition().getZ() > zb){
c.destroyChunk();
removeList.add(c);
}else{
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();
}
}
for(Chunk c: removeList){
removeByChunk(c);
}
removeList.clear();
for(Chunk c : chunks){
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();
removeList.clear();
updateWorldTime = System.currentTimeMillis() - current;
}
public void render(){