commit 5b4a2d760a47396194aa652928550c1a801b665d
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Thu Mar 14 15:25:50 2019 +0000

    1) new 'Rubik' app (skeleton)
    2) MeshCubes: add support for custom texture mappings on each side (Rubik needs that!)

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index fbd8372..b1b6d68 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -52,6 +52,6 @@
         <activity android:name=".glow.GlowActivity"/>
         <activity android:name=".movingglow.MovingGlowActivity"/>
         <activity android:name=".earth.EarthActivity"/>
-        <activity android:name=".earth.EarthActivity"/>
+        <activity android:name=".rubik.RubikActivity"/>
     </application>
 </manifest>
diff --git a/src/main/java/org/distorted/examples/TableOfContents.java b/src/main/java/org/distorted/examples/TableOfContents.java
index 897ed9c..27cd57d 100644
--- a/src/main/java/org/distorted/examples/TableOfContents.java
+++ b/src/main/java/org/distorted/examples/TableOfContents.java
@@ -69,6 +69,7 @@ import org.distorted.examples.stencil.StencilActivity;
 import org.distorted.examples.glow.GlowActivity;
 import org.distorted.examples.movingglow.MovingGlowActivity;
 import org.distorted.examples.earth.EarthActivity;
+import org.distorted.examples.rubik.RubikActivity;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -406,6 +407,15 @@ public class TableOfContents extends ListActivity
    activityMapping.put(i++, EarthActivity.class);
    }
 
+   {
+   final Map<String, Object> item = new HashMap<>();
+   item.put(ITEM_IMAGE, R.drawable.icon_example_wip);
+   item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_rubik));
+   item.put(ITEM_SUBTITLE, getText(R.string.example_rubik_subtitle));
+   data.add(item);
+   activityMapping.put(i++, RubikActivity.class);
+   }
+
    final SimpleAdapter dataAdapter = new SimpleAdapter(this, data, R.layout.toc_item, new String[] {ITEM_IMAGE, ITEM_TITLE, ITEM_SUBTITLE}, new int[] {R.id.Image, R.id.Title, R.id.SubTitle});
    setListAdapter(dataAdapter);  
       
diff --git a/src/main/java/org/distorted/examples/flag/FlagRenderer.java b/src/main/java/org/distorted/examples/flag/FlagRenderer.java
index 639489f..bf9cf3f 100644
--- a/src/main/java/org/distorted/examples/flag/FlagRenderer.java
+++ b/src/main/java/org/distorted/examples/flag/FlagRenderer.java
@@ -97,8 +97,15 @@ class FlagRenderer implements GLSurfaceView.Renderer
       effects.apply( new MatrixEffectQuaternion(mQuat1, mCenter) );
       effects.apply( new MatrixEffectQuaternion(mQuat2, mCenter) );
 
+      final int GRIDX = 50;
+      final int GRIDY = 30;
+
+      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f      ,1.0f      );
+      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRIDX,1.0f      );
+      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f      ,1.0f/GRIDY);
+
       mScreen = new DistortedScreen();
-      mScreen.attach(mTexture, effects, new MeshCubes(50,30,1) );
+      mScreen.attach(mTexture, effects, new MeshCubes(GRIDX,GRIDY,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB) );
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/examples/objecttree/ObjectTreeRenderer.java b/src/main/java/org/distorted/examples/objecttree/ObjectTreeRenderer.java
index ac45b8f..5f9f74d 100644
--- a/src/main/java/org/distorted/examples/objecttree/ObjectTreeRenderer.java
+++ b/src/main/java/org/distorted/examples/objecttree/ObjectTreeRenderer.java
@@ -44,6 +44,7 @@ import org.distorted.library.type.Dynamic;
 import org.distorted.library.type.Dynamic1D;
 import org.distorted.library.type.Static1D;
 import org.distorted.library.type.Static3D;
