commit 94cc96ff8b7915d627da1f26293438b0ef4ae4b2
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Wed Apr 3 23:48:28 2019 +0100

    Improve the Rubik App.

diff --git a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
index 20f0bc7..600583c 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
@@ -29,6 +29,7 @@ import org.distorted.library.effect.MatrixEffectMove;
 import org.distorted.library.effect.MatrixEffectQuaternion;
 import org.distorted.library.effect.MatrixEffectScale;
 import org.distorted.library.effect.PostprocessEffectGlow;
+import org.distorted.library.effect.VertexEffectSink;
 import org.distorted.library.main.Distorted;
 import org.distorted.library.main.DistortedEffects;
 import org.distorted.library.main.DistortedScreen;
@@ -45,8 +46,8 @@ import javax.microedition.khronos.opengles.GL10;
 
 class RubikRenderer implements GLSurfaceView.Renderer
 {
-            static final int NUM_CUBES = 3;
-    private static final int VERTICES  = 5;
+            static final int NUM_CUBES =   6;
+    private static final int VERTICES  =  10;
     private static final int SIZE      = 200;
 
     private static final float CUBE_SCREEN_RATIO = 0.5f;
@@ -64,7 +65,8 @@ class RubikRenderer implements GLSurfaceView.Renderer
     private Static1D mGlowRadius;
     private Static4D mGlowColor;
 
-    Static4D mQuatCurrent, mQuatAccumulated;
+    private Static4D mQuatCurrent, mQuatAccumulated;
+    private Static4D mTempCurrent, mTempAccumulated;
     int mScreenMin;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -76,13 +78,18 @@ class RubikRenderer implements GLSurfaceView.Renderer
       mScreen = new DistortedScreen();
       mScreen.setProjection(90.0f, 0.1f);
 
-      mQuatCurrent     = new Static4D(           0,         0,           0,          1);  // unity quaternion
-      mQuatAccumulated = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
+      mTempCurrent     = new Static4D(0,0,0,1);
+      mTempAccumulated = initializeQuat();
+      mQuatCurrent     = new Static4D(0,0,0,1);
+      mQuatAccumulated = initializeQuat();
 
       mCubes = new MeshCubes[NUM_CUBES][NUM_CUBES][NUM_CUBES];
       mEffects = new DistortedEffects[NUM_CUBES][NUM_CUBES][NUM_CUBES];
       Static3D[][][] cubeVectors = new Static3D[NUM_CUBES][NUM_CUBES][NUM_CUBES];
 
+      VertexEffectSink sink = new VertexEffectSink( new Static1D(3.0f),
+                                                    new Static3D(SIZE*0.5f, SIZE*0.5f, SIZE*0.5f),
+                                                    new Static4D(0,0,0, SIZE*0.72f) );
       mMove  = new Static3D(0,0,0);
       mScale = new Static3D(1,1,1);
       mCenter= new Static3D(0,0,0);
@@ -123,12 +130,12 @@ class RubikRenderer implements GLSurfaceView.Renderer
         for(int y = 0; y< NUM_CUBES; y++)
           for(int z = 0; z< NUM_CUBES; z++)
             {
-            tmpLeft  = (x==          0 ? mapLeft  :mapBlack);
+            tmpLeft  = (x==            0 ? mapLeft  :mapBlack);
             tmpRight = (x== NUM_CUBES -1 ? mapRight :mapBlack);
             tmpFront = (z== NUM_CUBES -1 ? mapFront :mapBlack);
-            tmpBack  = (z==          0 ? mapBack  :mapBlack);
+            tmpBack  = (z==            0 ? mapBack  :mapBlack);
             tmpTop   = (y== NUM_CUBES -1 ? mapTop   :mapBlack);
-            tmpBottom= (y==          0 ? mapBottom:mapBlack);
+            tmpBottom= (y==            0 ? mapBottom:mapBlack);
 
             mCubes[x][y][z] = new MeshCubes(VERTICES,VERTICES,VERTICES, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
 
@@ -136,6 +143,7 @@ class RubikRenderer implements GLSurfaceView.Renderer
 
             mEffects[x][y][z] = new DistortedEffects();
 
+            mEffects[x][y][z].apply(sink);
             mEffects[x][y][z].apply(move);
             mEffects[x][y][z].apply(scale);
             mEffects[x][y][z].apply(quat1);
@@ -149,6 +157,9 @@ class RubikRenderer implements GLSurfaceView.Renderer
     public void onDrawFrame(GL10 glUnused) 
       {
       mScreen.render( System.currentTimeMillis() );
+
+      mQuatCurrent.set(mTempCurrent);
+      mQuatAccumulated.set(mTempAccumulated);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -224,6 +235,7 @@ class RubikRenderer implements GLSurfaceView.Renderer
              mScreen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
 
       PostprocessEffectGlow.enable();
+      VertexEffectSink.enable();
 
       try
         {
@@ -290,4 +302,26 @@ class RubikRenderer implements GLSurfaceView.Renderer
 
       mEffects[mLastCol][mLastRow][mLastSli].apply(new PostprocessEffectGlow(mGlowRadius,mGlowColor));
       }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Initial rotation of the cube. Something semi-random that looks good.
+
+    Static4D initializeQuat()
+      {
+      return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    void setQuatCurrent(Static4D current)
+      {
+      mTempCurrent.set(current);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    void setQuatAccumulated(Static4D accumulated)
+      {
+      mTempAccumulated.set(accumulated);
+      }
 }
diff --git a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
index 2e5a72b..701fbe3 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
@@ -31,7 +31,7 @@ import org.distorted.library.type.Static4D;
 
 class RubikSurfaceView extends GLSurfaceView
 {
-    private final static int ERROR  =-1;
+    private final static int NONE  =-1;
     private final static int FRONT  = 0;
     private final static int BACK   = 1;
     private final static int LEFT   = 2;
@@ -42,6 +42,7 @@ class RubikSurfaceView extends GLSurfaceView
     private boolean mDragging;
     private int mX, mY;
     private int mTouchedRow, mTouchedCol, mTouchedSli;
+    private Static4D mQuatCurrent, mQuatAccumulated;
     private RubikRenderer mRenderer;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -52,6 +53,10 @@ class RubikSurfaceView extends GLSurfaceView
 
       mDragging = false;
       mRenderer = new RubikRenderer(this);
+
+      mQuatCurrent     = new Static4D(0,0,0,1);
+      mQuatAccumulated = mRenderer.initializeQuat();
+
       final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
       final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
       setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
@@ -76,7 +81,7 @@ class RubikSurfaceView extends GLSurfaceView
 
       switch(action)
          {
-         case MotionEvent.ACTION_DOWN: if( faceTouched(x,y) != ERROR )
+         case MotionEvent.ACTION_DOWN: if( faceTouched(x,y) != NONE )
                                          {
                                          mRenderer.abortLastEffect();
                                          mRenderer.applyNewEffect(mTouchedCol, mTouchedRow, mTouchedSli);
@@ -88,11 +93,18 @@ class RubikSurfaceView extends GLSurfaceView
                                          mDragging = true;
                                          }
                                        break;
-         case MotionEvent.ACTION_MOVE: if( mDragging ) mRenderer.mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
+         case MotionEvent.ACTION_MOVE: if( mDragging )
+                                         {
+                                         mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
+                                         mRenderer.setQuatCurrent(mQuatCurrent);
+                                         }
                                        break;
          case MotionEvent.ACTION_UP  : mDragging = false;
-                                       mRenderer.mQuatAccumulated.set(quatMultiply(mRenderer.mQuatCurrent, mRenderer.mQuatAccumulated));
-                                       mRenderer.mQuatCurrent.set(0f, 0f, 0f, 1f);
+                                       mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
+                                       mQuatCurrent.set(0f, 0f, 0f, 1f);
+
+                                       mRenderer.setQuatCurrent(mQuatCurrent);
+                                       mRenderer.setQuatAccumulated(mQuatAccumulated);
                                        break;
          }
 
@@ -164,7 +176,7 @@ class RubikSurfaceView extends GLSurfaceView
       if( absY>absX && absY>absZ ) return rotatedY>0 ? TOP:BOTTOM;
       if( absZ>absX && absZ>absY ) return rotatedZ>0 ? FRONT:BACK;
 
-      return ERROR;
+      return NONE;
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -194,15 +206,15 @@ class RubikSurfaceView extends GLSurfaceView
 
     private Static4D rotateVector(Static4D vector)
       {
-      float qx = mRenderer.mQuatAccumulated.get1();
-      float qy = mRenderer.mQuatAccumulated.get2();
-      float qz = mRenderer.mQuatAccumulated.get3();
-      float qw = mRenderer.mQuatAccumulated.get4();
+      float qx = mQuatAccumulated.get1();
+      float qy = mQuatAccumulated.get2();
+      float qz = mQuatAccumulated.get3();
+      float qw = mQuatAccumulated.get4();
 
       Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
       Static4D tmp = quatMultiply(quatInverted,vector);
 
-      return quatMultiply(tmp,mRenderer.mQuatAccumulated);
+      return quatMultiply(tmp,mQuatAccumulated);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -347,7 +359,7 @@ class RubikSurfaceView extends GLSurfaceView
       mTouchedCol = -1;
       mTouchedSli = -1;
 
-      return ERROR;
+      return NONE;
       }
 }
 
