I'm trying to make a voxel engine (NOT based on an octree model). Currently I have a setup with a Chunk class, which stores the following:
int Elements[4096];
float Colors[4096 * 3];
vector<Quad> mesh;
Each of these corresponds to data for the (16x16x16) chunk; Elements
just holds an int
for the voxel being solid or not (just realized I could make this a bool, slap). Colors
holds the three RGB values corresponding to the color of each voxel. Quad
is a class I created that can generate two triangles for a quad given a direction, color, and position. To generate the mesh
, I loop over all the elements in the array (currently avoiding any of the edge faces since there's only one chunk), check around each for any empty neighbors, and add a quad to the mesh for each exposed face.
This all works fine for one chunk, and I can generate the data within using Simplex Noise. However, I want to generate more than just one chunk, I want a whole world!
To generate multiple chunks my naive solution before creating a full world class was to have a vector<Chunk> chunks;
in main
, and then to make my previous VBO an array of VBOs. I ran into a bunch of errors trying to pass multiple chunks from the vector into entries in the VBOs, and I can only get it to display one. I've looked at pretty much every site that has information on storing voxels, including this excellent blog post. I have a little trouble following along even though I think I grasp the basic concept. One big issue is that I'm not sure how to pass vertex data in a set of constant sized VBOs or one large VBO since the chunks are supposed to be changing pretty regularly (this whole thing is an attempt at 3d falling sand simulation a la Noita), and of course the meshes can be a wide range of sizes.
Overall the problem is I'm just not quite sure how to implement this, be it vertex pooling like above, a vector of chunks and passing their meshes to each VBO in an array, creating an element buffer instead and splitting color and vertex data into separate arrays, etc. I don't really know where to proceed from where I am to build this system, and I lack some of the experience required to understand some of the more advanced implementations of these methods. I also need some way for chunks to view each other's meshes to draw quads on the borders, and currently I run into an issue if a mesh has no quads; since the vector of quads is null
it throws an error and the program won't launch, which is why before mesh generation I also add a single white quad in the middle of the chunk image.
I want to get this sort of right from the start, or at least a clean enough system to work forward with optimizations in the future. Any help/advice is vastly appreciated!