Vertex cache object (VBO)

Source: Internet
Author: User
  • Create VBO
  • Draw VBO
  • Update VBO
  • Instance

Gl_arb_vertex_buffer_objectExpansion is committed to providing the advantages of vertex arrays and display lists to improve OpenGL efficiency, while avoiding their implementation shortcomings. The vertex cache object (VBO) allows the vertex array data to be stored in the server's high-performance graphics card memory and provides efficient data transmission. If the cached object is used to save pixel data, it is called the pixel cache object (PBO ).

Using Vertex Arrays can reduce the number of function calls and reuse of shared vertices. However, the disadvantage of the vertex array is that the vertex array function is in the client state, and each reference must re-send data to the server.

In addition, the display list is a server function, so it is not limited by the overhead of data transmission. However, once the display list is compiled, the data in the display list cannot be modified.

The vertex cache object (VBO) creates a "cache object" for the vertex attribute in the server-side High-Performance Memory and provides access functions that reference these arrays. These functions are the same as those used in the vertex array, for exampleGlvertexpointer(),Glnormalpointer(),Gltexcoordpointer.

The memory management in the vertex cache object places the cache object in the most appropriate memory location according to user prompts ("target" and "usage" mode. Therefore, memory management can optimize the cache by balancing the system, the memory of the card, and the memory of the card.

Unlike the display list, data in the vertex cache object can be read and updated by ing to the client memory space.

Another important advantage of VBO is that it can share cached data on multiple clients just like displaying lists and textures. Because VBO is on the server side, multiple clients can access the same cache through corresponding identifiers.

Create VBO

Three steps are required to create a VBO:

  1. Use glgenbuffers () to generate a new cache object.
  2. Use glbindbuffer () to bind a cache object.
  3. Use glbufferdata () to copy vertex data to the cache object.
Glgenbuffers ()

Glgenbuffers () creates a cache object and returns the identifier of the cached object. It requires two parameters: the first is the number of caches to be created, and the second is the address of the gluint variable or array used to store a single ID or multiple IDs.

void glGenBuffers(GLsizei n, GLuint* buffers)
Glbindbuffer ()

After a cache object is created, we need to connect the cached object to the corresponding cache before using the cached object. Glbindbuffer () has two parameters: Target and buffer.

void glBindBuffer(GLenum target, GLuint buffer)

Target tells VBO whether the cached object will save the vertex array data or index array data: gl_array_buffer or gl_element_array. Gl_array_buffer is used for any vertex attributes, such as vertex coordinates, texture coordinates, normal and color component arrays. Use gl_element_array to bind the index data used for gldraw [range] elements. Note: The target flag helps VBO determine the most effective position of the cache object. For example, some systems save the index to the AGP or system memory and store the vertex in the graphics card memory.

When glbindbuffer () is called for the first time, VBO initializes the cache with a memory cache of 0 and sets the initial status of VBO, such as purpose and access attributes.

Glbufferdata ()

After cache initialization, you can use glbufferdata () to copy data to the cache object.

void glBufferData(GLenum target,GLsizeiptr size, const GLvoid*data, GLenum usage);

The first target parameter can be gl_array_buffer or gl_element_array. Size indicates the number of bytes of data to be transferred. The third parameter is the source data array pointer. If data is null, VBO only reserves the memory space of the given data size. Another performance prompt of the last parameter usage flag VBO, which provides how the cache object will use static, dynamic or stream, and read, copy, or draw.

VBO specifies nine enumerated values for the usage flag:

GL_STATIC_DRAWGL_STATIC_READGL_STATIC_COPYGL_DYNAMIC_DRAWGL_DYNAMIC_READGL_DYNAMIC_COPYGL_STREAM_DRAWGL_STREAM_READGL_STREAM_COPY

"Static" indicates that the data in the VBO will not be changed (used multiple times at a time), "dynamic" indicates that the data will be frequently changed (specified and used repeatedly ), "stream" indicates that each frame of data needs to be changed (once specified )." Draw "indicates that the data will be sent to the GPU for drawing (application to GL)," read "indicates that the data will be read by the Client Program (GL to the application ), "copy" indicates that data can be drawn and read (GL to GL ).

Note that only the draw flag is useful for VBO. The copy and read flags make more sense for vertex/frame cache objects (PBO or FBO). For example, gl_static_draw and gl_stream_draw use the graphics card memory and gl_dynamic uses the AGP memory. The _ read _ related cache is more suitable for system memory or AGP memory, because the data is more accessible.

Glbuffersubdata ()
void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data);

Similar to glbufferdata (), glbuffersubdata () is used to copy data to VBO, but it only replaces a certain range of data starting from the given offset to the existing cache. (Before using glbuffersubdata (), the entire cache must be specified by glbufferdata .)

Gldeletebuffers ()
void glDrawBuffers(GLsizei n, const GLenum* bufs);

