commit 140f2c4ed92bad1c5068d4231621c357eca47d42
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Apr 9 15:00:40 2021 +0200

    Face cubit creation: progress, a cube renders correctly :)

diff --git a/src/main/java/org/distorted/examples/meshfile/FactoryCubit.java b/src/main/java/org/distorted/examples/meshfile/FactoryCubit.java
index fa688cf..fad77dc 100644
--- a/src/main/java/org/distorted/examples/meshfile/FactoryCubit.java
+++ b/src/main/java/org/distorted/examples/meshfile/FactoryCubit.java
@@ -263,15 +263,13 @@ class FactoryCubit
     mBuffer[1] = v1z*v2x - v2z*v1x;
     mBuffer[2] = v1x*v2y - v2x*v1y;
 
-android.util.Log.e("D", " buffer: "+mBuffer[0]+" "+mBuffer[1]+" "+mBuffer[2]);
+    float len = mBuffer[0]*mBuffer[0] + mBuffer[1]*mBuffer[1] + mBuffer[2]*mBuffer[2];
+    len = (float)Math.sqrt(len);
+    mBuffer[0] /= len;
+    mBuffer[1] /= len;
+    mBuffer[2] /= len;
 
-
-    if( mBuffer[2]<0.0f )
-      {
-      mBuffer[0] *= -1.0f;
-      mBuffer[1] *= -1.0f;
-      mBuffer[2] *= -1.0f;
-      }
+//android.util.Log.e("D", " normal: "+mBuffer[0]+" "+mBuffer[1]+" "+mBuffer[2]);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -378,45 +376,47 @@ android.util.Log.e("D", " buffer: "+mBuffer[0]+" "+mBuffer[1]+" "+mBuffer[2]);
 
     computeNormalVector(vert3D,0,1,foundIndex);
 
+    // rotate so that the normal vector becomes (0,0,1)
+    float axisX, axisY, axisZ;
+
     if( mBuffer[0]!=0.0f || mBuffer[1]!=0.0f )
       {
-      // rotate so that the normal vector becomes (0,0,1)
-      float axisX = -mBuffer[1];
-      float axisY =  mBuffer[0];
-      float axisZ = 0.0f;
-
-      float vecLen = mBuffer[0]*mBuffer[0] + mBuffer[1]*mBuffer[1] + mBuffer[2]*mBuffer[2];
-      vecLen = (float)Math.sqrt(vecLen);
-      mBuffer[0] /= vecLen;
-      mBuffer[1] /= vecLen;
-      mBuffer[2] /= vecLen;
+      axisX = -mBuffer[1];
+      axisY =  mBuffer[0];
+      axisZ = 0.0f;
 
       float axiLen = axisX*axisX + axisY*axisY + axisZ*axisZ;
       axiLen = (float)Math.sqrt(axiLen);
       axisX /= axiLen;
       axisY /= axiLen;
       axisZ /= axiLen;
+      }
+    else
+      {
+      axisX = 0.0f;
+      axisY = 1.0f;
+      axisZ = 0.0f;
+      }
 
-      float cosTheta = mBuffer[2];
-      //float sinTheta = axiLen / vecLen;
+    float cosTheta = mBuffer[2];
+  //float sinTheta = axiLen / vecLen;
 
-      float sinHalfTheta = (float)Math.sqrt(0.5f*(1-cosTheta));
-      float cosHalfTheta = (float)Math.sqrt(0.5f*(1+cosTheta));
+    float sinHalfTheta = (float)Math.sqrt(0.5f*(1-cosTheta));
+    float cosHalfTheta = (float)Math.sqrt(0.5f*(1+cosTheta));
 
-      mQuat1[0] = axisX*sinHalfTheta;
-      mQuat1[1] = axisY*sinHalfTheta;
-      mQuat1[2] = axisZ*sinHalfTheta;
-      mQuat1[3] = cosHalfTheta;
-      mQuat2[0] = axisX*sinHalfTheta;
-      mQuat2[1] = axisY*sinHalfTheta;
-      mQuat2[2] = axisZ*sinHalfTheta;
-      mQuat2[3] = -cosHalfTheta;
+    mQuat1[0] = axisX*sinHalfTheta;
+    mQuat1[1] = axisY*sinHalfTheta;
+    mQuat1[2] = axisZ*sinHalfTheta;
+    mQuat1[3] = cosHalfTheta;
+    mQuat2[0] = axisX*sinHalfTheta;
+    mQuat2[1] = axisY*sinHalfTheta;
+    mQuat2[2] = axisZ*sinHalfTheta;
+    mQuat2[3] = -cosHalfTheta;
 
-      for (float[] vert : vert3D)
-        {
-        quatMultiply(mQuat1, vert, mQuat3);
-        quatMultiply(mQuat3, mQuat2, vert);
-        }
+    for (float[] vert : vert3D)
+      {
+      quatMultiply(mQuat1, vert, mQuat3);
+      quatMultiply(mQuat3, mQuat2, vert);
       }
 
     // fit the whole thing in a square and remember the scale & 2D vertices
