commit 2d203318d3ae740067b447970b54c090d0554a76
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Mon Dec 10 15:42:59 2018 +0000

    MeshCubes: Correct texturing of side walls. (still not everything is right there)

diff --git a/src/main/java/org/distorted/library/mesh/MeshCubes.java b/src/main/java/org/distorted/library/mesh/MeshCubes.java
index 173c313..98ad025 100644
--- a/src/main/java/org/distorted/library/mesh/MeshCubes.java
+++ b/src/main/java/org/distorted/library/mesh/MeshCubes.java
@@ -674,7 +674,19 @@ public class MeshCubes extends MeshBase
      }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-   
+// TODO
+// Texturing of side walls still not quite right. Example:
+
+// 5x5x4 :
+//
+// 1 1 1 1 1
+// 1 0 0 0 1
+// 1 0 0 0 1
+// 1 0 0 0 1
+// 1 1 1 1 1
+//
+// - then the right and bottom internal walls don't look right.
+
   private void addSideVertex(Edge curr, boolean back, int slice, int side, float[] attribs)
      {
      //android.util.Log.e("CUBES", "adding Side vertex!");
@@ -705,18 +717,19 @@ public class MeshCubes extends MeshBase
                    /////////////////////////////////////////////////////////////////
                    // clever stuff so that the sides of the mesh are textured nicely
                    tmp1 = (float)(row-slice)/mRows;
-                   tmp2 = 1.0f-tmp1;
+                   tmp2 = tmp1+1.0f;
 
-                   if( tmp1 != (int)tmp1 )
+                   if( tmp2 != (int)tmp2 )
                      {
-                     tmp2 = tmp2 - (int)tmp2;
-                     if (tmp2 < 0.0f ) tmp2 += 1.0f;
-                     if ( ((int)tmp1)%2 == 1) tmp2 = 1.0f - tmp2;
+                     if( tmp2<0.0f ) tmp2 = tmp2 - (int)tmp2 + 1.0f;
+                     else            tmp2 = tmp2 - (int)tmp2;
+
+                     if( ((int)tmp1)%2 != 0 ) tmp2 = 1.0f-tmp2;
                      }
                    else
                      {
-                     tmp2 = ((int)tmp1)%2;
-                     if( tmp2 <0.0f ) tmp2 = -tmp2;
+                     tmp2 = ((int)tmp2)%2;
+                     if( tmp2<0.0f ) tmp2= -tmp2;
                      }
                    /////////////////////////////////////////////////////////////////
                    // end clever stuff
@@ -744,19 +757,20 @@ public class MeshCubes extends MeshBase
 
                    /////////////////////////////////////////////////////////////////
                    // clever stuff so that the sides of the mesh are textured nicely
-                   tmp1 = (float)(row-slice)/mRows;
-                   tmp2 = 1.0f-tmp1;
+                   tmp1 = (float)(row+slice)/mRows;
+                   tmp2 = tmp1-1.0f;
 
-                   if( tmp1 != (int)tmp1 )
+                   if( tmp2 != (int)tmp2 )
                      {
-                     tmp2 = tmp2 - (int)tmp2;
-                     if (tmp2 < 0.0f ) tmp2 += 1.0f;
-                     if ( ((int)tmp1)%2 == 0 ) tmp2 = 1.0f - tmp2;
+                     if( tmp2<0.0f ) tmp2 = tmp2 - (int)tmp2 + 1.0f;
+                     else            tmp2 = tmp2 - (int)tmp2;
+
+                     if( ((int)tmp1)%2 == 0 ) tmp2 = 1.0f-tmp2;
                      }
                    else
                      {
-                     tmp2 = 1-((int)tmp1)%2;
-                     if( tmp2 <0.0f ) tmp2 = -tmp2;
+                     tmp2 = ((int)tmp2)%2;
+                     if( tmp2<0.0f ) tmp2= -tmp2;
                      }
                    /////////////////////////////////////////////////////////////////
                    // end clever stuff
@@ -782,7 +796,27 @@ public class MeshCubes extends MeshBase
                    attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorr*mInflateY[row][col]/2.0f;
                    attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorr*z;
 
-                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = (float)(col-slice)/mCols;
+                   /////////////////////////////////////////////////////////////////
+                   // clever stuff so that the sides of the mesh are textured nicely
+                   tmp1 = (float)(col-slice)/mCols;
+                   tmp2 = tmp1+1.0f;
+
+                   if( tmp2 != (int)tmp2 )
+                     {
+                     if( tmp2<0.0f ) tmp2 = tmp2 - (int)tmp2 + 1.0f;
+                     else            tmp2 = tmp2 - (int)tmp2;
+
+                     if( ((int)tmp1)%2 != 0 ) tmp2 = 1.0f-tmp2;
+                     }
+                   else
+                     {
+                     tmp2 = ((int)tmp2)%2;
+                     if( tmp2<0.0f ) tmp2= -tmp2;
+                     }
+                   /////////////////////////////////////////////////////////////////
+                   // end clever stuff
+
+                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = tmp2;
                    attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - y;
                    break;
        case EAST : row = (back  ? (curr.row):(curr.row+1));
@@ -803,35 +837,31 @@ public class MeshCubes extends MeshBase
                    attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorr*mInflateY[row][col]/2.0f;
                    attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorr*z;
 
-                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = (float)(col+slice)/mCols;
-                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - y;
-                   break;
-       }
-/*
-     float tex0 =  attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ];
-     float tex1 =  attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1];
-
-     if(tex0<0.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = tex0 - (int)tex0;
-     if(tex1<0.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = tex1 - (int)tex1;
-
-     if(tex0>1.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = tex0 - (int)tex0;
-     if(tex1>1.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = tex1 - (int)tex1;
-
-   //  tex0 =  attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ];
-   //  tex1 =  attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1];
-
-  float t = -1.4f;
+                   /////////////////////////////////////////////////////////////////
+                   // clever stuff so that the sides of the mesh are textured nicely
+                   tmp1 = (float)(col+slice)/mCols;
+                   tmp2 = tmp1-1.0f;
 
-  float r = t - (int)t;
-  android.util.Log.e("mesh", "r="+r);
+                   if( tmp2 != (int)tmp2 )
+                     {
+                     if( tmp2<0.0f ) tmp2 = tmp2 - (int)tmp2 + 1.0f;
+                     else            tmp2 = tmp2 - (int)tmp2;
 
+                     if( ((int)tmp1)%2 == 0 ) tmp2 = 1.0f-tmp2;
+                     }
+                   else
+                     {
+                     tmp2 = ((int)tmp2)%2;
+                     if( tmp2<0.0f ) tmp2= -tmp2;
+                     }
+                   /////////////////////////////////////////////////////////////////
+                   // end clever stuff
 
-     if( curr.side==NORTH )
-       {
-       android.util.Log.e("mesh", "tex0:" + tex0 + " slice=" + slice+" row="+row+" col="+col);
-       android.util.Log.e("mesh", "tex1:" + tex1 + " slice=" + slice+" row="+row+" col="+col);
+                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = tmp2;
+                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - y;
+                   break;
        }
-*/
+
      currVert++;
      }
 
