0

I have a VBO and I instance it and a VAO in a Line drawing class called Line with a call to

glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

that creates them before my do-while loop where the rendering occurs. Constructed like this

Line X= Line();

During the rendering loop I do a draw call from the same class function that I instanced like this.

X.draw(vec3 start ,vec3 end);

In the draw function I bind VAO and VBO and draw. Then I call

 X.draw(vec3 start ,vec3 end);

again from the next line in my do-while loop. and a few more times. Each time it draws a line where I want without issue. But my question is, is it overwriting the VBO? or just pushing the new data in to the )index position and heaping up data behind it in the VBO with each call for a new line? And if so how long will it go on? It appears to concatenate the new vertex's with the old. But If I have a program that's using lines as bullets or something, I might get many instances of lines flying around and maybe I'm using the wrong methods and need to wipe the buffer or it gets overwritten with each new binding in the draw calls. My draw call looks like this for clarity.

int Line::draw(vec3 start, vec3 end){
glUseProgram(shaderProgram);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "MVP"), 1, GL_FALSE, &MVP[0][0]);
glUniform3fv(glGetUniformLocation(shaderProgram, "color"), 1, &lineColor[0]);
glLineWidth(4.0f);
vertices = {
    start.x, start.y, start.z,
    end.x, end.y, end.z
};
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), vertices.data(), GL_STATIC_DRAW); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3* sizeof(float), (void*)0);
glDrawArrays(GL_LINES, 0, 2);
glEnableVertexAttribArray(0);
return 0;
}
Tuna
  • 113
  • 1
  • 11

1 Answers1

1

"It appears to concatenate the new vertex's with the old"

This shouldn't happen if you are only instantiating (calling glGenBuffers) a fixed number of Lines. As user253751 said, each glBufferData should overwrite the last, provided you are giving it the correct buffer handle.

If you are drawing something with constantly changing vertices, use GL_DYNAMIC_DRAW instead. This should be fine for small geometries like text and bullet trails. For complicated geometries of course you are using a uniform transformation to move it every frame and needing only to buffer once.