C++ - Buffer combining adding extra empty values

2019-08-29 09:58发布

问题:

I am trying to fill two buffers, an index buffer object and a vertex buffer object in C++.

// Create the IBO and VBO data
GLushort* iboData = new GLushort[polyProcessed * 3];
Vertex* vboData = new Vertex[vertProcessed];

int iboPos = 0;
int vboPos = 0;

// Create the VBO and IBO
for(int i = 0; i < fragMap[0x36]; i++)
{
    // Copy the data to the IBO
    memcpy(iboData + iboPos, zmeshes[i].indices, zmeshes[i].numPoly * 3 * sizeof(GLushort));//sizeof(*zmeshes[i].indices));

    // Advance the position
    iboPos += zmeshes[i].numPoly * 3 * sizeof(GLshort);

    // Copy the data to the VBO
    memcpy(vboData + vboPos, zmeshes[i].vertices, zmeshes[i].numVert * sizeof(Vertex));//sizeof(*zmeshes[i].vertices));

    // Advance the position
    vboPos += zmeshes[i].numVert * sizeof(Vertex);

    errorLog.writeSuccess("Build log: VBO size %i VBO pos %i IBO size %i IBO pos %i", zmeshes[i].numVert * sizeof(Vertex), 
                          vboPos, zmeshes[i].numPoly * 3 * sizeof(GLshort), iboPos);
}

It's simply getting the size of the data to be copied, copying it, and then advancing the position (where to put the next data in the buffer).

I get an output like this:

<---> Index dump: 0
<---> Index dump: 1
<---> Index dump: 2
<---> Index dump: 2
<---> Index dump: 3
<---> Index dump: 0
<---> Index dump: 4
<---> Index dump: 5
<---> Index dump: 23
<---> Index dump: 23
<---> Index dump: 22
<---> Index dump: 4
<---> Index dump: 26
<---> Index dump: 6
<---> Index dump: 7
<---> Index dump: 7
<---> Index dump: 8
<---> Index dump: 26
<---> Index dump: 9
<---> Index dump: 34
<---> Index dump: 10
<---> Index dump: 10
<---> Index dump: 11
<---> Index dump: 9
<---> Index dump: 12
<---> Index dump: 25
<---> Index dump: 13
<---> Index dump: 13
<---> Index dump: 14
<---> Index dump: 12
<---> Index dump: 13
<---> Index dump: 25
<---> Index dump: 31
<---> Index dump: 31
<---> Index dump: 15
<---> Index dump: 13
<---> Index dump: 28
<---> Index dump: 33
<---> Index dump: 16
<---> Index dump: 36
<---> Index dump: 33
<---> Index dump: 35
<---> Index dump: 31
<---> Index dump: 24
<---> Index dump: 15
<---> Index dump: 17
<---> Index dump: 25
<---> Index dump: 18
<---> Index dump: 18
<---> Index dump: 25
<---> Index dump: 12
<---> Index dump: 27
<---> Index dump: 34
<---> Index dump: 9
<---> Index dump: 2
<---> Index dump: 1
<---> Index dump: 19
<---> Index dump: 30
<---> Index dump: 32
<---> Index dump: 20
<---> Index dump: 29
<---> Index dump: 25
<---> Index dump: 17
<---> Index dump: 17
<---> Index dump: 21
<---> Index dump: 29
<---> Index dump: 32
<---> Index dump: 26
<---> Index dump: 8
<---> Index dump: 8
<---> Index dump: 20
<---> Index dump: 32
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0
<---> Index dump: 0

This can be seen as the correct 72 values being copied in and 72 0's being copied in behind it. Did I do something wrong in the copying of my buffer or does this indicate a problem elsewhere?

A further explanation:

Mesh 1 - 72 indices Mesh 2 - 300 indices Mesh 3 - 45 indices.

Using the function above, it produces a buffer that does this:

[0]-[71] - Mesh 1 vertices all correct [72]-[142] - Empty [142]-[EndofBuffer] - Mesh 2

It creates duplicate sizes filled with 0's.

回答1:

Since iboData is a GLushort*, then iboData+iboPos actually points to the memory location (int)iboData + iboPos * sizeof(GLushort). You're confusing byte offsets with array offsets. In your two += statements, don't multiply by the sizeof you used in memcpy, as iboPos records the GLushort offset, not the byte offset.



回答2:

Keith Randall had the right solution. I was confusing the two. Here is what I ended up doing:

// Create the VBO and IBO
    for(int i = 0; i < fragMap[0x36]; i++)
    {
        // Copy the data to the IBO
        memcpy(&iboData[iboPos], zmeshes[i].indices, zmeshes[i].numPoly * 3 * sizeof(GLushort));//sizeof(*zmeshes[i].indices));

        // Advance the position
        iboPos += zmeshes[i].numPoly * 3;

        // Copy the data to the VBO
        memcpy(&vboData[vboPos], zmeshes[i].vertices, zmeshes[i].numVert * sizeof(Vertex));//sizeof(*zmeshes[i].vertices));

        // Advance the position
        vboPos += zmeshes[i].numVert;

        errorLog.writeSuccess("Build log: VBO size %i VBO pos %i IBO size %i IBO pos %i", zmeshes[i].numVert * sizeof(Vertex), 
                              vboPos, zmeshes[i].numPoly * 3 * sizeof(GLshort), iboPos);
    }

I wanted the offset into the array. Not the offset into the buffer.