When VBO is no longer used, you can use gldeletebuffers () to delete one or more vbrs. After a mixed storage object is deleted, its content will be lost.

The following code creates a single VBO instance for the vertex coordinates. Note: After you copy data to VBO, you can allocate memory for the vertex array in the application.

Gluint vboid; // VBO identifier glfloat * vertices = new glfloat [vcount * 3]; // create a vertex array... // create a new VBO and obtain the relevant identifier glgenbuffers (1, & vboid); // bind the VBO to use glbindbuffer (gl_array_buffer_arb, vboid ); // update data to vboglbufferdata (gl_array_buffer_arb, datasize, vertices, gl_static_draw_arb); // After copying data to VBO, you can safely delete Delete [] vertices ;... // Delete vbogldeletebuffers (1, & vboid) when the program is terminated );
Draw VBO

Because VBO is based on the existing vertex array implementation, rendering VBO is almost the same as using the vertex array rendering. The only difference is that the vertex array pointer is now used as the Offset Value of the currently bound cache object. Therefore, Apis other than glbindbuffer () are not required for VBO painting.

// Bind vboglbindbuffer (gl_array_buffer_arb, vboid1) to the vertex array and index array; // The vertex coordinate glbindbuffer (gl_element_array_buffer_arb, vboid2 ); // index coordinates // except the pointer, the pointer is consistent with the vertex array, and the glableclientstate (gl_vertex_array); // enable the vertex coordinate array glvertexpointer (3, gl_float, 0, 0 ); // The last parameter is offset, instead of PTR // use the Index Array offset to draw six quadrilateral gldrawelements (gl_quads, 24, gl_unsigned_byte, 0); gldisableclientstate (gl_vertex_array ); // disable vertex array // bind with 0. Therefore, switch to glbindbuffer (gl_array_buffer_arb, 0); glbindbuffer (gl_element_array_buffer_arb, 0 );

Binding a cache object with 0 will disable the VBO operation. After using VBO, it is best to disable it, so the vertex array operation with absolute pointer will be re-enabled.

Update VBO

The advantage of VBO over the display list is that the client can read and edit the cached object data, but the display list cannot do this. The easiest way to update a VBO is to use glbufferdata () or glbuffersubdata () to re-copy the new data to the bound VBO. In this case, your application must always have a valid vertex array. That is to say, you must always have two vertices: one in the application and the other in the VBO.

Another way to modify the cache object is to map the cache object to the client memory. The client can update the data using the ing cache pointer. The following describes how to map VBO to the client memory and how to access ing data.

Glmapbuffer ()

VBO provides glmapbuffer () to Bind Cache objects to the client memory.

void* glMapBuffer(GLenum target, GLenum access);

If OpenGL can map the cache object to the client address space, glmapbuffer () returns a pointer to the cache. Otherwise, it returns NULL.

The first parameter target is included in glbindbuffer (). The second parameter access flag specifies how to use ing data: read, rewrite, or both.

GL_READ_ONLYGL_WRITE_ONLYGL_READ_WRITE

Note that glmapbuffer () causes synchronization problems. If the GPU is still working on this cached object, glmapbuffer () waits until the GPU stops working on the corresponding cached object.

To avoid waiting, you can use null to call glbufferdata () and then call glmapbuffer (). In this way, the previous data is discarded and glmapbuffer () immediately returns a newly allocated pointer, even if the GPU is still working on the previous data.

However, since you discard the previous data, this method is only valid when you want to update the entire dataset. If you only want to change some data or read data, you 'd better not release the previous data.

Glunmapbuffer ()
GLboolean glUnmapBuffer(GLenum target)

After modifying VBO data, you must unmap the cached object from the client memory. If the call succeeds, glunmapbuffer () returns gl_true. If gl_false is returned, the bound VBO cached content is bad. Corruption occurs because of changes in the display resolution or specific events in the window system. In this case, data must be resold.

The following is the code for changing the VBO instance using the binding method.

// Bind and map the vboglbindbuffer (gl_array_buffer_arb, vboid); float * PTR = (float *) glmapbuffer (cursor, gl_write_only_arb); // if the pointer is null (mapped ), update vboif (PTR) {updatemyvbo (PTR ,...); // modify the cached data glunmapbufferarb (gl_array_buffer_arb); // unmap after use} // You can plot the updated VBO...
Instance

 

This instance program creates VBO jitter along the normal line of defense. It maps to VBO and updates vertex data every frame using a pointer pointing to the ing cache. You can compare the performance with the traditional vertex array implementation method.

It uses two vertex caches: one for vertex coordinates and normal vectors, and the other for storing only the index array.

Download the source file and binary file: vbo.zip,vbosimple.zip.

Vbosimple is a simple example of using VBO and vertex arrays to draw cubes. You can easily see the similarities and differences between VBO and VA.

 

Http://www.songho.ca/opengl/gl_vbo.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.