commit 5f2853bebe2a9aa466fafbfce2432ef7b3e717d3
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Wed May 17 11:02:58 2017 +0100

    Full support for any-depth MeshCubes.

diff --git a/src/main/java/org/distorted/library/MeshCubes.java b/src/main/java/org/distorted/library/MeshCubes.java
index 2f0dfbf..9cb21a4 100644
--- a/src/main/java/org/distorted/library/MeshCubes.java
+++ b/src/main/java/org/distorted/library/MeshCubes.java
@@ -109,10 +109,11 @@ public class MeshCubes extends MeshObject
           }
         }
 
-      int frontVert = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
-      int sideVert  = 2*( mSideWalls + mSideBends + mEdgeNum -1);
-      int firstWinding= (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0;
-      int dataL = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
+      int frontVert       = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
+      int sideVertOneSlice= 2*( mSideWalls + mSideBends + mEdgeNum -1);
+      int sideVert        = 2*(mSlices-1) + mSlices*sideVertOneSlice;
+      int firstWinding    = (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0;
+      int dataL           = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
 /*
       android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
       android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
@@ -185,8 +186,6 @@ public class MeshCubes extends MeshObject
          for(int i=0; i<mRows; i++)
            mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0);
 
-       //android.util.Log.d("cubes", "VERT STRING:"+debug(mBoundingVert,3));
-
        markRegions();
        numVertices = computeDataLength();
 
@@ -498,8 +497,9 @@ public class MeshCubes extends MeshObject
 
   private int buildIthSide(Edge curr, int vertex, float[] attribs)
      {
-     Edge prev; 
-     
+     Edge prev, next;
+     int col, row, side;
+
      if( curr.side==NORTH ) // water outside
        {
        prev = new Edge(WEST,curr.row,curr.col);
@@ -509,38 +509,36 @@ public class MeshCubes extends MeshObject
        prev = curr;
        curr = new Edge(EAST,curr.row+1,curr.col-1);
        }
-     
-     int col = curr.col;
-     int row = curr.row;
-     int side= curr.side;  
-     Edge next = getNextEdge(curr);
-     
-     addSideVertex(curr,BACK,LOWER,prev.side,vertex,attribs);
-     vertex++;
-     
-     do
+
+     for(int i=0; i<mSlices; i++)
        {
-       if( prev.side!=curr.side )
+       col = curr.col;
+       row = curr.row;
+       side= curr.side;
+       next = getNextEdge(curr);
+     
+       addSideVertex(curr,BACK,i+1,prev.side,vertex++,attribs);
+
+       do
          {
-         addSideVertex(curr,BACK,LOWER,prev.side,vertex,attribs);
-         vertex++;
-         addSideVertex(curr,BACK,UPPER,prev.side,vertex,attribs);
-         vertex++;
-         }
+         if( prev.side!=curr.side )
+           {
+           addSideVertex(curr,BACK,i+1,prev.side,vertex++,attribs);
+           addSideVertex(curr,BACK,i  ,prev.side,vertex++,attribs);
+           }
        
-       addSideVertex(curr,FRONT,LOWER,next.side,vertex,attribs);
-       vertex++;
-       addSideVertex(curr,FRONT,UPPER,next.side,vertex,attribs);
-       vertex++;
+         addSideVertex(curr,FRONT,i+1,next.side,vertex++,attribs);
+         addSideVertex(curr,FRONT,i  ,next.side,vertex++,attribs);
        
-       prev = curr;
-       curr = next; 
-       next = getNextEdge(curr);
-       }
-     while( curr.col!=col || curr.row!=row || curr.side!=side );
-     
-     vertex = repeatLast(vertex,attribs);
+         prev = curr;
+         curr = next;
+         next = getNextEdge(curr);
+         }
+       while( curr.col!=col || curr.row!=row || curr.side!=side );
      
+       vertex = repeatLast(vertex,attribs);
+       }
+
      return vertex;
      }
 
@@ -616,7 +614,7 @@ public class MeshCubes extends MeshObject
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
    
-  private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] attribs)
+  private void addSideVertex(Edge curr, boolean back, int slice,int side, int vertex, float[] attribs)
      {
      //android.util.Log.e("CUBES", "adding Side vertex!");
 
@@ -630,44 +628,44 @@ public class MeshCubes extends MeshObject
 
                    attribs[8*vertex  ] = x - 0.5f;
                    attribs[8*vertex+1] = 0.5f - (float)curr.row/mRows;
-                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
+                   attribs[8*vertex+2] = FRONTZ + ((BACKZ-FRONTZ)*slice)/mSlices;
                    attribs[8*vertex+3] = side==NORTH ? 0.0f : (side==WEST?-R:R);
                    attribs[8*vertex+4] = 1.0f;
-                   attribs[8*vertex+5] = lower ? -R:R;
+                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
                    attribs[8*vertex+6] = x;
-                   attribs[8*vertex+7] = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
+                   attribs[8*vertex+7] = 1.0f-(float)(curr.row-slice)/mRows;
                    break;
        case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
 
                    attribs[8*vertex  ] = x - 0.5f;
                    attribs[8*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
-                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
+                   attribs[8*vertex+2] = FRONTZ + ((BACKZ-FRONTZ)*slice)/mSlices;
                    attribs[8*vertex+3] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
                    attribs[8*vertex+4] =-1.0f;
-                   attribs[8*vertex+5] = lower ? -R:R;
+                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
                    attribs[8*vertex+6] = x;
-                   attribs[8*vertex+7] = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
+                   attribs[8*vertex+7] = 1.0f - (float)(curr.row+1+slice)/mRows;
                    break;
        case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
 
                    attribs[8*vertex  ] = (float)curr.col/mCols -0.5f;
                    attribs[8*vertex+1] = 0.5f - y;
-                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
+                   attribs[8*vertex+2] = FRONTZ + ((BACKZ-FRONTZ)*slice)/mSlices;
                    attribs[8*vertex+3] =-1.0f;
                    attribs[8*vertex+4] = side==WEST ? 0.0f : (side==NORTH?-R:R);
-                   attribs[8*vertex+5] = lower ? -R:R;
-                   attribs[8*vertex+6] = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
+                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
+                   attribs[8*vertex+6] = (float)(curr.col-slice)/mCols;
                    attribs[8*vertex+7] = 1.0f - y;
                    break;
        case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
 
                    attribs[8*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
                    attribs[8*vertex+1] = 0.5f - y;
-                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
+                   attribs[8*vertex+2] = FRONTZ + ((BACKZ-FRONTZ)*slice)/mSlices;
                    attribs[8*vertex+3] = 1.0f;
                    attribs[8*vertex+4] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
-                   attribs[8*vertex+5] = lower ? -R:R;
-                   attribs[8*vertex+6] = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
+                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
+                   attribs[8*vertex+6] = (float)(curr.col+1+slice)/mCols;
                    attribs[8*vertex+7] = 1.0f - y;
                    break;
        }
