{steinsoft.net}
 
 
 
 
 
Home/News
Code Snippets
Board
Projects
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
Home :: Programming :: Code Snippets :: OpenGL :: The Vertex Buffer Extension

[ The Vertex Buffer Extension ]

If you are looking for an extension that both improves the speed of your OpenGL application and is also available on many graphic cards, then you're right here. We're speaking about the ARB_vertex_buffer_object extension which is supported by many graphic cards and that allows to store vertex buffers in the graphic card's memory. This might greatly improve rendering speed.

Before using it it's an extension, so we have to check whether it's available. Then we need to get the function pointers to its 3 extension functions. Check out the code section for glext.h.

#include <GL/glext.h>
#include <SDL.h> //if using SDL

bool supportsVertexBuffer = false;

//check whether extension is supported
if (strstr(glGetString(GL_EXTENSIONS,"ARB_vertex_buffer_object"))
{
   //get function pointers using SDL_GL_GetProcAddress(..)
   // (could be using wglGetProcAddress(..) too)
   PFNGLBINDBUFFERARBPROC pglBindBufferARB = (PFNGLBINDBUFFERARBPROC) 
       SDL_GL_GetProcAddress("glBindBufferARB");
   PFNGLGENBUFFERSARBPROC pglGenBuffersARB = (PFNGLGENBUFFERSARBPROC) 
       SDL_GL_GetProcAddress("glGenBuffersARB");
   PFNGLBUFFERDATAARBPROC pglBufferDataARB = (PFNGLBUFFERDATAARBPROC)
       SDL_GL_GetProcAddress("glBufferDataARB");

   supportsVertexBuffer = true;
}

When we're sure that the extension is supported by hardware, we have to create a vertex buffer object using existing vertex data that we want to use at lightspeed - okay nearly. These vertex buffer objects are handled like OpenGL vertex arrays.

//some data
GLfloat data[] = { -1.0f,  1.0f, 0.0f,
                   -1.0f, -2.0f, 0.0f,
                    1.0f, 0.0f, 0.0f,
                    1.0f, 2.0f, 0.0f };
GLuint bufferObject; //the name says it..

//the classic gen and bind thingies we know from OpenGL
// creates and selects the buffer object
pglGenBuffersARB(1, &bufferObject);
pglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferObject);
//store data into buffer object, which is accelerated gc memory 
//when available
pglBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(data), data,
   GL_STATIC_DRAW_ARB);

The vertex buffer object is ready and stored in the graphics card's memory. Now it can be used as any normal OpenGL vertex array.

//select vertex buffer object
pglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferObject);
//use it like a vertex array and draw quads
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, 4);

That's it! I think this is one of the most easy OpenGL extensions that really help improve game speed. And the best thing is that it's supported by pratically all modern cards. Happy C0ding then!

 Last edited 2004-09-28 16:18:36 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0