Lets say I want to load a Map that is 20x20 chunks, each chunk is 10x10x2 blocks, that is a lot of blocks (80,000). So, in a small map, I would just initiate all of the chunks before the game starts, but now, I am interested in more efficient ways. I have tried to only initiate the chunks within a 2x2 area of the player, but RAM keeps stacking (from ~52mb to ~250mb) by the time I trek the whole map. Here is some code I have tried to implement. If anyone can give me a better suggestion, please do so.
private void setView() {
int lowX = (getChunk((int) position.x, (int) position.z)[0]) - 2;
int highX = (getChunk((int) position.x, (int) position.z)[0]) + 2;
int lowY = (getChunk((int) position.x, (int) position.z)[1]) - 2;
int highY = (getChunk((int) position.x, (int) position.z)[1]) + 2;
if (lowX < 0) {
lowX = 0;
}
if (highX > width) {
highX = width;
}
if (lowY < 0) {
lowY = 0;
}
if (highY > height) {
highY = height;
}
if (view[0] != lowX || view[1] != highX || view[2] != lowY || view[3] != highY) {
view[0] = lowX;
view[1] = highX;
view[2] = lowY;
view[3] = highY;
loadChunks();
}
}
private void loadChunks() {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (x >= view[0] && x <= view[1] && y >= view[2] && y <= view[3]) {
chunk[x][y] = new Chunk(x, y);
chunk[x][y].setTexAmount(4);
} else {
chunk[x][y] = null;
}
}
}
}
private void renderChunks() {
int lowX = view[0];
int highX = view[1];
int lowY = view[2];
int highY = view[3];
for (int x = lowX; x < highX; x++) {
for (int y = lowY; y < highY; y++) {
chunk[x][y].renderChunk();
}
}
}
If it matters, I am using Vertex Buffer Objects for all of my 3D rendering.
Also, a sub question: Would it be worth the code for me to convert my 2D HUD from immediate mode to VBO?