HOME

TheInfoList



OR:

A vertex buffer object (VBO) is an
OpenGL OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardwa ...
feature that provides methods for uploading vertex data (
position Position often refers to: * Position (geometry), the spatial location (rather than orientation) of an entity * Position, a job or occupation Position may also refer to: Games and recreation * Position (poker), location relative to the dealer * ...
,
normal vector In geometry, a normal is an object such as a line, ray, or vector that is perpendicular to a given object. For example, the normal line to a plane curve at a given point is the (infinite) line perpendicular to the tangent line to the curve at ...
, 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 buffer This is a glossary of terms relating to computer graphics. For more general computer hardware terms, see glossary of computer hardware terms. 0–9 A B ...
s in
Direct3D Direct3D is a graphics application programming interface (API) for Microsoft Windows. Part of DirectX, Direct3D is used to render three-dimensional graphics in applications where performance is important, such as games. Direct3D uses hardware a ...
. The vertex buffer object specification has been standardized by th
OpenGL Architecture Review Board
as of
OpenGL OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardwa ...
Version 1.5 (in 2003). Similar functionality was available before the standardization of VBOs via the
Nvidia Nvidia CorporationOfficially written as NVIDIA and stylized in its logo as VIDIA with the lowercase "n" the same height as the uppercase "VIDIA"; formerly stylized as VIDIA with a large italicized lowercase "n" on products from the mid 1990s to ...
-created extension "vertex array range" or
ATI Ati or ATI may refer to: * Ati people, a Negrito ethnic group in the Philippines **Ati language (Philippines), the language spoken by this people group ** Ati-Atihan festival, an annual celebration held in the Philippines *Ati language (China), a ...
's "vertex array object" extension.


Basic 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.


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 (counter-clockwise winding) 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(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 incase 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() /*--------------------------------------------------------------*/
Fragment Shader: /*---------------- "exampleFragmentShader.frag" ----------------*/ #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 (counter-clockwise winding) */ float data 3] = ; /*---------------------- Initialise VBO - (Note: do only once, at start of program) ---------------------*/ /* Create a new VBO and use the variable "triangleVBO" 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); /* Specify that our coordinate data is going into attribute index 0(shaderAttribute), and contains three floats per vertex */ glVertexAttribPointer(shaderAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0); /* Enable attribute index 0(shaderAttribute) as being used */ glEnableVertexAttribArray(shaderAttribute); /* Make the new VBO active. */ glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); /*-------------------------------------------------------------------------------------------------------*/ /*--------------------- Load Vertex and Fragment shaders from files and compile them --------------------*/ /* Read our shaders into the appropriate buffers */ vertexSource = filetobuf("exampleVertexShader.vert"); fragmentSource = filetobuf("exampleFragmentShader.frag"); /* Assign our handles a "name" to new shader objects */ vertexShader = glCreateShader(GL_VERTEX_SHADER); fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); /* Associate the source code buffers with each handle */ glShaderSource(vertexShader, 1, (const GLchar**)&vertexSource, 0); glShaderSource(fragmentShader, 1, (const GLchar**)&fragmentSource, 0); /* Free the temporary allocated memory */ free(vertexSource); free(fragmentSource); /* Compile our shader objects */ glCompileShader(vertexShader); glCompileShader(fragmentShader); /*-------------------------------------------------------------------------------------------------------*/ /*-------------------- 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(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); /* Bind attribute index 0 (shaderAttribute) to in_Position*/ /* "in_Position" will represent "data" array's contents in the vertex shader */ glBindAttribLocation(shaderProgram, shaderAttribute, "in_Position"); /* Link shader program*/ glLinkProgram(shaderProgram); /*-------------------------------------------------------------------------------------------------------*/ /* Set shader program as being actively used */ glUseProgram(shaderProgram); /* Set background colour to BLACK */ glClearColor(0.0, 0.0, 0.0, 1.0); /* Clear background with BLACK colour */ glClear(GL_COLOR_BUFFER_BIT); /* 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(GL_TRIANGLES, 0, (sizeof(data) / 3) / sizeof(GLfloat)); /*---------------------------------------------------------------*/


References

{{reflist, 2


External links


Vertex Buffer Object Whitepaper
OpenGL 3D computer graphics