@@ -594,7 +594,11 @@ android.util.Log.e("D", " buffer: "+mBuffer[0]+" "+mBuffer[1]+" "+mBuffer[2]);
       for(int previous=0; previous<face; previous++)
         {
         info = mFaceInfo.get(previous);
-        if( tryFindingRotation(info,newInfo.vertices) ) break;
+        if( tryFindingRotation(newInfo,info.vertices) )
+          {
+          android.util.Log.e("D", "comparing face "+face+" to "+previous+" SUCCESS!!");
+          break;
+          }
         }
 
       mFaceInfo.add(newInfo);
@@ -637,6 +641,10 @@ android.util.Log.e("D", " buffer: "+mBuffer[0]+" "+mBuffer[1]+" "+mBuffer[2]);
     for(int face=0; face<numFaces; face++)
       {
       info = mFaceInfo.get(face);
+
+      android.util.Log.d("D", "--------------");
+      printInfo(info);
+
       band = bands[bandIndexes[face]];
       bandsComputed = computeBands( band[0], (int)band[1], band[2], band[3], (int)band[4]);
       meshes[face] = new MeshPolygon(info.vertices,bandsComputed,(int)band[5],(int)band[6]);
diff --git a/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java b/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
index 7dc0492..4cae4f5 100644
--- a/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
+++ b/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
@@ -31,10 +31,7 @@ import org.distorted.library.effect.Effect;
 import org.distorted.library.effect.EffectType;
 import org.distorted.library.effect.MatrixEffectQuaternion;
 import org.distorted.library.effect.MatrixEffectScale;
-import org.distorted.library.effect.VertexEffect;
-import org.distorted.library.effect.VertexEffectDeform;
 import org.distorted.library.effect.VertexEffectDisappear;
-import org.distorted.library.effect.VertexEffectRotate;
 import org.distorted.library.main.DistortedEffects;
 import org.distorted.library.main.DistortedLibrary;
 import org.distorted.library.main.DistortedScreen;
@@ -54,8 +51,6 @@ import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;
 
 import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
-import static org.distorted.examples.meshfile.FactoryCubit.COS18;
-import static org.distorted.examples.meshfile.FactoryCubit.SIN18;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -65,11 +60,11 @@ class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.Excep
     private static final float SQ3 = (float)Math.sqrt(3);
     private final float DEFAULT_SCALE = 0.3f;
 
-    private GLSurfaceView mView;
+    private final GLSurfaceView mView;
     private DistortedTexture mTexture;
-    private DistortedScreen mScreen;
-    private DistortedEffects mEffects;
-    private Static3D mScale;
+    private final DistortedScreen mScreen;
+    private final DistortedEffects mEffects;
+    private final Static3D mScale;
     private long mTime;
     private float mCurrentScale;
     private MeshBase mMesh;
@@ -258,40 +253,6 @@ class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.Excep
 
   private MeshBase createStaticMesh()
     {
-    /*
-    MeshBase triangle = new MeshTriangle(5);
-
-    VertexEffectMove move = new VertexEffectMove( new Static3D(1,0,0) );
-    triangle.apply(move);
-
-    triangle.mergeEffComponents();
-
-    VertexEffectScale scale = new VertexEffectScale( new Static3D(2,1,1) );
-    triangle.apply(scale);
-
-    return triangle;
-     */
-/*
-    final float DIST  = 0.2f;
-    float[] vertices = new float[6];
-
-    vertices[0] =  0.5f-DIST;
-    vertices[1] = -0.5f-DIST;
-
-    vertices[2] =  0.5f-DIST;
-    vertices[3] =  0.5f-DIST;
-
-    vertices[4] = -0.5f-DIST;
-    vertices[5] =  0.5f-DIST;
-
-    float[] bands0 = new float[] {1.0f, 0.0f, 0.5f, 0.03f, 0.0f, 0.05f};
-
-    MeshBase mesh = new MeshPolygon(vertices,bands0,0,0);
-    mesh.setShowNormals(true);
-
-    return mesh;
-    */
-
     final float IVY_D = 0.10f;
     final int   IVY_N = 8;
 
@@ -344,17 +305,17 @@ class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.Excep
 
       final int[][] vertIndexes = new int[][]
           {
-              {0,1,3,2},
-              {5,4,6,7},
-              {5,1,0,4},
-              {6,2,3,7},
-              {4,0,2,6},
-              {1,5,7,3}
+              {2,3,1,0},   // counterclockwise!
+              {7,6,4,5},
+              {4,0,1,5},
+              {7,3,2,6},
+              {6,2,0,4},
+              {3,7,5,1}
           };
 
       final float[][] bands = new float[][]
           {
-              {0.048f,35,0.5f,0.7f,5,  2,2}
+              {0.05f,45,0.6f,0.5f,5,  2,2}
           };
 
       final int[] bandIndexes = new int[] { 0,0,0,0,0,0 };
