commit b2913a86137d24ba8fad35cb1b73d80b0b3a9127
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Thu Jun 8 17:41:47 2023 +0200

    build for API 34.

diff --git a/build.gradle b/build.gradle
index d1ae542..8088650 100644
--- a/build.gradle
+++ b/build.gradle
@@ -7,11 +7,11 @@ android {
             keyAlias = 'distorted'
         }
     }
-    compileSdkVersion 32
+    compileSdkVersion 34
 
     defaultConfig {
         minSdkVersion 21
-        targetSdkVersion 32
+        targetSdkVersion 34
     }
 
     buildTypes {
diff --git a/src/main/java/org/distorted/library/mesh/MeshPolygon.java b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
index 719c189..5933574 100644
--- a/src/main/java/org/distorted/library/mesh/MeshPolygon.java
+++ b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
@@ -33,16 +33,23 @@ import org.distorted.library.main.DistortedLibrary;
  */
 public class MeshPolygon extends MeshBase
   {
+  private static final int SHAPE_DD = 0;
+  private static final int SHAPE_DU = 1;
+  private static final int SHAPE_UD = 2;
+  private static final int SHAPE_UU = 3;
+  private static final int SHAPE_DUD= 4;
+
   private float[] mPolygonVertices;
   private int mNumPolygonVertices;
   private float[] mPolygonBands;
   private int mNumPolygonBands;
+  private boolean[] mEdgeUp;
 
   private int remainingVert;
   private int numVertices;
   private int extraIndex, extraVertices;
 
-  private float[] mBandQuot;
+  //private float[] mBandQuot;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // polygonVertices>=3 , polygonBands>=2
@@ -61,7 +68,7 @@ public class MeshPolygon extends MeshBase
 
      remainingVert = numVertices;
      }
-
+/*
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private void computeCache()
@@ -78,7 +85,7 @@ public class MeshPolygon extends MeshBase
       mBandQuot[band] = (mPolygonBands[2*prev+1]-mPolygonBands[2*next+1]) / (mPolygonBands[2*next]-mPolygonBands[2*prev]);
       }
     }
-
+*/
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private float getSpecialQuot(int index)
@@ -129,11 +136,36 @@ public class MeshPolygon extends MeshBase
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot, float[] attribs1, float[] attribs2)
+  private float computeZEdge(float quot)
+    {
+    if( quot>=1.0f ) return 0.0f;
+
+    for(int band=1; band<mNumPolygonBands; band++)
+      {
+      float curr = mPolygonBands[2*band];
+
+      if( curr<=quot )
+        {
+        float prev = mPolygonBands[2*band-2];
+        float prevH= mPolygonBands[2*band-1];
+        float currH= mPolygonBands[2*band+1];
+
+        float A = (prev-quot)/(prev-curr);
+
+        return A*currH + (1-A)*prevH;
+        }
+      }
+
+    return 0.0f;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
+                        int edgeShape, float[] attribs1, float[] attribs2)
     {
     remainingVert--;
 
-    float vx,vy,vz;
     float Xfirst= mPolygonVertices[2*polyVertex  ];
     float Yfirst= mPolygonVertices[2*polyVertex+1];
     float Xlast = mPolygonVertices[2*polyEndVer  ];
@@ -141,33 +173,31 @@ public class MeshPolygon extends MeshBase
 
     float xEdge = Xfirst + quot*(Xlast-Xfirst);
     float yEdge = Yfirst + quot*(Ylast-Yfirst);
+    float zEdge;
 
-    float x = mPolygonBands[2*polyBand]*xEdge;
-    float y = mPolygonBands[2*polyBand]*yEdge;
-
-    if( quot==0.0f || quot==1.0f )
-      {
-      vx = mBandQuot[polyBand]*xEdge;
-      vy = mBandQuot[polyBand]*yEdge;
-      vz = xEdge*xEdge + yEdge*yEdge;
-      }
-    else
+    switch(edgeShape)
       {
-      vx = mBandQuot[polyBand]*(Ylast-Yfirst);
-      vy = mBandQuot[polyBand]*(Xfirst-Xlast);
-      float tmp = Xfirst*Ylast - Xlast*Yfirst;
-      vz = (tmp<0 ? -tmp:tmp);
+      case SHAPE_DD : zEdge = 0.0f;
+                      break;
+      case SHAPE_UU : zEdge = mPolygonBands[2*mNumPolygonBands-1];
+                      break;
+      case SHAPE_DU : zEdge = quot>=0.5f ? mPolygonBands[2*mNumPolygonBands-1] : computeZEdge(1-2*quot);
+                      break;
+      case SHAPE_UD : zEdge = quot<=0.5f ? mPolygonBands[2*mNumPolygonBands-1] : computeZEdge(2*quot-1);
+                      break;
+      default       : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1);
+                      break;
       }
 
