Vertex buffer object
Feature of OpenGL for storing vertex data
A vertex buffer object (VBO) is an OpenGL feature that provides methods for uploading vertex data (position, normal vector, color, etc.) to the video device for non-immediate-mode rendering. VBOs offer substantial performance gains over immediate mode rendering primarily because the data reside in video device memory rather than system memory and so it can be rendered directly by the video device. These are equivalent to vertex buffers in Direct3D.
The vertex buffer object specification has been standardized by the OpenGL Architecture Review Board Archived 2011-11-24 at the Wayback Machine as of OpenGL Version 1.5 (in 2003). Similar functionality was available before the standardization of VBOs via the Nvidia-created extension "vertex array range" or ATI's "vertex array object" extension.
01Basic VBO functions
The following functions form the core of VBO access and manipulation:
- In OpenGL 1.4:
- glGenBuffersARB(sizei n, uint *buffers)
- Generates a new VBO and returns its ID number as an unsigned integer. Id 0 is reserved.
- glBindBufferARB(enum target, uint buffer)
- Use a previously created buffer as the active VBO.
- glBufferDataARB(enum target, sizeiptrARB size, const void *data, enum usage)
- Upload data to the active VBO.
- glDeleteBuffersARB(sizei n, const uint *buffers)
- Deletes the specified number of VBOs from the supplied array or VBO id.
- In OpenGL 2.1, OpenGL 3.x and OpenGL 4.x:
- glGenBuffers(sizei n, uint *buffers)
- Generates a new VBO and returns its ID number as an unsigned integer. Id 0 is reserved.
- glBindBuffer(enum target, uint buffer)
- Use a previously created buffer as the active VBO.
- glBufferData(enum target, sizeiptrARB size, const void *data, enum usage)
- Upload data to the active VBO.
- glDeleteBuffers(sizei n, const uint *buffers)
- Deletes the specified number of VBOs from the supplied array or VBO id.
02Example 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 (counter-clockwise winding) float data[] = {1.0, 0.0, 1.0, 0.0, 0.0, -1.0, -1.0, 0.0, 1.0}; //try float data[] = {0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0, -1.0, 0.0}; if the above doesn't work. //Create a new VBO and use the variable id to store the VBO id glGenBuffers(1, &triangleVBO); //Make the new VBO active glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); //Upload vertex data to the video device glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); //Make the new VBO active. Repeat here in case it has changed since initialisation glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); //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(3, GL_FLOAT, 0, NULL); //Establish array contains vertices (not normals, colours, texture coords etc) glEnableClientState(GL_VERTEX_ARRAY); //Actually draw the triangle, giving the number of vertices provided glDrawArrays(GL_TRIANGLES, 0, sizeof(data) / sizeof(float) / 3); //Force display to be drawn now glFlush();In C, using OpenGL 3.x and OpenGL 4.x
Vertex Shader:
/*----------------- "exampleVertexShader.vert" -----------------*/ #version 150 // Specify which version of GLSL we are using. // in_Position was bound to attribute index 0("shaderAttribute") in vec3 in_Position; void main() { gl_Position = vec4(in_Position.x, in_Position.y, in_Position.z, 1.0); } /*--------------------------------------------------------------*/
Fragment Shader:
Main OpenGL Program:
Sources and credits
This article is adapted from the Wikipedia article “Vertex buffer object”, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.
Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.