commit 28f4aba078ab71a08f73b02e05c9bf40fb697aaa
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Mon Apr 12 00:57:48 2021 +0200

    Face cubit creation: progress: cube & tetrahedron work; dino cubit does not.
    Also, the texture is not yet moved inside the (0,1)x(0,1) square.

diff --git a/src/main/java/org/distorted/examples/meshfile/FactoryCubit.java b/src/main/java/org/distorted/examples/meshfile/FactoryCubit.java
index 9b6619b..431d652 100644
--- a/src/main/java/org/distorted/examples/meshfile/FactoryCubit.java
+++ b/src/main/java/org/distorted/examples/meshfile/FactoryCubit.java
@@ -313,7 +313,6 @@ class FactoryCubit
     int len = vert3D.length;
     StickerInfo sInfo = new StickerInfo();
     sInfo.vertices = new float[2*len];
-    mStickerInfo.add(sInfo);
 
     for( int vertex=0; vertex<len; vertex++ )
       {
@@ -321,6 +320,9 @@ class FactoryCubit
       sInfo.vertices[2*vertex+1] = vert3D[vertex][1] / info.scale;
       }
 
+    mStickerInfo.add(sInfo);
+
+    info.sticker = mStickerInfo.size() -1;
     info.flip = false;
     }
 
@@ -382,7 +384,7 @@ class FactoryCubit
       axisY =  mBuffer[0];
       axisZ = 0.0f;
 
-      float axiLen = axisX*axisX + axisY*axisY + axisZ*axisZ;
+      float axiLen = axisX*axisX + axisY*axisY;
       axiLen = (float)Math.sqrt(axiLen);
       axisX /= axiLen;
       axisY /= axiLen;
@@ -403,32 +405,37 @@ class FactoryCubit
     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;
+    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);
+      quatMultiply(mQuat1, vert  , mQuat3);
+      quatMultiply(mQuat3, mQuat2, vert  );
       }
 
     // fit the whole thing in a square and remember the scale & 2D vertices
     fitInSquare(info, vert3D);
 
     // remember the rotation
