Performance and VBO Chunks

Firstly, my code is in no way optimized. I also use the old glTranslatf () and all this because I donโ€™t know a new way to do something. Here is my code:

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); } 

}

And the VBO class:

 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); } 

} I put out two pieces, so about 8000 cubes. I have heard of people receiving 10,000 cubes and supporting 60 FPS, but when I do two pieces, I get 1 FPS. My equipment is not a problem, it's decent. I know that I need to optimize my code like crazy, but I worry that even with optimization it will still be slow! Can someone tell me what I'm doing wrong here?

+4
source share
1 answer

Using VBO with one cube in it and glTranslate() around it will not help your performance at all.

VBO for this fragment should contain several cubes (from 16 3 to 2048 for the worst volume of a chessboard). You should only release glTranslate() at the piece level, and not in the cube.

EDIT:

You have a volume database, usually stored in a three-dimensional array. Each element of the array is a class / struct (or even just int or char !)), Which contains information about the type of block (grass, dirt, stone, etc.) and another state. You also need a function like bool IsOpaque( const BlockType& block) , which can tell you whether a given block is graphically opaque or not.

Iterate over all the blocks in this piece. If the block IsOpaque() checks its six neighbors (ยฑ X, ยฑ Y, ยฑ Z). If the neighbor !IsOpaque() generates vertices for two triangles (or one quad) for this side of the cube (translated accordingly ( not via glTranslatef() !), Depending on the position in the piece) and add them to the buffer.

When you're done sorting through the piece, load the vertex buffer into VBO.

Or you can get a fancier .

+7
source

All Articles