Erstmal hallo zusammen, ich bin neu hier und habe schon mal eine erste Frage.
Ich will ein Voxel Spiel mit LWJGL machen. Um eine hohe performance zu erreichen habe ich vor Vertexbuffers zu verwenden. Dazu habe ich mal diesen Code geschrieben:
Hauptklasse:
Chunk klasse:
Das ganze kompiliert bei mir fehlerfrei und läuft auch ohne Warnungen. Es wird jedoch kein Block gerendert. Könnte mir vieleicht jemand einen Tipp geben was ich falsch gemacht habe und/oder wie es besser gewesen wäre?
Gruss Tiim
Ich will ein Voxel Spiel mit LWJGL machen. Um eine hohe performance zu erreichen habe ich vor Vertexbuffers zu verwenden. Dazu habe ich mal diesen Code geschrieben:
Hauptklasse:
Java:
package ch.yourcraft.game;
import ch.yourcraft.game.entity.Chunk;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
/**
*
* @author Tim
*/
public class Main {
/**
* @param args the command line arguments
*
* TODO: catch LWJGL exception.
*/
public static void main(String[] args) throws LWJGLException {
//Display
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
Keyboard.create();
Chunk ch = new Chunk();
//Init GL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
//Test Block
ch.set(1, 1, 1, (byte) 1);
//Main Loop
while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
ch.render();
Display.update();
}
//Cleanup
ch.dispose();
}
}
Java:
package ch.yourcraft.game.entity;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
/**
*
* @author Tim
*/
public class Chunk /*implements Disposable*/ {
private static final int SIZE_X = 16;
private static final int SIZE_Y = 16;
private static final int SIZE_Z = 16;
private static final int POINTS_PER_QUAD = 4; //3 = Triangles, 4 = Quads
private static final int SITES_PER_QUBE = 6; //6 = Quads, 12 = Triangles
private static final int POLYGON_MODE = GL11.GL_QUADS;
private static final byte BLOCK_AIR = 0;
private byte[][][] data = new byte[SIZE_X][SIZE_Y][SIZE_Z];
int vertexBufferObject;
int vertecies = 0;
private boolean changed = true;
public Chunk() {
vertexBufferObject = GL15.glGenBuffers();
}
/**
* Cleans up the data.
*/
/*@Override*/
public void dispose() {
GL15.glDeleteBuffers(vertexBufferObject);
data = null;
changed = false;
}
/**
* Returns the ID of a block
*
* @param x
* @param y
* @param z
* @return
*/
public byte get(int x, int y, int z) {
try {
return data[x][y][z];
} catch (ArrayIndexOutOfBoundsException ex) {
return 1;
}
}
/**
* Set the ID of a block.
*
* @param x
* @param y
* @param z
* @param id
*/
public void set(int x, int y, int z, byte id) {
data[x][y][z] = id;
changed = true;
}
/**
* Renders the chunk and updates the vbo.
*/
public void render() {
if (changed) {
update();
}
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferObject);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDrawArrays(POLYGON_MODE, 0, vertecies);
}
/**
* Updates the vertex buffer object.
*/
private void update() {
changed = false;
vertecies = 0;
IntBuffer verts = BufferUtils.createIntBuffer(SIZE_X * SIZE_Y * SIZE_Z * SITES_PER_QUBE * POINTS_PER_QUAD);
for (int x = 0; x < SIZE_X; ++x) {
for (int y = 0; y < SIZE_Y; ++y) {
for (int z = 0; z < SIZE_Z; ++z) {
if (get(x, y, z) == BLOCK_AIR) {
continue;
}
//Left
if (get(x - 1, y, z) == BLOCK_AIR) {
System.out.println("Left");
vertecies += 4;
putPoint(x, y, z, verts);
putPoint(x, y + 1, z, verts);
putPoint(x, y + 1, z + 1, verts);
putPoint(x, y, z + 1, verts);
}
//Right
if (get(x + 1, y, z) == BLOCK_AIR) {
System.out.println("Right");
vertecies += 4;
putPoint(x + 1, y, z, verts);
putPoint(x + 1, y + 1, z, verts);
putPoint(x + 1, y + 1, z + 1, verts);
putPoint(x + 1, y, z + 1, verts);
}
//Behind
if (get(x, y, z - 1) == BLOCK_AIR) {
System.out.println("Behind");
vertecies += 4;
putPoint(x, y, z, verts);
putPoint(x, y + 1, z, verts);
putPoint(x + 1, y + 1, z, verts);
putPoint(x + 1, y, z, verts);
}
//Before
if (get(x, y, z + 1) == BLOCK_AIR) {
System.out.println("Before");
vertecies += 4;
putPoint(x, y, z + 1, verts);
putPoint(x + 1, y, z + 1, verts);
putPoint(x + 1, y + 1, z + 1, verts);
putPoint(x, y + 1, z + 1, verts);
}
//Under
if (get(x, y - 1, z) == BLOCK_AIR) {
System.out.println("Under");
vertecies += 4;
putPoint(x, y, z, verts);
putPoint(x, y, z + 1, verts);
putPoint(x + 1, y, z + 1, verts);
putPoint(x + 1, y, z, verts);
}
//Over
if (get(x, y + 1, z) == BLOCK_AIR) {
System.out.println("Over");
vertecies += 4;
putPoint(x, y + 1, z, verts);
putPoint(x, y + 1, z + 1, verts);
putPoint(x + 1, y + 1, z + 1, verts);
putPoint(x + 1, y + 1, z, verts);
}
}
}
}
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferObject);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verts, GL15.GL_DYNAMIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
/**
* Adds a vertex to a IntBuffer.
*
* @param x
* @param y
* @param z
* @param verts the IntBuffer
*/
private static void putPoint(int x, int y, int z, IntBuffer verts) {
verts.put(x);
verts.put(y);
verts.put(z);
}
}
Das ganze kompiliert bei mir fehlerfrei und läuft auch ohne Warnungen. Es wird jedoch kein Block gerendert. Könnte mir vieleicht jemand einen Tipp geben was ich falsch gemacht habe und/oder wie es besser gewesen wäre?
Gruss Tiim