OpenGL Converting from busy vertex buffer to element buffer

Viewed 54

If you look at my history you can see some more context for what I'm trying to achieve.

I'm trying to draw multiple cubes with an element buffer without using instancing but for the sake of simplicity i'll use quads drawn with triangles.

If I have a VBO my array would look like below:

float cubeVertices[] = {
        //FRONT
        -0.5f, 0.5f, 0.5f, //front top left
        -0.5f, -0.5f, 0.5f, //front bottom left
        0.5f, 0.5f, 0.5f, //front top right

        0.5f, 0.5f, 0.5f, //front top right
        -0.5f, -0.5f, 0.5f, //front bottom left
        0.5f, -0.5f, 0.5f //front bottom right
    };

And converted to an element buffer it looks like:

float cubeVertices[] = {
        -0.5f, 0.5f, 0.5f, //front top left
        0.5f, 0.5f, 0.5f, //front top right
        -0.5f, -0.5f, 0.5f, //front bottom left
        0.5f, -0.5f, 0.5f //front bottom right
    };
    
    unsigned int cubeIndices[] = {
        0, 2, 1, //FRONT
        1, 2, 3
    };

But what do I do if I want to draw multiple? (WITHOUT INSTANCING)

For a VBO it seems simple enough

float cubeVertices[] = {
        //FRONT            translate(x, y, z)
        -0.5f, 0.5f, 0.5f, 0, 0, 0,//front top left
        -0.5f, -0.5f, 0.5f, 0, 0, 0,//front bottom left
        0.5f, 0.5f, 0.5f, 0, 0, 0,//front top right

        0.5f, 0.5f, 0.5f, 0, 0, 0,//front top right
        -0.5f, -0.5f, 0.5f, 0, 0, 0,//front bottom left
        0.5f, -0.5f, 0.5f, 0, 0, 0,//front bottom right

        //FRONT            translate(x, y, z)
        -0.5f, 0.5f, 0.5f, 1, 1, 1,//front top left
        -0.5f, -0.5f, 0.5f, 1, 1, 1,//front bottom left
        0.5f, 0.5f, 0.5f, 1, 1, 1,//front top right

        0.5f, 0.5f, 0.5f, 1, 1, 1,//front top right
        -0.5f, -0.5f, 0.5f, 1, 1, 1,//front bottom left
        0.5f, -0.5f, 0.5f, 1, 1, 1,//front bottom right
    };

Where each face is moved by the translate of (x y z) like model = glm::translate(model, glm::vec3(x, y, z)) But this has a lot of duplicate data like repeating the translation 6 times per quad and writing the vertex drawn locations twice per quad and an extra 2 duplicate vertices per quad. Now imagine that for more advanced models and it seems like the waste of memory would get quite large.

So how do I convert the above to an element buffer? Ideally without duplicating data obviously. I'll write some code/pseudo code to help explain what I'm after.

float cubeVertices[] = {
        -0.5f, 0.5f, 0.5f, //front top left
        0.5f, 0.5f, 0.5f, //front top right
        -0.5f, -0.5f, 0.5f, //front bottom left
        0.5f, -0.5f, 0.5f //front bottom right
    };
    
    unsigned int cubeIndices[] = {
        0, 2, 1, //FRONT
        1, 2, 3
    };
    
    unsigned int cubeLocations[] = {
        0, 0, 0,
        1, 1, 1
    };

for (int loc = 0; loc < 2; ++loc)
{
    for (int i = 0; i < 6; ++i)
    {
             //VERTEX COORDS             //MODEL LOCATION
        Draw(cubeVertices[cubeIndices[i]], cubeLocations[loc]);
    }
}

**But done in one draw call, e.g. a simple mesh (Not instancing)**

The above would generates a memory efficient version of the VBO as an element array Not sure how well I've articulated what I'm after is but I've tried my best to provide an explanation of my goal.

Think of a basic minecraft clone where all the unneeded drawn faces really starts to add up and instancing no longer works so meshing is now required.

0 Answers
Related