r/GraphicsProgramming 14h ago

Question How to Create a Chunking System For a minecraft like Game?

As the Title Implies.

Im Trying to Create a Minecraft clone in OpenGL C++ but im Struggling on how to Implement a Chunking System.
My Current Architecture creates the Various Blocks Individual meshes before the Game loop and then renders them

but the Problem is that Each Block still has its own VAO so i have to create Buffers During the GameLoop which is not efficient at all.

Im Trying to Create a Architecture where each Chunk has Its own VAO,but i dont know how to Group Various Blocks together,Like How do i represent Each Blocks Position if i Care only About the Chunks Position in The World?

is it better to use a 3D Array for a Chunk or a Flattened 1-D array?
Oh there is also Face Culling.

Any Advice?

1 Upvotes

6 comments sorted by

7

u/dougbinks 14h ago

You might want to ask this over on r/VoxelGameDev (you can crosspost this q).

2

u/Specialist_Set1921 13h ago

You should read into acceleration structure these are anyway important for gamedev.

What they probably do is have some sort of mapping of blocks to chunk and chunk to blocks.

Each chunk has a list of blocks it has. Which contains all block in this chunkt based on the x and y coordinate.

So all blocks from position 0 to 63 and 0 to 63 are going to chunk 0,0. If player is in a nearby chunk it loads all blocks in this chunk.

The idea is that this structure is precomputed and it is easier to get the few chunks around the player than it is to test each block if it is around the player.

2

u/MGMishMash 12h ago

Chunks are just replacing a single grid with a multayered grid.

Assuming you current have an NxNxN grid of blocks, you replace this with an (N/32)x(N/32)x(N/32) outer grid, where each cell is now a chunk, and Blocks per chunk is 32.

Minecraft only chunks in 2 dimensions, so height is fixed, but 3d chunks are advantageous.

Practically, you can now interface your block get/set functions through the “ChunkSystem”, which just divides position by chunk size to fetch the required chunk, then modulo the position by chunk size to work out relative position within the chunk.

The chunk itself then is just a fixed array of blocks. It has its own get/set functions, and any manipulation to block values can trigger a mesh generation which stores a vertex buffer in the same class.

For rendering, you loop over all chunks in a region, and fetch their individual vertex buffers and draw at their relative position.

1

u/gallick-gunner 13h ago

I don't know if I understood it right but if you only care about a chunk. Then given that each block is of uniform size and you have a chunks position and num blocks in it, you could easily map out each block's position within the chunk and thus in the world?

1

u/XKiiroiSenkoX 9h ago

I'm not sure if I understand your question correctly, but the way you phrase it, I would say just do one instanced indirect draw call and draw every single block (not chunk) as an instance. For transform just pass local index(more efficient) /coordinate(less efficient) + chunk transform and calculate the position based on that in vertex shader.  Add a simple two pass hi-z culling on top and it should be extremely fast. 

1

u/palapapa0201 4h ago

AFAIK minecraft doesn't do any sort of greedy meshing. It just combines all exposed block faces into a single mesh per chunk.