I have created a Vertex Buffer Object containing only vertices for triangles, for drawing with the GL_TRIANGLES option. My VBO has no color information because I change the color every frame.
Now I’m trying to draw individual triangles in a loop after setting the default vertex color like this in every iteration:
glColor4f(red, green, blue, 1);
But I’m not sure how to perform the actual drawing.
Must I use glDrawArrays if I want to pick one or two triangles out of the VBO to draw them with a specific color, or must I use glDrawElements?
Is there a more efficient way to set the color for each triangle in the VBO and then draw it? Or is it fine to call glColor44 and glDrawArrays in a loop for every frame?
First of all, I would rather suggest to ignore the slightly higher memory cost and just store a color with each vertex inside the VBO and therefore just duplicate the triangle’s color for each of the triangle’s vertices (you cannot set per-triangle colors). This will most probably be much more efficient than drawing single triangles in a loop. Keep in mind that the advantage of VBOs is not only their possible GPU storage, but also the fact that you don’t need driver calls for each and every triangle or even vertex. So just duplicate your per-triangle colors into per-vertex colors and draw everything with a single call to
glDrawArrays(glDrawElementswon’t buy you much if you need to duplicate almost every vertex anyway, which makes indices just useless).Said that, you can of course draw individual triangles with
glDrawArrays, that’s what thefirstandcountparameters are for. So if you have a VBO containing the 9 vertices of 3 triangles, just callto draw only the 2nd triangle. That easy. And likewise can you use the
countandoffsetparameters ofglDrawElementsto select a particular part of the index array to draw.