Dynamically render lines between vertices OpenGL

2.2k views Asked by At

How does one efficiently update the vertices between render calls and dynamically render lines between some of these points?

I am working on a basic OpenGL example and it's a static vertices array that renders using glDrawArrays and the GL_TRIANGLES Macro.

I would like to experiment a little with dynamically updated vertices and draw basic lines between random points.

1

There are 1 answers

7
Andon M. Coleman On BEST ANSWER

You actually have two questions here, one has to do with updating vertex data and the other has to do with drawing a subset of the vertices contained in your buffer.

When it comes to updating data in a vertex buffer, you can't (or rather, shouldn't) change data in a Vertex Buffer Objet that is flagged as static. I assume that is what you mean by static vertex data? The usage flag on VBOs is just a hint, but calling a VBO STATIC is supposed to tell the driver that you are going to send it data once and draw multiple times.

Your appropriate flag would either be STREAM (updated at least once after the initial data is supplied) or DYNAMIC (updated frequently).

Now when it comes to actually changing data in a Vertex Buffer Object, you have two options:

  1. Map it into your application's address space using glMapBuffer (...) and have the driver detect which regions of memory you modified.

  2. Send contiguous blocks of data using glBufferSubData (...).

Since you said you want to update random locations in your buffer, you might be better off mapping the buffer instead of making potentially hundreds of calls to glBufferSubData (...).

As for drawing lines between a couple of vertices, what you want to do is use an Index Array. In modern OpenGL you will use the same mechanism (namely Vertex Buffer Objects) to store indices into your vertex buffer. You call glDrawElements (...) with the type of primitive (GL_LINES) in this case to construct using the vertex indices in the Index Array, the first index to draw and the number of indices total.

glDrawElements (...) will run through your index array starting at the offset you supply and draw (NumberOfElements / NumberOfElementsPerPrimitiveType) primitives. For GL_LINES, each line is defined by 2 indices (so you will draw n/2 lines if you supply n indices).