Question

I have been developing an application in which i read from a txt file a couple of points, and i need to create a line graph to display the points.

I'm using glDrawElements,with an array of points and another array of indices. I need the following to be displayed:

  • x and y axis
  • possibility to change axis colors
  • couple of buffers for points and colors

So far, my approach was the following:

  • i created one VBO
  • with 2 vertex array object, one to store information for the axis, another for the graph
  • and i have a couple of buffers, for multiple lines and corresponding indices
  • the color of each line is in the same buffer as the points, like {{XYZW}, {RGBA}}.

Do i need to really create two different vertex arrays, or can i use a single one for both the axes and the graph, with the corresponding buffers. My point is, when do we know we should create different vertex arrays (glGenVertexArrays) to store something to be drawn?

Was it helpful?

Solution

As you have hopefully noticed, you have to enable attribute arrays with glEnableVertexAttribArray when dealing with vertex array objects. Even though you can bind different buffers per attribute, you can't bind different attributes 'per draw call' or similar. This means that everything pointed to by a VAO must have the same attributes stored. You could, of course, ignore some attributes given by the VAO, but this may lead to inefficiency and it probably wouldn't be a good practice.

This again means that, every draw call with the same VAO bound, uses the same attributes. I think this is the best way to determine whether you need a new VAO or not, first determine if you need a different set of attributes. You can then easily determine what piece of the object to draw by pointing to a set of indices at some other location within your index buffer.

In a more object orientated scene (like a game, in which you have distinct meshes) the most practical way is to simply create one or more VAOs per mesh. This allows you to have different types of buffers per mesh (static, dynamic or stream). You could even have a couple of large buffers (1 to 8 MB per buffer or similar) and have multiple VAOs which use the same buffer. I often do this to avoid frequent memory allocation.

I just gave you two options:

  • A new VAO for each new set of attributes
  • A new VAO for each distinct object in the scene

It depends on the type of scene you have which is the most convenient, but in most cases you probably won't find that big of a difference in performance. There are of course cases in which one has slightly better performance, but you should figure that out yourself.

Considering your problem, I can see two options. You didn't say whether you are going to update the vertices often or not. If you are constantly altering your graph and updating your vertices, I would give the axes their own VAO, as they will never change, ergo they can use a static buffer while the graph itself can use a dynamic or stream buffer. But if the axes are part of the entire graph, thus meaning the graph itself won't be updated often, you can just throw them all in a single VAO for convenience.

Remember these are just suggestions, you should choose what fits best in your framework and/or has the best performance or simply which is the most convenient.

Hope this helped a bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top