It's not obvious from the documentation when glVertexAttribPointer
should be called. It looks like it's part of VBO initialisation, but I notice example code calling it during rendering.
glVertexAttribPointer(vertexAttributeId, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), reinterpret_cast<const GLvoid*>(offsetof(Vertex2D, m_x)));
Should glVertexAttribPointer
be called during initialisation of a GL_ARRAY_BUFFER
or should it be called during rendering (after a call to glBindBuffer
)?
glVertexAttribPointer specifies the location and data format of the array of generic vertex attributes at index index to use when rendering.
Description. glEnableVertexAttribArray enables the generic vertex attribute array specified by index . glDisableVertexAttribArray disables the generic vertex attribute array specified by index . By default, all client-side capabilities are disabled, including all generic vertex attribute arrays.
A Vertex Array Object (VAO) is an object which contains one or more Vertex Buffer Objects and is designed to store the information for a complete rendered object. In our example this is a diamond consisting of four vertices as well as a color for each vertex.
pointer. Specifies a offset of the first component of the first generic vertex attribute in the array in the data store of the buffer currently bound to the GL_ARRAY_BUFFER target. The initial value is 0.
The function glVertexAttribPointer
specifies the format and source buffer (ignoring the deprecated usage of client arrays) of a vertex attribute that is used when rendering something (i.e. the next glDraw...
call).
Now there are two scenarios. You either use vertex array objects (VAOs) or you don't (though not using VAOs is deprecated and discouraged/prohibited in modern OpenGL). If you're not using VAOs, then you would usually call glVertexAttribPointer
(and the corresponding glEnableVertexAttribArray
) right before rendering to setup the state properly. If using VAOs though, you actually call it (and the enable function) inside the VAO creation code (which is usually part of some initialization or object creation), since its settings are stored inside the VAO and all you need to do when rendering is bind the VAO and call a draw function.
But no matter when you call glVertexAttribPointer
, you should bind the corresponding buffer right before (no matter when that was actually created and filled), since the glVertexAttribPointer
function sets the currently bound GL_ARRAY_BUFFER
as source buffer for this attribute (and stores this setting, so afterwards you can freely bind another VBO).
So in modern OpenGL using VAOs (which is recommended), it's usually similar to this workflow:
//initialization glGenVertexArrays glBindVertexArray glGenBuffers glBindBuffer glBufferData glVertexAttribPointer glEnableVertexAttribArray glBindVertexArray(0) glDeleteBuffers //you can already delete it after the VAO is unbound, since the //VAO still references it, keeping it alive (see comments below). ... //rendering glBindVertexArray glDrawWhatever
When not using VAOs it would be something like that:
//initialization glGenBuffers glBindBuffer glBufferData ... //rendering glBindBuffer glVertexAttribPointer glEnableVertexAttribArray glDrawWhatever
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With