Reputation: 24717
I am using Open GL ES 2.0 in iOS (using GLkit) and wonder what would be the best way to draw multiple objects, say polylines:
Use a separate vertex buffer for each polyline, without an index buffer, and simply draw GL_LINE_LOOP. In this case there would be a draw call for each object.
Gather all the vertices into one buffer and prepare an index buffer. In this case there would be one draw call which will draw GL_LINES.
Any other way to do this.
Also, how would the answer change if each line has a different color (but the color does not change for each vertex).
Upvotes: 0
Views: 370
Reputation: 3446
Since we're defining “best” in terms of performance:
It should always be faster to use a single vertex buffer, as long as all your polylines have the same vertex attributes and it’s straightforward to gather them into a single vertex buffer (i.e. you don’t frequently need to gather and repack them). This requires fewer OpenGL ES API calls to rebind the data, since you can use a single call to glVertexAttribPointer*
per attribute and rely on the first
argument to glDrawArrays
or offset indices (as you described in your question).
Assuming each polyline has distinct vertices and vertex counts, instancing doesn’t apply. I think at this point there are probably two good options here, along a space-for-CPU-time tradeoff curve:
glVertexAttrib4f
, rather than using actual per-vertex array data. Loop through your polylines, updating the color with glVertexAttrib4f
, then using glDrawArrays(GL_LINE_LOOP, first, count)
, where first
and count
are defined by how you packed your polylines into the single vertex buffer.Note that this proposed 1) is similar to the 1) in your question, but all vertex data lives in a single vertex buffer anyway. Since both solutions now use a single vertex buffer, the choice is primarily between whether you are willing to make additional calls to glVertexAttrib4f
and glDrawArrays
in order to avoid allocating extra memory for repeated colors and index data. Note that changing a vertex attribute’s current value and the start
offset used for each draw should still be significantly cheaper than binding new vertex buffers and resetting vertex array state.
Upvotes: 1