Hey all, I am writing a voxel engine to learn a bit of opengl and 3d programming. I’m am stumped on what is the best way to render chunks around the players location. Right now, my chunk manager class will only render a set amount chunks at 0,0,0 outwards. What I want to do is have a middle chunks on the players location and render chunks around the player.
This is my current ChunkManager class.
public class ChunkManager {
private ArrayList<Chunk> chunks;
private int CHUNKS_LOADED_PER_FRAME = 3;
private int renderedChunks = 0, numChunks = 0;
private boolean chunksInitiated = false;
private Vector3f playerPosition;
SimplexNoise simplexNoise = new SimplexNoise(100, 0.05, 5000);
public ChunkManager() {
chunks = new ArrayList<Chunk>();
}
public void update(Vector3f playerPosition) {
this.playerPosition = playerPosition;
if(!chunksInitiated)
initChunks();
loadChunks();
createChunks();
renderChunks();
}
public void initChunks() {
chunks.add(new Chunk(-playerPosition.getX(), -playerPosition.getZ(), 0, 0, simplexNoise));
for (int x = 0; x < Constants.VIEW_DISTANCE; x++) {
for (int z = 0; z < Constants.VIEW_DISTANCE; z++) {
chunks.add(new Chunk(playerPosition.getX(), playerPosition.getZ(), x, z, simplexNoise));
}
}
chunksInitiated=true;
}
public void loadChunks() {
int chunksCreated = 0;
for(Chunk chunk : chunks) {
if (!chunk.isChunkLoaded() && chunksCreated < CHUNKS_LOADED_PER_FRAME) {
chunk.createBlocks();
chunk.setChunkLoaded(true);
chunksCreated++;
}
}
}
public void createChunks() {
int chunksCreated = 0;
for(Chunk chunk : chunks) {
if (!chunk.isChunkCreated() && chunk.isChunkLoaded() && chunksCreated < CHUNKS_LOADED_PER_FRAME) {
chunk.createChunk();
chunk.setChunkCreated(true);
chunksCreated++;
}
}
}
public void renderChunks() {
for(Chunk chunk : chunks) {
if (chunk.isChunkCreated()) {
chunk.drawChunk();
numChunks++;
}
}
renderedChunks = numChunks;
numChunks = 0;
}
public int getNumberOfChunks() {
return renderedChunks;
}
}
I am very much a beginner at this, a push in the right direction on how to go about achieving what I want would be great.
Cheers.