I’ve just added VBOs to my display, and having problems. It worked fine with vertex arrays, and was a little faster than immediate mode. However, the VBOs are very slow, and crash when I add in textures…
Here’s the rendering code…
typedef int QUADIX[4];
QUADIX quads[20];
int vbo_v, vbo_n, vbo_t;
if (luse_vbo) {
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_v);
glVertexPointer(3, GL_DOUBLE, 0, 0);
if (vbo_n > 0) {
glEnableClientState(GL_NORMAL_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_n);
glNormalPointer(GL_DOUBLE, 0, 0);
}
// The textures are making this crash...
//if (vbo_t > 0) {
// glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_t);
// glTexCoordPointer(3, GL_DOUBLE, 0, 0);
//}
}
else {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_DOUBLE, 0, (double *)vertex_array);
if (normals_count > 0) {
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_DOUBLE, 0, (double *)normals_array);
}
if (texcoords_count > 0) {
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(3, GL_DOUBLE, 0, (double *)texcoords_array);
}
}
glDrawElements(GL_QUADS, quad_count*4, GL_UNSIGNED_INT, &quads);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_INDEX_ARRAY);
if (luse_vbo) {
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
and the initialisation code…
// VBO vertices...
if (vertex_count > 0) {
glGenBuffersARB(1, &vbo_v);
glBindBufferARB(GL_ARRAY_BUFFER, vbo_v);
glBufferDataARB(GL_ARRAY_BUFFER, (int)(vertex_count * sizeof(POINT3)),
vertex_array, GL_STATIC_DRAW);
}
// VBO normals...
if (normals_count > 0) {
glGenBuffersARB(1, &vbo_n);
glBindBufferARB(GL_ARRAY_BUFFER, vbo_n);
glBufferDataARB(GL_ARRAY_BUFFER, (int)(normals_count * sizeof(POINT3)),
normals_array, GL_STATIC_DRAW);
}
// VBO texture coords...
if (texcoords_count > 0) {
glGenBuffersARB(1, &vbo_t);
glBindBufferARB(GL_ARRAY_BUFFER, vbo_t);
glBufferDataARB(GL_ARRAY_BUFFER, (int)(texcoords_count * sizeof(POINT2)),
texcoord_array, GL_STATIC_DRAW);
}
// VBO indices...
if (index_count > 0) {
glGenBuffersARB(1, &vbo_i);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, vbo_i);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER, (int)(index_count * sizeof(QUADIX)),
index_array, GL_STATIC_DRAW);
}
// unbind buffers, so that normal array code will work later
glBindBufferARB(GL_ARRAY_BUFFER, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
The VBO code crashes during rendering, on the call to glDrawElements(). The non-VBO code is fine, and the VBOs are still initialised, even when the VBO rendering code is not used. At this stage, I am binding the indices into an array, but I’m not using this during rendering time.
- I am not using display lists.
- The vertex, normals and texture arrays ARE the same size. They come from an OBJ file, but I reprocess them to be 1 to 1 correspondence.
I can’t see any fundamental difference between what the VBO code is doing differently from the array code. I must be misunderstanding something or leaving something out. Anyone know what it is?
Edit for more info = the VBO code always crashes on index_array[10]. My arrays are all dynamically allocated, and I have checked that they are big enough. Remember, everything works fine just using the vertex arrays.