4
votes

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.

2
You need to think to names as black boxes, not as integers. Suppose they are really complicated opaque values so that you want to let OpenGL generate them for you in all circumstances.Jack

2 Answers

5
votes

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();
2
votes

The OpenGL 4.6 Spec states in Section 6.1 states that glBindBuffer has to throw an GL_INVALID_OPERATION exception when buffer does not point to a name previously reserved by glGenBuffers:

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.

The same information is stated in the official docs.

I'm not sure why docs.GL doesn't state that information. It might be that we are interpreting the sentence in a wrong way since it states that buffer is created. Technically, only the buffer name is created by the call to glGenBuffers, but the buffer object is created when the name gets bound for the first time.