Vertex buffer object

Last updated

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.

Contents

The vertex buffer object specification has been standardized by the OpenGL Architecture Review Board 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" [1] or ATI's "vertex array object" [2] 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, [3] OpenGL 3.x [4] and OpenGL 4.x: [5]
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 identifierGLuinttriangleVBO;//Vertices of a triangle (counter-clockwise winding)floatdata[]={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 idglGenBuffers(1,&triangleVBO);//Make the new VBO activeglBindBuffer(GL_ARRAY_BUFFER,triangleVBO);//Upload vertex data to the video deviceglBufferData(GL_ARRAY_BUFFER,sizeof(data),data,GL_STATIC_DRAW);//Make the new VBO active. Repeat here in case it has changed since initialisationglBindBuffer(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 hereglVertexPointer(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 providedglDrawArrays(GL_TRIANGLES,0,sizeof(data)/sizeof(float)/3);//Force display to be drawn nowglFlush();

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

Vertex Shader:

/*----------------- "exampleertexShader.vert" -----------------*/#version 150 // Specify which version of GLSL we are using.// in_Position was bound to attribute index 0("shaderAttribute")invec3in_Position;voidmain(){gl_Position=vec4(in_Position.x,in_Position.y,in_Position.z,1.0);}/*--------------------------------------------------------------*/


Fragment Shader:

/*---------------- "exampleFragmentShader.frag" ----------------*/#version 150 // Specify which version of GLSL we are using.precisionhighpfloat;// Video card drivers require this line to function properlyoutvec4fragColor;voidmain(){fragColor=vec4(1.0,1.0,1.0,1.0);//Set colour of each fragment to WHITE}/*--------------------------------------------------------------*/


Main OpenGL Program:

/*--------------------- Main OpenGL Program ---------------------*//* Create a variable to hold the VBO identifier */GLuinttriangleVBO;/* This is a handle to the shader program */GLuintshaderProgram;/* These pointers will receive the contents of our shader source code files */GLchar*vertexSource,*fragmentSource;/* These are handles used to reference the shaders */GLuintvertexShader,fragmentShader;constunsignedintshaderAttribute=0;/* Vertices of a triangle (counter-clockwise winding) */floatdata[3][3]={{0.0,1.0,0.0},{-1.0,-1.0,0.0},{1.0,-1.0,0.0}};/*---------------------- 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,(constGLchar**)&vertexSource,0);glShaderSource(fragmentShader,1,(constGLchar**)&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));/*---------------------------------------------------------------*/

Related Research Articles

<span class="mw-page-title-main">OpenGL</span> Cross-platform graphics API

OpenGL 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 hardware-accelerated rendering.

In computer science, a mask or bitmask is data that is used for bitwise operations, particularly in a bit field. Using a mask, multiple bits in a byte, nibble, word, etc. can be set either on or off, or inverted from on to off in a single bitwise operation. An additional use of masking involves predication in vector processing, where the bitmask is used to select which element operations in the vector are to be executed and which are not.

<span class="mw-page-title-main">Shader</span> Type of program in a graphical processing unit (GPU)

In computer graphics, a shader is a computer program that calculates the appropriate levels of light, darkness, and color during the rendering of a 3D scene—a process known as shading. Shaders have evolved to perform a variety of specialized functions in computer graphics special effects and video post-processing, as well as general-purpose computing on graphics processing units.

In computer graphics, a computer graphics pipeline, rendering pipeline or simply graphics pipeline, is a conceptual model that describes what steps a graphics system needs to perform to render a 3D scene to a 2D screen. Once a 3D model has been created, for instance in a video game or any other 3D computer animation, the graphics pipeline is the process of turning that 3D model into what the computer displays.   Because the steps required for this operation depend on the software and hardware used and the desired display characteristics, there is no universal graphics pipeline suitable for all cases. However, graphics application programming interfaces (APIs) such as Direct3D and OpenGL were created to unify similar steps and to control the graphics pipeline of a given hardware accelerator. These APIs abstract the underlying hardware and keep the programmer away from writing code to manipulate the graphics hardware accelerators.

