I'm not entirely sure why it works myself, but I'll explain as best I can.
First, I create a grid of vertices, constructed row by row, left to right.
For indices, I have this code for a loop:
var i = 0
var y = 0
var x = 0
while y < gridSize-1:
while x < gridSize-1:
indices.append(i+gridSize)
indices.append(i+1)
indices.append(i)
indices.append(i+gridSize)
indices.append(i+gridSize+1)
indices.append(i+1)
x+=1
i+=1
x = 0
y+=1
i+=1
I have a specific order that worked for indices(for a single plane), it goes:
- bottomleft
- topright
- topleft
- bottomleft
- bottomright
- topright
And this creates two triangles out of four vertices(top and bottom may be reversed). For the index code, I adapted this to work in a loop(with the variable i). The loop just loops over X and Y directions, adding vertices based on the variable i, which is increased every quad. Because the index code to make a quad assumes that there's vertices to the right and bottom, I subtract 1 from gridSize in the loop. Last, I have to increase i by 2 at the end of a row. I'm not entirely sure why, but it works when I do.