2
votes
void prepare_axes(void) { 
axes[0][0] = -win_width / 2.5f; axes[0][1] = 0.0f; 
axes[1][0] = win_width / 2.5f; axes[1][1] = 0.0f;    
axes[2][0] = 0.0f; axes[2][1] = -win_height / 2.5f;     
axes[3][0] = 0.0f; axes[3][1] = win_height / 2.5f;

// Initialize vertex buffer object.     
glGenBuffers(1, &VBO_axes);
glBindBuffer(GL_ARRAY_BUFFER, VBO_axes);
glBufferData(GL_ARRAY_BUFFER, sizeof(axes), axes, GL_STATIC_DRAW);

// Initialize vertex array object.  
glGenVertexArrays(1, &VAO_axes); 
glBindVertexArray(VAO_axes);

glBindBuffer(GL_ARRAY_BUFFER, VBO_axes); 
glVertexAttribPointer(LOC_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

glEnableVertexAttribArray(0); 
glBindBuffer(GL_ARRAY_BUFFER, 0); 
glBindVertexArray(0); 
}

I was looking into OpenGL source code, but I didn't understand why call glBindBuffer(GL_Array_Buffer, VBO_axes) twice.
From my understanding, after the first time they call glGenBuffer, glBindBuffer and glBufferData, the information of the variable 'axes' is stored in GL_ARRAY_BUFFER.
After that, they generate, bind VAO and call glBindBuffer again.
I can't understand why they did that.
I knew that glBufferData() deletes the current buffer store if there already was one, and creates a new buffer store. This means that the information of the variable 'axes' is fade away when calling glBindBuffer secondly.
Help me please..

1
A call to glBindBuffer occurs within the context of the currently bound VAO. So the first call to glBindBuffer will bind VBO_axes to whichever VAO is bound at that point (presumably the default VAO) whereas the second call to glBindBuffer will bind to VAO_axes. (Not sure I've got the terminology entirely correct here, so I'm leaving this as a comment rather than an answer.)G.M.
@G.M.: "A call to glBindBuffer occurs within the context of the currently bound VAO." No, it doesn't. glBindBuffer only affects VAO state if you're binding to the GL_ELEMENT_ARRAY_BUFFER target. The GL_ARRAY_BUFFER binding is not part of VAO state.Nicol Bolas
@NicolBolas I stand corrected. Thanks for the clarification.G.M.

1 Answers

0
votes

Technically, they don't have to do the extra bind in this case. However, it's a good idea to do so, because it's very easy to accidentally break code that doesn't use it. If the programmer wants to change the buffer creation code to create two buffer objects, using the GL_ARRAY_BUFFER binding, then the subsequent code would be broken.

However, if you bind the correct buffer, then it will always work, no matter what you did before.