I recently re coded my chunk class because it badly needed it. I only got through the basics of the class before realizing it wasn’t rendering anything! I make a new VBO, store 16^3 cubes in it, then translate the VBO to its render position. Here’s the chunk class:
package com.mime.skyfox.Level;
import static org.lwjgl.opengl.GL11.GL_COLOR_ARRAY;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
import static org.lwjgl.opengl.GL11.glColorPointer;
import static org.lwjgl.opengl.GL11.glDisableClientState;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL11.glEnableClientState;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertexPointer;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL15;
import com.mime.skyfox.Blocks.Block;
public class BaseChunk {
// x - left/right y - up/down y - forward/backward
public static final int CHUNK_X = 16;
public static final int CHUNK_Y = 16;
public static final int CHUNK_Z = 16;
public boolean rebuild = false;
FloatBuffer blockPositionData;
FloatBuffer vertexColorData;
private Block[][][] blocks;
private int vboBlockData;
private int vboColorData;
private int chunkStartX;
private int chunkStartY;
private int chunkStartZ;
public BaseChunk(int x, int z, int y) {
chunkStartX = x;
chunkStartY = y;
chunkStartZ = z;
blocks = new Block[CHUNK_X][CHUNK_Z][CHUNK_Y];
for (int xx = 0; xx < CHUNK_X; xx++) {
for (int zz = 0; zz < CHUNK_Z; zz++) {
for (int yy = 0; yy < CHUNK_Y; yy++) {
blocks[xx][zz][yy] = Block.Grass;
}
}
}
}
public void rebuildChunk() {
vboBlockData = GL15.glGenBuffers();
blockPositionData = BufferUtils.createFloatBuffer((CHUNK_X * CHUNK_Z * CHUNK_Y) * 6 * 12);
vboColorData = GL15.glGenBuffers();
vertexColorData = BufferUtils.createFloatBuffer((CHUNK_X * CHUNK_Z * CHUNK_Y) * 6 * 12);
for (int xx = 0; xx < CHUNK_X; xx++) {
for (int zz = 0; zz < CHUNK_Z; zz++) {
for (int yy = 0; yy < CHUNK_Y; yy++) {
blockPositionData.put(makeCube((float) chunkStartX + xx, (float) chunkStartZ + zz, chunkStartY + yy));
vertexColorData.put(getColor(Block.Grass));
}
}
}
vertexColorData.flip();
blockPositionData.flip();
}
private float[] makeCube(float x, float z, float y) {
int offset = Block.size / 2;
return new float[] { x + offset, y + offset, z, x - offset, y + offset, z, x - offset, y + offset, z - Block.size, x + offset, y + offset, z - Block.size, x + offset, y - offset, z - Block.size, x - offset, y - offset, z - Block.size, x - offset, y - offset, z, x + offset, y - offset, z, x + offset, y + offset, z - Block.size, x - offset, y + offset, z - Block.size, x - offset, y - offset, z - Block.size, x + offset, y - offset, z - Block.size, x + offset, y - offset, z, x - offset, y - offset, z, x - offset, y + offset, z, x + offset, y + offset, z, x - offset, y + offset, z - Block.size, x - offset, y + offset, z, x - offset, y - offset, z, x - offset, y - offset, z - Block.size, x + offset, y + offset, z, x + offset, y + offset, z - Block.size, x + offset, y - offset,
z - Block.size, x + offset, y - offset, z };
}
private int getColor(Block block) {
return block.color;
}
public void render(int x, int z, int y) {
glBindBuffer(GL_ARRAY_BUFFER, vboBlockData);
glVertexPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vboColorData);
glColorPointer(3, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
for (int yy = 0; yy < y; yy++) {
for (int xx = 0; xx < x; xx++) {
for (int zz = 0; zz < z; zz++) {
glPushMatrix();
glTranslatef(xx, zz, yy);
glDrawArrays(GL_QUADS, 0, 24);
glPopMatrix();
}
}
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
}
And I call it like this:
baseChunk = new BaseChunk(16, 16, 16);
baseChunk.rebuildChunk();
baseChunk.render(16, 16, 16);
Could it have something to do with my offsets in the make cube method?