0

我试图在 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);
}

它只是获取要复制的数据的大小,复制它,然后推进位置(将下一个数据放入缓冲区的位置)。

我得到这样的输出:

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

这可以看作是复制了正确的 72 个值,并在其后面复制了 72 个 0。我是否在复制缓冲区时做错了什么,或者这是否表明其他地方有问题?

进一步的解释:

Mesh 1 - 72 个索引 Mesh 2 - 300 个索引 Mesh 3 - 45 个索引。

使用上面的函数,它会产生一个缓冲区来执行此操作:

[0]-[71] - 网格 1 顶点全部正确 [72]-[142] - 空 [142]-[EndofBuffer] - 网格 2

它会创建用 0 填充的重复大小。

4

2 回答 2

3

既然iboData是a GLushort*,那么iboData+iboPos实际指向的内存位置(int)iboData + iboPos * sizeof(GLushort)。您将字节偏移量与数组偏移量混淆了。在您的两个+=语句中,不要乘以sizeof您使用的 in memcpy,因为iboPos记录的是GLushort偏移量,而不是字节偏移量。

于 2012-07-27T02:28:02.353 回答
0

Keith Randall 有正确的解决方案。我混淆了两者。这是我最终做的事情:

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

我想要数组中的偏移量。不是缓冲区的偏移量。

于 2012-07-27T02:44:02.763 回答