ThinMatrix

Terrain Generation

Aug 7th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. package generation;
  2.  
  3. public class GridGenerator {
  4.    
  5.     public static int[] generateGridIndexBuffer(int vertexCount, boolean evenTile) {
  6.         int testBit = (vertexCount%2==0) && !evenTile ? 1 : 0;
  7.         int indexCount = (vertexCount - 1) * (vertexCount - 1) * 6;
  8.         int[] indices = new int[indexCount];
  9.         int pointer = 0;
  10.         for (int col = 0; col < vertexCount - 1; col++) {
  11.             for (int row = 0; row < vertexCount - 1; row++) {
  12.                 int topLeft = (row * vertexCount) + col;
  13.                 int topRight = topLeft + 1;
  14.                 int bottomLeft = ((row + 1) * vertexCount) + col;
  15.                 int bottomRight = bottomLeft + 1;
  16.                 if (row % 2 == 0) {
  17.                     pointer = storeQuad1(indices, pointer, topLeft, topRight, bottomLeft, bottomRight, col % 2 == testBit);
  18.                 } else {
  19.                     pointer = storeQuad2(indices, pointer, topLeft, topRight, bottomLeft, bottomRight, col % 2 == testBit);
  20.                 }
  21.             }
  22.         }
  23.         return indices;
  24.     }
  25.  
  26.     private static int storeQuad1(int[] indices, int pointer, int topLeft, int topRight, int bottomLeft,
  27.             int bottomRight, boolean mixed) {
  28.         indices[pointer++] = topLeft;
  29.         indices[pointer++] = bottomLeft;
  30.         indices[pointer++] = mixed ? topRight : bottomRight;
  31.         indices[pointer++] = bottomRight;
  32.         indices[pointer++] = topRight;
  33.         indices[pointer++] = mixed ? bottomLeft : topLeft;
  34.         return pointer;
  35.     }
  36.  
  37.     private static int storeQuad2(int[] indices, int pointer, int topLeft, int topRight, int bottomLeft,
  38.             int bottomRight, boolean mixed) {
  39.         indices[pointer++] = topRight;
  40.         indices[pointer++] = topLeft;
  41.         indices[pointer++] = mixed ? bottomRight : bottomLeft;
  42.         indices[pointer++] = bottomLeft;
  43.         indices[pointer++] = bottomRight;
  44.         indices[pointer++] = mixed ? topLeft : topRight;
  45.         return pointer;
  46.     }
  47.  
  48. }
Add Comment
Please, Sign In to add comment