Tuesday, June 7, 2016

Vertex Buffer Object

Cited Official Sitehttps://www.opengl.org/wiki/Vertex_Specification


...

Vertex Buffer Object

A Vertex_buffer_object (VBO) is a Buffer Object which is used as the source for vertex array data. It is no different from any other buffer object, and a buffer object used for Transform Feedback or asynchronous pixel transfers can be used as source values for vertex arrays.


There are two ways to use buffer objects as the source for vertex data. This section describes the combined format method. A method that separates the format specification from buffers is described below. The two are functionally equivalent, but the separate method (and easier to understand); however, it requires OpenGL 4.3 or ARB_vertex_attrib_binding. 

The format and source buffer for an attribute array can be set by doing the following. First, the buffer that the attribute comes from must be bound to GL_ARRAY_BUFFER. 
Note: The GL_ARRAY_BUFFER binding is NOT part of the VAO's state! I know that's confusing, but that's the way it is. 
Once the buffer is bound, call one of these functions: void glVertexAttribPointer​( GLuint index​, GLint size​, GLenum type​, GLboolean normalized​, GLsizei stride​, const void *offset​); void glVertexAttribIPointer​( GLuint index​, GLint size​, GLenum type​, GLsizei stride​, const void *offset​ ); void glVertexAttribLPointer​( GLuint index​, GLint size​, GLenum type​, GLsizei stride​, const void *offset​ );


All of these functions do more or less the same thing. The difference between them will be discussed later. Note that the last function is only available on OpenGL 4.1 or ARB_vertex_attrib_64bit.
These functions say that the attribute index index​ will get its attribute data from whatever buffer object is currently bound to GL_ARRAY_BUFFER. It is vital to understand that this association is made when this function is called. For example, let's say we do this:glBindBuffer(GL_ARRAY_BUFFER, buf1); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);


The first line binds buf1​ to the GL_ARRAY_BUFFER binding. The second line says that attribute index 0 gets its vertex array data from buf1​, because that's the buffer that was bound to GL_ARRAY_BUFFER when theglVertexAttribPointer​ was called.
The third line binds the buffer object 0 to the GL_ARRAY_BUFFER binding. What does this do to the association between attribute 0 and buf1​? 

Nothing! Changing the GL_ARRAY_BUFFER binding changes nothing about vertex attribute 0. Only calls to glVertexAttribPointer​ can do that. 

Think of it like this. glBindBuffer​ sets a global variable, then glVertexAttribPointer​ reads that global variable and stores it in the VAO. Changing that global variable after it's been read doesn't affect the VAO. You can think of it that way because that's exactly how it works. 

This is also why GL_ARRAY_BUFFER is not VAO state; the actual association between an attribute index and a buffer is made by glVertexAttribPointer​. 

Note that it is an error to call the glVertexAttribPointer​ functions if 0 is currently bound to GL_ARRAY_BUFFER.


No comments:

Post a Comment