Vertex Buffer Objects - what you can and can not do with them
In 3D games, models consisting of thousands of vertices and polygons, are rendered to screen at least at 30 frames per second. For every frame, these vertices are transmitted from the memory to the GPU over the bus. For static models, this is a huge waste of time and bus bandwidth, because the vertices are the same for every frame. Why not cache the data in graphics memory and tell the GPU to get it there? This is what vertex buffer objects (VBOs) are all about.
In my 3D space game, I added the 3D model of an asteroid. The model has ~2700 vertices, which is not much at all (by modern standards), but I decided it should be a VBO and enable the code for even higher detailed models.
Never having used VBOs before, I took the safe route and implemented the asteroid first without any VBO code, and rewrote it later to use a VBO. This was a lot of extra work, but there were some lessons learned.
I found the following:
- use
GL_TRIANGLE_STRIP
for the most efficient way of rendering shapes. However, for most meshes from 3D modelers you would useGL_TRIANGLES
. For the asteroid, I useGL_TRIANGLE_STRIP
. - Use
glDrawElements()
rather thanglDrawArrays()
. Making an extra array with indices seems more work but is very worthwhile the extra effort. - I had to include degenerate triangles to stitch multiple triangle strips together. Degenerate triangles are ‘fake’ triangles that refer to the first vertex in the next triangle strip. (Consider triangle ABC, and degenerate triangle DEE to refer to EFG).
- ARB extensions for VBOs are old-fashioned by now. VBOs have been included in OpenGL for a long time and you can use the functions without the need for meddling with ARB extensions.
- Allocating a new VBO for every type of coordinate is wasteful. Instead, allocate a single VBO to store vertices, texture coordinates, normals, colors, etc. and use
glBufferSubData()
to place the data into the vertex buffer object. - If you use
glDrawElements()
, you must buffer the indices using a separate VBO. The reason for this is that the indices have targetGL_ELEMENT_ARRAY_BUFFER
. I tried mixing indices into subbuffers (GL_ARRAY_BUFFER
s) and it did not work for me. - If you do not have an array at hand, but generate coordinates on the fly, use
glMapBuffer()
to get a pointer to memory. Mind that you are not directly writing into graphics memory; the buffer will be copied to graphics memory byglUnmapBuffer()
.
So, are VBOs the golden egg? Well, no. They are a great way of speeding up the rendering of high poly-count meshes. There are things that VBOs are simply not suited for. This has to do with both the limitations of OpenGL and what it is that you’re trying to achieve. For example, I tried loading the stars into a VBO. This turned out not to work very well, because in my code, stars are point sprites, and each star has a specific point size. Since you can not set the point size in a vertex array, it also makes no sense to use a VBO. I sorted the stars by size to draw them as a vertex array nevertheless, but this resulted in a low vertex count per call so it still makes no sense to use a VBO here.
I also noticed that 1D textures do not work in vertex arrays. Maybe I’m using them for the wrong purpose (texturing GL_LINE
s) but considering that glBegin
/glEnd
is practically deprecated there may be a problem here.
Another problem I had was with face culling. This is not the VBOs fault, but I suspect that when you drape a triangle strip into a convex shape, it may show artifacts. Enabling depth testing did not help me here, but disabling face culling did.
Although VBOs are an add-on to vertex arrays, it seems like OpenGL always requires you to turn your code inside out when you want to change something. I stuck to static meshes for now and decided to abstract a VBO class from it, which wasn’t exactly easy either. Anyway, here is a link to a site that I used to implement VBOs:
- OpenGL Vertex Buffer Objects at oZone3D.net