commit 98904e45468e62d6c655510bba118cdbba812aa6
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sun Jun 7 22:42:37 2020 +0100

    More progreess porting RubikCube. Rotation mostly working now.

diff --git a/src/main/java/org/distorted/main/RubikRenderer.java b/src/main/java/org/distorted/main/RubikRenderer.java
index 3980f6d6..5dea1d09 100644
--- a/src/main/java/org/distorted/main/RubikRenderer.java
+++ b/src/main/java/org/distorted/main/RubikRenderer.java
@@ -24,6 +24,7 @@ import android.opengl.GLSurfaceView;
 
 import org.distorted.effects.BaseEffect;
 import org.distorted.library.effect.EffectType;
+import org.distorted.library.effect.VertexEffectQuaternion;
 import org.distorted.library.effect.VertexEffectRotate;
 import org.distorted.library.main.DistortedLibrary;
 import org.distorted.library.main.DistortedScreen;
@@ -80,6 +81,7 @@ public class RubikRenderer implements GLSurfaceView.Renderer
       {
       DistortedLibrary.setMax(EffectType.VERTEX,25);    // 24 Cube Quats + Rotate
       VertexEffectRotate.enable();
+      VertexEffectQuaternion.enable();
       BaseEffect.Type.enableEffects();
 
       try
diff --git a/src/main/java/org/distorted/objects/Cubit.java b/src/main/java/org/distorted/objects/Cubit.java
index ecd293e6..163dac68 100644
--- a/src/main/java/org/distorted/objects/Cubit.java
+++ b/src/main/java/org/distorted/objects/Cubit.java
@@ -60,68 +60,57 @@ class Cubit
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Because of quatMultiplication, errors can accumulate - so to avoid this, we
 // correct the value of the 'scramble' quat to what it should be - one of the legal quats from the
-// list LEGAL_QUATS.
+// list QUATS.
 //
 // We also have to remember that the group of unit quaternions is a double-cover of rotations
 // in 3D ( q represents the same rotation as -q ) - so invert if needed.
-/*
-  private void normalizeScrambleQuat(Static4D quat)
-    {
-    final float MAX_ERROR = 0.0001f;
 
+  private int normalizeScrambleQuat(Static4D quat)
+    {
     float x = quat.get0();
     float y = quat.get1();
     float z = quat.get2();
     float w = quat.get3();
-    float diff;
+    float xd,yd,zd,wd;
+    float diff, mindiff = Float.MAX_VALUE;
+    int ret=0;
+    int num_quats = mParent.QUATS.length;
+    Static4D qt;
 
-    for(float legal: mParent.LEGAL_QUATS)
+    for(int q=0; q<num_quats; q++)
       {
-      diff = x-legal;
-      if( diff*diff<MAX_ERROR ) x = legal;
-      diff = y-legal;
-      if( diff*diff<MAX_ERROR ) y = legal;
-      diff = z-legal;
-      if( diff*diff<MAX_ERROR ) z = legal;
-      diff = w-legal;
-      if( diff*diff<MAX_ERROR ) w = legal;
-      }
+      qt = mParent.QUATS[q];
 
-    if( w<0 )
-      {
-      w = -w;
-      z = -z;
-      y = -y;
-      x = -x;
-      }
-    else if( w==0 )
-      {
-      if( z<0 )
+      xd = x - qt.get0();
+      yd = y - qt.get1();
+      zd = z - qt.get2();
+      wd = w - qt.get3();
+
+      diff = xd*xd + yd*yd + zd*zd + wd*wd;
+
+      if( diff < mindiff )
         {
-        z = -z;
-        y = -y;
-        x = -x;
+        ret = q;
+        mindiff = diff;
         }
-      else if( z==0 )
+
+      xd = x + qt.get0();
+      yd = y + qt.get1();
+      zd = z + qt.get2();
+      wd = w + qt.get3();
+
+      diff = xd*xd + yd*yd + zd*zd + wd*wd;
+
+      if( diff < mindiff )
         {
-        if( y<0 )
-          {
-          y = -y;
-          x = -x;
-          }
-        else if( y==0 )
-          {
-          if( x<0 )
-            {
-            x = -x;
-            }
-          }
+        ret = q;
+        mindiff = diff;
         }
       }
 
-    quat.set(x,y,z,w);
+    return ret;
     }
-*/
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private void modifyCurrentPosition(Static4D quat)
@@ -281,11 +270,15 @@ class Cubit
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  void removeRotationNow(Static4D quat)
+  int removeRotationNow(Static4D quat)
     {
     mQuatScramble.set(RubikSurfaceView.quatMultiply(quat,mQuatScramble));
- //   normalizeScrambleQuat( mQuatScramble );
+    int index = normalizeScrambleQuat( mQuatScramble );
+    mQuatScramble.set(mParent.QUATS[index]);
+
     modifyCurrentPosition(quat);
+
+    return index;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/objects/RubikObject.java b/src/main/java/org/distorted/objects/RubikObject.java
index 97270db0..8cd3aff9 100644
--- a/src/main/java/org/distorted/objects/RubikObject.java
+++ b/src/main/java/org/distorted/objects/RubikObject.java
@@ -55,6 +55,7 @@ public abstract class RubikObject extends DistortedNode
   private static final int TEXTURE_HEIGHT = 128;
 
   final Static3D[] ROTATION_AXIS;
+  final Static4D[] QUATS;
   final int NUM_FACES;
 
   static float OBJECT_SCREEN_RATIO;
@@ -95,7 +96,7 @@ public abstract class RubikObject extends DistortedNode
     mList = list;
     mOrigPos = getCubitPositions(size);
 
-    Static4D[] quats = getQuats();
+    QUATS = getQuats();
     NUM_CUBITS  = mOrigPos.length;
     ROTATION_AXIS = getRotationAxis();
     OBJECT_SCREEN_RATIO = getScreenRatio();
@@ -140,10 +141,10 @@ public abstract class RubikObject extends DistortedNode
 
     mEffects = new DistortedEffects();
 
-    int num_quats = quats.length;
+    int num_quats = QUATS.length;
     for(int q=0; q<num_quats; q++)
       {
-      VertexEffectQuaternion vq = new VertexEffectQuaternion(quats[q],CENTER);
+      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
       vq.setMeshAssociation(0,q);
       mEffects.apply(vq);
       }
@@ -263,6 +264,7 @@ public abstract class RubikObject extends DistortedNode
       Static4D quat;
       int axis, rowBitmap, angle;
       int corr = (360/getBasicAngle());
+      int index;
 
       for(int[] move: moves)
         {
@@ -274,7 +276,8 @@ public abstract class RubikObject extends DistortedNode
         for(int j=0; j<NUM_CUBITS; j++)
           if( belongsToRotation(j,axis,rowBitmap) )
             {
-            mCubits[j].removeRotationNow(quat);
+            index = mCubits[j].removeRotationNow(quat);
+            mMesh.setEffectAssociation(j,mCubits[j].computeAssociation(),index);
             }
         }
       }
@@ -412,7 +415,11 @@ public abstract class RubikObject extends DistortedNode
 
   public void solve()
     {
-    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].solve();
+    for(int i=0; i<NUM_CUBITS; i++)
+      {
+      mCubits[i].solve();
+      mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -561,7 +568,8 @@ public abstract class RubikObject extends DistortedNode
      for(int i=0; i<NUM_CUBITS; i++)
        if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
          {
-         mCubits[i].removeRotationNow(quat);
+         int index = mCubits[i].removeRotationNow(quat);
+         mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
          }
      }
 
