I'm trying to create a Minecraft look-alike with opengl and have gotten to the point where I can draw a "chunk" of cubes clustered together. However, I can't seem to find a good method for removing all the unseen faces from inside the cluster of cubes (a 16x16x16 cube of cubes).
I've created the basic infrastructure for a single cube (with all face coordinates separated from each other), then copy all the cube information and draw it with glm::translate and drawVertexArrays.
e.g.: for the back face
float cubeMapTest1[] =
{
// Back face
-0.5f, -0.5, -0.5f, 0.0f, 0.0f, // Bottom-left
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // bottom-right
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left
};
//...
unsigned int VBOs[6], VAOs[6];
glGenVertexArrays(6, VAOs);
glGenBuffers(6, VBOs);
glBindVertexArray(VAOs[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeMapTest1), cubeMapTest1, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
//...
//^^^repeat for all 6 faces^^^
//then create a cube of values from 0 to 16^3 to be drawn to later
std::vector<glm::vec3> cubePositions;
int i = 0;
int chunkSize = 16;
int chunkVolume = chunkSize * chunkSize * chunkSize;
for (size_t j = 0; j < chunkSize; j++)
{
for (size_t k = 0; k < chunkSize; k++)
{
for (size_t h = 0; h < chunkSize; h++)
{
i++;
//order of h j k affect layers
cubePositions.resize(i + 1);
cubePositions[i] = { h, j, k };
}
}
}
//start while loop
//draw
//...
With this, should I be using normals to determine which inner faces to "cull"? What's the best culling method given my goal? I know it's messy and my question is kinda vague, but I don't know where to start with this problem. I'll optimize it later with instancing and indices as well.