commit eeb5d11578b9c7ea8e765a0fb355391f18d4f405
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sat Aug 29 14:25:02 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 ca2cb65..9670061 100644
--- a/src/main/java/org/distorted/library/mesh/MeshPolygon.java
+++ b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
@@ -37,6 +37,8 @@ public class MeshPolygon extends MeshBase
   private int remainingVert;
   private int numVertices;
 
+  private float[] mBandQuot;
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // polygonVertices>=3 , polygonBands>=2
 
@@ -48,66 +50,68 @@ public class MeshPolygon extends MeshBase
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private int addVertex(int vertex, int polyBand, int polyVertex, int index, float[] attribs1, float[] attribs2)
+  private void computeCache()
     {
-    remainingVert--;
-
-    if( polyBand<mNumPolygonBands-1 )
-      {
-      float band = mPolygonBands[2*polyBand  ];
-      float elev = mPolygonBands[2*polyBand+1];
+    mBandQuot = new float[mNumPolygonBands];
 
-      float Xfirst  = mPolygonVertices[2*polyVertex  ];
-      float Yfirst  = mPolygonVertices[2*polyVertex+1];
+    int next, prev;
 
-      int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
-
-      float Xlast   = mPolygonVertices[2*polyEndVer  ];
-      float Ylast   = mPolygonVertices[2*polyEndVer+1];
+    for(int band=0; band<mNumPolygonBands; band++)
+      {
+      next = (band==mNumPolygonBands-1 ? band : band+1);
+      prev = (band==                 0 ? band : band-1);
 
-      float quot = (float)index / (mNumPolygonBands-1-polyBand);
+      mBandQuot[band] = (mPolygonBands[2*prev+1]-mPolygonBands[2*next+1]) / (mPolygonBands[2*next]-mPolygonBands[2*prev]);
+      }
+    }
 
-      float xEdge = Xfirst + quot*(Xlast-Xfirst);
-      float yEdge = Yfirst + quot*(Ylast-Yfirst);
+///////////////////////////////////////////////////////////////////////////////////////////////////
 
-      float x = band*xEdge;
-      float y = band*yEdge;
+  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, int index, float[] attribs1, float[] attribs2)
+    {
+    remainingVert--;
 
-      int nextPolyBand = polyBand+1;
-      int prevPolyBand = (polyBand==0 ? polyBand : polyBand-1);
+    float quot = polyBand<mNumPolygonBands-1 ? (float)index / (mNumPolygonBands-1-polyBand) : 1.0f;
 
-      float concentricStep = mPolygonBands[2*nextPolyBand]-mPolygonBands[2*prevPolyBand];
-      float nx = concentricStep*xEdge;
-      float ny = concentricStep*yEdge;
+    float vx,vy,vz;
 
-      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);
+    float Xfirst= mPolygonVertices[2*polyVertex  ];
+    float Yfirst= mPolygonVertices[2*polyVertex+1];
+    float Xlast = mPolygonVertices[2*polyEndVer  ];
+    float Ylast = mPolygonVertices[2*polyEndVer+1];
 
-      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] =    x;
-      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] =    y;
-      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = elev;
+    float xEdge = Xfirst + quot*(Xlast-Xfirst);
+    float yEdge = Yfirst + quot*(Ylast-Yfirst);
 
-      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;
+    float x = mPolygonBands[2*polyBand]*xEdge;
+    float y = mPolygonBands[2*polyBand]*yEdge;
 
-      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
-      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
+    if( quot==0.0f || quot==1.0f )
+      {
+      vx = mBandQuot[polyBand]*xEdge;
+      vy = mBandQuot[polyBand]*yEdge;
+      vz = xEdge*xEdge + yEdge*yEdge;
       }
     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];
+      vx = mBandQuot[polyBand]*(Ylast-Yfirst);
+      vy = mBandQuot[polyBand]*(Xfirst-Xlast);
+      float tmp = Xfirst*Ylast - Xlast*Yfirst;
+      vz = (tmp<0 ? -tmp:tmp);
+      }
 
-      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;
+    float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
 
-      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = 0.5f;
-      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f;
-      }
+    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;
+
+    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
+    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
 
     return vertex+1;
     }
@@ -118,20 +122,21 @@ public class MeshPolygon extends MeshBase
     {
     if( polyVertex==0 )
       {
-      vertex = addVertex(vertex,polyBand,polyVertex,0,attribs1,attribs2);
+      vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
 
       if( polyBand>0 )
         {
-        vertex = addVertex(vertex,polyBand,polyVertex,0,attribs1,attribs2);
+        vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
         }
       }
 
     int numPairs = mNumPolygonBands-1-polyBand;
+    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
 
-    for(int pair=0; pair<numPairs; pair++)
+    for(int index=0; index<numPairs; index++)
       {
-      vertex = addVertex(vertex,polyBand+1,polyVertex,pair  ,attribs1,attribs2);
-      vertex = addVertex(vertex,polyBand  ,polyVertex,pair+1,attribs1,attribs2);
+      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,index  ,attribs1,attribs2);
+      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,index+1,attribs1,attribs2);
       }
 
     return vertex;
@@ -172,6 +177,7 @@ public class MeshPolygon extends MeshBase
     mNumPolygonBands      = mPolygonBands.length /2;
 
     computeNumberOfVertices(mNumPolygonVertices,mNumPolygonBands);
+    computeCache();
 
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
