3
votes

I use old way to provide the data to vertex buffer in OpenGL

glGenBuffers(1, buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(pos), pos, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);

Since OpenGL 4.3 there a new slightly more clear way to provide data to vertex shader appears

const GLfloat colors[] =
{
    1.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 1.0f
};
glBindVertexBuffer(1, buffer, 0, 3 * sizeof(GLfloat));

glVertexAttribFormat(1, 3, GL_FLOAT, GL_FALSE, 0);
glVertexAttribBinding(1, 1);
glEnableVertexAttribArray(1);

but I can't find any resources explaining how to provide data using new way. Of course I can do like this

glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); //i can do even so, and later I can use glBindVertexBuffer

but it looks not really elegant. If I will not use glBindBuffer and only use glBindVertexBuffer and then use glBufferData, I'll overwrite data in buffer that was bind using glBindBuffer.
So question is: can I provide data to vertex buffer other way or I should always use glBindBuffer, glBufferData and only after that use glBindVertexBuffer, glVertexBufferFormat, glVertexAttribFormat and glVertexAttribBinding. Thank you!

2

2 Answers

3
votes

From what I can tell from the OpenGL 4.3 specification, you will still need to bind the vertex buffer to a "regular" binding point in order to update its data:

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);

This problem has effectively been solved in the 4.5 specification with the introduction of DSA functions, which allow you to update the buffer data without binding it to a slot first:

glNamedBufferSubData(buffer, offset, size, data);
4
votes

glVertexAttribFormat and glVertexAttribBinding are telling OpenGL how the data is organized in the buffer; essentially they replace glVertexAttribPointer (and only glVertexAttribPointer). Loading the data still happens with glBuffer[Sub]Data.