+import org.distorted.library.type.Static4D;
 
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
@@ -181,9 +182,14 @@ class ObjectTreeRenderer implements GLSurfaceView.Renderer
       DistortedEffects gridEffects = new DistortedEffects();
 
       final int GRID = 20;
+      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
+      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
+      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
 
       if( mMeshFlat==null ) mMeshFlat = new MeshFlat(1,1);
-      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1);
+      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
+
+      int gridDepth = mGridTexture.getDepth(mMeshCubes);
 
       mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshFlat);
       mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
@@ -209,7 +215,7 @@ class ObjectTreeRenderer implements GLSurfaceView.Renderer
       Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
       sinkDyn.add(new Static1D(1.0f));
       sinkDyn.add(new Static1D(0.3f));
-      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
+      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2.0f, gridHeight/2.0f, gridDepth/2.0f) );
       gridEffects.apply(sink);
 
       VertexEffectSink.enable();
diff --git a/src/main/java/org/distorted/examples/rubik/RubikActivity.java b/src/main/java/org/distorted/examples/rubik/RubikActivity.java
new file mode 100644
index 0000000..2bab5fc
--- /dev/null
+++ b/src/main/java/org/distorted/examples/rubik/RubikActivity.java
@@ -0,0 +1,71 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2016 Leszek Koltunski                                                               //
+//                                                                                               //
+// This file is part of Distorted.                                                               //
+//                                                                                               //
+// Distorted is free software: you can redistribute it and/or modify                             //
+// it under the terms of the GNU General Public License as published by                          //
+// the Free Software Foundation, either version 2 of the License, or                             //
+// (at your option) any later version.                                                           //
+//                                                                                               //
+// Distorted is distributed in the hope that it will be useful,                                  //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
+// GNU General Public License for more details.                                                  //
+//                                                                                               //
+// You should have received a copy of the GNU General Public License                             //
+// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.examples.rubik;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+import org.distorted.library.main.Distorted;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+public class RubikActivity extends Activity
+{
+    private RubikSurfaceView mView;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    @Override
+    protected void onCreate(Bundle icicle)
+      {
+      super.onCreate(icicle);
+      mView = new RubikSurfaceView(this);
+      setContentView(mView);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onPause() 
+      {
+      mView.onPause();
+      Distorted.onPause();
+      super.onPause();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onResume() 
+      {
+      super.onResume();
+      mView.onResume();
+      }
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onDestroy() 
+      {
+      Distorted.onDestroy();  
+      super.onDestroy();
+      }
+    
+}
diff --git a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
new file mode 100644
index 0000000..a8b4b0a
--- /dev/null
+++ b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
@@ -0,0 +1,173 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2016 Leszek Koltunski                                                               //
+//                                                                                               //
+// This file is part of Distorted.                                                               //
+//                                                                                               //
+// Distorted is free software: you can redistribute it and/or modify                             //
+// it under the terms of the GNU General Public License as published by                          //
+// the Free Software Foundation, either version 2 of the License, or                             //
+// (at your option) any later version.                                                           //
+//                                                                                               //
+// Distorted is distributed in the hope that it will be useful,                                  //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
+// GNU General Public License for more details.                                                  //
+//                                                                                               //
+// You should have received a copy of the GNU General Public License                             //
+// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.examples.rubik;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.opengl.GLSurfaceView;
+
+import org.distorted.examples.R;
+import org.distorted.library.effect.MatrixEffectMove;
+import org.distorted.library.effect.MatrixEffectQuaternion;
+import org.distorted.library.effect.MatrixEffectScale;
+import org.distorted.library.effect.VertexEffect;
+import org.distorted.library.effect.VertexEffectDistort;
+import org.distorted.library.main.Distorted;
+import org.distorted.library.main.DistortedEffects;
+import org.distorted.library.main.DistortedScreen;
+import org.distorted.library.main.DistortedTexture;
+import org.distorted.library.mesh.MeshCubes;
+import org.distorted.library.mesh.MeshFlat;
+import org.distorted.library.type.Dynamic3D;
+import org.distorted.library.type.Static3D;
+import org.distorted.library.type.Static4D;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class RubikRenderer implements GLSurfaceView.Renderer
+{
+    private static final int CUBE_SIZE = 3;
+    private static final int VERTICES  = 5;
+    private static final int SIZE      = 200;
+
+    private GLSurfaceView mView;
+    private DistortedTexture mTexture;
+    private DistortedScreen mScreen;
+    private Static3D mMove, mScale, mCenter;
+    private MeshCubes[][][] mCubes;
+    private DistortedEffects[][][] mEffects;
+
+    Static4D mQuat1, mQuat2;
+    int mScreenMin;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    RubikRenderer(GLSurfaceView v)
+      {
+      mView = v;
+
+      mScreen = new DistortedScreen();
+
+      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
+      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
+
+      mCubes = new MeshCubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
+      mEffects = new DistortedEffects[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
+      Static3D[][][] cubeVectors = new Static3D[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
+
+      mMove  = new Static3D(0,0,0);
+      mScale = new Static3D(1,1,1);
+      mCenter= new Static3D(0,0,0);
+
+      MatrixEffectMove       move  = new MatrixEffectMove(mMove);
+      MatrixEffectScale      scale = new MatrixEffectScale(mScale);
+      MatrixEffectQuaternion quat1 = new MatrixEffectQuaternion(mQuat1, mCenter);
+      MatrixEffectQuaternion quat2 = new MatrixEffectQuaternion(mQuat2, mCenter);
+
+      for(int x=0; x<CUBE_SIZE; x++)
+        for(int y=0; y<CUBE_SIZE; y++)
+          for(int z=0; z<CUBE_SIZE; z++)
+            {
+            mCubes[x][y][z] = new MeshCubes(VERTICES,VERTICES,VERTICES);
+
+            cubeVectors[x][y][z] = new Static3D( SIZE*(x-0.5f*(CUBE_SIZE-1)), SIZE*(y-0.5f*(CUBE_SIZE-1)), SIZE*(z-0.5f*(CUBE_SIZE-1)) );
+
+            mEffects[x][y][z] = new DistortedEffects();
+
+            mEffects[x][y][z].apply(move);
+            mEffects[x][y][z].apply(scale);
+            mEffects[x][y][z].apply(quat1);
+            mEffects[x][y][z].apply(quat2);
+            mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
+            }
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public void onDrawFrame(GL10 glUnused) 
+      {
+      mScreen.render( System.currentTimeMillis() );
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
+      {
+      mScreenMin = width<height ? width:height;
+
+      float w = mTexture.getWidth();
+      float h = mTexture.getHeight();
+      float d = mTexture.getDepth(mCubes[0][0][0]);
+
+      float factor = 0.6f*(width>height ? height/h:width/w)/CUBE_SIZE;
+
+      mCenter.set(w/2,h/2,d/2);
+      mMove.set( (width-factor*w)/2 , (height-factor*h)/2 , -factor*d/2 );
+      mScale.set(factor,factor,factor);
+
+      mScreen.resize(width, height);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
+      {
+      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
+      Bitmap bitmap;
+        
+      try 
+        {
+        bitmap = BitmapFactory.decodeStream(is);
+        } 
+      finally 
+        {
+        try 
+          {
+          is.close();
+          } 
+        catch(IOException e) { }
+        }  
+
+      if( mTexture==null ) mTexture = new DistortedTexture(SIZE,SIZE);
+      mTexture.setTexture(bitmap);
+
+      mScreen.detachAll();
+
+      for(int x=0; x<CUBE_SIZE; x++)
+        for(int y=0; y<CUBE_SIZE; y++)
+          for(int z=0; z<CUBE_SIZE; z++)
+             mScreen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
+
+      try
+        {
+        Distorted.onCreate(mView.getContext());
+        }
+      catch(Exception ex)
+        {
+        android.util.Log.e("Rubik", ex.getMessage() );
+        }
+      }
+}
diff --git a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
new file mode 100644
index 0000000..cdc8f67
--- /dev/null
+++ b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
@@ -0,0 +1,132 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2016 Leszek Koltunski                                                               //
+//                                                                                               //
+// This file is part of Distorted.                                                               //
+//                                                                                               //
+// Distorted is free software: you can redistribute it and/or modify                             //
+// it under the terms of the GNU General Public License as published by                          //
+// the Free Software Foundation, either version 2 of the License, or                             //
+// (at your option) any later version.                                                           //
+//                                                                                               //
+// Distorted is distributed in the hope that it will be useful,                                  //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
+// GNU General Public License for more details.                                                  //
+//                                                                                               //
+// You should have received a copy of the GNU General Public License                             //
+// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.examples.rubik;
+
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.pm.ConfigurationInfo;
+import android.opengl.GLSurfaceView;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class RubikSurfaceView extends GLSurfaceView
+{
+    private int mX, mY;
+    private RubikRenderer mRenderer;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public RubikSurfaceView(Context context)
+      {
+      super(context);
+
+      mX = -1;
+      mY = -1;
+
+      mRenderer = new RubikRenderer(this);
+      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
+      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
+      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
+      setRenderer(mRenderer);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public RubikRenderer getRenderer()
+      {
+      return mRenderer;
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    @Override public boolean onTouchEvent(MotionEvent event)
+      {
+      int action = event.getAction();
+      int x = (int)event.getX();
+      int y = (int)event.getY();
+
+      switch(action)
+         {
+         case MotionEvent.ACTION_DOWN: mX = x;
+                                       mY = y;
+                                       break;
+
+         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
+                                         {
+                                         float px = mY-y;
+                                         float py = mX-x;
+                                         float pz = 0;
+                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
+
+                                         if( plen>0 )
+                                           {
+                                           px /= plen;
+                                           py /= plen;
+                                           pz /= plen;
+
+                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
+                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
+
+                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
+                                           }
+                                         }
+                                       break;
+
+         case MotionEvent.ACTION_UP  : mX = -1;
+                                       mY = -1;
+
+                                       float qx = mRenderer.mQuat1.get1();
+                                       float qy = mRenderer.mQuat1.get2();
+                                       float qz = mRenderer.mQuat1.get3();
+                                       float qw = mRenderer.mQuat1.get4();
+
+                                       float rx = mRenderer.mQuat2.get1();
+                                       float ry = mRenderer.mQuat2.get2();
+                                       float rz = mRenderer.mQuat2.get3();
+                                       float rw = mRenderer.mQuat2.get4();
+
+                                       // This is quaternion multiplication. (tx.ty.tz.tw)
+                                       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
+                                       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
+                                       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
+                                       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
+                                       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
+
+                                       // The point of this is so that there are always
+                                       // exactly 2 quaternions: Quat1 representing the rotation
+                                       // accumulating only since the last screen touch, and Quat2
+                                       // which remembers the combined effect of all previous
+                                       // swipes.
+                                       // We cannot be accumulating an ever-growing list of quaternions
+                                       // and add a new one every time user swipes the screen - there
+                                       // is a limited number of slots in the EffectQueueMatrix!
+                                       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
+                                       mRenderer.mQuat2.set(tx, ty, tz, tw);
+
+                                       break;
+         }
+
+      return true;
+      }
+
+}
+
diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml
index 41e1eda..0ca6aa1 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -182,6 +182,8 @@
     <string name="example_moving_glow_subtitle">See moving objects glowing with light.</string>
     <string name="example_earth">Earth</string>
     <string name="example_earth_subtitle">Test the sphere Mesh by showing the Earth in cosmos.</string>
+    <string name="example_rubik">Rubik Cube</string>
+    <string name="example_rubik_subtitle">Moveable Rubik Cube.</string>
 
     <string name="example_movingeffects_toast">Click on \'RESET\' and define your path by touching the screen. Then click on one of the effects and see it move along your path.</string>
     <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>
