[英]glDrawArrays Exception_Access_Violation
I am attempting to implement a method to draw a bunch of cubes using vertexBufferObjects in OpenGL and Java however am running into a problem when calling the glDrawArrays command. 我正在尝试实现一种在OpenGL和Java中使用vertexBufferObjects绘制一堆多维数据集的方法,但是在调用glDrawArrays命令时遇到了问题。
Essentially what this program does is cycle through x, y, z coordinates and calculates from there where the vertices of a cube centered at that coordinate are and then enters these vertices to a float buffer. 实质上,该程序所做的是在x,y,z坐标之间循环,并从此处计算以该坐标为中心的多维数据集的顶点在何处,然后将这些顶点输入到float缓冲区中。 (Note I am only entering the vertex data for one face at the moment as to keep the code simple while it is perfected)
(请注意,我目前仅输入一张面的顶点数据,以使代码在完善后保持简单)
The error that occurs is: 发生的错误是:
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);
Your call to glVertexPointer
is wrong. 您对
glVertexPointer
调用是错误的。 Its first parameter is not the number of overall floats, but the number of floats (or rather components) of a single vertex, in your case 3. The reason for the access violation is just, that the glVertexPointer
call fails and glDrawArrays
then uses the default parameters, which might specify 4 components per vertex, or not use your buffer object or use some other unspecified parameters not matching your vertex data. 在您的情况3中,它的第一个参数不是总浮点数,而是单个顶点的浮点数(或分量)。导致访问冲突的原因仅仅是,
glVertexPointer
调用失败,然后glDrawArrays
使用默认参数,可能会为每个顶点指定4个组件,或者不使用缓冲区对象或使用一些其他未指定参数,这些参数与顶点数据不匹配。 So just replace it by 因此,只需将其替换为
glVertexPointer(3, GL_FLOAT, 0, 0L);
The variable name verticesPerObject
is a bit misleading anyway, since it doesn't contain the number of vertices, but the number of floats, but this is mere cosmetics, the rest of its usage is correct. 无论如何,变量名
verticesPerObject
有点误导,因为它不包含顶点数,但包含浮点数,但这仅是化妆品,其余用法是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.