Saturday, March 9, 2013

[opengl] glGenBuffers, glBindBuffer, glBufferData

--- Nvidia ParticleSystem particleSystem.cpp ---


uint
ParticleSystem::createVBO(uint size)
{
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    return vbo;
}

--- glGenBuffers()

http://stackoverflow.com/questions/12102864/assist-me-to-understand-opengl-glgenbuffers

glGenBuffers doesn't work quite like what you expect. When you call glGenBuffers, it doesn't actuallycreate anything. It just returns a list of integers that are not currently used as buffer names.
The actual 'object' is not created until you call glBindBuffer. So you can just make up any integer you like and pass it to glBindBuffer and a valid buffer will be created at that index. glGenBuffers is actually not required at all, it's just there as a convenience function to give you an unused integer.
So if you just create an array of random integers, as long as none of them overlap, you can use that as your list of buffers without calling glGenBuffers. That's why your code works whether you tell glGenBuffers to create 1 or 2 buffers. As long as you have two buffers with two different names, it doesn't matter where the integer came from.
A little demonstration:
int buf;
glGenBuffers(1, &buf);
glIsBuffer(buf); //FALSE - buffer has not been created yet
glBindBuffer(GL_ARRAY_BUFFER, buf);
glIsBuffer(buf); //TRUE - buffer created on bind

http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml

--- glBufferData()

http://stackoverflow.com/a/5402762


What does glBufferData do?
glBufferData does at least two things, and optionally a third. It allocates buffer storage and lays down the usage of the buffer. Optionally, if the pointer argument is non-null, it will fill the buffer with the pointed-to data. The similar glBufferSubData differs insofar as it only updates (possibly a subrange of) data, it does not allocate storage or anything else.

http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml


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

Parameters

target
Specifies the target buffer object. The symbolic constant must be GL_ARRAY_BUFFERGL_ATOMIC_COUNTER_BUFFERGL_COPY_READ_BUFFERGL_COPY_WRITE_BUFFERGL_DRAW_INDIRECT_BUFFER,GL_DISPATCH_INDIRECT_BUFFERGL_ELEMENT_ARRAY_BUFFERGL_PIXEL_PACK_BUFFERGL_PIXEL_UNPACK_BUFFERGL_SHADER_STORAGE_BUFFERGL_TEXTURE_BUFFERGL_TRANSFORM_FEEDBACK_BUFFER, or GL_UNIFORM_BUFFER.
size
Specifies the size in bytes of the buffer object's new data store.
data
Specifies a pointer to data that will be copied into the data store for initialization, or NULL if no data is to be copied.
usage
Specifies the expected usage pattern of the data store. The symbolic constant must be GL_STREAM_DRAWGL_STREAM_READGL_STREAM_COPYGL_STATIC_DRAWGL_STATIC_READGL_STATIC_COPY,GL_DYNAMIC_DRAWGL_DYNAMIC_READ, or GL_DYNAMIC_COPY.

Description

glBufferData creates a new data store for the buffer object currently bound to target. Any pre-existing data store is deleted. The new data store is created with the specified size in bytes and usage. If data is not NULL, the data store is initialized with data from this pointer. In its initial state, the new data store is not mapped, it has a NULL mapped pointer, and its mapped access is GL_READ_WRITE.
usage is a hint to the GL implementation as to how a buffer object's data store will be accessed. This enables the GL implementation to make more intelligent decisions that may significantly impact buffer object performance. It does not, however, constrain the actual usage of the data store. 



--- glBindBuffer()

http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml


void glBindBuffer(GLenum  target,
 GLuint  buffer);


.... Buffer object names are unsigned integers. The value zero is reserved, ..., buffer set to zero effectively unbinds any buffer object previously bound, and restores client memory usage for that buffer object target (if supported for that target)...

No comments:

Post a Comment