为什么在调用glMultiDrawElementsInDirect时出现INVALID_OPERATION错误

2022-05-07 00:00:00 opengl java lwjgl

我当前正在尝试将glMultiDrawElementsIndirect与Java中的LWJGL一起使用,但出现错误INVALID_OPERATION。

glMultiDrawElementsIndirect(GL_TRIANGLES,  GL_UNSIGNED_INT, this.modelManager.indirect, NB_TYPE_MESH, 0);

我在文档中看到此错误与GL_DRAW_INDIRECT_BUFFERGL_ELEMENT_ARRAY_BUFFER有关,但我不知道问题在我的代码中的哪里。

间接缓冲区

int[] indirect = new int[NB_TYPE_MESH*5];
for(int i=0; i<2; i++) {
    indirect[i] = getElementCount().get(i);
    indirect[1+i] = Game.NB_MAX_OBJECTS/2;
    indirect[2+i] = 0;
    indirect[3+i] = i==0?0:getElementCount().get(i-1);
    indirect[4+i] = i; // maybe 0
}
vboIdIndirect = glGenBuffers();
IntBuffer gIndirectBuffer = MemoryUtil.memAllocInt(indirect.length);
gIndirectBuffer.put(indirect).flip();
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, vboIdIndirect);
glBufferData(GL_DRAW_INDIRECT_BUFFER, gIndirectBuffer, GL_DYNAMIC_DRAW);

和我的元素缓冲区

vboId = glGenBuffers();
vboIdList.add(vboId);
indicesBuffer = MemoryUtil.memAllocInt(indices.length);
indicesBuffer.put(indices).flip();

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_DYNAMIC_DRAW);

索引包含NB_TYPE_MESH网格类型的索引


解决方案

如果在调用glMultiDrawElementsIndirect时将缓冲区绑定到目标GL_DRAW_INDIRECT_BUFFER,间接参数将被解释为进入此缓冲区的以基本计算机单位为单位的偏移量。

因此在绘制元素之前需要绑定GL_DRAW_INDIRECT_BUFFER,但间接参数必须为null

glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, this.modelManager.indirect, NB_TYPE_MESH, 0);

glBindBuffer(GL_DRAW_INDIRECT_BUFFER, vboIdIndirect);
glMultiDrawElementsIndirect(GL_TRIANGLES,  GL_UNSIGNED_INT, null, NB_TYPE_MESH, 0);

另见Java Code Examples glMultiDrawElementsIndirect()

相关文章