-    float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
+    float q =  mPolygonBands[2*polyBand];
+
+    float x = q*xEdge;
+    float y = q*yEdge;
+    float z = q*zEdge + mPolygonBands[2*polyBand+1];
 
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x;
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
-    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = mPolygonBands[2*polyBand+1];
-
-    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = vx/len;
-    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = vy/len;
-    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = vz/len;
+    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
 
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
@@ -177,16 +207,57 @@ public class MeshPolygon extends MeshBase
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2)
+  private void addVertexNormal(int vertex, int end1, int end2, float[] attribs1)
+    {
+    android.util.Log.e("D", "vertex="+vertex+" end1="+end1+" end2="+end2);
+
+    int iv = VERT1_ATTRIBS*vertex + POS_ATTRIB;
+    int i1 = VERT1_ATTRIBS*end1   + POS_ATTRIB;
+    int i2 = VERT1_ATTRIBS*end2   + POS_ATTRIB;
+
+    float vx = attribs1[iv  ];
+    float vy = attribs1[iv+1];
+    float vz = attribs1[iv+2];
+
+    float x1 = attribs1[i1  ];
+    float y1 = attribs1[i1+1];
+    float z1 = attribs1[i1+2];
+
+    float x2 = attribs1[i2  ];
+    float y2 = attribs1[i2+1];
+    float z2 = attribs1[i2+2];
+
+    float dx1 = vx-x1;
+    float dy1 = vy-y1;
+    float dz1 = vz-z1;
+
+    float dx2 = vx-x2;
+    float dy2 = vy-y2;
+    float dz2 = vz-z2;
+
+    float cx = dy1*dz2 - dz1*dy2;
+    float cy = dz1*dx2 - dx1*dz2;
+    float cz = dx1*dy2 - dy1*dx2;
+
+    float len = (float)Math.sqrt(cx*cx + cy*cy + cz*cz);
+
+    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
+
+    attribs1[index  ] = cx/len;
+    attribs1[index+1] = cy/len;
+    attribs1[index+2] = cz/len;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private int createBandStrip(int vertex, int polyBand, int polyVertex, int edgeShape, float[] attribs1, float[] attribs2)
     {
+    int initVertex = vertex;
+
     if( polyVertex==0 )
       {
-      vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
-
-      if( polyBand>0 )
-        {
-        vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
-        }
+      vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2);
+      if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2);
       }
 
     boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
@@ -210,23 +281,65 @@ public class MeshPolygon extends MeshBase
         quot2 = getQuot(index+1,polyBand  , isExtra);
         }
 
-      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
-      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
+      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,edgeShape,attribs1,attribs2);
+      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,edgeShape,attribs1,attribs2);
+      }
+
+    if( polyVertex==0 )
+      {
+      if( polyBand>0 ) addVertexNormal(initVertex,initVertex+3,initVertex+2,attribs1);
+      else             addVertexNormal(initVertex,initVertex+2,initVertex+1,attribs1);
+      }
+    else
+      {
+      addVertexNormal(initVertex,initVertex-1,initVertex+1,attribs1);
+      }
+
+    boolean lower = (polyVertex>0 || polyBand>0);
+
+    for(int index=initVertex+1; index<vertex-1; index++)
+      {
+      addVertexNormal(index,(lower ? index+2 : index-1),index+1,attribs1);
+      lower = !lower;
       }
 
