I’m trying to draw a cylinder using VBOs and I’m stuck with this access violation.
This is the code (formatted for the example):
int N;
float *vertexArray, normalArray;
void fillArrays(float height, float radius, int num) {
N = num;
vertexArray = (float*) malloc(sizeof(float)*36*num);
normalArray = (float*) malloc(sizeof(float)*36*num);
...
}
The function above just fills the arrays with the normal vectors and vertices.
void drawCylinder() {
GLuint buffers[2];
glGenBuffers(2, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, 144*N, vertexArray, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,buffers[1]);
glBufferData(GL_ARRAY_BUFFER, 144*N, normalArray, GL_STATIC_DRAW);
glVertexPointer(3,GL_FLOAT,0,vertexArray);
glNormalPointer(GL_FLOAT,0,normalArray);
glDrawArrays(GL_TRIANGLES, 0, 12*N);
}
The exception is thrown in the glDrawArrays call. I’m calling drawCylinder() in GLUT’s display callback function (renderfunc). The main function looks like this:
void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Glew - VBOs - Cylinder");
glutDisplayFunc(renderfunc);
glutIdleFunc(renderfunc);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
fillArrays(3,1,20);
glutMainLoop();
}
There are probably various issues with my code, I’ve just started playing around with VBOs (and with the openGL libs for that matter). But is there an obvious reason for this exception to be thrown? I’m using Visual Studio, and as I said, the access violation is apparently detected when calling glDrawArrays.
If you’re using VBOs the
gl*Pointer()pointerargument should be an offset into your buffer, not a native pointer. Native pointers are for vertex arrays.Also, re-uploading your vertex data before each draw call defeats the purpose of VBOs somewhat, especially with the
GL_STATIC_DRAWhint.This is a decent VBO example.