Reputation: 149
When using OpenGl I usually create a VBO and pass the vertex data to it with
glBufferData();
I have been reading this source recently, and please let me cite one part of it:
While creating the new storage, any pre-existing data store is deleted. The new data store is created with the specified size in bytes and usage. If data is not NULL, the data store is initialized with data from this pointer. In its initial state, the new data store is not mapped, it has a NULL mapped pointer, and its mapped access is GL_READ_WRITE.
From this I conclude, that while buffer data function is set, the data from the buffer is copied to the VBO.
Now, when calling glVertexAttribPointer()
I have to pass a pointer to the data again.
I understand the purpose of attrib pointers - they tell the OpenGL state machine how the data is organised in the VBO.
However, passing the pointer both to VBO and then to the glVertexAttribPointer
function seems redundant, since the VAO saves the attrib pointer configuration anyway, and to call glVertexAttribPointer
one has to store the vertex data in the program memory anyway - so the memory gets duplicated (one primal copy and the copy made by glBufferData()
).
Why is this apparent redundancy present? Or maybe I understand something the wrong way? Maybe I misunderstood the documentation and the data is not copied to VBO, but what would be the purpose of VBOs anyway then?
Upvotes: 1
Views: 207
Reputation: 210928
You don't have to. If a named buffer object is bound to the ARRAY_BUFFER target then the last argument (pointer) of glVertexAttribPointer
is treated as a byte offset into the buffer object's data store.
Either (only compatibility profile):
float *data;
glBindBuffer(GL_ARRAY_BUFFER, 0); // 0 is default
glVertexAttribPointer(..., data);
or
float *data;
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, ..., data, ...);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(..., (void*)0);
Upvotes: 0