+    addVertexNormal(vertex-1,vertex-2,vertex-3,attribs1);
+
     return vertex;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private int computeEdgeShape(int curr)
+    {
+    if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD;
+
+    int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1);
+    int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1);
+
+    boolean l = mEdgeUp[prev];
+    boolean r = mEdgeUp[next];
+
+    return l ? (r ? SHAPE_UU : SHAPE_UD) : (r ? SHAPE_DU : SHAPE_DUD);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private void buildGrid(float[] attribs1, float[] attribs2)
     {
     int vertex=0;
 
+    int[] edgeShape = new int[mNumPolygonVertices];
+
+    for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
+      edgeShape[polyVertex] = computeEdgeShape(polyVertex);
+
     for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
       for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
         {
-        vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
+        android.util.Log.e("D", "creating strip polyBand="+polyBand+" polyVertex="+polyVertex+" : "+vertex);
+        vertex = createBandStrip(vertex,polyBand,polyVertex,edgeShape[polyVertex],attribs1,attribs2);
+        android.util.Log.e("D", "  "+vertex);
         }
     }
 
@@ -243,6 +356,14 @@ public class MeshPolygon extends MeshBase
  *                   From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
  *                   its elevation). The polygon is split into such concentric bands.
  *                   Must be band[2*i] > band[2*(i+1)] !
+ * @param edgeUp     N booleans - one for each edge; the first connects vertices (0,1) and (2,3);
+ *                   then just like 'verticesXY', i.e. counterclockwise.
+ *                   If this is null, all edges are by default 'down'.
+ *                   'Down' means that edge's Z is equal to 0; 'up' means that its Z, at least in its
+ *                   middle, is equal to the highest elevation in the middle of the mesh.
+ *                   If the 'previous' edge is also up, then the Z is up horizontally from its middle
+ *                   to the left vertex; else it goes down along the same function it goes along the bands.
+ *                   Same with the right half of the edge.
  * @param exIndex    This and the next parameter describe how to make the mesh denser at the
  *                   polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the
  *                   outermost band (and 2 triangles of the next band, and 1 triangle of the third
@@ -252,7 +373,7 @@ public class MeshPolygon extends MeshBase
  *                   all bands go to.
  * @param centerY    Y coordinate of the center.
  */
-  public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices, float centerX, float centerY)
+  public MeshPolygon(float[] verticesXY, float[] bands, boolean[] edgeUp, int exIndex, int exVertices, float centerX, float centerY)
     {
     super();
 
@@ -260,6 +381,7 @@ public class MeshPolygon extends MeshBase
     mPolygonBands      = bands;
     mNumPolygonVertices= mPolygonVertices.length /2;
     mNumPolygonBands   = mPolygonBands.length /2;
+    mEdgeUp            = edgeUp;
     extraIndex         = exIndex;
     extraVertices      = exVertices;
 
@@ -273,7 +395,7 @@ public class MeshPolygon extends MeshBase
       }
 
     computeNumberOfVertices();
-    computeCache();
+    //computeCache();
 
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
@@ -301,7 +423,7 @@ public class MeshPolygon extends MeshBase
 
   public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
     {
-    this(verticesXY,bands,exIndex,exVertices,0.0f,0.0f);
+    this(verticesXY,bands,null,exIndex,exVertices,0.0f,0.0f);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -311,7 +433,7 @@ public class MeshPolygon extends MeshBase
  */
   public MeshPolygon(float[] verticesXY, float[] bands)
     {
-    this(verticesXY,bands,0,0,0.0f,0.0f);
+    this(verticesXY,bands,null,0,0,0.0f,0.0f);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