<span class="mw-page-title-main">Java OpenGL</span>

Java OpenGL (JOGL) is a wrapper library that allows OpenGL to be used in the Java programming language. It was originally developed by Kenneth Bradley Russell and Christopher John Kline, and was further developed by the Sun Microsystems Game Technology Group. Since 2010, it has been an independent open-source project under a BSD license. It is the reference implementation for Java Bindings for OpenGL (JSR-231).

A shading language is a graphics programming language adapted to programming shader effects. Such language forms usually consist of special data types, like "vector", "matrix", "color" and "normal". Due to the variety of target markets for 3D computer graphics, different shading languages have been developed.

<span class="mw-page-title-main">OpenGL Shading Language</span> High-level shading language

OpenGL Shading Language (GLSL) is a high-level shading language with a syntax based on the C programming language. It was created by the OpenGL ARB to give developers more direct control of the graphics pipeline without having to use ARB assembly language or hardware-specific languages.

<span class="mw-page-title-main">Triangle strip</span> Set of triangles with shared vertices in a triangle mesh

In computer graphics, a triangle strip is a subset of triangles in a triangle mesh with shared vertices, and is a more memory-efficient method of storing information about the mesh. They are more efficient than un-indexed lists of triangles, but usually equally fast or slower than indexed triangle lists. The primary reason to use triangle strips is to reduce the amount of data needed to create a series of triangles. The number of vertices stored in memory is reduced from 3N to N + 2, where N is the number of triangles to be drawn. This allows for less use of disk space, as well as making them faster to load into RAM.

The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

In computer graphics, a fragment is the data necessary to generate a single pixel's worth of a drawing primitive in the frame buffer.

The Blinn–Phong reflection model, also called the modified Phong reflection model, is a modification developed by Jim Blinn to the Phong reflection model.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

<span class="mw-page-title-main">Triangle mesh</span> Polygon mesh composed of triangles

In computer graphics, a triangle mesh is a type of polygon mesh. It comprises a set of triangles that are connected by their common edges or vertices.

In computer science, a type punning is any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal language.

PLY is a computer file format known as the Polygon File Format or the Stanford Triangle Format. It was principally designed to store three-dimensional data from 3D scanners. The data storage format supports a relatively simple description of a single object as a list of nominally flat polygons. A variety of properties can be stored, including color and transparency, surface normals, texture coordinates and data confidence values. The format permits one to have different properties for the front and back of a polygon. There are two versions of the file format, one in ASCII, the other in binary.

<span class="mw-page-title-main">Perl OpenGL</span>

Perl OpenGL (POGL) is a portable, compiled wrapper library that allows OpenGL to be used in the Perl programming language.

ARB assembly language is a low-level shading language, which can be characterized as an assembly language. It was created by the OpenGL Architecture Review Board (ARB) to standardize GPU instructions controlling the hardware graphics pipeline.

Additive manufacturing file format (AMF) is an open standard for describing objects for additive manufacturing processes such as 3D printing. The official ISO/ASTM 52915:2016 standard is an XML-based format designed to allow any computer-aided design software to describe the shape and composition of any 3D object to be fabricated on any 3D printer via a computer-aided manufacturing software. Unlike its predecessor STL format, AMF has native support for color, materials, lattices, and constellations.

<span class="mw-page-title-main">Open Game Engine Exchange</span>

The Open Game Engine Exchange (OpenGEX) format is a text-based file format designed to facilitate the transfer of complex 3D scene data between applications such as modeling tools and game engines. The OpenGEX format is built upon the data structure concepts defined by the Open Data Description Language (OpenDDL), a generic language for the storage of arbitrary data in human-readable format. The OpenGEX file format is registered with the Internet Assigned Numbers Authority (IANA) as the model/vnd.opengex media type.

This is a glossary of terms relating to computer graphics.

References

  1. "GL_NV_vertex_array_range Whitepaper".
  2. "ATI_vertex_array_object".
  3. "OpenGL 2.1 function reference".
  4. "OpenGL 3.3 function reference".
  5. "OpenGL 4.2 function reference".