Vertex buffer object


A vertex buffer object is an OpenGL feature that provides methods for uploading vertex data to the video device for non-immediate-mode rendering. VBOs offer substantial performance gains over immediate mode rendering primarily because the data resides in the video device memory rather than the system memory and so it can be rendered directly by the video device. These are equivalent to in Direct3D.
The vertex buffer object specification has been standardized by the as of OpenGL Version 1.5. Similar functionality was available before the standardization of VBOs via the Nvidia-created extension "vertex array range" or ATI's "vertex array object" extension.

Basic VBO functions

The following functions form the core of VBO access and manipulation:

Example usage

In C, using OpenGL 2.1


//Initialise VBO - do only once, at start of program
//Create a variable to hold the VBO identifier
GLuint triangleVBO;
//Vertices of a triangle
float data = ;
//try float data = ; if the above doesn't work.
//Create a new VBO and use the variable id to store the VBO id
glGenBuffers;
//Make the new VBO active
glBindBuffer;
//Upload vertex data to the video device
glBufferData;
//Make the new VBO active. Repeat here incase changed since initialisation
glBindBuffer;
//Draw Triangle from VBO - do each time window, view point or data changes
//Establish its 3 coordinates per vertex with zero stride in this array; necessary here
glVertexPointer;
//Establish array contains vertices
glEnableClientState;
//Actually draw the triangle, giving the number of vertices provided
glDrawArrays / sizeof;
//Force display to be drawn now
glFlush;

In C, using OpenGL 3.x and OpenGL 4.x

Vertex Shader:

/*----------------- "exampleVertexShader.vert" -----------------*/
  1. version 150 // Specify which version of GLSL we are using.
// in_Position was bound to attribute index 0
in vec3 in_Position;
void main
/*--------------------------------------------------------------*/

Fragment Shader:

/*---------------- "exampleFragmentShader.frag" ----------------*/
  1. version 150 // Specify which version of GLSL we are using.
precision highp float; // Video card drivers require this line to function properly
out vec4 fragColor;
void main
/*--------------------------------------------------------------*/

Main OpenGL Program:

/*--------------------- Main OpenGL Program ---------------------*/
/* Create a variable to hold the VBO identifier */
GLuint triangleVBO;
/* This is a handle to the shader program */
GLuint shaderProgram;
/* These pointers will receive the contents of our shader source code files */
GLchar *vertexSource, *fragmentSource;
/* These are handles used to reference the shaders */
GLuint vertexShader, fragmentShader;
const unsigned int shaderAttribute = 0;
/* Vertices of a triangle */
float data = ;
/*---------------------- Initialise VBO - ---------------------*/
/* Create a new VBO and use the variable "triangleVBO" to store the VBO id */
glGenBuffers;
/* Make the new VBO active */
glBindBuffer;
/* Upload vertex data to the video device */
glBufferData;
/* Specify that our coordinate data is going into attribute index 0, and contains three floats per vertex */
glVertexAttribPointer;
/* Enable attribute index 0 as being used */
glEnableVertexAttribArray;
/* Make the new VBO active. */
glBindBuffer;
/*-------------------------------------------------------------------------------------------------------*/
/*--------------------- Load Vertex and Fragment shaders from files and compile them --------------------*/
/* Read our shaders into the appropriate buffers */
vertexSource = filetobuf;
fragmentSource = filetobuf;
/* Assign our handles a "name" to new shader objects */
vertexShader = glCreateShader;
fragmentShader = glCreateShader;
/* Associate the source code buffers with each handle */
glShaderSource;
glShaderSource;
/* Free the temporary allocated memory */
free;
free;
/* Compile our shader objects */
glCompileShader;
glCompileShader;
/*-------------------------------------------------------------------------------------------------------*/
/*-------------------- Create shader program, attach shaders to it and then link it ---------------------*/
/* Assign our program handle a "name" */
shaderProgram = glCreateProgram;
/* Attach our shaders to our program */
glAttachShader;
glAttachShader;
/* Bind attribute index 0 to in_Position*/
/* "in_Position" will represent "data" array's contents in the vertex shader */
glBindAttribLocation;
/* Link shader program*/
glLinkProgram;
/*-------------------------------------------------------------------------------------------------------*/
/* Set shader program as being actively used */
glUseProgram;
/* Set background colour to BLACK */
glClearColor;
/* Clear background with BLACK colour */
glClear;
/* Actually draw the triangle, giving the number of vertices provided by invoke glDrawArrays
while telling that our data is a triangle and we want to draw 0-3 vertexes
  • /
glDrawArrays);
/*---------------------------------------------------------------*/