5

When I first add some vertices to the buffer, these are the relevant functions I'm calling

    // Create and bind the object's Vertex Array Object:
    glGenVertexArrays(1, &_vao);
    glBindVertexArray(_vao);

    // Create and load vertex data into a Vertex Buffer Object:
    glGenBuffers(1, &_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, _vbo);
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW);

    // Tells OpenGL that there is vertex data in this buffer object and what form that vertex data takes:

    // Obtain attribute handles:
    _posAttrib = glGetAttribLocation(program, "position");
    glEnableVertexAttribArray(_posAttrib);
    glVertexAttribPointer(_posAttrib, // attribute handle
                          4,          // number of scalars per vertex
                          GL_FLOAT,   // scalar type
                          GL_FALSE,
                          0,
                          0);

    // Unbind vertex array:
    glBindVertexArray(0);

But later on in my program, I want to add some more vertices.

I do this by the following (within a separate function:

add_vertices(x,y); //adds the necessary vertices to the vector. 
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, (TRIANGLE_AMOUNT+1)*4*_number_of_circles * sizeof(float), &vertices[0], GL_STATIC_DRAW);

Assuming the funky size in the 2nd argument of glBufferData is fine, am I missing anything? Are there any other OpenGL functions that need calling?

I'm not getting any errors, but when I'm trying to draw the extra shapes with the new vertices by looping over glDrawArrays with different subsets of the vertices, nothing happens. Only the first shape gets drawn.

I hope that this is semi-coherent...let me know if there's any info I haven't provided.

Cheers.

James Adams
  • 678
  • 4
  • 11
  • 21
  • If you just want to replace the data in the buffer, you shouldn't generate a new one. `glBindBuffer`+ `glBufferData` is sufficient in this case. When you call glGenBuffers again, you get a completely new buffer, which means that you would have to update the vao. – BDL Nov 09 '15 at 10:45
  • Ahhh that's definitely progress. It's drawing new stuff now..but they just happen to be blobs rather than circles. Thanks mate! – James Adams Nov 09 '15 at 10:47

1 Answers1

2

In OpenGL, changing the buffers and exchanging the buffers data are two different things which require different actions to be taken afterwards:

Exchanging the data

In this case a previously generated vbo is required. To upload new data, it is only required to bind the buffer and to buffer new data:

glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, (TRIANGLE_AMOUNT+1)*4*_number_of_circles * sizeof(float),
    &vertices[0], GL_STATIC_DRAW);

Creating a new buffer

In this case a new buffer is generated by glGenerateBuffers and (in addition to uploading the data) all VAO bindings have to be updated.

Sidenote: In the code shown above, you create a new buffer without deleting the previous buffer.

BDL
  • 21,052
  • 22
  • 49
  • 55