Skip to content

Instantly share code, notes, and snippets.

@GideonPARANOID
Created May 7, 2015 09:58
Show Gist options
  • Save GideonPARANOID/3af1e399af6f4666d88a to your computer and use it in GitHub Desktop.
Save GideonPARANOID/3af1e399af6f4666d88a to your computer and use it in GitHub Desktop.
Operation for constructing a grid of points & respective draw order using an unbroken line of two zig zags - most efficient for larger grids
int length = 5;
float[] vertices = new float[length * length * 3];
short[] drawOrder = new short[(length * length * 2) - 1];
// building up grid points
{
int position = 0;
for (float i = 0f; i < length; i++) {
for (float j = 0f; j < length; j++) {
vertices[position++] = i * unitSize;
vertices[position++] = 0f;
vertices[position++] = j * unitSize;
}
}
}
// building up the draw order
{
boolean up = true, right = true;
// horizontal zig zag
for (int vertexIndex = 0, i = 0, max = length * length; i < max; i++) {
drawOrder[i] = (short) vertexIndex;
if (vertexIndex >= ((length * length) - length) && right) {
right = false;
vertexIndex++;
} else if (vertexIndex < length && !right) {
right = true;
vertexIndex++;
} else {
vertexIndex += (right ? length : -length);
}
}
// vertical zig zag
for (int vertexIndex = 0, i = (length * length * 2) - 2, max = length * length;
i >= max; i--) {
drawOrder[i] = (short) vertexIndex;
if ((vertexIndex + 1) % length == 0 && up) {
up = false;
vertexIndex += length;
} else if (vertexIndex % length == 0 && !up) {
up = true;
vertexIndex += length;
} else {
vertexIndex += (up ? 1 : -1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment