Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glVertexAttribPointer needed everytime glBindBuffer is called?

In OpenGL (OpenGL ES 2.0 in particular), is glVertexAttribPointer required to be called each time a new VBO is bound?

For example:

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer1);

glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,position));
glEnableVertexAttribArray(ATTRIB_POSITION);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,color);
glEnableVertexAttribArray(ATTRIB_COLOR);

glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);

// If vertexBuffer2 has the exact same attributes as vertexBuffer1
// is this legal or do the calls to glVertexAttribPointer (and glEnableVertexAttribArray)
// required again?

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer2);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);

I know that there are VAOs that address not having to call glVertexAttribPointer but I'm wondering if this is ok to do in OpenGL?

like image 455
OpenUserX03 Avatar asked Oct 01 '11 02:10

OpenUserX03


People also ask

What does glVertexAttribPointer do?

Vertex format. The glVertexAttribPointer functions state where an attribute index gets its array data from. But it also defines how OpenGL should interpret that data. Thus, these functions conceptually do two things: set the buffer object information on where the data comes from and define the format of that data.

When to use glMapBuffer?

Using glMapBuffer is useful for directly mapping data to a buffer, without first storing it in temporary memory. Think of directly reading data from file and copying it into the buffer's memory.

What is a VAO?

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.

What are OpenGL buffers?

Buffer Objects are OpenGL Objects that store an array of unformatted memory allocated by the OpenGL context (AKA the GPU). These can be used to store vertex data, pixel data retrieved from images or the framebuffer, and a variety of other things.


1 Answers

If you did this:

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer1);

glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,position));
glEnableVertexAttribArray(ATTRIB_POSITION);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,color);
glEnableVertexAttribArray(ATTRIB_COLOR);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);

Your code would work just fine. What all of the gl*Pointer calls do is look at what is stored in GL_ARRAY_BUFFER at the time the function is called, and create an association between that buffer and that attribute. So the only way to change what buffer gets used for an attribute is to call gl*Pointer.

like image 125
Nicol Bolas Avatar answered Nov 15 '22 08:11

Nicol Bolas