openglmergegreedyvoxel

OpenGL greedy meshing using VBO


I am trying to reduce the number of faces that are rendered in my voxel engine by implementing a greedy meshing algorithm similar to Mikola's (http://0fps.net/2012/06/30/meshing-in-a-minecraft-game/)

Since I am using a VBO to draw each triangle for every cube I am wondering how I will be able to merge the triangles of adjacent cubes?

For reference here is what my cube vertex array looks like (note that it is interleaved to allow for texture coordinates to be included):

GLfloat cubeInterleaved[] =
{
    // Front face       u  v
    -0.5f, -0.5f, 0.5f, 0, 0,
    0.5f, -0.5f, 0.5f, 1, 0,
    0.5f, 0.5f, 0.5f, 1, 1,
    -0.5f, 0.5f, 0.5f, 0, 1,
    // Right face
    0.5f, -0.5f, 0.5f, 0, 0,
    0.5f, -0.5f, -0.5f, 1, 0,
    0.5f, 0.5f, -0.5f, 1, 1,
    0.5f, 0.5f, 0.5f, 0, 1,
    // Back face
    0.5f, -0.5f, -0.5f, 0, 0,
    -0.5f, -0.5f, -0.5f, 1, 0,
    -0.5f, 0.5f, -0.5f, 1, 1,
    0.5f, 0.5f, -0.5f, 0, 1,
    // Left face
    -0.5f, -0.5f, -0.5f, 0, 0,
    -0.5f, -0.5f, 0.5f, 1, 0,
    -0.5f, 0.5f, 0.5f, 1, 1,
    -0.5f, 0.5f, -0.5f, 0, 1,
    // Top Face
    -0.5f, 0.5f, 0.5f, 0, 0,
    0.5f, 0.5f, 0.5f, 1, 0,
    0.5f, 0.5f, -0.5f, 1, 1,
    -0.5f, 0.5f, -0.5f, 0, 1,
    // Bottom Face
    0.5f, -0.5f, 0.5f, 0, 0,
    -0.5f, -0.5f, 0.5f, 1, 0,
    -0.5f, -0.5f, -0.5f, 1, 1,
    0.5f, -0.5f, -0.5f, 0, 1
};

Solution

  • Since I am using a VBO to draw each triangle for every cube I am confused as to how I will be able to merge the triangles of adjacent cubes.

    By using only a subset of the vertices, which is done by properly populating the index buffer for drawing.

    Take this set of 4 triangles for example

          5
        /   \
       3 --- 4
     /   \  /  \
    0 --- 1 --- 2
    

    You can either draw

    GL_TRIANGLES: 0 1 3 1 2 4 1 4 3 3 4 5
    

    or if all the vertices are coplanar you can just use the outer edges

    GL_TRIANGLES: 0 2 5
    

    And you can do the same with the vertices of blocks forming your world by appropriately populating the index buffer passed to glDrawElements