我正在尝试实现一种在 OpenGL 和 Java 中使用 vertexBufferObjects 绘制一堆立方体的方法,但是在调用 glDrawArrays 命令时遇到了问题。
本质上,该程序所做的是循环通过 x、y、z 坐标并从那里计算以该坐标为中心的立方体的顶点所在的位置,然后将这些顶点输入到浮点缓冲区。(注意我现在只输入一个面的顶点数据,以保持代码在完善的同时保持简单)
发生的错误是:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006056ec90, pid=6424, tid=7696
int verticesPerObject = 12; //number of vertices per square
int chunkSizeX = 4; //number of cubes in x direction
int chunkSizeY = 4; //number of cubes in y direction
int chunkSizeZ = 4; //number of cubes in z direction
FloatBuffer vertexData = BufferUtils.createFloatBuffer(chunkSizeX * chunkSizeY * chunkSizeZ * verticesPerObject);
for (int x = 0; x < chunkSizeX; x++) {
for (int y = 0; y < chunkSizeY; y++) {
for (int z = 0; z < chunkSizeZ; z++) {
vertexData.put(new float[]{
(float)x * blockWidth - blockWidth/2, (float)y * blockHeight - blockHeight/2, (float)z * blockDepth + blockDepth/2,
(float)x * blockWidth + blockWidth/2, (float)y * blockHeight - blockHeight/2, (float)z * blockDepth + blockDepth/2,
(float)x * blockWidth + blockWidth/2, (float)y * blockHeight + blockHeight/2, (float)z * blockDepth + blockDepth/2,
(float)x * blockWidth - blockWidth/2, (float)y * blockHeight + blockHeight/2, (float)z * blockDepth + blockDepth/2
});
}
}
}
vertexData.flip();
int vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(verticesPerObject, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS, 0, verticesPerObject);
glDisableClientState(GL_VERTEX_ARRAY);