Understanding glGenBuffers and glBindBuffer

1.glGenBuffers official explanation: generate  buffer  object names

unsigned int VBO;
glGenBuffers(1, &VBO);

void glGenBuffers(GLsizei n,GLuint * buffers);

The first parameter is the number of buffer objects to be generated, and the second is an array to be input to store the names of the buffer objects.

This function will return the names of n buffer objects in buffers.

In a word: save n currently unused buffer object names (that is, IDs) into the memory area pointed to by buffers.

Of course, you can also declare an unsigned int array, then the IDs of the created n buffer objects will be stored in the array in turn.

unsigned int VBO[3];
glGenBuffers(3,VBO);

 In other words, at this time, the VBO will contain the ID of a buffer object that has never been used, which is similar to naming the buffer and giving it a unique name.

Note: The glGenBuffers() function only generates the name of a buffer object. This buffer object does not have any meaning. It is a typeless buffer object. It is similar to a pointer variable in C language. We can allocate memory objects and use Its name to refer to this memory object. OpenGL has many types of buffer objects, so what type of buffer object is the following glBindBuffer() function.

2.glBindBuffer official explanation: bind a named buffer object

void glBindBuffer(GLenum target,GLuint buffer);

parameter:

target: The type of the buffer object, which can be GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER. buffer: Specifies the name (ID) of the buffer object, which is the name we generated with glGenBuffers.

The glBindBuffer function completes three tasks:
1. If it is the first time to bind the buffer, and the buffer is a non-zero unsigned int. Then a new buffer object of target type will be created and named buffer, or buffer points to this buffer object.
2. If buffer is an already created buffer object, it will become the currently activated target type buffer object.
3. If buffer is 0, OpenGL will no longer apply any buffer object to the current target.

OpenGL allows us to bind multiple buffer types at the same time, as long as these buffer types are different, in other words, at the same time, we cannot bind two buffer objects of the same type .

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

Guess you like

Origin blog.csdn.net/u012861978/article/details/130988753