Binding a second array buffer to draw a second shape messes up my rendering

I’m having trouble trying to understand the correct way to draw a second buffer with vertex data.

My program is drawing 2D polygons and is right now generating two shapes that I want to draw with OpenGL.

I set up my buffers when I initialize my context:

GLuint _posBufferId;
GLuint _posBuffer2Id;
std::vector<float> points1;
std::vector<float> points2;
//...generate data in point buffers
    glGenBuffers(1, &_posBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, _posBufferId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * points1.size(),, GL_STATIC_DRAW);

    glGenBuffers(1, &_posBuffer2Id);
    glBindBuffer(GL_ARRAY_BUFFER, _posBuffer2Id);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * points2.size(),, GL_STATIC_DRAW);

In my rendering loop, I try to render like this:


glUniform4f(_colorUId, 1, 0, 0, 1);
glUniformMatrix4fv(_mvpMatrixUId, 1, GL_FALSE, m);

glVertexAttribPointer(_posVId, 2, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, _posBufferId);
glDrawArrays(GL_TRIANGLE_FAN, 0, points1.size() / 2);

If I just do that, my first shape renders properly. However, now I want to render my second shape. Simply by adding one more line causes the issue:

glBindBuffer(GL_ARRAY_BUFFER, _posBuffer2Id);

Now the glDrawArrays call from earlier seems to be drawing data from my second buffer, not my first, even though the glDrawArrays call was issued before binding the second buffer. How do I get around this? Is there something I need to flush (I tried adding a glFlush call too, but it didn’t help)?

This may have something to do with it.

Notice the difference: _posBufferId2 vs. _posBuffer2Id Where is _posBuffer2Id defined, and what scope does it have?

If this isn’t related to your problem, show the code you are using for drawing the 2nd shape.

Also, you should be Checking for OpenGL Errors.

Actually, that’s my mistake in transcribing it. I had to clean up my code quite a bit to make it suitable to post. The variable is declared correctly in my source.

Also, I have implemented the error checking described on that web page, but am not having any errors reported. I’m not 100% sure it is working correctly, though since I am using Qt and it’s OpenGL API pipes everything through a wrapper. I’m not entirely sure what extra things Qt may be doing behind the scenes.

Draw calls (glDrawArrays etc) don’t care which buffer is bound to GL_ARRAY_BUFFER. It’s glVertexAttribPointer that is affected by the binding. So you need to call glBindBuffer(GL_ARRAY_BUFFER,...) followed by glVertexAttribPointer in order to change the buffer for an attribute.

A shader can have multiple inputs (attributes), and each one can be sourced from a different buffer. glVertexAttribPointer records the current GL_ARRAY_BUFFER binding along with the function’s arguments as part of the state for the specified attribute.

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.