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 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" [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:

/*----------------- "exampleVertexShader.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.

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

The computer graphics pipeline, also known as the rendering pipeline or graphics pipeline, is a framework within computer graphics that outlines the necessary procedures for transforming a three-dimensional (3D) scene into a two-dimensional (2D) representation on a screen. Once a 3D model is generated, the graphics pipeline converts the model into a visually perceivable format on the computer display. Due to the dependence on specific software, hardware configurations, and desired display attributes, a universally applicable graphics pipeline does not exist. Nevertheless, graphics application programming interfaces (APIs), such as Direct3D, OpenGL and Vulkan were developed to standardize common procedures and oversee the graphics pipeline of a given hardware accelerator. These APIs provide an abstraction layer over the underlying hardware, relieving programmers from the need to write code explicitly targeting various graphics hardware accelerators like AMD, Intel, Nvidia, and others.

<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 Game Technology Group at Sun Microsystems. 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. Shading languages usually consist of special data types like "vector", "matrix", "color" and "normal".

<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">Shadow mapping</span> Method to draw shadows in computer graphic images

Shadow mapping or shadowing projection is a process by which shadows are added to 3D computer graphics. This concept was introduced by Lance Williams in 1978, in a paper entitled "Casting curved shadows on curved surfaces." Since then, it has been used both in pre-rendered and realtime scenes in many console and PC games.

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

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

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

In computer programming, variadic templates are templates that take a variable number of arguments.

<span class="mw-page-title-main">Vertex (computer graphics)</span>

A vertex in computer graphics is a data structure that describes certain attributes, like the position of a point in 2D or 3D space, or multiple points on a surface.

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.

This is a glossary of terms relating to computer graphics.

References

  1. "GL_NV_vertex_array_range Whitepaper". Archived from the original on 2004-08-17. Retrieved 2024-06-24.{{cite web}}: CS1 maint: bot: original URL status unknown (link)
  2. "ATI_vertex_array_object". Archived from the original on 2012-07-04. Retrieved 2011-08-12.
  3. "OpenGL 2.1 function reference".
  4. "OpenGL 3.3 function reference".
  5. "OpenGL 4.2 function reference".