glDrawElements
can be used to save you from uploading a lot of vertices to the GPU.
Many 2D games use tiles, which are rendered in grids.
I have done some experimenting with the indices array. For a 3x3 grid of tiles, the indices for glDrawElements
are as follows:
- 0 1 2 2 1 3 -|- 2 3 4 4 3 5 -|- 4 5 6 6 5 7
- 1 8 3 3 8 9 -|- 3 9 5 5 9 10 -|- 5 10 7 7 10 11
- 8 12 9 9 12 13 -|- 9 13 10 10 13 14 -|- 10 14 11 11 14 15
Already, calculating this is incredibly tedious and the grid is only a 3x3 one.
The proportion of vertices used with glDrawElements
for power of two grids is (s + 1)^2 / s^2 * 6
, where s
is the grid width or height. Here are some grid sizes and savings:
- 10x10 : <0.2
- 30x20 : <0.18
- 50x50 : >0.17
- 100x100 : 0.17
How could one generate an array of indices in the correct order for a grid of tiles, of either width w
and height h
(or size s
for power of two grids if that's more simple)?