1
0
Fork 0

Upgrade performance

This commit is contained in:
MrDev023 2015-07-17 23:36:42 +02:00
parent f81044a2ba
commit 64b1ab1c6d
8 changed files with 335 additions and 43 deletions

View file

@ -3,8 +3,10 @@
layout (location = 0) in vec3 in_position;
out vec3 position;
out vec3 rotation;
void main(void){
rotation = vec3(0,0,0);
position = gl_Vertex.xyz;
gl_Position = ftransform();
}

View file

@ -38,7 +38,6 @@ public class DisplayManager {
* @Info Fait le rendu 3d
*/
public static void render3D(){
Camera.renderCamera();
Main.getGame().render();
}

View file

@ -1,5 +1,7 @@
package main;
import rendering.*;
import java.util.concurrent.*;
import org.lwjgl.input.*;
@ -21,26 +23,38 @@ public class Main {
private static final int width = 1280, height = 720;
private static AffinityLock al;
// private static AffinityLock al;
public static ExecutorService mainPool;
private static Game game;
private static SkyBox skybox;
/**
* @param args
* @Info Fonction principal
*/
public static void main(String[] args) {
// mainPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
// mainPool = ForkJoinPool.commonPool();
mainPool = Executors.newWorkStealingPool();
// AffinityLock.cpuLayout(new NoCpuLayout(Runtime.getRuntime().availableProcessors() + 8));
al = AffinityLock.acquireLock();
System.out.println(AffinityLock.cpuLayout().coresPerSocket());
// 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();
String back = "/tex/cubemap/back.jpg";
String bottom = "/tex/cubemap/bottom.jpg";
String front = "/tex/cubemap/front.jpg";
String top = "/tex/cubemap/top.jpg";
String left = "/tex/cubemap/left.jpg";
String right = "/tex/cubemap/right.jpg";
skybox = new SkyBox(new String[] { right, left, top, bottom, back,
front });
game = new Game();
Mouse.setGrabbed(true);
loop();
} catch (Exception e) {
@ -48,8 +62,9 @@ public class Main {
}
public static Runnable addThread(Runnable t,String name){
mainPool.submit(t);
// mainPool.execute(t);
new Thread(t, name).start();
// new Thread(t, name).start();
// System.out.println("Details" + AffinityLock.dumpLocks());
return t;
}
@ -78,8 +93,11 @@ public class Main {
timeTicks = System.currentTimeMillis() - current;
} else {
DisplayManager.clearScreen();
Shader.MAIN.bind();
DisplayManager.preRender3D();
Camera.renderCamera();
Shader.SKYBOX.bind();
skybox.render(Camera.getPosition());
Shader.MAIN.bind();
DisplayManager.render3D();
DisplayManager.preRender2D();
DisplayManager.render2D();
@ -95,7 +113,8 @@ public class Main {
+ timeTicks + "ms" + " | PX:"
+ Camera.getPosition().getX() + " PY:"
+ Camera.getPosition().getY() + " PZ:"
+ Camera.getPosition().getZ());
+ Camera.getPosition().getZ() + " | "
+ mainPool);
FPS = 0;
TICKS = 0;
elapsedInfo = 0;

View file

@ -0,0 +1,147 @@
package rendering;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.imageio.ImageIO;
import math.Vector3f;
import org.lwjgl.BufferUtils;
public class SkyBox {
private int vbo;
private FloatBuffer buffer;
private int textureID;
public SkyBox(String[] img){
vbo = glGenBuffers();
buffer = BufferUtils.createFloatBuffer(3 * 4 * 6);
buffer.put(blockData());
buffer.flip();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
createCubeMap(img);
}
private void createCubeMap(String[] img){
textureID = glGenTextures();
// glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
for(int i = 0; i < img.length; i++){
TextureData data = decode(img[i]);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0 , GL_RGBA,data.width, data.height,0,GL_RGBA,GL_UNSIGNED_BYTE,data.buffer);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
private TextureData decode(String path){
int[] pixels = null;
int width = 0,height = 0;
try{
BufferedImage image = ImageIO.read(SkyBox.class.getResource(path));
width = image.getWidth();
height = image.getHeight();
pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
}catch(IOException e){
e.printStackTrace();
}
int[] data = new int[pixels.length];
for(int i = 0; i < pixels.length; i++){
int a = (pixels[i] & 0xff000000) >> 24;
int r = (pixels[i] & 0xff0000) >> 16;
int g = (pixels[i] & 0xff00) >> 8;
int b = (pixels[i] & 0xff);
data[i] = a << 24 | b << 16 | g << 8 | r;
}
IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
buffer.put(data).flip();
return new TextureData(glGenTextures(),width,height,buffer);
}
public void render(Vector3f pos){
glPushMatrix();
// glLoadIdentity();
glTranslatef(pos.x, pos.y + 1.75f, pos.z);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3,GL_FLOAT, false, 3*4, 0);
glDrawArrays(GL_QUADS, 0, 4 * 6);
glDisableVertexAttribArray(0);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
glPopMatrix();
}
private float[] blockData(){
int size = 500;
return new float[] {
+size,-size,-size,
-size,-size,-size,
-size,+size,-size,
+size,+size,-size,
-size,-size,+size,
+size,-size,+size,
+size,+size,+size,
-size,+size,+size,
-size,-size,-size,
+size,-size,-size,
+size,-size,+size,
-size,-size,+size,
+size,+size,-size,
-size,+size,-size,
-size,+size,+size,
+size,+size,+size,
-size,+size,-size,
-size,-size,-size,
-size,-size,+size,
-size,+size,+size,
+size,-size,-size,
+size,+size,-size,
+size,+size,+size,
+size,-size,+size
};
}
class TextureData{
public TextureData(int id,int width, int height, IntBuffer buffer) {
this.id = id;
this.width = width;
this.height = height;
this.buffer = buffer;
}
public int id;
public int width, height;
public IntBuffer buffer;
}
}

View file

@ -1,6 +1,10 @@
package world;
import java.util.*;
import java.util.concurrent.*;
import vanilla.java.affinity.*;
import world.trees.*;
import blocks.*;
import main.*;
import math.*;
@ -86,6 +90,12 @@ public class Chunk {
return blocks[x][y][z];
}
public void addBlock(int x, int y, int z,Block b) {
if (x < 0 || y < 0 || z < 0 || x >= SIZE || y >= SIZE || z >= SIZE)
return;
blocks[x][y][z] = b;
}
public void destroyChunk() {
vbo.destroyVBO();
IsDestroy = false;
@ -95,12 +105,18 @@ public class Chunk {
int xx = this.x * SIZE + x;
int yy = this.y * SIZE + y;
int zz = this.z * SIZE + z;
boolean up = world.getBlock(xx, yy + 1, zz) == null;
boolean down = world.getBlock(xx, yy - 1, zz) == null;
boolean left = world.getBlock(xx - 1, yy, zz) == null;
boolean right = world.getBlock(xx + 1, yy, zz) == null;
boolean front = world.getBlock(xx, yy, zz - 1) == null;
boolean back = world.getBlock(xx, yy, zz + 1) == null;
boolean up = true;
boolean down = true;
boolean left = true;
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;
if (!up && !down && !left && !right && !front && !back)
return;
if (blocks[x][y][z] == null)
@ -112,12 +128,6 @@ 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
@ -311,26 +321,47 @@ class Generate implements Runnable {
*/
public void run() {
AffinityLock al = null;
int cpuId = 0;
try{
al = AffinityLock.acquireCore();
}catch(Exception e){}
// AffinityLock al = null;
// int cpuId = 0;
// try{
// al = AffinityLock.acquireLock();
// }catch(Exception e){}
long current = System.currentTimeMillis();
boolean IsError = true;
Noise noise = new Noise(world.seed, 20, 5);
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 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;
// 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;
int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
if(chunk.getX() < xa || chunk.getX() > xb || chunk.getZ() < za || chunk.getZ() > zb ){
System.out.println(Thread.currentThread().getName() + " stopped | " + (System.currentTimeMillis()-current));
Thread.currentThread().stop();
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);
// }
// }
}
}
while(IsError){
@ -340,6 +371,15 @@ class Generate implements Runnable {
for (int i = 0; i < chunk.SIZE; i++) {
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;
int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
if(chunk.getX() < xa || chunk.getX() > xb || chunk.getZ() < za || chunk.getZ() > zb ){
System.out.println(Thread.currentThread().getName() + " stopped | " + (System.currentTimeMillis()-current));
Thread.currentThread().stop();
return;
}
chunk.loopChunk(i, j, k);
}
}
@ -350,9 +390,9 @@ class Generate implements Runnable {
IsError = true;
}
}
System.out.println(Thread.currentThread().getName() + " | " + (System.currentTimeMillis()-current) + " | " + cpuId);
System.out.println(Thread.currentThread().getName() + " terminated | " + (System.currentTimeMillis()-current));
Thread.currentThread().stop();
al.release();
// al.release();
}
}

View file

@ -20,10 +20,23 @@ public class Noise {
}
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;
int xmin = 0,xmax = 0,zmin = 0,zmax = 0;
if(x<0){
xmin = (int)(double)x/octave;
xmax = (int) xmin - 1;
}else{
xmin = (int)(double)x/octave;
xmax = (int) xmin + 1;
}
if(z<0){
zmin = (int)(double)z/octave;
zmax = (int) zmin - 1;
}else{
zmin = (int)(double)z/octave;
zmax = (int) zmin + 1;
}
Vector2f a = new Vector2f(xmin,zmin);
Vector2f b = new Vector2f(xmax,zmin);

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 = 2;
public static final int VIEW_CHUNK = 8;
public static WorldNoise worldNoise;
public ArrayList<Chunk> chunks = new ArrayList<Chunk>();
@ -96,6 +96,21 @@ public class World {
}
return c;
}
public void addBlock(int x,int y,int z,Block b){
int xc = (x / Chunk.SIZE);
int zc = (z / Chunk.SIZE);
int yc = (y / Chunk.SIZE);
Chunk chunk = getChunk(xc, yc, zc);
if(chunk == null)return;
int xb = x % Chunk.SIZE;
int yb = y % Chunk.SIZE;
int zb = z % Chunk.SIZE;
chunk.addBlock(xb, yb, zb, b);
}
public Block getBlock(int x, int y, int z) {
int xc = (x / Chunk.SIZE);

View file

@ -0,0 +1,57 @@
package world.trees;
import world.*;
import blocks.*;
public class Tree {
public static void addOak(World world, int x, int y, int z) {
int treeHeight = 9;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
float ii = i - 4.5f;
float jj = j - 4.5f;
float kk = k - 4.5f;
float l = (float) Math.sqrt(ii * ii + jj * jj + kk * kk);
if (l < 4.5f) {
world.addBlock(x + (int) ii, y + (int) jj + treeHeight,
z + (int) kk, Block.LEAF);
}
}
}
}
for (int i = 0; i < treeHeight; i++) {
world.addBlock(x, y + i, z, Block.OAK_WOOD);
}
}
public static void addFir(World world, int x, int y, int z) {
int treeHeight = 13;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 13; j++) {
for (int k = 0; k < 9; k++) {
float ii = i - 4f;
float jj = j - 3f;
float kk = k - 4f;
float l = (float) Math.sqrt(ii * ii + kk * kk);
float size = 1;
size -= (float)j /13.0f;
if(l < 4.5f * size){
world.addBlock(x + (int) ii, y + (int) jj + 7, z
+ (int) kk, Block.FIR_LEAF);
}
}
}
}
for (int i = 0; i < treeHeight; i++) {
world.addBlock(x, y + i, z, Block.FIR_WOOD);
}
}
}