commit 724f67eeae3a411197435a7ac78a64b54f10aef2
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Jun 9 12:59:19 2023 +0200

    MeshPolygon: fix the normals.

diff --git a/src/main/java/org/distorted/library/mesh/MeshPolygon.java b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
index f3ef746..ddee9d8 100644
--- a/src/main/java/org/distorted/library/mesh/MeshPolygon.java
+++ b/src/main/java/org/distorted/library/mesh/MeshPolygon.java
@@ -33,6 +33,7 @@ import org.distorted.library.main.DistortedLibrary;
  */
 public class MeshPolygon extends MeshBase
   {
+  private static final float NOT_DONE_YET = -1000;
   private static final int SHAPE_DD = 0;
   private static final int SHAPE_DU = 1;
   private static final int SHAPE_UD = 2;
@@ -49,7 +50,7 @@ public class MeshPolygon extends MeshBase
   private int numVertices;
   private int extraIndex, extraVertices;
 
-  //private float[] mBandQuot;
+  private float[] mBandQuot;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // polygonVertices>=3 , polygonBands>=2
@@ -68,7 +69,7 @@ public class MeshPolygon extends MeshBase
 
      remainingVert = numVertices;
      }
-/*
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private void computeCache()
@@ -85,7 +86,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)
@@ -199,6 +200,24 @@ public class MeshPolygon extends MeshBase
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
 
+    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
+
+    if( quot==0.0f || quot==1.0f )
+      {
+      float vx = mBandQuot[polyBand]*xEdge;
+      float vy = mBandQuot[polyBand]*yEdge;
+      float vz = xEdge*xEdge + yEdge*yEdge;
+      float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
+
+      attribs1[index  ] = vx/len;
+      attribs1[index+1] = vy/len;
+      attribs1[index+2] = vz/len;
+      }
+    else
+      {
+      attribs1[index  ] = NOT_DONE_YET;
+      }
+
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
 
@@ -209,43 +228,46 @@ public class MeshPolygon extends MeshBase
 
   private void addVertexNormal(int vertex, int end1, int end2, float[] attribs1)
     {
-    android.util.Log.e("D", "vertex="+vertex+" end1="+end1+" end2="+end2);
+    // 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;
+    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
 
-    float vx = attribs1[iv  ];
-    float vy = attribs1[iv+1];
-    float vz = attribs1[iv+2];
+    if( attribs1[index] == NOT_DONE_YET)
+      {
+      int iv = VERT1_ATTRIBS*vertex + POS_ATTRIB;
+      int i1 = VERT1_ATTRIBS*end1   + POS_ATTRIB;
+      int i2 = VERT1_ATTRIBS*end2   + POS_ATTRIB;
 
-    float x1 = attribs1[i1  ];
-    float y1 = attribs1[i1+1];
-    float z1 = attribs1[i1+2];
+      float vx = attribs1[iv  ];
+      float vy = attribs1[iv+1];
+      float vz = attribs1[iv+2];
 
-    float x2 = attribs1[i2  ];
-    float y2 = attribs1[i2+1];
-    float z2 = attribs1[i2+2];
+      float x1 = attribs1[i1  ];
+      float y1 = attribs1[i1+1];
+      float z1 = attribs1[i1+2];
 
-    float dx1 = vx-x1;
-    float dy1 = vy-y1;
-    float dz1 = vz-z1;
+      float x2 = attribs1[i2  ];
+      float y2 = attribs1[i2+1];
+      float z2 = attribs1[i2+2];
 
-    float dx2 = vx-x2;
-    float dy2 = vy-y2;
-    float dz2 = vz-z2;
+      float dx1 = vx-x1;
+      float dy1 = vy-y1;
+      float dz1 = vz-z1;
 
-    float cx = dy1*dz2 - dz1*dy2;
-    float cy = dz1*dx2 - dx1*dz2;
-    float cz = dx1*dy2 - dy1*dx2;
+      float dx2 = vx-x2;
+      float dy2 = vy-y2;
+      float dz2 = vz-z2;
 
-    float len = (float)Math.sqrt(cx*cx + cy*cy + cz*cz);
+      float cx = dy1*dz2 - dz1*dy2;
+      float cy = dz1*dx2 - dx1*dz2;
+      float cz = dx1*dy2 - dy1*dx2;
 
-    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
+      float len = (float)Math.sqrt(cx*cx + cy*cy + cz*cz);
 
-    attribs1[index  ] = cx/len;
-    attribs1[index+1] = cy/len;
-    attribs1[index+2] = cz/len;
+      attribs1[index  ] = cx/len;
+      attribs1[index+1] = cy/len;
+      attribs1[index+2] = cz/len;
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -395,7 +417,7 @@ public class MeshPolygon extends MeshBase
       }
 
     computeNumberOfVertices();
-    //computeCache();
+    computeCache();
 
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
