首先,我的代码没有经过优化。我也在使用旧的 glTranslatf() 和所有这些,因为我不知道新的做事方式。这是我的代码:
public class GenChunk {
Chunk c;
VBO vbo = new VBO();
int worldSize = 16;
int var4 = 16; // x
int var5 = 16; // z
int var6 = 16; // y
int xOffSet = 0;
int zOffSet = 0;
public GenChunk() {
gen();
}
public void gen() {
for (int x = xOffSet; x < worldSize; x++) {
for (int z = zOffSet; z < worldSize; z++) {
for (int y = 0; y < worldSize; y++) {
glPushMatrix();
glTranslatef(x, z, y);
newChunk();
glPopMatrix();
xOffSet += 16;
zOffSet += 16;
}
}
}
}
private void newChunk() {
vbo = new VBO();
glBindBuffer(GL_ARRAY_BUFFER, vbo.vboVHandle);
glVertexPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vbo.vboCHandle);
glColorPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
for (int y = 0; y < var6; y++) {
for (int x = 0; x < var5; x++) {
for (int z = 0; z < var6; z++) {
glPushMatrix();
glTranslatef(x, z, y);
glDrawArrays(GL_QUADS, 0, 24);
glPopMatrix();
}
}
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
}
和 VBO 类:
public class VBO {
public int vboVHandle;
public int vboCHandle;
public float size = 0.5f;
public float color = 0.5f;
public FloatBuffer vertices;
public FloatBuffer colorData;
public VBO(){
colorData = BufferUtils.createFloatBuffer(3 * 4 * 6);
colorData.put(new float[]{
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
color, color, color,
-color, color, color,
-color, color, color
});
colorData.flip();
vertices = BufferUtils.createFloatBuffer(3 * 4 * 6);
vertices.put(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});
vertices.flip();
vboVHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVHandle);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vboCHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboCHandle);
glBufferData(GL_ARRAY_BUFFER, colorData, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
我渲染出两个块,大约 8000 个立方体。我听说有人渲染 10,000 个立方体并仍然保持 60 FPS,但是当我渲染两个块时,我得到 1 FPS。我的硬件不是问题,它还不错。我知道我需要疯狂地优化我的代码,但我担心即使进行了优化,它仍然会很慢!谁能告诉我我在这里做错了什么?