-1

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

1 Answers1

1

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

Johann
  • 3
  • 3
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • For your 4 triangle example I see how that could work if the VBO was setup to draw one 4 triangle shape and merge that VBO's triangles, however my problem is that my index array can only modify which vertices will be drawn from my vertex array (which a cube that is already reduced to the fewest number of triangles possible), and my vertex array will only draw 1 cube at a time, so I don't see how this could merge triangles from adjacent cubes. Thank you for your help. –  Apr 22 '15 at 16:22
  • 1
    @user4743748: Using only one cube at a time is highly inefficient. You want your triangle/drawing calls ratio to be over 1000. You should pack as many cubes as possible into a single VBO. – datenwolf Apr 22 '15 at 16:55
  • Ohh.. so right now in order to draw a "chunk" of cubes I have each cube drawn individually by the same cube VBO, but instead I should make a VBO that contains all of the vertices of all of the cubes within a "chunk" of cubes (1,000's of vertices) and then update my index buffer depending on which faces I want to be rendered from that very large VBO? –  Apr 22 '15 at 17:04
  • @user4743748: Exactly. One VBO with lots of cube and an element index buffer updated according what you want to be displayed. – datenwolf Apr 22 '15 at 17:58