When creating the vertexbuffer, the term i * n + j
is used to calculate the index of the current vertex. j
is used for the position on the x-axis. So if you increment j
and keep i
, you'll get to the vertex to the right on the same row. The index is also increased by 1. If you increment i
and keep j
, you'll get to the vertex below the current one. The index is increased by n
(the width of the field). This is natural, because the vertices in between have to be there.
Using a width of n = 4
, you get the following numbering
\ j| 0 | 1 | 2 | 3 |
i \| | | | |
---+----+----+----+----+
0 | 0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 | 7 |
2 | 8 | 9 | 10 | 11 |
...
You see that moving from row i
to row i+1
results in the index being increased by the fields width, while going from column j
to j+1
results in the index increased by 1. This results in the formula j + i * n
.
The same formula is used for the calculation of the index buffer.
The first triangle is:
col j , row i -> j + i * n
col j + 1, row i -> j + 1 + i * n
col j , row i + 1 -> j + (i + 1) * n