Advertisement
Guest User

Untitled

a guest
Jun 10th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. public Mesh getFromSurfaceNet(List<double[]> vertices, List<int[]> indexes) {
  2.         int[] meshIndexes;
  3.         Vector3f [] meshVertices = new Vector3f[vertices.size() * 4];
  4.         Vector2f[] meshTexCoords = new Vector2f[vertices.size() * 4];
  5.        
  6.        
  7.         //Pre calculate the index and vertice buffer size:
  8.         int indexBufferSize = 0;
  9.        
  10.         Iterator<int[]> countIndexes = indexes.iterator();
  11.         while (countIndexes.hasNext()) {
  12.             int[] array = countIndexes.next();
  13.             for (int i = 0; i < array.length; i++) {
  14.                 indexBufferSize++;
  15.             }
  16.         }
  17.        
  18.         meshIndexes = new int[indexBufferSize];
  19.        
  20.         //Process vertices
  21.         Iterator<double[]> vertIter = vertices.iterator();
  22.         int verticeCount = 0;        
  23.        
  24.         while (vertIter.hasNext()) {
  25.             double[] verticeQuadlet = vertIter.next();
  26.             for (int i = 0; i < verticeQuadlet.length; i++) {
  27.                 if ((verticeQuadlet.length - 3) > -1) {
  28.                     Vector3f vec = new Vector3f((float)verticeQuadlet[i], (float)verticeQuadlet[i+1], (float)verticeQuadlet[+2]);
  29.                     meshVertices[verticeCount] = vec;
  30.                     i++; //Skip the next two floats, we have buffered them in vec.
  31.                     i++;
  32.                     verticeCount++;
  33.                 }
  34.             }
  35.            
  36.         }
  37.        
  38.         //Check work:
  39.         for (int i = 0; i < meshVertices.length; i++) {
  40.             //log("Vertice: " + meshVertices[i].toString());
  41.         }
  42.         //End check
  43.        
  44.         log("Before index unpack");
  45.         //Process indexes
  46.         Iterator<int[]> indexIter = indexes.iterator();
  47.        
  48.         int indexCount = 0;
  49.         while (indexIter.hasNext()) {
  50.             int[] temp = indexIter.next();
  51.             for (int i = 0; i < temp.length; i++) {
  52.                 if (temp[i] == 0) {
  53.                     log("ZERO ALERT ############################################");
  54.                 }
  55.                 meshIndexes[indexCount + i] = temp[i];
  56.             }
  57.            
  58.             indexCount++;
  59.         }
  60.        
  61.         //I currently trim this later as I get 5 times as many 0 entries as real ones print out at this point.
  62.         //it doesn't seem to affect the rendering at all though when they are trimmed
  63.         //Check work:
  64.         for (int i = 0; i < meshIndexes.length; i++) {
  65.             //log("Index @ : " + i + " || " + meshIndexes[i]);
  66.         }
  67.         //End check
  68.        
  69.        
  70.         //Add texture coords:
  71.         int count = 0;
  72.         for (int i = 0; i < meshTexCoords.length; i++) {
  73.            
  74.             switch (count) {
  75.                 case 0:
  76.                     meshTexCoords[i] = new Vector2f(0,0);
  77.                     break;
  78.    
  79.                 case 1:
  80.                     meshTexCoords[i] = new Vector2f(1, 0);
  81.                     break;
  82.  
  83.                 case 2:
  84.                     meshTexCoords[i] = new Vector2f(0, 1);
  85.                     break;
  86.  
  87.                 case 3:
  88.                     meshTexCoords[i] = new Vector2f(1, 1);
  89.                     break;
  90.             }
  91.             //log("COUNT: " + count);
  92.             count = count + 1;
  93.            
  94.             if (count == 3) {
  95.                 count = 0;
  96.             }
  97.         }
  98.         //Check work:
  99.         for (Vector2f meshTexCoord : meshTexCoords) {
  100.             //log("Tex Coords: " + meshTexCoord);
  101.         }
  102.         //End check
  103.        
  104.         log("Culling indexes:");
  105.         log("Current meshIndexes length: " + meshIndexes.length);
  106.         int goodCount = 0;
  107.         for (int i = 0; i < meshIndexes.length; i++) {
  108.             if (meshIndexes[i] != 0) {
  109.                 goodCount++;
  110.             }
  111.         }
  112.        
  113.         int[] goodIndexes = new int[goodCount];
  114.         int goodTicker = 0;
  115.         for (int i = 0; i < meshIndexes.length; i++) {
  116.             if (meshIndexes[i] != 0) {
  117.                 goodIndexes[goodTicker] = meshIndexes[i];
  118.                 goodTicker++;
  119.             }
  120.         }
  121.         log("trimmed meshIndexes to goodIndexes, length: " + goodIndexes.length);
  122.        
  123.         Mesh mesh = new Mesh();
  124.        
  125.         mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(meshVertices));
  126.         mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(meshTexCoords));
  127.         mesh.setBuffer(Type.Index,    3, BufferUtils.createIntBuffer(goodIndexes));
  128.         mesh.updateBound();
  129.        
  130.        
  131.         return mesh;
  132.        
  133.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement