C ++-缓冲区组合,添加额外的空值 [英] C++ - Buffer combining adding extra empty values
问题描述
我正在尝试填充两个缓冲区,即C ++中的索引缓冲区对象和顶点缓冲区对象.
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).
我得到这样的输出:
<---> 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.我在复制缓冲区时是否做错了什么,或者这表明其他地方有问题?
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?
进一步的解释:
网格1-72索引 网格2-300索引 网格3-45个索引.
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]-网格1顶点全部正确 [72]-[142]-空 [142]-[EndofBuffer]-网格2
[0]-[71] - Mesh 1 vertices all correct [72]-[142] - Empty [142]-[EndofBuffer] - Mesh 2
它会创建重复的大小,并用0填充.
It creates duplicate sizes filled with 0's.
推荐答案
由于iboData
是GLushort*
,因此iboData+iboPos
实际上指向内存位置(int)iboData + iboPos * sizeof(GLushort)
.您将字节偏移量与数组偏移量混淆了.在您的两个+=
语句中,请勿乘以memcpy
中使用的sizeof
,因为iboPos
记录的是GLushort
偏移量,而不是字节偏移量.
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.
这篇关于C ++-缓冲区组合,添加额外的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!