commit e5d9b235d5dd334e5b6e757b238b3352052aa189
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Wed Aug 3 00:42:26 2016 +0100

    Further simplify and speed up the DistortedCubes target.

diff --git a/src/main/java/org/distorted/library/DistortedCubesGrid.java b/src/main/java/org/distorted/library/DistortedCubesGrid.java
index 6af646a..2c90023 100644
--- a/src/main/java/org/distorted/library/DistortedCubesGrid.java
+++ b/src/main/java/org/distorted/library/DistortedCubesGrid.java
@@ -413,32 +413,12 @@ class DistortedCubesGrid extends DistortedObjectGrid
    
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-   private int addFrontVertex(int corner, int vertex, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
+   private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
      {
      remainingVert--;
 
-     float x=0,y=0;
-     int index=0;
-
-     switch(corner)
-       {
-       case NW: x = (float)col/mCols;
-                y = (float)row/mRows;
-                index = 0;
-                break;
-       case SW: x = (float)col/mCols;
-                y = (float)(row+1)/mRows;
-                index = 1;
-                break;
-       case NE: x = (float)(col+1)/mCols;
-                y = (float)row/mRows;
-                index = 2;
-                break;
-       case SE: x = (float)(col+1)/mCols;
-                y = (float)(row+1)/mRows;
-                index = 3;
-                break;
-       }
+     float x = (float)col/mCols;
+     float y = (float)row/mRows;
 
      position[3*vertex  ] = x-0.5f;
      position[3*vertex+1] = 0.5f-y;
@@ -464,17 +444,17 @@ class DistortedCubesGrid extends DistortedObjectGrid
 
      //android.util.Log.d("CUBES", "buildFrontBack");
 
-     for(int i=0; i<mRows; i++)
+     for(int row=0; row<mRows; row++)
        {
        last =0;
          
-       for(int j=0; j<mCols; j++)
+       for(int col=0; col<mCols; col++)
          {
-         current = mCubes[i][j];
+         current = mCubes[row][col];
 
          if( current%2 == 1 )
            {
-           currentBlockIsNE = isNE(i,j);
+           currentBlockIsNE = isNE(row,col);
 
            if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
              {
@@ -483,19 +463,34 @@ class DistortedCubesGrid extends DistortedObjectGrid
              vertex = repeatLast(vertex,position,normal,texture);
              }
 
-           createNormals(front,i,j);
+           createNormals(front,row,col);
 
-           if( (last!=current) || (lastBlockIsNE^currentBlockIsNE) )
+           if( currentBlockIsNE )
              {
-             if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
-             vertex= addFrontVertex( currentBlockIsNE ? NW:SW, vertex, vectZ, j, i, position, normal, texture);
-             if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
-             if( (lastBlockIsNE^currentBlockIsNE)
-                 || (!front && !seenLand)       ) vertex = repeatLast(vertex,position,normal,texture);
-             vertex= addFrontVertex( currentBlockIsNE ? SW:NW, vertex, vectZ, j, i, position, normal, texture);
+             if( (last!=current) || !lastBlockIsNE )
+               {
+               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
+               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
+               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
+               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
+               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
+               }
+             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, position, normal, texture);
+             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
+             }
+           else
+             {
+             if( (last!=current) || lastBlockIsNE )
+               {
+               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
+               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
+               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
+               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
+               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
+               }
+             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
+             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , position, normal, texture);
              }
-           vertex= addFrontVertex( currentBlockIsNE ? NE:SE, vertex, vectZ, j, i, position, normal, texture);
-           vertex= addFrontVertex( currentBlockIsNE ? SE:NE, vertex, vectZ, j, i, position, normal, texture);
 
            seenLand = true;
            lastBlockIsNE = currentBlockIsNE;
@@ -512,12 +507,12 @@ class DistortedCubesGrid extends DistortedObjectGrid
 
    private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
      {
-     remainingVert--;
-
      //android.util.Log.e("CUBES", "repeating last vertex!");
 
      if( vertex>0 )
        {
+       remainingVert--;
+
        position[3*vertex  ] = position[3*vertex-3];
        position[3*vertex+1] = position[3*vertex-2];
        position[3*vertex+2] = position[3*vertex-1];
@@ -730,6 +725,8 @@ class DistortedCubesGrid extends DistortedObjectGrid
  */
    public DistortedCubesGrid(int cols, String desc, boolean frontOnly)
       {
+      //android.util.Log.d("CUBES","calculating dataLength...");
+
       prepareDataStructures(cols,desc,frontOnly);
        
       int numVertices=0;
@@ -737,6 +734,8 @@ class DistortedCubesGrid extends DistortedObjectGrid
       float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
       float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
 
+      //android.util.Log.d("CUBES","building front grid...");
+
       numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
       
       if( !frontOnly )
@@ -748,7 +747,13 @@ class DistortedCubesGrid extends DistortedObjectGrid
 
           numVertices = repeatLast(numVertices,positionData,normalData,textureData);
           }
+
+        //android.util.Log.d("CUBES","building side grid...");
+
         numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
+
+        //android.util.Log.d("CUBES","building back grid...");
+
         numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
         }
       