-    info.qx = mQuat1[0];
-    info.qy = mQuat1[1];
-    info.qz = mQuat1[2];
-    info.qw =-mQuat1[3];
+    info.qx =-mQuat1[0];
+    info.qy =-mQuat1[1];
+    info.qz =-mQuat1[2];
+    info.qw = mQuat1[3];
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private float computeCos(float x1, float y1, float x2, float y2, float len1, float len2)
     {
-    return (x1*x2+y1*y2) / (len1*len2);
+    float ret =  (x1*x2+y1*y2) / (len1*len2);
+
+    if( ret> 1.0f ) return  1.0f;
+    if( ret<-1.0f ) return -1.0f;
+
+    return ret;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -436,7 +443,12 @@ class FactoryCubit
 
   private float computeSin(float x1, float y1, float x2, float y2, float len1, float len2)
     {
-    return (x2*y1-x1*y2) / (len1*len2);
+    float ret = (x2*y1-x1*y2) / (len1*len2);
+
+    if( ret> 1.0f ) return  1.0f;
+    if( ret<-1.0f ) return -1.0f;
+
+    return ret;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -481,23 +493,26 @@ class FactoryCubit
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private void correctInfo(FaceInfo info, float scale, float sin, float cos, int oldSticker, boolean flip)
+  private void correctInfo(FaceInfo info, float scale, float cos, int oldSticker, boolean flip)
     {
     mStickerInfo.remove(info.sticker);
 
-    info.flip = flip;
+    info.flip    = flip;
     info.sticker = oldSticker;
-    info.scale *= scale;
+    info.scale  *= scale;
+
+    mQuat1[0] = info.qx;
+    mQuat1[1] = info.qy;
+    mQuat1[2] = info.qz;
+    mQuat1[3] = info.qw;
 
-    mQuat1[0] = 0.0f;
-    mQuat1[1] = 0.0f;
-    mQuat1[2] = sin;
-    mQuat1[3] = cos;
+    float sinHalf = (float)Math.sqrt(0.5f*(1-cos));
+    float cosHalf = (float)Math.sqrt(0.5f*(1+cos));
 
-    mQuat2[0] = info.qx;
-    mQuat2[1] = info.qy;
-    mQuat2[2] = info.qz;
-    mQuat2[3] = info.qw;
+    mQuat2[0] = 0.0f;
+    mQuat2[1] = 0.0f;
+    mQuat2[2] = sinHalf;
+    mQuat2[3] = cosHalf;
 
     quatMultiply( mQuat1, mQuat2, mQuat3 );
 
@@ -514,18 +529,18 @@ class FactoryCubit
     {
     for(int vertex=0; vertex<len; vertex++)
       {
-      float xR = newVert[2*vertex  ];
-      float yR = newVert[2*vertex+1];
+      float xR = preVert[2*vertex  ];
+      float yR = preVert[2*vertex+1];
       float lenRotV = (float)Math.sqrt(xR*xR+yR*yR);
-      float cos = computeCos(xR,yR,preVert[0],preVert[1], lenRotV, lenVert);
-      float sin = computeSin(xR,yR,preVert[0],preVert[1], lenRotV, lenVert);
+      float cos = computeCos(xR,yR,newVert[0],newVert[1], lenRotV, lenVert);
+      float sin = computeSin(xR,yR,newVert[0],newVert[1], lenRotV, lenVert);
 
       rotateAllVertices(buffer,len,newVert,sin,cos);
 
       if( isScaledVersionOf(buffer,preVert,len) )
         {
         float scale = preVert[0]!=0.0f ? buffer[0]/preVert[0] : buffer[1]/preVert[1];
-        correctInfo(info,scale,sin,cos,oldSticker,inverted);
+        correctInfo(info,scale,cos,oldSticker,inverted);
         return true;
         }
       }
@@ -660,14 +675,15 @@ class FactoryCubit
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private void printStickerInfo()
+  private void printInfo()
     {
-    String ver="";
     int stickers = mStickerInfo.size();
 
+    android.util.Log.d("D", "-------------------------");
+
     for(int s=0; s<stickers; s++)
       {
-      ver = "";
+      String ver = "";
       StickerInfo info = mStickerInfo.get(s);
       int len = info.vertices.length/2;
 
@@ -675,17 +691,23 @@ class FactoryCubit
         {
         ver += ("("+info.vertices[2*i]+","+info.vertices[2*i+1]+") ");
         }
+
+      android.util.Log.e("D", "sticker "+s+" "+ver);
       }
 
-    android.util.Log.e("D", "vertices= "+ver);
-    }
+    android.util.Log.d("D", "-------------------------");
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
+    int faces = mFaceInfo.size();
 
-  private void printFaceInfo(FaceInfo info)
-    {
-    android.util.Log.e("D", "q=("+info.qx+", "+info.qy+", "+info.qz+", "+info.qw+") v=("
+    for(int f=0; f<faces; f++)
+      {
+      FaceInfo info = mFaceInfo.get(f);
+
+      android.util.Log.e("D", "q=("+info.qx+", "+info.qy+", "+info.qz+", "+info.qw+") v=("
                        +info.vx+", "+info.vy+", "+info.vz+") scale="+info.scale+" sticker="+info.sticker);
+      }
+
+    android.util.Log.d("D", "-------------------------");
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -704,15 +726,13 @@ class FactoryCubit
     FaceInfo fInfo;
     StickerInfo sInfo;
 
-    //printStickerInfo();
+    printInfo();
 
     for(int face=0; face<numFaces; face++)
       {
       fInfo = mFaceInfo.get(face);
       sInfo = mStickerInfo.get(fInfo.sticker);
 
-      //printFaceInfo(fInfo);
-
       band = bands[bandIndexes[face]];
       bandsComputed = computeBands( band[0], (int)band[1], band[2], band[3], (int)band[4]);
       meshes[face] = new MeshPolygon(sInfo.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 87128c7..fc8f0a3 100644
--- a/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
+++ b/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
@@ -291,7 +291,17 @@ class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.Excep
 
     private void createMesh()
       {
-      final float[][] vertices = new float[][]
+      int mode = 2;
+      float[][] vertices  = null;
+      int[][] vertIndexes = null;
+      float[][] bands     = null;
+      int[] bandIndexes   = null;
+      float[][] corners   = null;
+      int[] cornerIndexes = null;
+
+      if( mode==0 ) // CUBE
+        {
+        vertices = new float[][]
           {
               { 0.5f, 0.5f, 0.5f },
               { 0.5f, 0.5f,-0.5f },
@@ -303,7 +313,7 @@ class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.Excep
               {-0.5f,-0.5f,-0.5f },
           };
 
-      final int[][] vertIndexes = new int[][]
+        vertIndexes = new int[][]
           {
               {2,3,1,0},   // counterclockwise!
               {7,6,4,5},
@@ -313,19 +323,85 @@ class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.Excep
               {3,7,5,1}
           };
 
-      final float[][] bands = new float[][]
+        bands = new float[][]
           {
               {0.05f,45,0.6f,0.5f,5,  2,2}
           };
 
-      final int[] bandIndexes = new int[] { 0,0,0,0,0,0 };
+        bandIndexes = new int[] { 0,0,0,0,0,0 };
 
-      final float[][] corners = new float[][]
+        corners = new float[][]
           {
               { 0.01f, 0.10f }
           };
 
-      final int[] cornerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
+        cornerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
+        }
+      else if( mode==1 ) // TETRAHEDRON
+        {
+        vertices = new float[][]
+          {
+              {-0.5f, SQ2/4, 0.0f},
+              { 0.5f, SQ2/4, 0.0f},
+              { 0.0f,-SQ2/4, 0.5f},
+              { 0.0f,-SQ2/4,-0.5f}
+          };
+
+        vertIndexes = new int[][]
+          {
+              {2,1,0},   // counterclockwise!
+              {2,3,1},
+              {3,2,0},
+              {3,0,1}
+          };
+
+        bands = new float[][]
+          {
+              {0.05f,30,0.6f,0.5f,5,  2,2}
+          };
+
+        bandIndexes = new int[] { 0,0,0,0 };
+
+        corners = new float[][]
+          {
+              { 0.02f, 0.10f }
+          };
+
+        cornerIndexes = new int[] { 0,0,0,0 };
+        }
+      else if( mode==2 )  // DINO
+        {
+        vertices = new float[][]
+          {
+              {-0.5f, 0.0f, 0.0f},
+              { 0.5f, 0.0f, 0.0f},
+              { 0.0f,-0.5f, 0.0f},
+              { 0.0f, 0.0f,-0.5f}
+          };
+
+        vertIndexes = new int[][]
+          {
+              {2,1,0},   // counterclockwise!
+              {2,3,1},
+              {3,2,0},
+              {3,0,1}
+          };
+
+        bands = new float[][]
+          {
+              {0.028f,30,0.166f,0.8f,7,  2,5},
+              {0.028f,30,0.166f,0.8f,7,  1,2}
+          };
+
+        bandIndexes = new int[] { 0,1,1,0 };
+
+        corners = new float[][]
+          {
+              { 0.01f, 0.04f }
+          };
+
+        cornerIndexes = new int[] { 0,0,0,0 };
+        }
 
       FactoryCubit factory = FactoryCubit.getInstance();
       mMesh = factory.createRoundedSolid(vertices, vertIndexes, bands, bandIndexes, corners, cornerIndexes);
