Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "glGenBuffers" really important if we have glBindBuffer?

Tags:

c++

opengl

Well, I'm starting with OpenGl, and by reading documentation about glBindBuffer, I'm a bit confused.

"glBindBuffer binds a buffer object to the specified buffer binding point. Calling glBindBuffer with target set to one of the accepted symbolic constants and buffer set to the name of a buffer object binds that buffer object name to the target. If no buffer object with name buffer exists, one is created with that name. When a buffer object is bound to a target, the previous binding for that target is automatically broken." Source: http://docs.gl/gl4/glBindBuffer

Does that mean if I don't create buffer object with "foo" name, but I call glBindBuffer, it will create one for me with that name ("foo")?

If so, following code should work fine:

GLuint bar = 70;
glBindBuffer(GL_ARRAY_BUFFER, bar);

-> Create buffer object, "connect" it with bar (70) and bind it to GL_ARRAY_BUFFER.

like image 258
Danijel Avatar asked Mar 05 '23 05:03

Danijel


1 Answers

No, this code will work only in a compatibility profile context (or OpenGL ES).

See OpenGL 4.6 API Core Profile Specification - 2.6.1 Object Management- page 28

[...] the command GenBuffers returns one or more previously unused buffer object names.
Generated names are marked by the GL as used, for the purpose of name generation only. Object names marked in this fashion will not be returned by additional calls to generate names of the same type until the names are marked unused again by deleting them [...]

This means that glGenBuffers does nothing else than reserving names (values). Further calls to glGenBuffers will not return the same values. If glGenBuffers is always used to get name values for buffer objects, then you can be sure that the value isn't already used for an other buffer object.

But in desktop OpenGL core profile specification it is not allowed to use an name for glBindBuffer, which wasn't reserved (returned) by glGenBuffers.

See OpenGL 4.6 API Core Profile Specification - 6.1 Creating and Binding Buffer Objects - page 62

An INVALID_OPERATION error is generated if buffer is not zero or a name returned from a previous call to GenBuffers, or if such a name has since been deleted with DeleteBuffers.

This part of the specification is missing in the OpenGL 4.6 API Compatibility Profile Specification - 6.1 Creating and Binding Buffer Objects - page 62

That is a bit confusing, but that's the specification.

This behavior is verifiable with the code of your question. The following code returns no error using a compatibility profile context but returns GL_INVALID_OPERATION using a core profile context:

GLuint bar = 70;
glBindBuffer(GL_ARRAY_BUFFER, bar); 
GLenum error = glGetError();
like image 162
Rabbid76 Avatar answered Mar 11 '23 15:03

Rabbid76