commit ac6a08e773083f81730ca68a6918210a0b91db67
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Aug 28 15:56:24 2020 +0100

    Progress with MeshPolygon.

diff --git a/src/main/java/org/distorted/library/mesh/MeshPolygon.java b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
index 30da3d1..ca2cb65 100644
--- a/src/main/java/org/distorted/library/mesh/MeshPolygon.java
+++ b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
@@ -43,6 +43,7 @@ public class MeshPolygon extends MeshBase
   private void computeNumberOfVertices(int numPolygonVertices, int numPolygonBands)
      {
      numVertices = (numPolygonVertices*numPolygonBands+2)*(numPolygonBands-1) - 1;
+     remainingVert = numVertices;
      }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -51,32 +52,62 @@ public class MeshPolygon extends MeshBase
     {
     remainingVert--;
 
-    float band = mPolygonBands[2*polyBand  ];
-    float elev = mPolygonBands[2*polyBand+1];
+    if( polyBand<mNumPolygonBands-1 )
+      {
+      float band = mPolygonBands[2*polyBand  ];
+      float elev = mPolygonBands[2*polyBand+1];
+
+      float Xfirst  = mPolygonVertices[2*polyVertex  ];
+      float Yfirst  = mPolygonVertices[2*polyVertex+1];
+
+      int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
+
+      float Xlast   = mPolygonVertices[2*polyEndVer  ];
+      float Ylast   = mPolygonVertices[2*polyEndVer+1];
+
+      float quot = (float)index / (mNumPolygonBands-1-polyBand);
 
-    float Xfirst  = mPolygonVertices[2*polyVertex  ]*band;
-    float Yfirst  = mPolygonVertices[2*polyVertex+1]*band;
+      float xEdge = Xfirst + quot*(Xlast-Xfirst);
+      float yEdge = Yfirst + quot*(Ylast-Yfirst);
 
-    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
+      float x = band*xEdge;
+      float y = band*yEdge;
 
-    float Xlast   = mPolygonVertices[2*polyEndVer  ]*band;
-    float Ylast   = mPolygonVertices[2*polyEndVer+1]*band;
+      int nextPolyBand = polyBand+1;
+      int prevPolyBand = (polyBand==0 ? polyBand : polyBand-1);
 
-    float quot = (float)index / (polyBand-1);
+      float concentricStep = mPolygonBands[2*nextPolyBand]-mPolygonBands[2*prevPolyBand];
+      float nx = concentricStep*xEdge;
+      float ny = concentricStep*yEdge;
 
-    float x = Xfirst + quot*(Xlast-Xfirst);
-    float y = Yfirst + quot*(Ylast-Yfirst);
+      float nz = mPolygonBands[2*prevPolyBand+1]-mPolygonBands[2*nextPolyBand+1];
+      float lenXYZ = (float)Math.sqrt(nx*nx + ny*ny + nz*nz);
+      float lenXY  = (float)Math.sqrt(nx*nx + ny*ny);
 
-    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] =    x;
-    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] =    y;
-    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = elev;
+      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] =    x;
+      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] =    y;
+      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = elev;
 
-    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;  //
-    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;  // TODO
-    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;  //
+      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = (nz*nx) / (lenXY*lenXYZ);
+      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = (nz*ny) / (lenXY*lenXYZ);
+      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = lenXY / lenXYZ;
 
-    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
-    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
+      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
+      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
+      }
+    else
+      {
+      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = 0.0f;
+      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = 0.0f;
+      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = mPolygonBands[2*polyBand+1];
+
+      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
+      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
+      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
+
+      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = 0.5f;
+      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f;
+      }
 
     return vertex+1;
     }
@@ -112,10 +143,10 @@ public class MeshPolygon extends MeshBase
     {
     int vertex=0;
 
-    for(int band=0; band<mNumPolygonBands-1; band++)
+    for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
       for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
         {
-        vertex = createBandStrip(vertex,band,polyVertex,attribs1,attribs2);
+        vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
         }
     }
 
@@ -129,6 +160,7 @@ public class MeshPolygon extends MeshBase
  * @param bands      2K floats; K pairs of two floats each describing a single band.
  *                   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)] !
  */
   public MeshPolygon(float[] verticesXY, float[] bands)
     {
@@ -137,7 +169,7 @@ public class MeshPolygon extends MeshBase
     mPolygonVertices      = verticesXY;
     mPolygonBands         = bands;
     mNumPolygonVertices   = mPolygonVertices.length /2;
-    mNumPolygonBands      = mPolygonBands.length;
+    mNumPolygonBands      = mPolygonBands.length /2;
 
     computeNumberOfVertices(mNumPolygonVertices,mNumPolygonBands);
 
